Skip to content

Commit 894fa0a

Browse files
committed
test: Add test for npm-usage.js, and fix 'npm --long' output
1 parent 13a5e31 commit 894fa0a

File tree

4 files changed

+647
-35
lines changed

4 files changed

+647
-35
lines changed

lib/access.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const output = require('./utils/output.js')
88
const otplease = require('./utils/otplease.js')
99
const usageUtil = require('./utils/usage.js')
1010
const getIdentity = require('./utils/get-identity.js')
11-
const { prefix } = npm
1211

1312
const usage = usageUtil(
1413
'npm access',
@@ -165,7 +164,7 @@ const getPackage = async (name, requireScope) => {
165164
return name.trim()
166165
else {
167166
try {
168-
const pkg = await readPackageJson(path.resolve(prefix, 'package.json'))
167+
const pkg = await readPackageJson(path.resolve(npm.prefix, 'package.json'))
169168
name = pkg.name
170169
} catch (err) {
171170
if (err.code === 'ENOENT') {

lib/utils/npm-usage.js

+25-33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const { cmdList } = require('./cmd-list')
66

77
module.exports = (valid = true) => {
88
npm.config.set('loglevel', 'silent')
9+
const usesBrowser = npm.config.get('viewer') === 'browser'
10+
? ' (in a browser)' : ''
911
npm.log.level = 'silent'
1012
output(`
1113
Usage: npm <command>
@@ -16,8 +18,8 @@ npm test run this project's tests
1618
npm run <foo> run the script named <foo>
1719
npm <command> -h quick help on <command>
1820
npm -l display usage info for all commands
19-
npm help <term> search for help on <term> (in a browser)
20-
npm help npm more involved overview (in a browser)
21+
npm help <term> search for help on <term>${usesBrowser}
22+
npm help npm more involved overview${usesBrowser}
2123
2224
All commands:
2325
${npm.config.get('long') ? usages() : ('\n ' + wrap(cmdList))}
@@ -40,44 +42,34 @@ npm@${npm.version} ${dirname(dirname(__dirname))}
4042
}
4143

4244
const wrap = (arr) => {
43-
var out = ['']
44-
var l = 0
45-
var line
45+
const out = ['']
4646

47-
line = process.stdout.columns
48-
if (!line)
49-
line = 60
50-
else
51-
line = Math.min(60, Math.max(line - 16, 24))
47+
const line = !process.stdout.columns ? 60
48+
: Math.min(60, Math.max(process.stdout.columns - 16, 24))
5249

53-
arr.sort(function (a, b) {
54-
return a < b ? -1 : 1
55-
})
56-
.forEach(function (c) {
57-
if (out[l].length + c.length + 2 < line)
58-
out[l] += ', ' + c
59-
else {
60-
out[l++] += ','
61-
out[l] = c
62-
}
63-
})
50+
let l = 0
51+
for (const c of arr.sort((a, b) => a < b ? -1 : 1)) {
52+
if (out[l].length + c.length + 2 < line)
53+
out[l] += ', ' + c
54+
else {
55+
out[l++] += ','
56+
out[l] = c
57+
}
58+
}
6459
return out.join('\n ').substr(2)
6560
}
6661

6762
const usages = () => {
6863
// return a string of <command>: <usage>
69-
var maxLen = 0
70-
return cmdList.reduce(function (set, c) {
71-
set.push([c, require(`./${npm.deref(c)}.js`).usage || ''])
64+
let maxLen = 0
65+
return cmdList.reduce((set, c) => {
66+
set.push([c, require(`../${npm.deref(c)}.js`).usage ||
67+
/* istanbul ignore next - all commands should have usage */ ''])
7268
maxLen = Math.max(maxLen, c.length)
7369
return set
74-
}, []).sort((a, b) => {
75-
return a[0].localeCompare(b[0])
76-
}).map(function (item) {
77-
var c = item[0]
78-
var usage = item[1]
79-
return '\n ' +
80-
c + (new Array(maxLen - c.length + 2).join(' ')) +
81-
(usage.split('\n').join('\n' + (new Array(maxLen + 6).join(' '))))
82-
}).join('\n')
70+
}, [])
71+
.sort((a, b) => a[0].localeCompare(b[0]))
72+
.map(([c, usage]) => `\n ${c}${' '.repeat(maxLen - c.length + 1)}${
73+
(usage.split('\n').join('\n' + ' '.repeat(maxLen + 5)))}`)
74+
.join('\n')
8375
}

0 commit comments

Comments
 (0)