UNPKG

2.48 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5/**
6 * This is where we finally parse and handle arguments passed to the `mocha` executable.
7 * Option parsing is handled by {@link https://npm.im/yargs yargs}.
8 * If executed via `node`, this module will run {@linkcode module:lib/cli/cli.main main()}.
9 *
10 * @private
11 * @module
12 */
13
14const debug = require('debug')('mocha:cli:cli');
15const symbols = require('log-symbols');
16const yargs = require('yargs/yargs');
17const path = require('path');
18const {loadOptions, YARGS_PARSER_CONFIG} = require('./options');
19const commands = require('./commands');
20const ansi = require('ansi-colors');
21const {repository, homepage, version, gitter} = require('../../package.json');
22
23/**
24 * - Accepts an `Array` of arguments
25 * - Modifies {@link https://nodejs.org/api/modules.html#modules_module_paths Node.js' search path} for easy loading of consumer modules
26 * - Sets {@linkcode https://nodejs.org/api/errors.html#errors_error_stacktracelimit Error.stackTraceLimit} to `Infinity`
27 * @summary Mocha's main entry point from the command-line.
28 * @param {string[]} argv - Array of arguments to parse, or by default the lovely `process.argv.slice(2)`
29 */
30exports.main = (argv = process.argv.slice(2)) => {
31 debug('entered main with raw args', argv);
32 // ensure we can require() from current working directory
33 module.paths.push(process.cwd(), path.resolve('node_modules'));
34
35 Error.stackTraceLimit = Infinity; // configurable via --stack-trace-limit?
36
37 var args = loadOptions(argv);
38
39 yargs()
40 .scriptName('mocha')
41 .command(commands.run)
42 .command(commands.init)
43 .updateStrings({
44 'Positionals:': 'Positional Arguments',
45 'Options:': 'Other Options',
46 'Commands:': 'Commands'
47 })
48 .fail((msg, err, yargs) => {
49 debug(err);
50 yargs.showHelp();
51 console.error(`\n${symbols.error} ${ansi.red('ERROR:')} ${msg}`);
52 process.exit(1);
53 })
54 .help('help', 'Show usage information & exit')
55 .alias('help', 'h')
56 .version('version', 'Show version number & exit', version)
57 .alias('version', 'V')
58 .wrap(process.stdout.columns ? Math.min(process.stdout.columns, 80) : 80)
59 .epilog(
60 `Mocha Resources
61 Chat: ${ansi.magenta(gitter)}
62 GitHub: ${ansi.blue(repository.url)}
63 Docs: ${ansi.yellow(homepage)}
64 `
65 )
66 .parserConfiguration(YARGS_PARSER_CONFIG)
67 .config(args)
68 .parse(args._);
69};
70
71// allow direct execution
72if (require.main === module) {
73 exports.main();
74}