yargs 14.1.0 → 14.2.3

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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,35 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### 14.2.2
6
+
7
+ ### Bug Fixes
8
+
9
+ * temporary fix for libraries that call Object.freeze() ([#1483](https://www.github.com/yargs/yargs/issues/1483)) ([99c2dc8](https://www.github.com/yargs/yargs/commit/99c2dc850e67c606644f8b0c0bca1a59c87dcbcd))
10
+
11
+ ### [14.2.1](https://github.com/yargs/yargs/compare/v14.2.0...v14.2.1) (2019-10-30)
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * stop-parse was not being respected by commands ([#1459](https://github.com/yargs/yargs/issues/1459)) ([e78e76e](https://github.com/yargs/yargs/commit/e78e76e3ac0551d4f30c71a05ddb21582960fcef))
17
+
18
+ ## [14.2.0](https://github.com/yargs/yargs/compare/v14.1.0...v14.2.0) (2019-10-07)
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * async middleware was called twice ([#1422](https://github.com/yargs/yargs/issues/1422)) ([9a42b63](https://github.com/yargs/yargs/commit/9a42b63))
24
+ * fix promise check to accept any spec conform object ([#1424](https://github.com/yargs/yargs/issues/1424)) ([0be43d2](https://github.com/yargs/yargs/commit/0be43d2))
25
+ * groups were not being maintained for nested commands ([#1430](https://github.com/yargs/yargs/issues/1430)) ([d38650e](https://github.com/yargs/yargs/commit/d38650e))
26
+ * **docs:** broken markdown link ([#1426](https://github.com/yargs/yargs/issues/1426)) ([236e24e](https://github.com/yargs/yargs/commit/236e24e))
27
+ * support merging deeply nested configuration ([#1423](https://github.com/yargs/yargs/issues/1423)) ([bae66fe](https://github.com/yargs/yargs/commit/bae66fe))
28
+
29
+
30
+ ### Features
31
+
32
+ * **deps:** introduce yargs-parser with support for unknown-options-as-args ([#1440](https://github.com/yargs/yargs/issues/1440)) ([4d21520](https://github.com/yargs/yargs/commit/4d21520))
33
+
5
34
  ## [14.1.0](https://github.com/yargs/yargs/compare/v14.0.0...v14.1.0) (2019-09-06)
6
35
 
7
36
 
@@ -18,7 +47,6 @@ All notable changes to this project will be documented in this file. See [standa
18
47
  ### Features
19
48
 
20
49
  * make it possible to merge configurations when extending other config. ([#1411](https://github.com/yargs/yargs/issues/1411)) ([5d7ad98](https://github.com/yargs/yargs/commit/5d7ad98))
21
- * **deps:** yargs-parser with support for collect-unknown-options ([#1421](https://github.com/yargs/yargs/issues/1421)) ([d388a7c](https://github.com/yargs/yargs/commit/d388a7c))
22
50
 
23
51
  ## [14.0.0](https://github.com/yargs/yargs/compare/v13.3.0...v14.0.0) (2019-07-30)
24
52
 
@@ -56,7 +56,7 @@ function applyExtends (config, cwd, mergeExtends) {
56
56
 
57
57
  defaultConfig = isPath ? JSON.parse(fs.readFileSync(pathToDefault, 'utf8')) : require(config.extends)
58
58
  delete config.extends
59
- defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault))
59
+ defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault), mergeExtends)
60
60
  }
61
61
 
62
62
  previouslyVisitedConfigs = []
package/lib/command.js CHANGED
@@ -227,9 +227,12 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
227
227
 
228
228
  if (commandHandler.handler && !yargs._hasOutput()) {
229
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)
230
234
 
231
235
  innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false)
232
-
233
236
  let handlerResult
234
237
  if (isPromise(innerArgv)) {
235
238
  handlerResult = innerArgv.then(argv => commandHandler.handler(argv))
@@ -349,7 +352,12 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
349
352
  // short-circuit parse.
350
353
  if (!unparsed.length) return
351
354
 
352
- const parsed = Parser.detailed(unparsed, options)
355
+ const config = Object.assign({}, options.configuration, {
356
+ 'populate--': true
357
+ })
358
+ const parsed = Parser.detailed(unparsed, Object.assign({}, options, {
359
+ configuration: config
360
+ }))
353
361
 
354
362
  if (parsed.error) {
355
363
  yargs.getUsageInstance().fail(parsed.error.message, parsed.error)
package/lib/is-promise.js CHANGED
@@ -1,3 +1,3 @@
1
1
  module.exports = function isPromise (maybePromise) {
2
- return maybePromise instanceof Promise
2
+ return !!maybePromise && !!maybePromise.then && (typeof maybePromise.then === 'function')
3
3
  }
package/lib/middleware.js CHANGED
@@ -40,8 +40,7 @@ function applyMiddleware (argv, yargs, middlewares, beforeValidation) {
40
40
  const beforeValidationError = new Error('middleware cannot return a promise when applyBeforeValidation is true')
41
41
  return middlewares
42
42
  .reduce((accumulation, middleware) => {
43
- if (middleware.applyBeforeValidation !== beforeValidation &&
44
- !isPromise(accumulation)) {
43
+ if (middleware.applyBeforeValidation !== beforeValidation) {
45
44
  return accumulation
46
45
  }
47
46
 
package/lib/validation.js CHANGED
@@ -224,43 +224,36 @@ module.exports = function validation (yargs, usage, y18n) {
224
224
  return implied
225
225
  }
226
226
 
227
+ function keyExists (argv, val) {
228
+ // convert string '1' to number 1
229
+ let num = Number(val)
230
+ val = isNaN(num) ? val : num
231
+
232
+ if (typeof val === 'number') {
233
+ // check length of argv._
234
+ val = argv._.length >= val
235
+ } else if (val.match(/^--no-.+/)) {
236
+ // check if key/value doesn't exist
237
+ val = val.match(/^--no-(.+)/)[1]
238
+ val = !argv[val]
239
+ } else {
240
+ // check if key/value exists
241
+ val = argv[val]
242
+ }
243
+ return val
244
+ }
245
+
227
246
  self.implications = function implications (argv) {
228
247
  const implyFail = []
229
248
 
230
249
  Object.keys(implied).forEach((key) => {
231
250
  const origKey = key
232
251
  ;(implied[key] || []).forEach((value) => {
233
- let num
234
252
  let key = origKey
235
253
  const origValue = value
254
+ key = keyExists(argv, key)
255
+ value = keyExists(argv, value)
236
256
 
237
- // convert string '1' to number 1
238
- num = Number(key)
239
- key = isNaN(num) ? key : num
240
-
241
- if (typeof key === 'number') {
242
- // check length of argv._
243
- key = argv._.length >= key
244
- } else if (key.match(/^--no-.+/)) {
245
- // check if key doesn't exist
246
- key = key.match(/^--no-(.+)/)[1]
247
- key = !argv[key]
248
- } else {
249
- // check if key exists
250
- key = argv[key]
251
- }
252
-
253
- num = Number(value)
254
- value = isNaN(num) ? value : num
255
-
256
- if (typeof value === 'number') {
257
- value = argv._.length >= value
258
- } else if (value.match(/^--no-.+/)) {
259
- value = value.match(/^--no-(.+)/)[1]
260
- value = !argv[value]
261
- } else {
262
- value = argv[value]
263
- }
264
257
  if (key && !value) {
265
258
  implyFail.push(` ${origKey} -> ${origValue}`)
266
259
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yargs",
3
- "version": "14.1.0",
3
+ "version": "14.2.3",
4
4
  "description": "yargs the modern, pirate-themed, successor to optimist.",
5
5
  "main": "./index.js",
6
6
  "contributors": [
@@ -29,7 +29,7 @@
29
29
  "string-width": "^3.0.0",
30
30
  "which-module": "^2.0.0",
31
31
  "y18n": "^4.0.0",
32
- "yargs-parser": "^14.0.0"
32
+ "yargs-parser": "^15.0.1"
33
33
  },
34
34
  "devDependencies": {
35
35
  "chai": "^4.2.0",
package/yargs.js CHANGED
@@ -94,14 +94,17 @@ function Yargs (processArgs, cwd, parentRequire) {
94
94
  })
95
95
  })
96
96
 
97
- // preserve all groups not set to local.
98
- preservedGroups = Object.keys(groups).reduce((acc, groupName) => {
99
- const keys = groups[groupName].filter(key => !(key in localLookup))
100
- if (keys.length > 0) {
101
- acc[groupName] = keys
102
- }
103
- return acc
104
- }, {})
97
+ // add all groups not set to local to preserved groups
98
+ Object.assign(
99
+ preservedGroups,
100
+ Object.keys(groups).reduce((acc, groupName) => {
101
+ const keys = groups[groupName].filter(key => !(key in localLookup))
102
+ if (keys.length > 0) {
103
+ acc[groupName] = keys
104
+ }
105
+ return acc
106
+ }, {})
107
+ )
105
108
  // groups can now be reset
106
109
  groups = {}
107
110
 
@@ -235,6 +238,7 @@ function Yargs (processArgs, cwd, parentRequire) {
235
238
  function populateParserHintArray (type, keys, value) {
236
239
  keys = [].concat(keys)
237
240
  keys.forEach((key) => {
241
+ key = sanitizeKey(key)
238
242
  options[type].push(key)
239
243
  })
240
244
  }
@@ -290,8 +294,8 @@ function Yargs (processArgs, cwd, parentRequire) {
290
294
 
291
295
  function populateParserHintObject (builder, isArray, type, key, value) {
292
296
  if (Array.isArray(key)) {
297
+ const temp = Object.create(null)
293
298
  // an array of keys with one value ['x', 'y', 'z'], function parse () {}
294
- const temp = {}
295
299
  key.forEach((k) => {
296
300
  temp[k] = value
297
301
  })
@@ -302,6 +306,7 @@ function Yargs (processArgs, cwd, parentRequire) {
302
306
  builder(k, key[k])
303
307
  })
304
308
  } else {
309
+ key = sanitizeKey(key)
305
310
  // a single key value pair 'x', parse() {}
306
311
  if (isArray) {
307
312
  options[type][key] = (options[type][key] || []).concat(value)
@@ -311,6 +316,13 @@ function Yargs (processArgs, cwd, parentRequire) {
311
316
  }
312
317
  }
313
318
 
319
+ // TODO(bcoe): in future major versions move more objects towards
320
+ // Object.create(null):
321
+ function sanitizeKey (key) {
322
+ if (key === '__proto__') return '___proto___'
323
+ return key
324
+ }
325
+
314
326
  function deleteFromParserHintObject (optionKey) {
315
327
  // delete from all parsing hints:
316
328
  // boolean, array, key, alias, etc.
@@ -1022,13 +1034,12 @@ function Yargs (processArgs, cwd, parentRequire) {
1022
1034
  enumerable: true
1023
1035
  })
1024
1036
 
1025
- self._parseArgs = function parseArgs (args, shortCircuit, _skipValidation, commandIndex) {
1026
- let skipValidation = !!_skipValidation
1037
+ self._parseArgs = function parseArgs (args, shortCircuit, _calledFromCommand, commandIndex) {
1038
+ let skipValidation = !!_calledFromCommand
1027
1039
  args = args || processArgs
1028
1040
 
1029
1041
  options.__ = y18n.__
1030
1042
  options.configuration = self.getParserConfiguration()
1031
-
1032
1043
  // Deprecated
1033
1044
  let pkgConfig = pkgUp()['yargs']
1034
1045
  if (pkgConfig) {
@@ -1036,7 +1047,14 @@ function Yargs (processArgs, cwd, parentRequire) {
1036
1047
  options.configuration = Object.assign({}, pkgConfig, options.configuration)
1037
1048
  }
1038
1049
 
1039
- const parsed = Parser.detailed(args, options)
1050
+ const populateDoubleDash = !!options.configuration['populate--']
1051
+ const config = Object.assign({}, options.configuration, {
1052
+ 'populate--': true
1053
+ })
1054
+ const parsed = Parser.detailed(args, Object.assign({}, options, {
1055
+ configuration: config
1056
+ }))
1057
+
1040
1058
  let argv = parsed.argv
1041
1059
  if (parseContext) argv = Object.assign({}, argv, parseContext)
1042
1060
  const aliases = parsed.aliases
@@ -1051,7 +1069,7 @@ function Yargs (processArgs, cwd, parentRequire) {
1051
1069
  // are two passes through the parser. If completion
1052
1070
  // is being performed short-circuit on the first pass.
1053
1071
  if (shortCircuit) {
1054
- return argv
1072
+ return (populateDoubleDash || _calledFromCommand) ? argv : self._copyDoubleDash(argv)
1055
1073
  }
1056
1074
 
1057
1075
  // if there's a handler associated with a
@@ -1084,7 +1102,8 @@ function Yargs (processArgs, cwd, parentRequire) {
1084
1102
  // commands are executed using a recursive algorithm that executes
1085
1103
  // the deepest command first; we keep track of the position in the
1086
1104
  // argv._ array that is currently being executed.
1087
- return command.runCommand(cmd, self, parsed, i + 1)
1105
+ const innerArgv = command.runCommand(cmd, self, parsed, i + 1)
1106
+ return populateDoubleDash ? innerArgv : self._copyDoubleDash(innerArgv)
1088
1107
  } else if (!firstUnknownCommand && cmd !== completionCommand) {
1089
1108
  firstUnknownCommand = cmd
1090
1109
  break
@@ -1093,7 +1112,8 @@ function Yargs (processArgs, cwd, parentRequire) {
1093
1112
 
1094
1113
  // run the default command, if defined
1095
1114
  if (command.hasDefaultCommand() && !skipDefaultCommand) {
1096
- return command.runCommand(null, self, parsed)
1115
+ const innerArgv = command.runCommand(null, self, parsed)
1116
+ return populateDoubleDash ? innerArgv : self._copyDoubleDash(innerArgv)
1097
1117
  }
1098
1118
 
1099
1119
  // recommend a command if recommendCommands() has
@@ -1110,7 +1130,8 @@ function Yargs (processArgs, cwd, parentRequire) {
1110
1130
  self.exit(0)
1111
1131
  }
1112
1132
  } else if (command.hasDefaultCommand() && !skipDefaultCommand) {
1113
- return command.runCommand(null, self, parsed)
1133
+ const innerArgv = command.runCommand(null, self, parsed)
1134
+ return populateDoubleDash ? innerArgv : self._copyDoubleDash(innerArgv)
1114
1135
  }
1115
1136
 
1116
1137
  // we must run completions first, a user might
@@ -1128,7 +1149,7 @@ function Yargs (processArgs, cwd, parentRequire) {
1128
1149
 
1129
1150
  self.exit(0)
1130
1151
  })
1131
- return argv
1152
+ return (populateDoubleDash || _calledFromCommand) ? argv : self._copyDoubleDash(argv)
1132
1153
  }
1133
1154
 
1134
1155
  // Handle 'help' and 'version' options
@@ -1172,6 +1193,22 @@ function Yargs (processArgs, cwd, parentRequire) {
1172
1193
  else throw err
1173
1194
  }
1174
1195
 
1196
+ return (populateDoubleDash || _calledFromCommand) ? argv : self._copyDoubleDash(argv)
1197
+ }
1198
+
1199
+ // to simplify the parsing of positionals in commands,
1200
+ // we temporarily populate '--' rather than _, with arguments
1201
+ // after the '--' directive. After the parse, we copy these back.
1202
+ self._copyDoubleDash = function (argv) {
1203
+ if (!argv._ || !argv['--']) return argv
1204
+ argv._.push.apply(argv._, argv['--'])
1205
+
1206
+ // TODO(bcoe): refactor command parsing such that this delete is not
1207
+ // necessary: https://github.com/yargs/yargs/issues/1482
1208
+ try {
1209
+ delete argv['--']
1210
+ } catch (_err) {}
1211
+
1175
1212
  return argv
1176
1213
  }
1177
1214