1 | #!/usr/bin/env node
|
2 |
|
3 | const dotenv = require('dotenv');
|
4 | dotenv.load();
|
5 | const Path = require('path');
|
6 | let yargs = require('yargs');
|
7 | const merge = require('merge-async-iterators');
|
8 | const streamAsync = require('streams-to-async-iterator')
|
9 | const options = require('./options');
|
10 | const mochista = require('..');
|
11 | const utils = require('lib/utils');
|
12 |
|
13 | yargs = yargs
|
14 | .scriptName(require('../package.json').name)
|
15 | .wrap(null)
|
16 | .options(options)
|
17 | .env('MOCHISTA');
|
18 |
|
19 | let argv = yargs.argv;
|
20 | dotenv.load({ path: argv.opts });
|
21 | yargs = yargs.env();
|
22 | argv = yargs.argv;
|
23 |
|
24 | process.env.NODE_V8_COVERAGE = argv.coverageDir = process.env.NODE_V8_COVERAGE || Path.join(argv.cwd || process.cwd(), argv.coverageDir || 'coverage', '.tmp');
|
25 |
|
26 | main(argv).catch(error => {
|
27 | process.exitCode = error.exitCode || 1;
|
28 | if (error instanceof utils.Error && !error.failures) {
|
29 | console.error(error.message);
|
30 | } else {
|
31 | if (!error.failures) {
|
32 | console.error(error);
|
33 | }
|
34 | }
|
35 | });
|
36 |
|
37 | async function main(argv = {}) {
|
38 |
|
39 | const { watcher, run } = await mochista(argv);
|
40 | const stdin = argv.watch && streamAsync(process.stdin.setEncoding('utf8'));
|
41 | const merged = merge([watcher, stdin].filter(Boolean), { yieldIterator: true });
|
42 |
|
43 | let firstRun = true;
|
44 |
|
45 | for await (const { iterator } of merged) {
|
46 | try {
|
47 | if (
|
48 | firstRun
|
49 | || iterator === stdin
|
50 | || !(typeof argv.watch === 'string' && argv.watch.startsWith('i'))
|
51 | ) {
|
52 | firstRun = false;
|
53 | await run();
|
54 | }
|
55 | } catch (error) {
|
56 | if (argv.watch) {
|
57 | if (!error.failures) {
|
58 | console.error(error);
|
59 | }
|
60 | } else {
|
61 | throw error;
|
62 | }
|
63 | }
|
64 | if (argv.watch) {
|
65 | process.stdout.write('Press Enter to re-run');
|
66 | process.stdout.cursorTo(0);
|
67 | } else {
|
68 | break;
|
69 | }
|
70 | }
|
71 | }
|