• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const { commands } = require('./cmd-list')
2
3const COL_MAX = 60
4const COL_MIN = 24
5const COL_GUTTER = 16
6const INDENT = 4
7
8const indent = (repeat = INDENT) => ' '.repeat(repeat)
9const indentNewline = (repeat) => `\n${indent(repeat)}`
10
11module.exports = (npm) => {
12  const browser = npm.config.get('viewer') === 'browser' ? ' (in a browser)' : ''
13  const allCommands = npm.config.get('long') ? cmdUsages(npm.constructor) : cmdNames()
14
15  return `npm <command>
16
17Usage:
18
19npm install        install all the dependencies in your project
20npm install <foo>  add the <foo> dependency to your project
21npm test           run this project's tests
22npm run <foo>      run the script named <foo>
23npm <command> -h   quick help on <command>
24npm -l             display usage info for all commands
25npm help <term>    search for help on <term>${browser}
26npm help npm       more involved overview${browser}
27
28All commands:
29${allCommands}
30
31Specify configs in the ini-formatted file:
32${indent() + npm.config.get('userconfig')}
33or on the command line via: npm <command> --key=value
34
35More configuration info: npm help config
36Configuration fields: npm help 7 config
37
38npm@${npm.version} ${npm.npmRoot}`
39}
40
41const cmdNames = () => {
42  const out = ['']
43
44  const line = !process.stdout.columns ? COL_MAX
45    : Math.min(COL_MAX, Math.max(process.stdout.columns - COL_GUTTER, COL_MIN))
46
47  let l = 0
48  for (const c of commands) {
49    if (out[l].length + c.length + 2 < line) {
50      out[l] += ', ' + c
51    } else {
52      out[l++] += ','
53      out[l] = c
54    }
55  }
56
57  return indentNewline() + out.join(indentNewline()).slice(2)
58}
59
60const cmdUsages = (Npm) => {
61  // return a string of <command>: <usage>
62  let maxLen = 0
63  const set = []
64  for (const c of commands) {
65    set.push([c, Npm.cmd(c).describeUsage.split('\n')])
66    maxLen = Math.max(maxLen, c.length)
67  }
68
69  return set.map(([name, usageLines]) => {
70    const gutter = indent(maxLen - name.length + 1)
71    const usage = usageLines.join(indentNewline(INDENT + maxLen + 1))
72    return indentNewline() + name + gutter + usage
73  }).join('\n')
74}
75