diff options
author | Gar <gar+gh@danger.computer> | 2022-03-24 17:18:56 +0300 |
---|---|---|
committer | Luke Karrys <luke@lukekarrys.com> | 2022-03-28 23:21:36 +0300 |
commit | d8dcc02cfd354c1314c45d6530ec926cd138210c (patch) | |
tree | ba3721d4c4f153fe005bf9e08d450d9e2473a9a4 /lib | |
parent | a64acc0bf01e4bc68b26ead5b2d5c6db47ef16c2 (diff) |
fix: consolidate command alias code
Diffstat (limited to 'lib')
-rw-r--r-- | lib/commands/completion.js | 7 | ||||
-rw-r--r-- | lib/npm.js | 23 | ||||
-rw-r--r-- | lib/utils/cmd-list.js | 158 | ||||
-rw-r--r-- | lib/utils/deref-command.js | 31 | ||||
-rw-r--r-- | lib/utils/npm-usage.js | 2 |
5 files changed, 105 insertions, 116 deletions
diff --git a/lib/commands/completion.js b/lib/commands/completion.js index 0317753a1..22f91b0ae 100644 --- a/lib/commands/completion.js +++ b/lib/commands/completion.js @@ -30,7 +30,6 @@ // const { definitions, shorthands } = require('../utils/config/index.js') -const deref = require('../utils/deref-command.js') const { aliases, cmdList, plumbing } = require('../utils/cmd-list.js') const aliasNames = Object.keys(aliases) const fullList = cmdList.concat(aliasNames).filter(c => !plumbing.includes(c)) @@ -152,7 +151,7 @@ class Completion extends BaseCommand { // check if there's a command already. const cmd = parsed.argv.remain[1] if (!cmd) { - return this.wrap(opts, cmdCompl(opts)) + return this.wrap(opts, cmdCompl(opts, this.npm)) } Object.keys(parsed).forEach(k => this.npm.config.set(k, parsed[k])) @@ -269,13 +268,13 @@ const isFlag = word => { // complete against the npm commands // if they all resolve to the same thing, just return the thing it already is -const cmdCompl = opts => { +const cmdCompl = (opts, npm) => { const matches = fullList.filter(c => c.startsWith(opts.partialWord)) if (!matches.length) { return matches } - const derefs = new Set([...matches.map(c => deref(c))]) + const derefs = new Set([...matches.map(c => npm.deref(c))]) if (derefs.size === 1) { return [...derefs] } diff --git a/lib/npm.js b/lib/npm.js index fb4fb8d13..74825c97c 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -11,7 +11,6 @@ const usage = require('./utils/npm-usage.js') const which = require('which') const fs = require('@npmcli/fs') -const deref = require('./utils/deref-command.js') const LogFile = require('./utils/log-file.js') const Timers = require('./utils/timers.js') const Display = require('./utils/display.js') @@ -19,6 +18,7 @@ const log = require('./utils/log-shim') const replaceInfo = require('./utils/replace-info.js') const updateNotifier = require('./utils/update-notifier.js') const pkg = require('../package.json') +const cmdList = require('./utils/cmd-list.js') let warnedNonDashArg = false const _load = Symbol('_load') @@ -31,7 +31,6 @@ class Npm extends EventEmitter { command = null updateNotification = null loadErr = null - deref = deref argv = [] #loadPromise = null @@ -61,6 +60,26 @@ class Npm extends EventEmitter { return this.constructor.version } + deref (c) { + if (!c) { + return + } + if (c.match(/[A-Z]/)) { + c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase()) + } + if (cmdList.plumbing.indexOf(c) !== -1) { + return c + } + // first deref the abbrev, if there is one + // then resolve any aliases + // so `npm install-cl` will resolve to `install-clean` then to `ci` + let a = cmdList.abbrevs[c] + while (cmdList.aliases[a]) { + a = cmdList.aliases[a] + } + return a + } + // Get an instantiated npm command // npm.command is already taken as the currently running command, a refactor // would be needed to change this diff --git a/lib/utils/cmd-list.js b/lib/utils/cmd-list.js index 1469ebdd6..c1d20186a 100644 --- a/lib/utils/cmd-list.js +++ b/lib/utils/cmd-list.js @@ -1,5 +1,23 @@ -// short names for common things -const shorthands = { +const abbrev = require('abbrev') + +// plumbing should not have any aliases +const aliases = { + + // aliases + login: 'adduser', + author: 'owner', + home: 'docs', + issues: 'bugs', + info: 'view', + show: 'view', + find: 'search', + add: 'install', + unlink: 'uninstall', + remove: 'uninstall', + rm: 'uninstall', + r: 'uninstall', + + // short names for common things un: 'uninstall', rb: 'rebuild', list: 'ls', @@ -21,12 +39,11 @@ const shorthands = { 'clean-install-test': 'cit', x: 'exec', why: 'explain', -} - -const affordances = { la: 'll', verison: 'version', ic: 'ci', + + // typos innit: 'init', // manually abbrev so that install-test doesn't make insta stop working in: 'install', @@ -44,105 +61,90 @@ const affordances = { 'dist-tags': 'dist-tag', upgrade: 'update', udpate: 'update', - login: 'adduser', - 'add-user': 'adduser', - author: 'owner', - home: 'docs', - issues: 'bugs', - info: 'view', - show: 'view', - find: 'search', - add: 'install', - unlink: 'uninstall', - remove: 'uninstall', - rm: 'uninstall', - r: 'uninstall', rum: 'run-script', sit: 'cit', urn: 'run-script', ogr: 'org', + 'add-user': 'adduser', } // these are filenames in . +// Keep these sorted so that lib/utils/npm-usage.js outputs in order const cmdList = [ - 'ci', - 'install-ci-test', - 'install', - 'install-test', - 'uninstall', + 'access', + 'adduser', + 'audit', + 'bin', + 'bugs', 'cache', + 'ci', + 'completion', 'config', - 'set', - 'get', - 'update', - 'outdated', - 'prune', - 'pack', - 'find-dupes', 'dedupe', + 'deprecate', + 'diff', + 'dist-tag', + 'docs', + 'doctor', + 'edit', + 'exec', + 'explain', + 'explore', + 'find-dupes', + 'fund', + 'get', + 'help', 'hook', - - 'rebuild', + 'init', + 'install', + 'install-ci-test', + 'install-test', 'link', - - 'publish', - 'star', - 'stars', - 'unstar', - 'adduser', + 'll', 'login', // This is an alias for `adduser` but it can be confusing 'logout', - 'unpublish', - 'owner', - 'access', - 'team', - 'deprecate', - 'shrinkwrap', - 'token', - 'profile', - 'audit', - 'fund', - 'org', - - 'help', 'ls', - 'll', - 'search', - 'view', - 'init', - 'version', - 'edit', - 'explore', - 'docs', - 'repo', - 'bugs', - 'root', - 'prefix', - 'bin', - 'whoami', - 'diff', - 'dist-tag', + 'org', + 'outdated', + 'owner', + 'pack', 'ping', 'pkg', - - 'test', - 'stop', - 'start', + 'prefix', + 'profile', + 'prune', + 'publish', + 'rebuild', + 'repo', 'restart', + 'root', 'run-script', + 'search', + 'set', 'set-script', - 'completion', - 'doctor', - 'exec', - 'explain', + 'shrinkwrap', + 'star', + 'stars', + 'start', + 'stop', + 'team', + 'test', + 'token', + 'uninstall', + 'unpublish', + 'unstar', + 'update', + 'version', + 'view', + 'whoami', ] const plumbing = ['birthday', 'help-search'] +const abbrevs = abbrev(cmdList.concat(Object.keys(aliases))) module.exports = { - aliases: Object.assign({}, shorthands, affordances), - shorthands, - affordances, + abbrevs, + aliases, cmdList, plumbing, } diff --git a/lib/utils/deref-command.js b/lib/utils/deref-command.js deleted file mode 100644 index 0a3c8c90b..000000000 --- a/lib/utils/deref-command.js +++ /dev/null @@ -1,31 +0,0 @@ -// de-reference abbreviations and shorthands into canonical command name - -const { aliases, cmdList, plumbing } = require('./cmd-list.js') -const aliasNames = Object.keys(aliases) -const fullList = cmdList.concat(aliasNames).filter(c => !plumbing.includes(c)) -const abbrev = require('abbrev') -const abbrevs = abbrev(fullList) - -module.exports = c => { - if (!c || typeof c !== 'string') { - return '' - } - - if (c.match(/[A-Z]/)) { - c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase()) - } - - if (plumbing.indexOf(c) !== -1) { - return c - } - - // first deref the abbrev, if there is one - // then resolve any aliases - // so `npm install-cl` will resolve to `install-clean` then to `ci` - let a = abbrevs[c] - while (aliases[a]) { - a = aliases[a] - } - - return a || '' -} diff --git a/lib/utils/npm-usage.js b/lib/utils/npm-usage.js index b0c98b2ae..8d31f0155 100644 --- a/lib/utils/npm-usage.js +++ b/lib/utils/npm-usage.js @@ -45,7 +45,7 @@ const wrap = (arr) => { : Math.min(60, Math.max(process.stdout.columns - 16, 24)) let l = 0 - for (const c of arr.sort((a, b) => a < b ? -1 : 1)) { + for (const c of arr) { if (out[l].length + c.length + 2 < line) { out[l] += ', ' + c } else { |