Skip to content

Commit bb0eb52

Browse files
Benjamin E. Coemleguen
authored andcommitted
fix: populate correct value on yargs.parsed and stop warning on access (#1412)
* fix: populate correct type for yargs.parsed * fix: revert the warning message on yargs.parsed access
1 parent b774b5e commit bb0eb52

5 files changed

Lines changed: 86 additions & 36 deletions

File tree

‎index.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ function singletonify (inst) {
3232
return inst.$0
3333
})
3434
Argv.__defineGetter__('parsed', () => {
35-
console.warn('In future major releases of yargs, "parsed" will be a private field. Use the return value of ".parse()" or ".argv" instead')
3635
return inst.parsed
3736
})
3837
}

‎lib/command.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
174174
let numFiles = currentContext.files.length
175175
const parentCommands = currentContext.commands.slice()
176176

177-
// what does yargs look like after the buidler is run?
177+
// what does yargs look like after the builder is run?
178178
let innerArgv = parsed.argv
179179
let innerYargs = null
180180
let positionalMap = {}
@@ -230,14 +230,22 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
230230

231231
innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false)
232232

233-
const handlerResult = isPromise(innerArgv)
234-
? innerArgv.then(argv => commandHandler.handler(argv))
235-
: commandHandler.handler(innerArgv)
233+
let handlerResult
234+
if (isPromise(innerArgv)) {
235+
handlerResult = innerArgv.then(argv => commandHandler.handler(argv))
236+
} else {
237+
handlerResult = commandHandler.handler(innerArgv)
238+
}
236239

237240
if (isPromise(handlerResult)) {
238-
handlerResult.catch(error =>
239-
yargs.getUsageInstance().fail(null, error)
240-
)
241+
yargs.getUsageInstance().cacheHelpMessage()
242+
handlerResult.catch(error => {
243+
try {
244+
yargs.getUsageInstance().fail(null, error)
245+
} catch (err) {
246+
// fail's throwing would cause an unhandled rejection.
247+
}
248+
})
241249
}
242250
}
243251

‎lib/usage.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ module.exports = function usage (yargs, y18n) {
148148

149149
const defaultGroup = 'Options:'
150150
self.help = function help () {
151+
if (cachedHelpMessage) return cachedHelpMessage
151152
normalizeAliases()
152153

153154
// handle old demanded API
@@ -403,6 +404,13 @@ module.exports = function usage (yargs, y18n) {
403404
})
404405
}
405406

407+
// if yargs is executing an async handler, we take a snapshot of the
408+
// help message to display on failure:
409+
let cachedHelpMessage
410+
self.cacheHelpMessage = function () {
411+
cachedHelpMessage = this.help()
412+
}
413+
406414
// given a set of keys, place any keys that are
407415
// ungrouped under the 'Options:' grouping.
408416
function addUngroupedKeys (keys, aliases, groups) {

‎test/command.js‎

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,36 +1426,69 @@ describe('Command', () => {
14261426
})
14271427
})
14281428

1429-
// addresses https://github.com/yargs/yargs/issues/510
1430-
it('fails when the promise returned by the command handler rejects', (done) => {
1431-
const error = new Error()
1432-
yargs('foo')
1433-
.command('foo', 'foo command', noop, (yargs) => Promise.reject(error))
1434-
.fail((msg, err) => {
1435-
expect(msg).to.equal(null)
1436-
expect(err).to.equal(error)
1437-
done()
1429+
describe('async', () => {
1430+
// addresses https://github.com/yargs/yargs/issues/510
1431+
it('fails when the promise returned by the command handler rejects', (done) => {
1432+
const error = new Error()
1433+
yargs('foo')
1434+
.command('foo', 'foo command', noop, (yargs) => Promise.reject(error))
1435+
.fail((msg, err) => {
1436+
expect(msg).to.equal(null)
1437+
expect(err).to.equal(error)
1438+
done()
1439+
})
1440+
.parse()
1441+
})
1442+
1443+
it('succeeds when the promise returned by the command handler resolves', (done) => {
1444+
const handler = new Promise((resolve, reject) => {
1445+
setTimeout(() => {
1446+
return resolve(true)
1447+
}, 5)
14381448
})
1439-
.parse()
1440-
})
1449+
const parsed = yargs('foo hello')
1450+
.command('foo <pos>', 'foo command', () => {}, (yargs) => handler)
1451+
.fail((msg, err) => {
1452+
return done(Error('should not have been called'))
1453+
})
1454+
.parse()
14411455

1442-
it('succeeds when the promise returned by the command handler resolves', (done) => {
1443-
const handler = new Promise((resolve, reject) => {
1444-
setTimeout(() => {
1445-
return resolve(true)
1446-
}, 5)
1447-
})
1448-
const parsed = yargs('foo hello')
1449-
.command('foo <pos>', 'foo command', () => {}, (yargs) => handler)
1450-
.fail((msg, err) => {
1451-
return done(Error('should not have been called'))
1456+
handler.then(called => {
1457+
called.should.equal(true)
1458+
parsed.pos.should.equal('hello')
1459+
return done()
14521460
})
1453-
.parse()
1461+
})
14541462

1455-
handler.then(called => {
1456-
called.should.equal(true)
1457-
parsed.pos.should.equal('hello')
1458-
return done()
1463+
// see: https://github.com/yargs/yargs/issues/1144
1464+
it('displays error and appropriate help message when handler fails', (done) => {
1465+
const y = yargs('foo')
1466+
.command('foo', 'foo command', (yargs) => {
1467+
yargs.option('bar', {
1468+
describe: 'bar option'
1469+
})
1470+
}, (argv) => {
1471+
return Promise.reject(Error('foo error'))
1472+
})
1473+
.exitProcess(false)
1474+
// the bug reported in #1144 only happens when
1475+
// usage.help() is called, this does not occur when
1476+
// console output is suppressed. tldr; we capture
1477+
// the log output:
1478+
let errorLog = ''
1479+
y._getLoggerInstance().error = (out) => {
1480+
if (out) errorLog += `${out}\n`
1481+
}
1482+
y.parse()
1483+
// the promise returned by the handler rejects immediately,
1484+
// so we can wait for a small number of ms:
1485+
setTimeout(() => {
1486+
// ensure the appropriate help is displayed:
1487+
errorLog.should.include('bar option')
1488+
// ensure error was displayed:
1489+
errorLog.should.include('foo error')
1490+
return done()
1491+
}, 5)
14591492
})
14601493
})
14611494

‎yargs.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,12 @@ function Yargs (processArgs, cwd, parentRequire) {
544544
argsert('[string|array] [function|boolean|object] [function]', [args, shortCircuit, _parseFn], arguments.length)
545545
freeze()
546546
if (typeof args === 'undefined') {
547-
const parsed = self._parseArgs(processArgs)
547+
const argv = self._parseArgs(processArgs)
548+
const tmpParsed = self.parsed
548549
unfreeze()
549550
// TODO: remove this compatibility hack when we release yargs@15.x:
550-
return (this.parsed = parsed)
551+
self.parsed = tmpParsed
552+
return argv
551553
}
552554

553555
// a context object can optionally be provided, this allows

0 commit comments

Comments
 (0)