1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 |
|
4 | const chalk = require('chalk');
|
5 | const deepAssign = require('deep-assign');
|
6 | const fs = require('fs');
|
7 | const meow = require('meow');
|
8 | const path = require('path');
|
9 |
|
10 | const MochaChrome = require('../');
|
11 | const cli = meow(chalk`{underline Usage}
|
12 | $ mocha-chrome <file.html> [options]
|
13 |
|
14 |
|
15 | {underline Options}
|
16 | --mocha A JSON string representing a config object to pass to Mocha
|
17 | --log-level Specify a log level; trace, debug, info, warn, error
|
18 | --no-colors Disable colors in Mocha's output
|
19 | --reporter Specify the Mocha reporter to use
|
20 | --timeout Specify the test startup timeout to use
|
21 | --ignore-resource-errors Suppress resource error logging
|
22 |
|
23 |
|
24 | {underline Examples}
|
25 | $ mocha-chrome test.html --no-colors
|
26 | $ mocha-chrome test.html --reporter dot
|
27 | $ mocha-chrome test.html --mocha '\{"ui":"tdd"\}'
|
28 | `);
|
29 |
|
30 | if (!cli.input.length && !Object.getOwnPropertyNames(cli.flags).length) {
|
31 | cli.showHelp();
|
32 | }
|
33 |
|
34 | const file = cli.input[0];
|
35 | const flags = cli.flags;
|
36 | let url = file,
|
37 | code = 0;
|
38 |
|
39 | function fail (message) {
|
40 | console.log(message);
|
41 | process.exit(1);
|
42 | }
|
43 |
|
44 | if (!file && !file.length) {
|
45 | fail('You must specify a file to run');
|
46 | }
|
47 |
|
48 | if (!/^(file|http(s?)):\/\//.test(file)) {
|
49 | if (!fs.existsSync(file)) {
|
50 | url = 'file://' + path.resolve(path.join(process.cwd(), file));
|
51 | }
|
52 |
|
53 | if (!fs.existsSync(file)) {
|
54 | fail('You must specify an existing file.');
|
55 | }
|
56 |
|
57 | url = 'file://' + fs.realpathSync(file);
|
58 | }
|
59 |
|
60 | const mochaDefaults = {
|
61 | reporter: flags.reporter || 'spec',
|
62 | useColors: !flags.colors
|
63 | };
|
64 | const mocha = deepAssign(mochaDefaults, JSON.parse(flags.mocha || '{}'));
|
65 | const logLevel = flags.logLevel || 'error';
|
66 | const ignoreResourceErrors = flags.ignoreResourceErrors || false
|
67 | const options = { url, logLevel, mocha, ignoreResourceErrors };
|
68 | const runner = new MochaChrome(options);
|
69 |
|
70 | runner.on('ended', stats => {
|
71 | code = stats.failures;
|
72 | });
|
73 |
|
74 | runner.on('failure', message => {
|
75 | code = 1;
|
76 | });
|
77 |
|
78 | runner.on('exit', () => process.exit(code));
|
79 |
|
80 | (async function run () {
|
81 | await runner.connect();
|
82 | await runner.run();
|
83 | })();
|