Skip to content

Commit

Permalink
fix: do less work looking up commands (#6283)
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Mar 29, 2023
1 parent 9d2be4e commit e252532
Show file tree
Hide file tree
Showing 10 changed files with 568 additions and 551 deletions.
9 changes: 4 additions & 5 deletions lib/commands/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ const nopt = require('nopt')
const { resolve } = require('path')

const { definitions, shorthands } = require('../utils/config/index.js')
const { aliases, commands, plumbing } = require('../utils/cmd-list.js')
const aliasNames = Object.keys(aliases)
const fullList = commands.concat(aliasNames).filter(c => !plumbing.includes(c))
const { commands, aliases } = require('../utils/cmd-list.js')
const configNames = Object.keys(definitions)
const shorthandNames = Object.keys(shorthands)
const allConfs = configNames.concat(shorthandNames)
Expand Down Expand Up @@ -263,7 +261,8 @@ 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, npm) => {
const matches = fullList.filter(c => c.startsWith(opts.partialWord))
const allCommands = commands.concat(Object.keys(aliases))
const matches = allCommands.filter(c => c.startsWith(opts.partialWord))
if (!matches.length) {
return matches
}
Expand All @@ -273,7 +272,7 @@ const cmdCompl = (opts, npm) => {
return [...derefs]
}

return fullList
return allCommands
}

module.exports = Completion
23 changes: 18 additions & 5 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Config = require('@npmcli/config')
const chalk = require('chalk')
const which = require('which')
const fs = require('fs/promises')
const abbrev = require('abbrev')

// Patch the global fs module here at the app level
require('graceful-fs').gracefulify(require('fs'))
Expand All @@ -18,7 +19,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')
const { commands, aliases } = require('./utils/cmd-list.js')

class Npm extends EventEmitter {
static get version () {
Expand Down Expand Up @@ -84,18 +85,30 @@ class Npm extends EventEmitter {
if (!c) {
return
}

// Translate camelCase to snake-case (i.e. installTest to install-test)
if (c.match(/[A-Z]/)) {
c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase())
}
if (cmdList.plumbing.indexOf(c) !== -1) {

// if they asked for something exactly we are done
if (commands.includes(c)) {
return c
}

// if they asked for a direct alias
if (aliases[c]) {
return aliases[c]
}

const abbrevs = abbrev(commands.concat(Object.keys(aliases)))

// 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]
let a = abbrevs[c]
while (aliases[a]) {
a = aliases[a]
}
return a
}
Expand Down
150 changes: 71 additions & 79 deletions lib/utils/cmd-list.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,5 @@
const abbrev = require('abbrev')
const localeCompare = require('@isaacs/string-locale-compare')('en')

// plumbing should not have any aliases
const aliases = {

// aliases
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',
ln: 'link',
create: 'init',
i: 'install',
it: 'install-test',
cit: 'install-ci-test',
up: 'update',
c: 'config',
s: 'search',
se: 'search',
tst: 'test',
t: 'test',
ddp: 'dedupe',
v: 'view',
run: 'run-script',
'clean-install': 'ci',
'clean-install-test': 'install-ci-test',
x: 'exec',
why: 'explain',
la: 'll',
verison: 'version',
ic: 'ci',

// typos
innit: 'init',
// manually abbrev so that install-test doesn't make insta stop working
in: 'install',
ins: 'install',
inst: 'install',
insta: 'install',
instal: 'install',
isnt: 'install',
isnta: 'install',
isntal: 'install',
isntall: 'install',
'install-clean': 'ci',
'isntall-clean': 'ci',
hlep: 'help',
'dist-tags': 'dist-tag',
upgrade: 'update',
udpate: 'update',
rum: 'run-script',
sit: 'install-ci-test',
urn: 'run-script',
ogr: 'org',
'add-user': 'adduser',
}

// these are filenames in .
// These correspond to filenames in lib/commands
// Please keep this list sorted alphabetically
const commands = [
'access',
'adduser',
Expand All @@ -92,14 +23,15 @@ const commands = [
'fund',
'get',
'help',
'help-search',
'hook',
'init',
'install',
'install-ci-test',
'install-test',
'link',
'll',
'login', // This is an alias for `adduser` but it can be confusing
'login',
'logout',
'ls',
'org',
Expand Down Expand Up @@ -135,16 +67,76 @@ const commands = [
'version',
'view',
'whoami',
].sort(localeCompare)
]

// These must resolve to an entry in commands
const aliases = {

// aliases
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',
ln: 'link',
create: 'init',
i: 'install',
it: 'install-test',
cit: 'install-ci-test',
up: 'update',
c: 'config',
s: 'search',
se: 'search',
tst: 'test',
t: 'test',
ddp: 'dedupe',
v: 'view',
run: 'run-script',
'clean-install': 'ci',
'clean-install-test': 'install-ci-test',
x: 'exec',
why: 'explain',
la: 'll',
verison: 'version',
ic: 'ci',

const plumbing = ['help-search']
const allCommands = [...commands, ...plumbing].sort(localeCompare)
const abbrevs = abbrev(commands.concat(Object.keys(aliases)))
// typos
innit: 'init',
// manually abbrev so that install-test doesn't make insta stop working
in: 'install',
ins: 'install',
inst: 'install',
insta: 'install',
instal: 'install',
isnt: 'install',
isnta: 'install',
isntal: 'install',
isntall: 'install',
'install-clean': 'ci',
'isntall-clean': 'ci',
hlep: 'help',
'dist-tags': 'dist-tag',
upgrade: 'update',
udpate: 'update',
rum: 'run-script',
sit: 'install-ci-test',
urn: 'run-script',
ogr: 'org',
'add-user': 'adduser',
}

module.exports = {
abbrevs,
aliases,
commands,
plumbing,
allCommands,
}
13 changes: 7 additions & 6 deletions smoke-tests/tap-snapshots/test/index.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ All commands:
access, adduser, audit, bugs, cache, ci, completion,
config, dedupe, deprecate, diff, dist-tag, docs, doctor,
edit, exec, explain, explore, find-dupes, fund, get, help,
hook, init, install, install-ci-test, install-test, link,
ll, login, logout, ls, org, outdated, owner, pack, ping,
pkg, prefix, profile, prune, publish, query, rebuild, repo,
restart, root, run-script, search, set, shrinkwrap, star,
stars, start, stop, team, test, token, uninstall, unpublish,
unstar, update, version, view, whoami
help-search, hook, init, install, install-ci-test,
install-test, link, ll, login, logout, ls, org, outdated,
owner, pack, ping, pkg, prefix, profile, prune, publish,
query, rebuild, repo, restart, root, run-script, search,
set, shrinkwrap, star, stars, start, stop, team, test,
token, uninstall, unpublish, unstar, update, version, view,
whoami
Specify configs in the ini-formatted file:
{NPM}/{TESTDIR}/project/.npmrc
Expand Down
1 change: 1 addition & 0 deletions tap-snapshots/test/lib/commands/completion.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Array [
fund
get
help
help-search
hook
init
install
Expand Down
Loading

0 comments on commit e252532

Please sign in to comment.