Skip to content

Commit

Permalink
fix: consolidate command alias code
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar authored and lukekarrys committed Mar 28, 2022
1 parent a64acc0 commit d8dcc02
Show file tree
Hide file tree
Showing 15 changed files with 587 additions and 323 deletions.
2 changes: 1 addition & 1 deletion docs/content/commands/npm-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ npm install <tarball url>
npm install <git:// url>
npm install <github username>/<github project>

aliases: i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall, add
aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall
```

<!-- automatically generated, do not edit manually -->
Expand Down
2 changes: 1 addition & 1 deletion docs/content/commands/npm-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: Search for packages
```bash
npm search [search terms ...]

aliases: s, se, find
aliases: find, s, se
```

<!-- automatically generated, do not edit manually -->
Expand Down
2 changes: 1 addition & 1 deletion docs/content/commands/npm-uninstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: Remove a package
```bash
npm uninstall [<@scope>/]<pkg>...

aliases: un, unlink, remove, rm, r
aliases: unlink, remove, rm, r, un
```

<!-- automatically generated, do not edit manually -->
Expand Down
2 changes: 1 addition & 1 deletion docs/content/commands/npm-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: View registry info
```bash
npm view [<@scope>/]<pkg>[@<version>] [<field>[.subfield]...]

aliases: v, info, show
aliases: info, show, v
```

<!-- automatically generated, do not edit manually -->
Expand Down
7 changes: 3 additions & 4 deletions lib/commands/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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]
}
Expand Down
23 changes: 21 additions & 2 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ 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')
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')
Expand All @@ -31,7 +31,6 @@ class Npm extends EventEmitter {
command = null
updateNotification = null
loadErr = null
deref = deref
argv = []

#loadPromise = null
Expand Down Expand Up @@ -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
Expand Down
158 changes: 80 additions & 78 deletions lib/utils/cmd-list.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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',
Expand All @@ -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,
}
31 changes: 0 additions & 31 deletions lib/utils/deref-command.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/utils/npm-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit d8dcc02

Please sign in to comment.