forked from luontola/tdd-mooc-small-steps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcr.mjs
45 lines (39 loc) · 984 Bytes
/
tcr.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { execSync } from "child_process";
import chokidar from "chokidar";
function logCommand(command) {
console.log("\n> " + command);
return command;
}
function test() {
execSync(logCommand("npm run test"), {
stdio: "inherit",
env: { ...process.env, MAX_CHANGES: process.env.MAX_CHANGES || "1" },
});
}
function commit() {
console.log("Tests passed -> Commit changes");
execSync(logCommand('git commit --all --message="tcr: tests pass"'), {
stdio: "inherit",
});
}
function revert() {
console.log("Test failed -> Revert changes");
execSync(logCommand("git reset --hard"), { stdio: "inherit" });
}
try {
test();
} catch (e) {
console.log("TCR cancelled. Tests need to start green.");
process.exit(1);
}
chokidar.watch("src").on("all", (_event, _path) => {
const changes = execSync("git diff --numstat", { encoding: "utf8" });
if (changes !== "") {
try {
test();
commit();
} catch (e) {
revert();
}
}
});