diff options
author | isaacs <i@izs.me> | 2021-02-01 23:34:05 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2021-02-01 23:42:06 +0300 |
commit | 8c5ca2f516f5ac87f3bbd7f1fd95c0b283a21f14 (patch) | |
tree | 762fcc67e7958778f7d3a4a705c44bc2bab595f7 /lib | |
parent | 13a5e31781cdaa37d3f007e1c8583c7cb591c62a (diff) |
test: Add test for npm-usage.js, and fix 'npm --long' output
Diffstat (limited to 'lib')
-rw-r--r-- | lib/access.js | 3 | ||||
-rw-r--r-- | lib/utils/npm-usage.js | 58 |
2 files changed, 26 insertions, 35 deletions
diff --git a/lib/access.js b/lib/access.js index 68c6e628d..8a372d90c 100644 --- a/lib/access.js +++ b/lib/access.js @@ -8,7 +8,6 @@ const output = require('./utils/output.js') const otplease = require('./utils/otplease.js') const usageUtil = require('./utils/usage.js') const getIdentity = require('./utils/get-identity.js') -const { prefix } = npm const usage = usageUtil( 'npm access', @@ -165,7 +164,7 @@ const getPackage = async (name, requireScope) => { return name.trim() else { try { - const pkg = await readPackageJson(path.resolve(prefix, 'package.json')) + const pkg = await readPackageJson(path.resolve(npm.prefix, 'package.json')) name = pkg.name } catch (err) { if (err.code === 'ENOENT') { diff --git a/lib/utils/npm-usage.js b/lib/utils/npm-usage.js index 3ecf068ed..d4261f79d 100644 --- a/lib/utils/npm-usage.js +++ b/lib/utils/npm-usage.js @@ -6,6 +6,8 @@ const { cmdList } = require('./cmd-list') module.exports = (valid = true) => { npm.config.set('loglevel', 'silent') + const usesBrowser = npm.config.get('viewer') === 'browser' + ? ' (in a browser)' : '' npm.log.level = 'silent' output(` Usage: npm <command> @@ -16,8 +18,8 @@ npm test run this project's tests npm run <foo> run the script named <foo> npm <command> -h quick help on <command> npm -l display usage info for all commands -npm help <term> search for help on <term> (in a browser) -npm help npm more involved overview (in a browser) +npm help <term> search for help on <term>${usesBrowser} +npm help npm more involved overview${usesBrowser} All commands: ${npm.config.get('long') ? usages() : ('\n ' + wrap(cmdList))} @@ -40,44 +42,34 @@ npm@${npm.version} ${dirname(dirname(__dirname))} } const wrap = (arr) => { - var out = [''] - var l = 0 - var line + const out = [''] - line = process.stdout.columns - if (!line) - line = 60 - else - line = Math.min(60, Math.max(line - 16, 24)) + const line = !process.stdout.columns ? 60 + : Math.min(60, Math.max(process.stdout.columns - 16, 24)) - arr.sort(function (a, b) { - return a < b ? -1 : 1 - }) - .forEach(function (c) { - if (out[l].length + c.length + 2 < line) - out[l] += ', ' + c - else { - out[l++] += ',' - out[l] = c - } - }) + let l = 0 + for (const c of arr.sort((a, b) => a < b ? -1 : 1)) { + if (out[l].length + c.length + 2 < line) + out[l] += ', ' + c + else { + out[l++] += ',' + out[l] = c + } + } return out.join('\n ').substr(2) } const usages = () => { // return a string of <command>: <usage> - var maxLen = 0 - return cmdList.reduce(function (set, c) { - set.push([c, require(`./${npm.deref(c)}.js`).usage || '']) + let maxLen = 0 + return cmdList.reduce((set, c) => { + set.push([c, require(`../${npm.deref(c)}.js`).usage || + /* istanbul ignore next - all commands should have usage */ '']) maxLen = Math.max(maxLen, c.length) return set - }, []).sort((a, b) => { - return a[0].localeCompare(b[0]) - }).map(function (item) { - var c = item[0] - var usage = item[1] - return '\n ' + - c + (new Array(maxLen - c.length + 2).join(' ')) + - (usage.split('\n').join('\n' + (new Array(maxLen + 6).join(' ')))) - }).join('\n') + }, []) + .sort((a, b) => a[0].localeCompare(b[0])) + .map(([c, usage]) => `\n ${c}${' '.repeat(maxLen - c.length + 1)}${ + (usage.split('\n').join('\n' + ' '.repeat(maxLen + 5)))}`) + .join('\n') } |