forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
121 lines (115 loc) · 2.58 KB
/
tasks.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//@ts-check
process.env["BS_VSCODE"] = "1";
var fs = require("fs");
var path = require("path");
var cp = require("child_process");
var sourceDirs = [
"ext",
"common",
"syntax",
"depends",
"core",
"super_errors",
"outcome_printer",
"bsb",
"main",
"others",
"stdlib-406",
"runtime",
"test",
"ounit_tests",
"bsb_helper"
];
var buildAppending = false;
var isBuilding = false;
var ninjaFile = require("./ninja.js");
var jscompDir = path.join("..", "jscomp");
function rebuild() {
if (isBuilding) {
buildAppending = true;
} else {
console.log(">>>> Start compiling");
isBuilding = true;
var p = cp.spawn(ninjaFile.vendorNinjaPath, [], {
stdio: ["inherit", "inherit", "pipe"]
});
p.on("exit", buildFinished);
}
}
/**
*
* @param {number} code
* @param {string} signal
*/
function buildFinished(code, signal) {
isBuilding = false;
if (buildAppending) {
buildAppending = false;
rebuild();
} else {
if (code !== 0) {
console.log(`File "BUILD", line 1, characters 1-1:`);
console.log(`Error: Failed to build`);
}
console.log(">>>> Finish compiling (options: R|clean|config)");
// TODO: check ninja exit error code
if (code === 0) {
// This is not always correct
ninjaFile.updateDev();
}
}
}
/**
*
* @param {string} eventType
* @param {string} filename
*/
function onSourceChange(eventType, filename) {
// console.log('event ', eventType,filename)
if (filename.endsWith(".ml") || filename.endsWith(".mli")) {
rebuild();
}
}
sourceDirs.forEach(x => {
fs.watch(path.join(jscompDir, x), "utf8", onSourceChange);
});
rebuild();
var child_process = require("child_process");
var readline = require("readline");
readline
.createInterface({
input: process.stdin,
output: process.stdout
})
.on("line", input => {
switch (input.toLowerCase()) {
case "r":
rebuild();
break;
case "config":
if (isBuilding) {
console.log(`it's building`);
} else {
isBuilding = true;
child_process
.fork(path.join(__dirname, "ninja.js"), ["config"])
.on("close", () => {
isBuilding = false;
rebuild();
});
}
break;
case "clean":
if (isBuilding) {
console.log(`it's building`);
} else {
isBuilding = true;
child_process
.fork(path.join(__dirname, "ninja.js"), ["cleanbuild"])
.on("close", () => {
isBuilding = false;
});
}
break;
}
});