Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2021-02-01 23:34:05 +0300
committerisaacs <i@izs.me>2021-02-01 23:42:06 +0300
commit8c5ca2f516f5ac87f3bbd7f1fd95c0b283a21f14 (patch)
tree762fcc67e7958778f7d3a4a705c44bc2bab595f7 /lib/utils
parent13a5e31781cdaa37d3f007e1c8583c7cb591c62a (diff)
test: Add test for npm-usage.js, and fix 'npm --long' output
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/npm-usage.js58
1 files changed, 25 insertions, 33 deletions
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')
}