#!/usr/bin/env node 'use strict'; const chalk = require('chalk'); const deepAssign = require('deep-assign'); const fs = require('fs'); const meow = require('meow'); const path = require('path'); const MochaChrome = require('../'); const cli = meow(chalk`{underline Usage} $ mocha-chrome [options] {underline Options} --mocha A JSON string representing a config object to pass to Mocha --log-level Specify a log level; trace, debug, info, warn, error --no-colors Disable colors in Mocha's output --reporter Specify the Mocha reporter to use --timeout Specify the test startup timeout to use --ignore-resource-errors Suppress resource error logging {underline Examples} $ mocha-chrome test.html --no-colors $ mocha-chrome test.html --reporter dot $ mocha-chrome test.html --mocha '\{"ui":"tdd"\}' `); if (!cli.input.length && !Object.getOwnPropertyNames(cli.flags).length) { cli.showHelp(); } const file = cli.input[0]; const flags = cli.flags; let url = file, code = 0; function fail (message) { console.log(message); process.exit(1); } if (!file && !file.length) { fail('You must specify a file to run'); } if (!/^(file|http(s?)):\/\//.test(file)) { if (!fs.existsSync(file)) { url = 'file://' + path.resolve(path.join(process.cwd(), file)); } if (!fs.existsSync(file)) { fail('You must specify an existing file.'); } url = 'file://' + fs.realpathSync(file); } const mochaDefaults = { reporter: flags.reporter || 'spec', useColors: !flags.colors }; const mocha = deepAssign(mochaDefaults, JSON.parse(flags.mocha || '{}')); const logLevel = flags.logLevel || 'error'; const ignoreResourceErrors = flags.ignoreResourceErrors || false const options = { url, logLevel, mocha, ignoreResourceErrors }; const runner = new MochaChrome(options); runner.on('ended', stats => { code = stats.failures; }); runner.on('failure', message => { code = 1; }); runner.on('exit', () => process.exit(code)); (async function run () { await runner.connect(); await runner.run(); })();