Skip to content

Commit d8dcc02

Browse files
wraithgarlukekarrys
authored andcommitted
fix: consolidate command alias code
1 parent a64acc0 commit d8dcc02

15 files changed

+587
-323
lines changed

docs/content/commands/npm-install.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ npm install <tarball url>
2222
npm install <git:// url>
2323
npm install <github username>/<github project>
2424

25-
aliases: i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall, add
25+
aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall
2626
```
2727

2828
<!-- automatically generated, do not edit manually -->

docs/content/commands/npm-search.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ description: Search for packages
1313
```bash
1414
npm search [search terms ...]
1515

16-
aliases: s, se, find
16+
aliases: find, s, se
1717
```
1818

1919
<!-- automatically generated, do not edit manually -->

docs/content/commands/npm-uninstall.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ description: Remove a package
1313
```bash
1414
npm uninstall [<@scope>/]<pkg>...
1515

16-
aliases: un, unlink, remove, rm, r
16+
aliases: unlink, remove, rm, r, un
1717
```
1818

1919
<!-- automatically generated, do not edit manually -->

docs/content/commands/npm-view.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ description: View registry info
1313
```bash
1414
npm view [<@scope>/]<pkg>[@<version>] [<field>[.subfield]...]
1515

16-
aliases: v, info, show
16+
aliases: info, show, v
1717
```
1818

1919
<!-- automatically generated, do not edit manually -->

lib/commands/completion.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
//
3131

3232
const { definitions, shorthands } = require('../utils/config/index.js')
33-
const deref = require('../utils/deref-command.js')
3433
const { aliases, cmdList, plumbing } = require('../utils/cmd-list.js')
3534
const aliasNames = Object.keys(aliases)
3635
const fullList = cmdList.concat(aliasNames).filter(c => !plumbing.includes(c))
@@ -152,7 +151,7 @@ class Completion extends BaseCommand {
152151
// check if there's a command already.
153152
const cmd = parsed.argv.remain[1]
154153
if (!cmd) {
155-
return this.wrap(opts, cmdCompl(opts))
154+
return this.wrap(opts, cmdCompl(opts, this.npm))
156155
}
157156

158157
Object.keys(parsed).forEach(k => this.npm.config.set(k, parsed[k]))
@@ -269,13 +268,13 @@ const isFlag = word => {
269268

270269
// complete against the npm commands
271270
// if they all resolve to the same thing, just return the thing it already is
272-
const cmdCompl = opts => {
271+
const cmdCompl = (opts, npm) => {
273272
const matches = fullList.filter(c => c.startsWith(opts.partialWord))
274273
if (!matches.length) {
275274
return matches
276275
}
277276

278-
const derefs = new Set([...matches.map(c => deref(c))])
277+
const derefs = new Set([...matches.map(c => npm.deref(c))])
279278
if (derefs.size === 1) {
280279
return [...derefs]
281280
}

lib/npm.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ const usage = require('./utils/npm-usage.js')
1111
const which = require('which')
1212
const fs = require('@npmcli/fs')
1313

14-
const deref = require('./utils/deref-command.js')
1514
const LogFile = require('./utils/log-file.js')
1615
const Timers = require('./utils/timers.js')
1716
const Display = require('./utils/display.js')
1817
const log = require('./utils/log-shim')
1918
const replaceInfo = require('./utils/replace-info.js')
2019
const updateNotifier = require('./utils/update-notifier.js')
2120
const pkg = require('../package.json')
21+
const cmdList = require('./utils/cmd-list.js')
2222

2323
let warnedNonDashArg = false
2424
const _load = Symbol('_load')
@@ -31,7 +31,6 @@ class Npm extends EventEmitter {
3131
command = null
3232
updateNotification = null
3333
loadErr = null
34-
deref = deref
3534
argv = []
3635

3736
#loadPromise = null
@@ -61,6 +60,26 @@ class Npm extends EventEmitter {
6160
return this.constructor.version
6261
}
6362

63+
deref (c) {
64+
if (!c) {
65+
return
66+
}
67+
if (c.match(/[A-Z]/)) {
68+
c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase())
69+
}
70+
if (cmdList.plumbing.indexOf(c) !== -1) {
71+
return c
72+
}
73+
// first deref the abbrev, if there is one
74+
// then resolve any aliases
75+
// so `npm install-cl` will resolve to `install-clean` then to `ci`
76+
let a = cmdList.abbrevs[c]
77+
while (cmdList.aliases[a]) {
78+
a = cmdList.aliases[a]
79+
}
80+
return a
81+
}
82+
6483
// Get an instantiated npm command
6584
// npm.command is already taken as the currently running command, a refactor
6685
// would be needed to change this

lib/utils/cmd-list.js

+80-78
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
// short names for common things
2-
const shorthands = {
1+
const abbrev = require('abbrev')
2+
3+
// plumbing should not have any aliases
4+
const aliases = {
5+
6+
// aliases
7+
login: 'adduser',
8+
author: 'owner',
9+
home: 'docs',
10+
issues: 'bugs',
11+
info: 'view',
12+
show: 'view',
13+
find: 'search',
14+
add: 'install',
15+
unlink: 'uninstall',
16+
remove: 'uninstall',
17+
rm: 'uninstall',
18+
r: 'uninstall',
19+
20+
// short names for common things
321
un: 'uninstall',
422
rb: 'rebuild',
523
list: 'ls',
@@ -21,12 +39,11 @@ const shorthands = {
2139
'clean-install-test': 'cit',
2240
x: 'exec',
2341
why: 'explain',
24-
}
25-
26-
const affordances = {
2742
la: 'll',
2843
verison: 'version',
2944
ic: 'ci',
45+
46+
// typos
3047
innit: 'init',
3148
// manually abbrev so that install-test doesn't make insta stop working
3249
in: 'install',
@@ -44,105 +61,90 @@ const affordances = {
4461
'dist-tags': 'dist-tag',
4562
upgrade: 'update',
4663
udpate: 'update',
47-
login: 'adduser',
48-
'add-user': 'adduser',
49-
author: 'owner',
50-
home: 'docs',
51-
issues: 'bugs',
52-
info: 'view',
53-
show: 'view',
54-
find: 'search',
55-
add: 'install',
56-
unlink: 'uninstall',
57-
remove: 'uninstall',
58-
rm: 'uninstall',
59-
r: 'uninstall',
6064
rum: 'run-script',
6165
sit: 'cit',
6266
urn: 'run-script',
6367
ogr: 'org',
68+
'add-user': 'adduser',
6469
}
6570

6671
// these are filenames in .
72+
// Keep these sorted so that lib/utils/npm-usage.js outputs in order
6773
const cmdList = [
68-
'ci',
69-
'install-ci-test',
70-
'install',
71-
'install-test',
72-
'uninstall',
74+
'access',
75+
'adduser',
76+
'audit',
77+
'bin',
78+
'bugs',
7379
'cache',
80+
'ci',
81+
'completion',
7482
'config',
75-
'set',
76-
'get',
77-
'update',
78-
'outdated',
79-
'prune',
80-
'pack',
81-
'find-dupes',
8283
'dedupe',
84+
'deprecate',
85+
'diff',
86+
'dist-tag',
87+
'docs',
88+
'doctor',
89+
'edit',
90+
'exec',
91+
'explain',
92+
'explore',
93+
'find-dupes',
94+
'fund',
95+
'get',
96+
'help',
8397
'hook',
84-
85-
'rebuild',
98+
'init',
99+
'install',
100+
'install-ci-test',
101+
'install-test',
86102
'link',
87-
88-
'publish',
89-
'star',
90-
'stars',
91-
'unstar',
92-
'adduser',
103+
'll',
93104
'login', // This is an alias for `adduser` but it can be confusing
94105
'logout',
95-
'unpublish',
96-
'owner',
97-
'access',
98-
'team',
99-
'deprecate',
100-
'shrinkwrap',
101-
'token',
102-
'profile',
103-
'audit',
104-
'fund',
105-
'org',
106-
107-
'help',
108106
'ls',
109-
'll',
110-
'search',
111-
'view',
112-
'init',
113-
'version',
114-
'edit',
115-
'explore',
116-
'docs',
117-
'repo',
118-
'bugs',
119-
'root',
120-
'prefix',
121-
'bin',
122-
'whoami',
123-
'diff',
124-
'dist-tag',
107+
'org',
108+
'outdated',
109+
'owner',
110+
'pack',
125111
'ping',
126112
'pkg',
127-
128-
'test',
129-
'stop',
130-
'start',
113+
'prefix',
114+
'profile',
115+
'prune',
116+
'publish',
117+
'rebuild',
118+
'repo',
131119
'restart',
120+
'root',
132121
'run-script',
122+
'search',
123+
'set',
133124
'set-script',
134-
'completion',
135-
'doctor',
136-
'exec',
137-
'explain',
125+
'shrinkwrap',
126+
'star',
127+
'stars',
128+
'start',
129+
'stop',
130+
'team',
131+
'test',
132+
'token',
133+
'uninstall',
134+
'unpublish',
135+
'unstar',
136+
'update',
137+
'version',
138+
'view',
139+
'whoami',
138140
]
139141

140142
const plumbing = ['birthday', 'help-search']
143+
const abbrevs = abbrev(cmdList.concat(Object.keys(aliases)))
141144

142145
module.exports = {
143-
aliases: Object.assign({}, shorthands, affordances),
144-
shorthands,
145-
affordances,
146+
abbrevs,
147+
aliases,
146148
cmdList,
147149
plumbing,
148150
}

lib/utils/deref-command.js

-31
This file was deleted.

lib/utils/npm-usage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const wrap = (arr) => {
4545
: Math.min(60, Math.max(process.stdout.columns - 16, 24))
4646

4747
let l = 0
48-
for (const c of arr.sort((a, b) => a < b ? -1 : 1)) {
48+
for (const c of arr) {
4949
if (out[l].length + c.length + 2 < line) {
5050
out[l] += ', ' + c
5151
} else {

0 commit comments

Comments
 (0)