Skip to content

Commit

Permalink
test: Add test for npm-usage.js, and fix 'npm --long' output
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 1, 2021
1 parent 13a5e31 commit 894fa0a
Show file tree
Hide file tree
Showing 4 changed files with 647 additions and 35 deletions.
3 changes: 1 addition & 2 deletions lib/access.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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') {
Expand Down
58 changes: 25 additions & 33 deletions lib/utils/npm-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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))}
Expand All @@ -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')
}
Loading

0 comments on commit 894fa0a

Please sign in to comment.