yargs 15.3.1 → 15.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE +2 -3
  3. package/README.md +5 -5
  4. package/build/lib/apply-extends.d.ts +2 -0
  5. package/build/lib/apply-extends.js +65 -0
  6. package/build/lib/argsert.d.ts +2 -0
  7. package/build/lib/argsert.js +65 -0
  8. package/build/lib/command.d.ts +64 -0
  9. package/build/lib/command.js +416 -0
  10. package/build/lib/common-types.d.ts +36 -0
  11. package/build/lib/common-types.js +25 -0
  12. package/build/lib/completion-templates.d.ts +2 -0
  13. package/{lib → build/lib}/completion-templates.js +6 -5
  14. package/build/lib/completion.d.ts +21 -0
  15. package/build/lib/completion.js +135 -0
  16. package/build/lib/is-promise.d.ts +1 -0
  17. package/build/lib/is-promise.js +9 -0
  18. package/build/lib/levenshtein.d.ts +1 -0
  19. package/{lib → build/lib}/levenshtein.js +33 -33
  20. package/build/lib/middleware.d.ts +10 -0
  21. package/build/lib/middleware.js +57 -0
  22. package/build/lib/obj-filter.d.ts +1 -0
  23. package/build/lib/obj-filter.js +14 -0
  24. package/build/lib/parse-command.d.ts +11 -0
  25. package/build/lib/parse-command.js +36 -0
  26. package/build/lib/process-argv.d.ts +2 -0
  27. package/build/lib/process-argv.js +31 -0
  28. package/build/lib/usage.d.ts +49 -0
  29. package/build/lib/usage.js +540 -0
  30. package/build/lib/validation.d.ts +34 -0
  31. package/build/lib/validation.js +330 -0
  32. package/build/lib/yargs.d.ts +274 -0
  33. package/build/lib/yargs.js +1190 -0
  34. package/build/lib/yerror.d.ts +4 -0
  35. package/build/lib/yerror.js +11 -0
  36. package/index.js +1 -1
  37. package/locales/ja.json +6 -4
  38. package/package.json +26 -13
  39. package/yargs.js +2 -1291
  40. package/lib/apply-extends.js +0 -67
  41. package/lib/argsert.js +0 -68
  42. package/lib/command.js +0 -462
  43. package/lib/completion.js +0 -132
  44. package/lib/is-promise.js +0 -3
  45. package/lib/middleware.js +0 -64
  46. package/lib/obj-filter.js +0 -11
  47. package/lib/process-argv.js +0 -34
  48. package/lib/usage.js +0 -571
  49. package/lib/validation.js +0 -394
  50. package/lib/yerror.js +0 -11
package/lib/completion.js DELETED
@@ -1,132 +0,0 @@
1
- 'use strict'
2
- const path = require('path')
3
-
4
- // add bash completions to your
5
- // yargs-powered applications.
6
- module.exports = function completion (yargs, usage, command) {
7
- const self = {
8
- completionKey: 'get-yargs-completions'
9
- }
10
-
11
- let aliases
12
- self.setParsed = function setParsed (parsed) {
13
- aliases = parsed.aliases
14
- }
15
-
16
- const zshShell = (process.env.SHELL && process.env.SHELL.indexOf('zsh') !== -1) ||
17
- (process.env.ZSH_NAME && process.env.ZSH_NAME.indexOf('zsh') !== -1)
18
- // get a list of completion commands.
19
- // 'args' is the array of strings from the line to be completed
20
- self.getCompletion = function getCompletion (args, done) {
21
- const completions = []
22
- const current = args.length ? args[args.length - 1] : ''
23
- const argv = yargs.parse(args, true)
24
- const parentCommands = yargs.getContext().commands
25
-
26
- // a custom completion function can be provided
27
- // to completion().
28
- if (completionFunction) {
29
- if (completionFunction.length < 3) {
30
- const result = completionFunction(current, argv)
31
-
32
- // promise based completion function.
33
- if (typeof result.then === 'function') {
34
- return result.then((list) => {
35
- process.nextTick(() => { done(list) })
36
- }).catch((err) => {
37
- process.nextTick(() => { throw err })
38
- })
39
- }
40
-
41
- // synchronous completion function.
42
- return done(result)
43
- } else {
44
- // asynchronous completion function
45
- return completionFunction(current, argv, (completions) => {
46
- done(completions)
47
- })
48
- }
49
- }
50
-
51
- const handlers = command.getCommandHandlers()
52
- for (let i = 0, ii = args.length; i < ii; ++i) {
53
- if (handlers[args[i]] && handlers[args[i]].builder) {
54
- const builder = handlers[args[i]].builder
55
- if (typeof builder === 'function') {
56
- const y = yargs.reset()
57
- builder(y)
58
- return y.argv
59
- }
60
- }
61
- }
62
-
63
- if (!current.match(/^-/) && parentCommands[parentCommands.length - 1] !== current) {
64
- usage.getCommands().forEach((usageCommand) => {
65
- const commandName = command.parseCommand(usageCommand[0]).cmd
66
- if (args.indexOf(commandName) === -1) {
67
- if (!zshShell) {
68
- completions.push(commandName)
69
- } else {
70
- const desc = usageCommand[1] || ''
71
- completions.push(commandName.replace(/:/g, '\\:') + ':' + desc)
72
- }
73
- }
74
- })
75
- }
76
-
77
- if (current.match(/^-/) || (current === '' && completions.length === 0)) {
78
- const descs = usage.getDescriptions()
79
- const options = yargs.getOptions()
80
- Object.keys(options.key).forEach((key) => {
81
- const negable = !!options.configuration['boolean-negation'] && options.boolean.includes(key)
82
- // If the key and its aliases aren't in 'args', add the key to 'completions'
83
- let keyAndAliases = [key].concat(aliases[key] || [])
84
- if (negable) keyAndAliases = keyAndAliases.concat(keyAndAliases.map(key => `no-${key}`))
85
-
86
- function completeOptionKey (key) {
87
- const notInArgs = keyAndAliases.every(val => args.indexOf(`--${val}`) === -1)
88
- if (notInArgs) {
89
- const startsByTwoDashes = s => /^--/.test(s)
90
- const isShortOption = s => /^[^0-9]$/.test(s)
91
- const dashes = !startsByTwoDashes(current) && isShortOption(key) ? '-' : '--'
92
- if (!zshShell) {
93
- completions.push(dashes + key)
94
- } else {
95
- const desc = descs[key] || ''
96
- completions.push(dashes + `${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
97
- }
98
- }
99
- }
100
-
101
- completeOptionKey(key)
102
- if (negable && !!options.default[key]) completeOptionKey(`no-${key}`)
103
- })
104
- }
105
-
106
- done(completions)
107
- }
108
-
109
- // generate the completion script to add to your .bashrc.
110
- self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
111
- const templates = require('./completion-templates')
112
- let script = zshShell ? templates.completionZshTemplate : templates.completionShTemplate
113
- const name = path.basename($0)
114
-
115
- // add ./to applications not yet installed as bin.
116
- if ($0.match(/\.js$/)) $0 = `./${$0}`
117
-
118
- script = script.replace(/{{app_name}}/g, name)
119
- script = script.replace(/{{completion_command}}/g, cmd)
120
- return script.replace(/{{app_path}}/g, $0)
121
- }
122
-
123
- // register a function to perform your own custom
124
- // completions., this function can be either
125
- // synchrnous or asynchronous.
126
- let completionFunction = null
127
- self.registerFunction = (fn) => {
128
- completionFunction = fn
129
- }
130
-
131
- return self
132
- }
package/lib/is-promise.js DELETED
@@ -1,3 +0,0 @@
1
- module.exports = function isPromise (maybePromise) {
2
- return !!maybePromise && !!maybePromise.then && (typeof maybePromise.then === 'function')
3
- }
package/lib/middleware.js DELETED
@@ -1,64 +0,0 @@
1
- 'use strict'
2
-
3
- // hoisted due to circular dependency on command.
4
- module.exports = {
5
- applyMiddleware,
6
- commandMiddlewareFactory,
7
- globalMiddlewareFactory
8
- }
9
- const isPromise = require('./is-promise')
10
- const argsert = require('./argsert')
11
-
12
- function globalMiddlewareFactory (globalMiddleware, context) {
13
- return function (callback, applyBeforeValidation = false) {
14
- argsert('<array|function> [boolean]', [callback, applyBeforeValidation], arguments.length)
15
- if (Array.isArray(callback)) {
16
- for (let i = 0; i < callback.length; i++) {
17
- if (typeof callback[i] !== 'function') {
18
- throw Error('middleware must be a function')
19
- }
20
- callback[i].applyBeforeValidation = applyBeforeValidation
21
- }
22
- Array.prototype.push.apply(globalMiddleware, callback)
23
- } else if (typeof callback === 'function') {
24
- callback.applyBeforeValidation = applyBeforeValidation
25
- globalMiddleware.push(callback)
26
- }
27
- return context
28
- }
29
- }
30
-
31
- function commandMiddlewareFactory (commandMiddleware) {
32
- if (!commandMiddleware) return []
33
- return commandMiddleware.map(middleware => {
34
- middleware.applyBeforeValidation = false
35
- return middleware
36
- })
37
- }
38
-
39
- function applyMiddleware (argv, yargs, middlewares, beforeValidation) {
40
- const beforeValidationError = new Error('middleware cannot return a promise when applyBeforeValidation is true')
41
- return middlewares
42
- .reduce((accumulation, middleware) => {
43
- if (middleware.applyBeforeValidation !== beforeValidation) {
44
- return accumulation
45
- }
46
-
47
- if (isPromise(accumulation)) {
48
- return accumulation
49
- .then(initialObj =>
50
- Promise.all([initialObj, middleware(initialObj, yargs)])
51
- )
52
- .then(([initialObj, middlewareObj]) =>
53
- Object.assign(initialObj, middlewareObj)
54
- )
55
- } else {
56
- const result = middleware(argv, yargs)
57
- if (beforeValidation && isPromise(result)) throw beforeValidationError
58
-
59
- return isPromise(result)
60
- ? result.then(middlewareObj => Object.assign(accumulation, middlewareObj))
61
- : Object.assign(accumulation, result)
62
- }
63
- }, argv)
64
- }
package/lib/obj-filter.js DELETED
@@ -1,11 +0,0 @@
1
- 'use strict'
2
- module.exports = function objFilter (original, filter) {
3
- const obj = {}
4
- filter = filter || ((k, v) => true)
5
- Object.keys(original || {}).forEach((key) => {
6
- if (filter(key, original[key])) {
7
- obj[key] = original[key]
8
- }
9
- })
10
- return obj
11
- }
@@ -1,34 +0,0 @@
1
- function getProcessArgvBinIndex () {
2
- // The binary name is the first command line argument for:
3
- // - bundled Electron apps: bin argv1 argv2 ... argvn
4
- if (isBundledElectronApp()) return 0
5
- // or the second one (default) for:
6
- // - standard node apps: node bin.js argv1 argv2 ... argvn
7
- // - unbundled Electron apps: electron bin.js argv1 arg2 ... argvn
8
- return 1
9
- }
10
-
11
- function isBundledElectronApp () {
12
- // process.defaultApp is either set by electron in an electron unbundled app, or undefined
13
- // see https://github.com/electron/electron/blob/master/docs/api/process.md#processdefaultapp-readonly
14
- return isElectronApp() && !process.defaultApp
15
- }
16
-
17
- function isElectronApp () {
18
- // process.versions.electron is either set by electron, or undefined
19
- // see https://github.com/electron/electron/blob/master/docs/api/process.md#processversionselectron-readonly
20
- return !!process.versions.electron
21
- }
22
-
23
- function getProcessArgvWithoutBin () {
24
- return process.argv.slice(getProcessArgvBinIndex() + 1)
25
- }
26
-
27
- function getProcessArgvBin () {
28
- return process.argv[getProcessArgvBinIndex()]
29
- }
30
-
31
- module.exports = {
32
- getProcessArgvBin,
33
- getProcessArgvWithoutBin
34
- }