-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove yargs-unparser; run yargs only once #3833
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,7 @@ | |
|
||
const debug = require('debug')('mocha:cli:cli'); | ||
const symbols = require('log-symbols'); | ||
const yargs = require('yargs'); | ||
const path = require('path'); | ||
const {loadOptions, YARGS_PARSER_CONFIG} = require('./options'); | ||
const commands = require('./commands'); | ||
const ansi = require('ansi-colors'); | ||
const {repository, homepage, version, gitter} = require('../../package.json'); | ||
|
||
|
@@ -32,7 +29,33 @@ exports.main = (argv = process.argv.slice(2)) => { | |
|
||
Error.stackTraceLimit = Infinity; // configurable via --stack-trace-limit? | ||
|
||
yargs | ||
const args = exports.parseArgv(argv); | ||
const {handler} = require(`./${args.command.name}`); | ||
args.command = null; | ||
handler(args); | ||
}; | ||
|
||
exports.yargsParse = function(argv) { | ||
const {loadOptions} = require('./options'); | ||
return exports | ||
.createYargs() | ||
.parse(argv, Object.assign(loadOptions(argv), {command: {}})); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't understood yet (neither in the current master) why the object returned by
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, sounds like a separate bug. EDIT: I'm also not sure why mocha would even allow aliases in a config file; seems like it introduces confusion without much benefit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even |
||
|
||
exports.showUsageAndError = function(error) { | ||
exports | ||
.createYargs() | ||
.check(() => { | ||
throw error; | ||
}) | ||
.parse([]); | ||
}; | ||
|
||
exports.createYargs = function() { | ||
const yargs = require('yargs'); | ||
const {YARGS_PARSER_CONFIG} = require('./options'); | ||
const commands = require('./commands'); | ||
return yargs | ||
.scriptName('mocha') | ||
.command(commands.run) | ||
.command(commands.init) | ||
|
@@ -59,8 +82,14 @@ exports.main = (argv = process.argv.slice(2)) => { | |
Docs: ${ansi.yellow(homepage)} | ||
` | ||
) | ||
.parserConfiguration(YARGS_PARSER_CONFIG) | ||
.parse(argv, loadOptions(argv)); | ||
.parserConfiguration(YARGS_PARSER_CONFIG); | ||
}; | ||
|
||
exports.parseArgv = function(argv = process.argv.slice(2)) { | ||
if (argv.length === 2 && argv[0] === '--preParsedJsonOptions') { | ||
return JSON.parse(argv[1]); | ||
} | ||
return exports.yargsParse(argv); | ||
}; | ||
|
||
// allow direct execution | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way duplicate alias options don't get eliminated. With
unparse()
we get a clean array without any aliases, so I'm not sure wether we can do withoutunparse()
as easily.It's a very good idea not to parse a second time in "/cli.cli.js".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's necessary to remove duplicate alias options using this method. They won't pass through yargs' parser again; the object is passed straight to mocha.
When mocha v6 re-parses the output of yargs-unparser, it creates those duplicate aliases all over again. So either way, the output object contains aliases, and mocha is built to handle it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think the
mocha
instance looks quite messy with all those aliases, but it seems to be able to handle them correctly.This could be too early, since the coerce function of
reporter-option
hasn't run yet?