yargs 13.0.0-candidate.0 → 13.1.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,51 @@
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
+ <a name="13.1.0"></a>
6
+ # [13.1.0](https://github.com/yargs/yargs/compare/v13.0.0...v13.1.0) (2019-02-12)
7
+
8
+
9
+ ### Features
10
+
11
+ * add applyBeforeValidation, for applying sync middleware before validation ([5be206a](https://github.com/yargs/yargs/commit/5be206a))
12
+
13
+
14
+
15
+ <a name="13.0.0"></a>
16
+ # [13.0.0](https://github.com/yargs/yargs/compare/v12.0.5...v13.0.0) (2019-02-02)
17
+
18
+
19
+ ### Bug Fixes
20
+
21
+ * **deps:** Update os-locale to avoid security vulnerability ([#1270](https://github.com/yargs/yargs/issues/1270)) ([27bf739](https://github.com/yargs/yargs/commit/27bf739))
22
+ * **validation:** Use the error as a message when none exists otherwise ([#1268](https://github.com/yargs/yargs/issues/1268)) ([0510fe6](https://github.com/yargs/yargs/commit/0510fe6))
23
+ * better bash path completion ([#1272](https://github.com/yargs/yargs/issues/1272)) ([da75ea2](https://github.com/yargs/yargs/commit/da75ea2))
24
+ * middleware added multiple times due to reference bug ([#1282](https://github.com/yargs/yargs/issues/1282)) ([64af518](https://github.com/yargs/yargs/commit/64af518))
25
+
26
+
27
+ ### Chores
28
+
29
+ * drop Node 6 from testing matrix ([#1287](https://github.com/yargs/yargs/issues/1287)) ([ef16792](https://github.com/yargs/yargs/commit/ef16792))
30
+ * update dependencies ([#1284](https://github.com/yargs/yargs/issues/1284)) ([f25de4f](https://github.com/yargs/yargs/commit/f25de4f))
31
+
32
+
33
+ ### Features
34
+
35
+ * Add `.parserConfiguration()` method, deprecating package.json config ([#1262](https://github.com/yargs/yargs/issues/1262)) ([3c6869a](https://github.com/yargs/yargs/commit/3c6869a))
36
+ * adds config option for sorting command output ([#1256](https://github.com/yargs/yargs/issues/1256)) ([6916ce9](https://github.com/yargs/yargs/commit/6916ce9))
37
+ * options/positionals with leading '+' and '0' no longer parse as numbers ([#1286](https://github.com/yargs/yargs/issues/1286)) ([e9dc3aa](https://github.com/yargs/yargs/commit/e9dc3aa))
38
+ * support promises in middleware ([f3a4e4f](https://github.com/yargs/yargs/commit/f3a4e4f))
39
+
40
+
41
+ ### BREAKING CHANGES
42
+
43
+ * options with leading '+' or '0' now parse as strings
44
+ * dropping Node 6 which hits end of life in April 2019
45
+ * see [yargs-parser@12.0.0 CHANGELOG](https://github.com/yargs/yargs-parser/blob/master/CHANGELOG.md#breaking-changes)
46
+ * we now warn if the yargs stanza package.json is used.
47
+
48
+
49
+
5
50
  <a name="12.0.5"></a>
6
51
  ## [12.0.5](https://github.com/yargs/yargs/compare/v12.0.4...v12.0.5) (2018-11-19)
7
52
 
package/lib/argsert.js CHANGED
@@ -1,10 +1,12 @@
1
1
  'use strict'
2
+
3
+ // hoisted due to circular dependency on command.
4
+ module.exports = argsert
2
5
  const command = require('./command')()
3
6
  const YError = require('./yerror')
4
7
 
5
8
  const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
6
-
7
- module.exports = function argsert (expected, callerArguments, length) {
9
+ function argsert (expected, callerArguments, length) {
8
10
  // TODO: should this eventually raise an exception.
9
11
  try {
10
12
  // preface the argument description with "cmd", so
package/lib/command.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  const inspect = require('util').inspect
4
4
  const isPromise = require('./is-promise')
5
- const {applyMiddleware} = require('./middleware')
5
+ const {applyMiddleware, commandMiddlewareFactory} = require('./middleware')
6
6
  const path = require('path')
7
7
  const Parser = require('yargs-parser')
8
8
 
@@ -17,9 +17,10 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
17
17
  let aliasMap = {}
18
18
  let defaultCommand
19
19
  globalMiddleware = globalMiddleware || []
20
- self.addHandler = function addHandler (cmd, description, builder, handler, _middlewares) {
20
+
21
+ self.addHandler = function addHandler (cmd, description, builder, handler, commandMiddleware) {
21
22
  let aliases = []
22
- const middlewares = (_middlewares || []).slice(0)
23
+ const middlewares = commandMiddlewareFactory(commandMiddleware)
23
24
  handler = handler || (() => {})
24
25
 
25
26
  if (Array.isArray(cmd)) {
@@ -224,6 +225,9 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
224
225
  positionalMap = populatePositionals(commandHandler, innerArgv, currentContext, yargs)
225
226
  }
226
227
 
228
+ const middlewares = globalMiddleware.slice(0).concat(commandHandler.middlewares || [])
229
+ applyMiddleware(innerArgv, yargs, middlewares, true)
230
+
227
231
  // we apply validation post-hoc, so that custom
228
232
  // checks get passed populated positional arguments.
229
233
  if (!yargs._hasOutput()) yargs._runValidation(innerArgv, aliases, positionalMap, yargs.parsed.error)
@@ -231,9 +235,7 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
231
235
  if (commandHandler.handler && !yargs._hasOutput()) {
232
236
  yargs._setHasOutput()
233
237
 
234
- const middlewares = globalMiddleware.slice(0).concat(commandHandler.middlewares || [])
235
-
236
- innerArgv = applyMiddleware(innerArgv, middlewares)
238
+ innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false)
237
239
 
238
240
  const handlerResult = isPromise(innerArgv)
239
241
  ? innerArgv.then(argv => commandHandler.handler(argv))
package/lib/middleware.js CHANGED
@@ -1,29 +1,61 @@
1
+ 'use strict'
2
+
3
+ // hoisted due to circular dependency on command.
4
+ module.exports = {
5
+ applyMiddleware,
6
+ commandMiddlewareFactory,
7
+ globalMiddlewareFactory
8
+ }
1
9
  const isPromise = require('./is-promise')
10
+ const argsert = require('./argsert')
2
11
 
3
- module.exports = function (globalMiddleware, context) {
4
- return function (callback) {
12
+ function globalMiddlewareFactory (globalMiddleware, context) {
13
+ return function (callback, applyBeforeValidation = false) {
14
+ argsert('<array|function> [boolean]', [callback, applyBeforeValidation], arguments.length)
5
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
+ }
6
22
  Array.prototype.push.apply(globalMiddleware, callback)
7
23
  } else if (typeof callback === 'function') {
24
+ callback.applyBeforeValidation = applyBeforeValidation
8
25
  globalMiddleware.push(callback)
9
26
  }
10
27
  return context
11
28
  }
12
29
  }
13
30
 
14
- module.exports.applyMiddleware = function (argv, middlewares) {
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')
15
41
  return middlewares
16
42
  .reduce((accumulation, middleware) => {
43
+ if (middleware.applyBeforeValidation !== beforeValidation &&
44
+ !isPromise(accumulation)) {
45
+ return accumulation
46
+ }
47
+
17
48
  if (isPromise(accumulation)) {
18
49
  return accumulation
19
50
  .then(initialObj =>
20
- Promise.all([initialObj, middleware(initialObj)])
51
+ Promise.all([initialObj, middleware(initialObj, yargs)])
21
52
  )
22
53
  .then(([initialObj, middlewareObj]) =>
23
54
  Object.assign(initialObj, middlewareObj)
24
55
  )
25
56
  } else {
26
- const result = middleware(argv)
57
+ const result = middleware(argv, yargs)
58
+ if (beforeValidation && isPromise(result)) throw beforeValidationError
27
59
 
28
60
  return isPromise(result)
29
61
  ? result.then(middlewareObj => Object.assign(accumulation, middlewareObj))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yargs",
3
- "version": "13.0.0-candidate.0",
3
+ "version": "13.1.0",
4
4
  "description": "yargs the modern, pirate-themed, successor to optimist.",
5
5
  "main": "./index.js",
6
6
  "contributors": [
package/yargs.js CHANGED
@@ -11,7 +11,7 @@ const Y18n = require('y18n')
11
11
  const objFilter = require('./lib/obj-filter')
12
12
  const setBlocking = require('set-blocking')
13
13
  const applyExtends = require('./lib/apply-extends')
14
- const middlewareFactory = require('./lib/middleware')
14
+ const {globalMiddlewareFactory} = require('./lib/middleware')
15
15
  const YError = require('./lib/yerror')
16
16
 
17
17
  exports = module.exports = Yargs
@@ -33,7 +33,7 @@ function Yargs (processArgs, cwd, parentRequire) {
33
33
  updateFiles: false
34
34
  })
35
35
 
36
- self.middleware = middlewareFactory(globalMiddleware, self)
36
+ self.middleware = globalMiddlewareFactory(globalMiddleware, self)
37
37
 
38
38
  if (!cwd) cwd = process.cwd()
39
39