help: show usage when help-search finds no results · npm/cli@1faa5b3 (original) (raw)
`@@ -6,15 +6,13 @@ help.completion = function (opts, cb) {
`
6
6
`getSections(cb)
`
7
7
`}
`
8
8
``
``
9
`+
const npmUsage = require('./utils/npm-usage.js')
`
9
10
`var path = require('path')
`
10
11
`var spawn = require('./utils/spawn')
`
11
12
`var npm = require('./npm.js')
`
12
13
`var log = require('npmlog')
`
13
14
`var openUrl = require('./utils/open-url')
`
14
15
`var glob = require('glob')
`
15
``
`-
var didYouMean = require('./utils/did-you-mean')
`
16
``
`-
var cmdList = require('./utils/cmd-list').cmdList
`
17
``
`-
var commands = cmdList
`
18
16
`var output = require('./utils/output.js')
`
19
17
``
20
18
`const usage = require('./utils/usage.js')
`
`@@ -42,7 +40,8 @@ function help (args, cb) {
`
42
40
`// npm help : show basic usage
`
43
41
`if (!section) {
`
44
42
`var valid = argv[0] === 'help' ? 0 : 1
`
45
``
`-
return npmUsage(valid, cb)
`
``
43
`+
npmUsage(argv[0] === 'help')
`
``
44
`+
return cb()
`
46
45
`}
`
47
46
``
48
47
`// npm -h: show command usage
`
`@@ -161,83 +160,6 @@ function htmlMan (man) {
`
161
160
`return 'file://' + path.resolve(__dirname, '..', 'docs', 'public', sect, f, 'index.html')
`
162
161
`}
`
163
162
``
164
``
`-
function npmUsage (valid, cb) {
`
165
``
`-
npm.config.set('loglevel', 'silent')
`
166
``
`-
log.level = 'silent'
`
167
``
`` -
output(`
``
168
``
`-
Usage: npm
`
169
``
-
170
``
`-
npm install install all the dependencies in your project
`
171
``
`-
npm install add the dependency to your project
`
172
``
`-
npm test run this project's tests
`
173
``
`-
npm run run the script named
`
174
``
`-
npm -h quick help on
`
175
``
`-
npm -l display usage info for all commands
`
176
``
`-
npm help search for help on
`
177
``
`-
npm help npm more involved overview
`
178
``
-
179
``
`-
All commands:
`
180
``
`-
${npm.config.get('long') ? usages() : ('\n ' + wrap(commands))}
`
181
``
-
182
``
`-
Specify configs in the ini-formatted file:
`
183
``
`-
${npm.config.get('userconfig')}
`
184
``
`-
or on the command line via: npm --key=value
`
185
``
-
186
``
`-
More configuration info: npm help config
`
187
``
`-
Configuration fields: npm help 7 config
`
188
``
-
189
``
`-
npm@${npm.version} ${path.dirname(__dirname)}
`
190
``
`` -
`)
``
191
``
-
192
``
`-
if (npm.argv.length > 1) {
`
193
``
`-
output(didYouMean(npm.argv[1], commands))
`
194
``
`-
}
`
195
``
-
196
``
`-
cb(valid)
`
197
``
`-
}
`
198
``
-
199
``
`-
function usages () {
`
200
``
`-
// return a string of :
`
201
``
`-
var maxLen = 0
`
202
``
`-
return commands.reduce(function (set, c) {
`
203
``
`` -
set.push([c, require(./${npm.deref(c)}.js).usage || ''])
``
204
``
`-
maxLen = Math.max(maxLen, c.length)
`
205
``
`-
return set
`
206
``
`-
}, []).sort((a, b) => {
`
207
``
`-
return a[0].localeCompare(b[0])
`
208
``
`-
}).map(function (item) {
`
209
``
`-
var c = item[0]
`
210
``
`-
var usage = item[1]
`
211
``
`-
return '\n ' +
`
212
``
`-
c + (new Array(maxLen - c.length + 2).join(' ')) +
`
213
``
`-
(usage.split('\n').join('\n' + (new Array(maxLen + 6).join(' '))))
`
214
``
`-
}).join('\n')
`
215
``
`-
}
`
216
``
-
217
``
`-
function wrap (arr) {
`
218
``
`-
var out = ['']
`
219
``
`-
var l = 0
`
220
``
`-
var line
`
221
``
-
222
``
`-
line = process.stdout.columns
`
223
``
`-
if (!line) {
`
224
``
`-
line = 60
`
225
``
`-
} else {
`
226
``
`-
line = Math.min(60, Math.max(line - 16, 24))
`
227
``
`-
}
`
228
``
-
229
``
`-
arr.sort(function (a, b) { return a < b ? -1 : 1 })
`
230
``
`-
.forEach(function (c) {
`
231
``
`-
if (out[l].length + c.length + 2 < line) {
`
232
``
`-
out[l] += ', ' + c
`
233
``
`-
} else {
`
234
``
`-
out[l++] += ','
`
235
``
`-
out[l] = c
`
236
``
`-
}
`
237
``
`-
})
`
238
``
`-
return out.join('\n ').substr(2)
`
239
``
`-
}
`
240
``
-
241
163
`function getSections (cb) {
`
242
164
`var g = path.resolve(__dirname, '../man/man[0-9]/*.[0-9]')
`
243
165
`glob(g, function (er, files) {
`