diff options
author | Luke Karrys <luke@lukekarrys.com> | 2022-10-26 06:33:28 +0300 |
---|---|---|
committer | Gar <wraithgar@github.com> | 2022-11-01 21:44:18 +0300 |
commit | 5fc516b8ca254207c402c9b14e5f7fc2a1424996 (patch) | |
tree | ab1cae6ede1a87a8429371cb6626cf5a1a7d9b06 /lib/utils | |
parent | f3753006e472c5d77a01d24f9ac105a8eadb09a2 (diff) |
chore: update snapshot tests for new docs
Diffstat (limited to 'lib/utils')
-rw-r--r-- | lib/utils/cmd-list.js | 10 | ||||
-rw-r--r-- | lib/utils/config/describe-all.js | 20 | ||||
-rw-r--r-- | lib/utils/config/flatten.js | 33 | ||||
-rw-r--r-- | lib/utils/config/index.js | 59 | ||||
-rw-r--r-- | lib/utils/did-you-mean.js | 4 | ||||
-rw-r--r-- | lib/utils/npm-usage.js | 6 |
6 files changed, 52 insertions, 80 deletions
diff --git a/lib/utils/cmd-list.js b/lib/utils/cmd-list.js index 38439542a..a1f5ece66 100644 --- a/lib/utils/cmd-list.js +++ b/lib/utils/cmd-list.js @@ -1,4 +1,5 @@ const abbrev = require('abbrev') +const localeCompare = require('@isaacs/string-locale-compare')('en') // plumbing should not have any aliases const aliases = { @@ -69,8 +70,7 @@ const aliases = { } // these are filenames in . -// Keep these sorted so that lib/utils/npm-usage.js outputs in order -const cmdList = [ +const commands = [ 'access', 'adduser', 'audit', @@ -141,11 +141,13 @@ const cmdList = [ ] const plumbing = ['birthday', 'help-search'] -const abbrevs = abbrev(cmdList.concat(Object.keys(aliases))) +const allCommands = [...commands, ...plumbing].sort(localeCompare) +const abbrevs = abbrev(commands.concat(Object.keys(aliases))) module.exports = { abbrevs, aliases, - cmdList, + commands, plumbing, + allCommands, } diff --git a/lib/utils/config/describe-all.js b/lib/utils/config/describe-all.js deleted file mode 100644 index 39f8d5fe4..000000000 --- a/lib/utils/config/describe-all.js +++ /dev/null @@ -1,20 +0,0 @@ -const definitions = require('./definitions.js') -const localeCompare = require('@isaacs/string-locale-compare')('en') -const describeAll = () => { - // sort not-deprecated ones to the top - /* istanbul ignore next - typically already sorted in the definitions file, - * but this is here so that our help doc will stay consistent if we decide - * to move them around. */ - const sort = ([keya, { deprecated: depa }], [keyb, { deprecated: depb }]) => { - return depa && !depb ? 1 - : !depa && depb ? -1 - : localeCompare(keya, keyb) - } - return Object.entries(definitions).sort(sort) - .map(([key, def]) => def.describe()) - .join( - '\n\n<!-- automatically generated, do not edit manually -->\n' + - '<!-- see lib/utils/config/definitions.js -->\n\n' - ) -} -module.exports = describeAll diff --git a/lib/utils/config/flatten.js b/lib/utils/config/flatten.js deleted file mode 100644 index 588d05bf0..000000000 --- a/lib/utils/config/flatten.js +++ /dev/null @@ -1,33 +0,0 @@ -// use the defined flattening function, and copy over any scoped -// registries and registry-specific "nerfdart" configs verbatim -// -// TODO: make these getters so that we only have to make dirty -// the thing that changed, and then flatten the fields that -// could have changed when a config.set is called. -// -// TODO: move nerfdart auth stuff into a nested object that -// is only passed along to paths that end up calling npm-registry-fetch. -const definitions = require('./definitions.js') -const flatten = (obj, flat = {}) => { - for (const [key, val] of Object.entries(obj)) { - const def = definitions[key] - if (def && def.flatten) { - def.flatten(key, obj, flat) - } else if (/@.*:registry$/i.test(key) || /^\/\//.test(key)) { - flat[key] = val - } - } - - // XXX make this the bin/npm-cli.js file explicitly instead - // otherwise using npm programmatically is a bit of a pain. - flat.npmBin = require.main ? require.main.filename - : /* istanbul ignore next - not configurable property */ undefined - flat.nodeBin = process.env.NODE || process.execPath - - // XXX should this be sha512? is it even relevant? - flat.hashAlgorithm = 'sha1' - - return flat -} - -module.exports = flatten diff --git a/lib/utils/config/index.js b/lib/utils/config/index.js index b0ad24d7e..d393aec22 100644 --- a/lib/utils/config/index.js +++ b/lib/utils/config/index.js @@ -1,6 +1,35 @@ -const flatten = require('./flatten.js') const definitions = require('./definitions.js') -const describeAll = require('./describe-all.js') + +// use the defined flattening function, and copy over any scoped +// registries and registry-specific "nerfdart" configs verbatim +// +// TODO: make these getters so that we only have to make dirty +// the thing that changed, and then flatten the fields that +// could have changed when a config.set is called. +// +// TODO: move nerfdart auth stuff into a nested object that +// is only passed along to paths that end up calling npm-registry-fetch. +const flatten = (obj, flat = {}) => { + for (const [key, val] of Object.entries(obj)) { + const def = definitions[key] + if (def && def.flatten) { + def.flatten(key, obj, flat) + } else if (/@.*:registry$/i.test(key) || /^\/\//.test(key)) { + flat[key] = val + } + } + + // XXX make this the bin/npm-cli.js file explicitly instead + // otherwise using npm programmatically is a bit of a pain. + flat.npmBin = require.main ? require.main.filename + : /* istanbul ignore next - not configurable property */ undefined + flat.nodeBin = process.env.NODE || process.execPath + + // XXX should this be sha512? is it even relevant? + flat.hashAlgorithm = 'sha1' + + return flat +} // aliases where they get expanded into a completely different thing // these are NOT supported in the environment or npmrc files, only @@ -26,30 +55,24 @@ const shorthands = { readonly: ['--read-only'], reg: ['--registry'], iwr: ['--include-workspace-root'], -} - -for (const [key, { short }] of Object.entries(definitions)) { - if (!short) { - continue - } - // can be either an array or string - for (const s of [].concat(short)) { - shorthands[s] = [`--${key}`] - } + ...Object.entries(definitions).reduce((acc, [key, { short = [] }]) => { + // can be either an array or string + for (const s of [].concat(short)) { + acc[s] = [`--${key}`] + } + return acc + }, {}), } module.exports = { get defaults () { // NB: 'default' is a reserved word - return Object.entries(definitions).map(([key, { default: def }]) => { - return [key, def] - }).reduce((defaults, [key, def]) => { - defaults[key] = def - return defaults + return Object.entries(definitions).reduce((acc, [key, { default: d }]) => { + acc[key] = d + return acc }, {}) }, definitions, flatten, shorthands, - describeAll, } diff --git a/lib/utils/did-you-mean.js b/lib/utils/did-you-mean.js index b859abaaf..10b33d5f8 100644 --- a/lib/utils/did-you-mean.js +++ b/lib/utils/did-you-mean.js @@ -1,10 +1,10 @@ const { distance } = require('fastest-levenshtein') const readJson = require('read-package-json-fast') -const { cmdList } = require('./cmd-list.js') +const { commands } = require('./cmd-list.js') const didYouMean = async (npm, path, scmd) => { // const cmd = await npm.cmd(str) - const close = cmdList.filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 && scmd !== cmd) + const close = commands.filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 && scmd !== cmd) let best = [] for (const str of close) { const cmd = await npm.cmd(str) diff --git a/lib/utils/npm-usage.js b/lib/utils/npm-usage.js index 431995ecf..1eebc305f 100644 --- a/lib/utils/npm-usage.js +++ b/lib/utils/npm-usage.js @@ -1,5 +1,5 @@ const { dirname } = require('path') -const { cmdList } = require('./cmd-list') +const { commands } = require('./cmd-list') const localeCompare = require('@isaacs/string-locale-compare')('en') module.exports = async (npm) => { @@ -35,7 +35,7 @@ const allCommands = async (npm) => { if (npm.config.get('long')) { return usages(npm) } - return ('\n ' + wrap(cmdList)) + return ('\n ' + wrap(commands)) } const wrap = (arr) => { @@ -60,7 +60,7 @@ const usages = async (npm) => { // return a string of <command>: <usage> let maxLen = 0 const set = [] - for (const c of cmdList) { + for (const c of commands) { const cmd = await npm.cmd(c) set.push([c, cmd.usage]) maxLen = Math.max(maxLen, c.length) |