UNPKG

1.8 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const dotenv = require('dotenv');
4dotenv.load();
5const Path = require('path');
6let yargs = require('yargs');
7const merge = require('merge-async-iterators');
8const streamAsync = require('streams-to-async-iterator')
9const options = require('./options');
10const mochista = require('..');
11const utils = require('lib/utils');
12
13yargs = yargs
14 .scriptName(require('../package.json').name)
15 .wrap(null)
16 .options(options)
17 .env('MOCHISTA');
18
19let argv = yargs.argv;
20dotenv.load({ path: argv.opts });
21yargs = yargs.env();
22argv = yargs.argv;
23
24process.env.NODE_V8_COVERAGE = argv.coverageDir = process.env.NODE_V8_COVERAGE || Path.join(argv.cwd || process.cwd(), argv.coverageDir || 'coverage', '.tmp');
25
26main(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
37async 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}