mocha-typescript
Version:
TypeScript decorators based wrapper over mocha's interface
132 lines • 3.9 kB
JavaScript
#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const spawn = require("cross-spawn");
const readline = require("readline");
const yargs = require("yargs");
const argv = yargs
.options({
p: {
alias: "project",
demand: false,
default: ".",
describe: "Path to tsconfig file or directory containing tsconfig, passed to `tsc -p <value>`.",
type: "string",
},
t: {
alias: "tsc",
demand: false,
default: "./node_modules/typescript/bin/tsc",
describe: "Path to executable tsc, by default points to typescript installed as dev dependency. Set to 'tsc' for global tsc installation.",
type: "string",
},
o: {
alias: "opts",
demand: false,
default: "./test/mocha.opts",
describe: "Path to mocha.opts file containing additional mocha configuration.",
type: "string",
},
m: {
alias: "mocha",
demand: false,
default: "./node_modules/mocha/bin/mocha",
describe: "Path to executable mocha, by default points to mocha installed as dev dependency.",
type: "string",
},
g: {
alias: "grep",
demand: false,
default: undefined,
describe: "Passed down to mocha: only run tests matching <pattern>",
type: "string",
},
f: {
alias: "fgrep",
demand: false,
default: undefined,
describe: "Passed down to mocha: only run tests containing <string>",
type: "string",
},
})
.help("h")
.alias("h", "help")
.argv;
const stdl = readline.createInterface({ input: process.stdin });
stdl.on("line", (line) => {
// TODO: handle "g <pattern>" or "f <pattern>" to run mocha with pattern
// Ctrl + R may restart mocha test run?
});
let mochap = null;
let errors = 0;
function compilationStarted() {
if (mochap) {
mochap.kill("SIGINT");
console.log();
}
mochap = null;
errors = 0;
}
function foundErrors() {
errors++;
}
function compilationComplete() {
if (errors) {
console.log(chalk_1.default.red("TS errors!"));
return;
}
else {
console.log(chalk_1.default.gray("Run mocha."));
}
const mochaOptions = ["--opts", argv.opts, "--colors"].concat(argv._);
if (argv.g) {
mochaOptions.push("-g");
mochaOptions.push(argv.g);
}
if (argv.f) {
mochaOptions.push("-f");
mochaOptions.push(argv.f);
}
mochap = spawn(argv.mocha, mochaOptions);
const source = mochap;
mochap.on("close", (code) => {
if (source === mochap) {
if (code) {
console.log(chalk_1.default.red("Exited with " + code));
}
else {
}
mochap = null;
}
});
mochap.stdout.on("data", (chunk) => {
// Ensure old processes won't interfere tsc, .pipe here may be good enough.
if (source === mochap) {
process.stdout.write(chunk);
}
});
mochap.stderr.on("data", (chunk) => {
// Ensure old processes won't interfere tsc, .pipe here may be good enough.
if (source === mochap) {
process.stderr.write(chunk);
}
});
}
const tscp = spawn(argv.tsc, ["-p", argv.project, "-w"]);
const tscl = readline.createInterface({ input: tscp.stdout });
tscl.on("line", (line) => {
if (line.indexOf("Compilation complete.") >= 0 || line.indexOf("Found ") >= 0) {
console.log(line);
compilationComplete();
}
else if (line.indexOf("File change detected.") >= 0) {
compilationStarted();
console.log(line);
}
else if (line.indexOf(": error TS") >= 0) {
console.log(line);
foundErrors();
}
});
//# sourceMappingURL=watch.js.map