From 3d6d9ac78231aecae7f82298e27fdfacf55411d5 Mon Sep 17 00:00:00 2001 From: Gar Date: Thu, 23 Mar 2023 13:52:32 -0700 Subject: [PATCH] fix: do less work looking up commands --- lib/commands/completion.js | 9 +- lib/npm.js | 23 +- lib/utils/cmd-list.js | 150 +++--- .../tap-snapshots/test/index.js.test.cjs | 13 +- .../test/lib/commands/completion.js.test.cjs | 1 + tap-snapshots/test/lib/docs.js.test.cjs | 421 +--------------- tap-snapshots/test/lib/npm.js.test.cjs | 450 ++++++++++++++++++ test/lib/docs.js | 4 +- test/lib/load-all-commands.js | 4 +- test/lib/npm.js | 43 +- 10 files changed, 567 insertions(+), 551 deletions(-) create mode 100644 tap-snapshots/test/lib/npm.js.test.cjs diff --git a/lib/commands/completion.js b/lib/commands/completion.js index 49a66627cca2c..efbad9d61001b 100644 --- a/lib/commands/completion.js +++ b/lib/commands/completion.js @@ -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) @@ -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 } @@ -273,7 +272,7 @@ const cmdCompl = (opts, npm) => { return [...derefs] } - return fullList + return allCommands } module.exports = Completion diff --git a/lib/npm.js b/lib/npm.js index 841d145ddcbad..19544407b8166 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -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')) @@ -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 () { @@ -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 } diff --git a/lib/utils/cmd-list.js b/lib/utils/cmd-list.js index 68074fe9a4286..e5479139033d5 100644 --- a/lib/utils/cmd-list.js +++ b/lib/utils/cmd-list.js @@ -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', @@ -92,6 +23,7 @@ const commands = [ 'fund', 'get', 'help', + 'help-search', 'hook', 'init', 'install', @@ -99,7 +31,7 @@ const commands = [ 'install-test', 'link', 'll', - 'login', // This is an alias for `adduser` but it can be confusing + 'login', 'logout', 'ls', 'org', @@ -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, } diff --git a/smoke-tests/tap-snapshots/test/index.js.test.cjs b/smoke-tests/tap-snapshots/test/index.js.test.cjs index d5ce8b826be1e..615fd5286307e 100644 --- a/smoke-tests/tap-snapshots/test/index.js.test.cjs +++ b/smoke-tests/tap-snapshots/test/index.js.test.cjs @@ -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 diff --git a/tap-snapshots/test/lib/commands/completion.js.test.cjs b/tap-snapshots/test/lib/commands/completion.js.test.cjs index 3e7125bc33ba9..b21e378b1cfbe 100644 --- a/tap-snapshots/test/lib/commands/completion.js.test.cjs +++ b/tap-snapshots/test/lib/commands/completion.js.test.cjs @@ -66,6 +66,7 @@ Array [ fund get help + help-search hook init install diff --git a/tap-snapshots/test/lib/docs.js.test.cjs b/tap-snapshots/test/lib/docs.js.test.cjs index fe9830b1fba8c..fc7f70481be7b 100644 --- a/tap-snapshots/test/lib/docs.js.test.cjs +++ b/tap-snapshots/test/lib/docs.js.test.cjs @@ -33,349 +33,6 @@ Configuration fields: npm help 7 config npm@{VERSION} {BASEDIR} ` -exports[`test/lib/docs.js TAP command list > abbrevs 1`] = ` -Object { - "ac": "access", - "acc": "access", - "acce": "access", - "acces": "access", - "access": "access", - "add": "add", - "add-": "add-user", - "add-u": "add-user", - "add-us": "add-user", - "add-use": "add-user", - "add-user": "add-user", - "addu": "adduser", - "addus": "adduser", - "adduse": "adduser", - "adduser": "adduser", - "aud": "audit", - "audi": "audit", - "audit": "audit", - "aut": "author", - "auth": "author", - "autho": "author", - "author": "author", - "b": "bugs", - "bu": "bugs", - "bug": "bugs", - "bugs": "bugs", - "c": "c", - "ca": "cache", - "cac": "cache", - "cach": "cache", - "cache": "cache", - "ci": "ci", - "cit": "cit", - "clean-install": "clean-install", - "clean-install-": "clean-install-test", - "clean-install-t": "clean-install-test", - "clean-install-te": "clean-install-test", - "clean-install-tes": "clean-install-test", - "clean-install-test": "clean-install-test", - "com": "completion", - "comp": "completion", - "compl": "completion", - "comple": "completion", - "complet": "completion", - "completi": "completion", - "completio": "completion", - "completion": "completion", - "con": "config", - "conf": "config", - "confi": "config", - "config": "config", - "cr": "create", - "cre": "create", - "crea": "create", - "creat": "create", - "create": "create", - "dd": "ddp", - "ddp": "ddp", - "ded": "dedupe", - "dedu": "dedupe", - "dedup": "dedupe", - "dedupe": "dedupe", - "dep": "deprecate", - "depr": "deprecate", - "depre": "deprecate", - "deprec": "deprecate", - "depreca": "deprecate", - "deprecat": "deprecate", - "deprecate": "deprecate", - "dif": "diff", - "diff": "diff", - "dist-tag": "dist-tag", - "dist-tags": "dist-tags", - "docs": "docs", - "doct": "doctor", - "docto": "doctor", - "doctor": "doctor", - "ed": "edit", - "edi": "edit", - "edit": "edit", - "exe": "exec", - "exec": "exec", - "expla": "explain", - "explai": "explain", - "explain": "explain", - "explo": "explore", - "explor": "explore", - "explore": "explore", - "find": "find", - "find-": "find-dupes", - "find-d": "find-dupes", - "find-du": "find-dupes", - "find-dup": "find-dupes", - "find-dupe": "find-dupes", - "find-dupes": "find-dupes", - "fu": "fund", - "fun": "fund", - "fund": "fund", - "g": "get", - "ge": "get", - "get": "get", - "he": "help", - "hel": "help", - "help": "help", - "hl": "hlep", - "hle": "hlep", - "hlep": "hlep", - "hom": "home", - "home": "home", - "hoo": "hook", - "hook": "hook", - "i": "i", - "ic": "ic", - "in": "in", - "inf": "info", - "info": "info", - "ini": "init", - "init": "init", - "inn": "innit", - "inni": "innit", - "innit": "innit", - "ins": "ins", - "inst": "inst", - "insta": "insta", - "instal": "instal", - "install": "install", - "install-ci": "install-ci-test", - "install-ci-": "install-ci-test", - "install-ci-t": "install-ci-test", - "install-ci-te": "install-ci-test", - "install-ci-tes": "install-ci-test", - "install-ci-test": "install-ci-test", - "install-cl": "install-clean", - "install-cle": "install-clean", - "install-clea": "install-clean", - "install-clean": "install-clean", - "install-t": "install-test", - "install-te": "install-test", - "install-tes": "install-test", - "install-test": "install-test", - "isnt": "isnt", - "isnta": "isnta", - "isntal": "isntal", - "isntall": "isntall", - "isntall-": "isntall-clean", - "isntall-c": "isntall-clean", - "isntall-cl": "isntall-clean", - "isntall-cle": "isntall-clean", - "isntall-clea": "isntall-clean", - "isntall-clean": "isntall-clean", - "iss": "issues", - "issu": "issues", - "issue": "issues", - "issues": "issues", - "it": "it", - "la": "la", - "lin": "link", - "link": "link", - "lis": "list", - "list": "list", - "ll": "ll", - "ln": "ln", - "logi": "login", - "login": "login", - "logo": "logout", - "logou": "logout", - "logout": "logout", - "ls": "ls", - "og": "ogr", - "ogr": "ogr", - "or": "org", - "org": "org", - "ou": "outdated", - "out": "outdated", - "outd": "outdated", - "outda": "outdated", - "outdat": "outdated", - "outdate": "outdated", - "outdated": "outdated", - "ow": "owner", - "own": "owner", - "owne": "owner", - "owner": "owner", - "pa": "pack", - "pac": "pack", - "pack": "pack", - "pi": "ping", - "pin": "ping", - "ping": "ping", - "pk": "pkg", - "pkg": "pkg", - "pre": "prefix", - "pref": "prefix", - "prefi": "prefix", - "prefix": "prefix", - "pro": "profile", - "prof": "profile", - "profi": "profile", - "profil": "profile", - "profile": "profile", - "pru": "prune", - "prun": "prune", - "prune": "prune", - "pu": "publish", - "pub": "publish", - "publ": "publish", - "publi": "publish", - "publis": "publish", - "publish": "publish", - "q": "query", - "qu": "query", - "que": "query", - "quer": "query", - "query": "query", - "r": "r", - "rb": "rb", - "reb": "rebuild", - "rebu": "rebuild", - "rebui": "rebuild", - "rebuil": "rebuild", - "rebuild": "rebuild", - "rem": "remove", - "remo": "remove", - "remov": "remove", - "remove": "remove", - "rep": "repo", - "repo": "repo", - "res": "restart", - "rest": "restart", - "resta": "restart", - "restar": "restart", - "restart": "restart", - "rm": "rm", - "ro": "root", - "roo": "root", - "root": "root", - "rum": "rum", - "run": "run", - "run-": "run-script", - "run-s": "run-script", - "run-sc": "run-script", - "run-scr": "run-script", - "run-scri": "run-script", - "run-scrip": "run-script", - "run-script": "run-script", - "s": "s", - "se": "se", - "sea": "search", - "sear": "search", - "searc": "search", - "search": "search", - "set": "set", - "sho": "show", - "show": "show", - "shr": "shrinkwrap", - "shri": "shrinkwrap", - "shrin": "shrinkwrap", - "shrink": "shrinkwrap", - "shrinkw": "shrinkwrap", - "shrinkwr": "shrinkwrap", - "shrinkwra": "shrinkwrap", - "shrinkwrap": "shrinkwrap", - "si": "sit", - "sit": "sit", - "star": "star", - "stars": "stars", - "start": "start", - "sto": "stop", - "stop": "stop", - "t": "t", - "tea": "team", - "team": "team", - "tes": "test", - "test": "test", - "to": "token", - "tok": "token", - "toke": "token", - "token": "token", - "ts": "tst", - "tst": "tst", - "ud": "udpate", - "udp": "udpate", - "udpa": "udpate", - "udpat": "udpate", - "udpate": "udpate", - "un": "un", - "uni": "uninstall", - "unin": "uninstall", - "unins": "uninstall", - "uninst": "uninstall", - "uninsta": "uninstall", - "uninstal": "uninstall", - "uninstall": "uninstall", - "unl": "unlink", - "unli": "unlink", - "unlin": "unlink", - "unlink": "unlink", - "unp": "unpublish", - "unpu": "unpublish", - "unpub": "unpublish", - "unpubl": "unpublish", - "unpubli": "unpublish", - "unpublis": "unpublish", - "unpublish": "unpublish", - "uns": "unstar", - "unst": "unstar", - "unsta": "unstar", - "unstar": "unstar", - "up": "up", - "upd": "update", - "upda": "update", - "updat": "update", - "update": "update", - "upg": "upgrade", - "upgr": "upgrade", - "upgra": "upgrade", - "upgrad": "upgrade", - "upgrade": "upgrade", - "ur": "urn", - "urn": "urn", - "v": "v", - "veri": "verison", - "veris": "verison", - "veriso": "verison", - "verison": "verison", - "vers": "version", - "versi": "version", - "versio": "version", - "version": "version", - "vi": "view", - "vie": "view", - "view": "view", - "who": "whoami", - "whoa": "whoami", - "whoam": "whoami", - "whoami": "whoami", - "why": "why", - "x": "x", -} -` - exports[`test/lib/docs.js TAP command list > aliases 1`] = ` Object { "add": "install", @@ -437,77 +94,6 @@ Object { } ` -exports[`test/lib/docs.js TAP command list > allCommands 1`] = ` -Array [ - "access", - "adduser", - "audit", - "bugs", - "cache", - "ci", - "completion", - "config", - "dedupe", - "deprecate", - "diff", - "dist-tag", - "docs", - "doctor", - "edit", - "exec", - "explain", - "explore", - "find-dupes", - "fund", - "get", - "help", - "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", -] -` - exports[`test/lib/docs.js TAP command list > commands 1`] = ` Array [ "access", @@ -532,6 +118,7 @@ Array [ "fund", "get", "help", + "help-search", "hook", "init", "install", @@ -578,12 +165,6 @@ Array [ ] ` -exports[`test/lib/docs.js TAP command list > plumbing 1`] = ` -Array [ - "help-search", -] -` - exports[`test/lib/docs.js TAP config > all definitions 1`] = ` #### \`_auth\` diff --git a/tap-snapshots/test/lib/npm.js.test.cjs b/tap-snapshots/test/lib/npm.js.test.cjs new file mode 100644 index 0000000000000..15e7d9c52a216 --- /dev/null +++ b/tap-snapshots/test/lib/npm.js.test.cjs @@ -0,0 +1,450 @@ +/* IMPORTANT + * This snapshot file is auto-generated, but designed for humans. + * It should be checked into source control and tracked carefully. + * Re-generate by setting TAP_SNAPSHOT=1 and running tests. + * Make sure to inspect the output below. Do not ignore changes! + */ +'use strict' +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 0 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` + +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 1 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` + +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 10 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` + +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 100 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` + +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 24 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` + +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 40 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` + +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 41 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` + +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 75 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` + +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 76 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` + +exports[`test/lib/npm.js TAP usage set process.stdout.columns column width 90 > must match snapshot 1`] = ` +npm + +Usage: + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +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, + 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: + {USERCONFIG} +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@9.6.2 {NPMROOT} +` diff --git a/test/lib/docs.js b/test/lib/docs.js index e8a188b6ad8c4..b8a1a4fc60074 100644 --- a/test/lib/docs.js +++ b/test/lib/docs.js @@ -66,12 +66,12 @@ t.test('usage', async t => { // are all in sync. eg, this will error if a command is removed but not its docs file t.strictSame( fsCommands.sort(localeCompare), - cmdList.allCommands, + cmdList.commands, 'command list and fs are the same' ) t.strictSame( allDocs.filter(f => !bareCommands.includes(f)).sort(localeCompare), - cmdList.allCommands, + cmdList.commands, 'command list and docs files are the same' ) diff --git a/test/lib/load-all-commands.js b/test/lib/load-all-commands.js index dd55560369310..1742376a36e69 100644 --- a/test/lib/load-all-commands.js +++ b/test/lib/load-all-commands.js @@ -5,12 +5,12 @@ const t = require('tap') const util = require('util') const { load: loadMockNpm } = require('../fixtures/mock-npm.js') -const { allCommands } = require('../../lib/utils/cmd-list.js') +const { commands } = require('../../lib/utils/cmd-list.js') const isAsyncFn = (v) => typeof v === 'function' && /^\[AsyncFunction:/.test(util.inspect(v)) t.test('load each command', async t => { - for (const cmd of allCommands) { + for (const cmd of commands) { t.test(cmd, async t => { const { npm, outputs, cmd: impl } = await loadMockNpm(t, { command: cmd, diff --git a/test/lib/npm.js b/test/lib/npm.js index e6936b3e36d5f..88b5b08873235 100644 --- a/test/lib/npm.js +++ b/test/lib/npm.js @@ -561,6 +561,7 @@ t.test('aliases and typos', async t => { await t.resolves(npm.cmd('it'), { name: 'install-test' }) await t.resolves(npm.cmd('installTe'), { name: 'install-test' }) await t.resolves(npm.cmd('access'), { name: 'access' }) + await t.resolves(npm.cmd('auth'), { name: 'owner' }) }) t.test('explicit workspace rejection', async t => { @@ -706,40 +707,18 @@ t.test('usage', async t => { }) t.test('set process.stdout.columns', async t => { - const { npm } = await loadMockNpm(t) + const { npm } = await loadMockNpm(t, { + config: { viewer: 'man' } + }) + t.cleanSnapshot = str => str.replace(npm.config.get('userconfig'), '{USERCONFIG}').replace(npm.npmRoot, '{NPMROOT}') - const colUsage = async (cols) => { - const usages = [] - for (const col of cols) { - mockGlobals(t, { 'process.stdout.columns': col }) + const widths = [0, 1, 10, 24, 40, 41, 75, 76, 90, 100] + for (const width of widths) { + t.test(`column width ${width}`, async t => { + mockGlobals(t, { 'process.stdout.columns': width }) const usage = await npm.usage - usages.push(usage) - } - return usages + t.matchSnapshot(usage) + }) } - - t.test('max size', async t => { - const usages = await colUsage([0, 76, 90, 100]) - t.equal(usages.filter(Boolean).length, 4) - t.equal(new Set([...usages]).size, 1) - }) - - t.test('across max boundary', async t => { - const usages = await colUsage([75, 76]) - t.equal(usages.filter(Boolean).length, 2) - t.equal(new Set([...usages]).size, 2) - }) - - t.test('min size', async t => { - const usages = await colUsage([1, 10, 24, 40]) - t.equal(usages.filter(Boolean).length, 4) - t.equal(new Set([...usages]).size, 1) - }) - - t.test('different cols within min/max', async t => { - const usages = await colUsage([40, 41]) - t.equal(usages.filter(Boolean).length, 2) - t.equal(new Set([...usages]).size, 2) - }) }) })