UNPKG

15.8 kBJavaScriptView Raw
1'use strict'
2
3const inspect = require('util').inspect
4const isPromise = require('./is-promise')
5const { applyMiddleware, commandMiddlewareFactory } = require('./middleware')
6const path = require('path')
7const Parser = require('yargs-parser')
8
9const DEFAULT_MARKER = /(^\*)|(^\$0)/
10
11// handles parsing positional arguments,
12// and populating argv with said positional
13// arguments.
14module.exports = function command (yargs, usage, validation, globalMiddleware) {
15 const self = {}
16 let handlers = {}
17 let aliasMap = {}
18 let defaultCommand
19 globalMiddleware = globalMiddleware || []
20
21 self.addHandler = function addHandler (cmd, description, builder, handler, commandMiddleware) {
22 let aliases = []
23 const middlewares = commandMiddlewareFactory(commandMiddleware)
24 handler = handler || (() => {})
25
26 if (Array.isArray(cmd)) {
27 aliases = cmd.slice(1)
28 cmd = cmd[0]
29 } else if (typeof cmd === 'object') {
30 let command = (Array.isArray(cmd.command) || typeof cmd.command === 'string') ? cmd.command : moduleName(cmd)
31 if (cmd.aliases) command = [].concat(command).concat(cmd.aliases)
32 self.addHandler(command, extractDesc(cmd), cmd.builder, cmd.handler, cmd.middlewares)
33 return
34 }
35
36 // allow a module to be provided instead of separate builder and handler
37 if (typeof builder === 'object' && builder.builder && typeof builder.handler === 'function') {
38 self.addHandler([cmd].concat(aliases), description, builder.builder, builder.handler, builder.middlewares)
39 return
40 }
41
42 // parse positionals out of cmd string
43 const parsedCommand = self.parseCommand(cmd)
44
45 // remove positional args from aliases only
46 aliases = aliases.map(alias => self.parseCommand(alias).cmd)
47
48 // check for default and filter out '*''
49 let isDefault = false
50 const parsedAliases = [parsedCommand.cmd].concat(aliases).filter((c) => {
51 if (DEFAULT_MARKER.test(c)) {
52 isDefault = true
53 return false
54 }
55 return true
56 })
57
58 // standardize on $0 for default command.
59 if (parsedAliases.length === 0 && isDefault) parsedAliases.push('$0')
60
61 // shift cmd and aliases after filtering out '*'
62 if (isDefault) {
63 parsedCommand.cmd = parsedAliases[0]
64 aliases = parsedAliases.slice(1)
65 cmd = cmd.replace(DEFAULT_MARKER, parsedCommand.cmd)
66 }
67
68 // populate aliasMap
69 aliases.forEach((alias) => {
70 aliasMap[alias] = parsedCommand.cmd
71 })
72
73 if (description !== false) {
74 usage.command(cmd, description, isDefault, aliases)
75 }
76
77 handlers[parsedCommand.cmd] = {
78 original: cmd,
79 description: description,
80 handler,
81 builder: builder || {},
82 middlewares,
83 demanded: parsedCommand.demanded,
84 optional: parsedCommand.optional
85 }
86
87 if (isDefault) defaultCommand = handlers[parsedCommand.cmd]
88 }
89
90 self.addDirectory = function addDirectory (dir, context, req, callerFile, opts) {
91 opts = opts || {}
92 // disable recursion to support nested directories of subcommands
93 if (typeof opts.recurse !== 'boolean') opts.recurse = false
94 // exclude 'json', 'coffee' from require-directory defaults
95 if (!Array.isArray(opts.extensions)) opts.extensions = ['js']
96 // allow consumer to define their own visitor function
97 const parentVisit = typeof opts.visit === 'function' ? opts.visit : o => o
98 // call addHandler via visitor function
99 opts.visit = function visit (obj, joined, filename) {
100 const visited = parentVisit(obj, joined, filename)
101 // allow consumer to skip modules with their own visitor
102 if (visited) {
103 // check for cyclic reference
104 // each command file path should only be seen once per execution
105 if (~context.files.indexOf(joined)) return visited
106 // keep track of visited files in context.files
107 context.files.push(joined)
108 self.addHandler(visited)
109 }
110 return visited
111 }
112 require('require-directory')({ require: req, filename: callerFile }, dir, opts)
113 }
114
115 // lookup module object from require()d command and derive name
116 // if module was not require()d and no name given, throw error
117 function moduleName (obj) {
118 const mod = require('which-module')(obj)
119 if (!mod) throw new Error(`No command name given for module: ${inspect(obj)}`)
120 return commandFromFilename(mod.filename)
121 }
122
123 // derive command name from filename
124 function commandFromFilename (filename) {
125 return path.basename(filename, path.extname(filename))
126 }
127
128 function extractDesc (obj) {
129 for (let keys = ['describe', 'description', 'desc'], i = 0, l = keys.length, test; i < l; i++) {
130 test = obj[keys[i]]
131 if (typeof test === 'string' || typeof test === 'boolean') return test
132 }
133 return false
134 }
135
136 self.parseCommand = function parseCommand (cmd) {
137 const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ')
138 const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/)
139 const bregex = /\.*[\][<>]/g
140 const parsedCommand = {
141 cmd: (splitCommand.shift()).replace(bregex, ''),
142 demanded: [],
143 optional: []
144 }
145 splitCommand.forEach((cmd, i) => {
146 let variadic = false
147 cmd = cmd.replace(/\s/g, '')
148 if (/\.+[\]>]/.test(cmd) && i === splitCommand.length - 1) variadic = true
149 if (/^\[/.test(cmd)) {
150 parsedCommand.optional.push({
151 cmd: cmd.replace(bregex, '').split('|'),
152 variadic
153 })
154 } else {
155 parsedCommand.demanded.push({
156 cmd: cmd.replace(bregex, '').split('|'),
157 variadic
158 })
159 }
160 })
161 return parsedCommand
162 }
163
164 self.getCommands = () => Object.keys(handlers).concat(Object.keys(aliasMap))
165
166 self.getCommandHandlers = () => handlers
167
168 self.hasDefaultCommand = () => !!defaultCommand
169
170 self.runCommand = function runCommand (command, yargs, parsed, commandIndex) {
171 let aliases = parsed.aliases
172 const commandHandler = handlers[command] || handlers[aliasMap[command]] || defaultCommand
173 const currentContext = yargs.getContext()
174 let numFiles = currentContext.files.length
175 const parentCommands = currentContext.commands.slice()
176
177 // what does yargs look like after the builder is run?
178 let innerArgv = parsed.argv
179 let innerYargs = null
180 let positionalMap = {}
181 if (command) {
182 currentContext.commands.push(command)
183 currentContext.fullCommands.push(commandHandler.original)
184 }
185 if (typeof commandHandler.builder === 'function') {
186 // a function can be provided, which builds
187 // up a yargs chain and possibly returns it.
188 innerYargs = commandHandler.builder(yargs.reset(parsed.aliases))
189 if (!innerYargs || (typeof innerYargs._parseArgs !== 'function')) {
190 innerYargs = yargs
191 }
192 if (shouldUpdateUsage(innerYargs)) {
193 innerYargs.getUsageInstance().usage(
194 usageFromParentCommandsCommandHandler(parentCommands, commandHandler),
195 commandHandler.description
196 )
197 }
198 innerArgv = innerYargs._parseArgs(null, null, true, commandIndex)
199 aliases = innerYargs.parsed.aliases
200 } else if (typeof commandHandler.builder === 'object') {
201 // as a short hand, an object can instead be provided, specifying
202 // the options that a command takes.
203 innerYargs = yargs.reset(parsed.aliases)
204 if (shouldUpdateUsage(innerYargs)) {
205 innerYargs.getUsageInstance().usage(
206 usageFromParentCommandsCommandHandler(parentCommands, commandHandler),
207 commandHandler.description
208 )
209 }
210 Object.keys(commandHandler.builder).forEach((key) => {
211 innerYargs.option(key, commandHandler.builder[key])
212 })
213 innerArgv = innerYargs._parseArgs(null, null, true, commandIndex)
214 aliases = innerYargs.parsed.aliases
215 }
216
217 if (!yargs._hasOutput()) {
218 positionalMap = populatePositionals(commandHandler, innerArgv, currentContext, yargs)
219 }
220
221 const middlewares = globalMiddleware.slice(0).concat(commandHandler.middlewares)
222 applyMiddleware(innerArgv, yargs, middlewares, true)
223
224 // we apply validation post-hoc, so that custom
225 // checks get passed populated positional arguments.
226 if (!yargs._hasOutput()) yargs._runValidation(innerArgv, aliases, positionalMap, yargs.parsed.error)
227
228 if (commandHandler.handler && !yargs._hasOutput()) {
229 yargs._setHasOutput()
230 // to simplify the parsing of positionals in commands,
231 // we temporarily populate '--' rather than _, with arguments
232 const populateDoubleDash = !!yargs.getOptions().configuration['populate--']
233 if (!populateDoubleDash) yargs._copyDoubleDash(innerArgv)
234
235 innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false)
236 let handlerResult
237 if (isPromise(innerArgv)) {
238 handlerResult = innerArgv.then(argv => commandHandler.handler(argv))
239 } else {
240 handlerResult = commandHandler.handler(innerArgv)
241 }
242
243 const handlerFinishCommand = yargs.getHandlerFinishCommand()
244 if (isPromise(handlerResult)) {
245 yargs.getUsageInstance().cacheHelpMessage()
246 handlerResult
247 .then(value => {
248 if (handlerFinishCommand) {
249 handlerFinishCommand(value)
250 }
251 })
252 .catch(error => {
253 try {
254 yargs.getUsageInstance().fail(null, error)
255 } catch (err) {
256 // fail's throwing would cause an unhandled rejection.
257 }
258 })
259 .then(() => {
260 yargs.getUsageInstance().clearCachedHelpMessage()
261 })
262 } else {
263 if (handlerFinishCommand) {
264 handlerFinishCommand(handlerResult)
265 }
266 }
267 }
268
269 if (command) {
270 currentContext.commands.pop()
271 currentContext.fullCommands.pop()
272 }
273 numFiles = currentContext.files.length - numFiles
274 if (numFiles > 0) currentContext.files.splice(numFiles * -1, numFiles)
275
276 return innerArgv
277 }
278
279 function shouldUpdateUsage (yargs) {
280 return !yargs.getUsageInstance().getUsageDisabled() &&
281 yargs.getUsageInstance().getUsage().length === 0
282 }
283
284 function usageFromParentCommandsCommandHandler (parentCommands, commandHandler) {
285 const c = DEFAULT_MARKER.test(commandHandler.original) ? commandHandler.original.replace(DEFAULT_MARKER, '').trim() : commandHandler.original
286 const pc = parentCommands.filter((c) => { return !DEFAULT_MARKER.test(c) })
287 pc.push(c)
288 return `$0 ${pc.join(' ')}`
289 }
290
291 self.runDefaultBuilderOn = function (yargs) {
292 if (shouldUpdateUsage(yargs)) {
293 // build the root-level command string from the default string.
294 const commandString = DEFAULT_MARKER.test(defaultCommand.original)
295 ? defaultCommand.original : defaultCommand.original.replace(/^[^[\]<>]*/, '$0 ')
296 yargs.getUsageInstance().usage(
297 commandString,
298 defaultCommand.description
299 )
300 }
301 const builder = defaultCommand.builder
302 if (typeof builder === 'function') {
303 builder(yargs)
304 } else {
305 Object.keys(builder).forEach((key) => {
306 yargs.option(key, builder[key])
307 })
308 }
309 }
310
311 // transcribe all positional arguments "command <foo> <bar> [apple]"
312 // onto argv.
313 function populatePositionals (commandHandler, argv, context, yargs) {
314 argv._ = argv._.slice(context.commands.length) // nuke the current commands
315 const demanded = commandHandler.demanded.slice(0)
316 const optional = commandHandler.optional.slice(0)
317 const positionalMap = {}
318
319 validation.positionalCount(demanded.length, argv._.length)
320
321 while (demanded.length) {
322 const demand = demanded.shift()
323 populatePositional(demand, argv, positionalMap)
324 }
325
326 while (optional.length) {
327 const maybe = optional.shift()
328 populatePositional(maybe, argv, positionalMap)
329 }
330
331 argv._ = context.commands.concat(argv._)
332
333 postProcessPositionals(argv, positionalMap, self.cmdToParseOptions(commandHandler.original))
334
335 return positionalMap
336 }
337
338 function populatePositional (positional, argv, positionalMap, parseOptions) {
339 const cmd = positional.cmd[0]
340 if (positional.variadic) {
341 positionalMap[cmd] = argv._.splice(0).map(String)
342 } else {
343 if (argv._.length) positionalMap[cmd] = [String(argv._.shift())]
344 }
345 }
346
347 // we run yargs-parser against the positional arguments
348 // applying the same parsing logic used for flags.
349 function postProcessPositionals (argv, positionalMap, parseOptions) {
350 // combine the parsing hints we've inferred from the command
351 // string with explicitly configured parsing hints.
352 const options = Object.assign({}, yargs.getOptions())
353 options.default = Object.assign(parseOptions.default, options.default)
354 options.alias = Object.assign(parseOptions.alias, options.alias)
355 options.array = options.array.concat(parseOptions.array)
356 delete options.config // don't load config when processing positionals.
357
358 const unparsed = []
359 Object.keys(positionalMap).forEach((key) => {
360 positionalMap[key].map((value) => {
361 if (options.configuration['unknown-options-as-args']) options.key[key] = true
362 unparsed.push(`--${key}`)
363 unparsed.push(value)
364 })
365 })
366
367 // short-circuit parse.
368 if (!unparsed.length) return
369
370 const config = Object.assign({}, options.configuration, {
371 'populate--': true
372 })
373 const parsed = Parser.detailed(unparsed, Object.assign({}, options, {
374 configuration: config
375 }))
376
377 if (parsed.error) {
378 yargs.getUsageInstance().fail(parsed.error.message, parsed.error)
379 } else {
380 // only copy over positional keys (don't overwrite
381 // flag arguments that were already parsed).
382 const positionalKeys = Object.keys(positionalMap)
383 Object.keys(positionalMap).forEach((key) => {
384 [].push.apply(positionalKeys, parsed.aliases[key])
385 })
386
387 Object.keys(parsed.argv).forEach((key) => {
388 if (positionalKeys.indexOf(key) !== -1) {
389 // any new aliases need to be placed in positionalMap, which
390 // is used for validation.
391 if (!positionalMap[key]) positionalMap[key] = parsed.argv[key]
392 argv[key] = parsed.argv[key]
393 }
394 })
395 }
396 }
397
398 self.cmdToParseOptions = function (cmdString) {
399 const parseOptions = {
400 array: [],
401 default: {},
402 alias: {},
403 demand: {}
404 }
405
406 const parsed = self.parseCommand(cmdString)
407 parsed.demanded.forEach((d) => {
408 const cmds = d.cmd.slice(0)
409 const cmd = cmds.shift()
410 if (d.variadic) {
411 parseOptions.array.push(cmd)
412 parseOptions.default[cmd] = []
413 }
414 cmds.forEach((c) => {
415 parseOptions.alias[cmd] = c
416 })
417 parseOptions.demand[cmd] = true
418 })
419
420 parsed.optional.forEach((o) => {
421 const cmds = o.cmd.slice(0)
422 const cmd = cmds.shift()
423 if (o.variadic) {
424 parseOptions.array.push(cmd)
425 parseOptions.default[cmd] = []
426 }
427 cmds.forEach((c) => {
428 parseOptions.alias[cmd] = c
429 })
430 })
431
432 return parseOptions
433 }
434
435 self.reset = () => {
436 handlers = {}
437 aliasMap = {}
438 defaultCommand = undefined
439 return self
440 }
441
442 // used by yargs.parse() to freeze
443 // the state of commands such that
444 // we can apply .parse() multiple times
445 // with the same yargs instance.
446 const frozens = []
447 self.freeze = () => {
448 const frozen = {}
449 frozens.push(frozen)
450 frozen.handlers = handlers
451 frozen.aliasMap = aliasMap
452 frozen.defaultCommand = defaultCommand
453 }
454 self.unfreeze = () => {
455 const frozen = frozens.pop()
456 handlers = frozen.handlers
457 aliasMap = frozen.aliasMap
458 defaultCommand = frozen.defaultCommand
459 }
460
461 return self
462}