forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
146 lines (136 loc) · 2.99 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//@ts-check
process.env["BS_VSCODE"] = "1";
var fs = require("fs");
var path = require("path");
var cp = require("child_process");
var sourceDirs = [
"ext",
"common",
"frontend",
"depends",
"core",
"super_errors",
"outcome_printer",
"bsb",
"main",
"others",
"stdlib-406",
"runtime",
"test",
"ml",
"ounit_tests",
"bsb_helper",
"napkin",
"js_parser",
];
var buildAppending = false;
var isBuilding = false;
var jscompDir = path.join("..", "jscomp");
/**
*
* @param {Date} d
*/
function showDate(d) {
return `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`;
}
function rebuild() {
if (isBuilding) {
buildAppending = true;
} else {
console.log(">>>> Start compiling");
isBuilding = true;
cp.fork(path.join(__dirname, "ninja.js"), ["build"]).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 "package.json", line 1, characters 1-1:`);
// console.log(`Error: Failed to build ${showDate(new Date())}`);
}
console.log(">>>> Finish compiling (options: R|clean|config)");
// TODO: check ninja exit error code
if (code === 0) {
// This is not always correct
// ninjaFile.updateDev();
// This file is cached
cp.fork(path.join(__dirname, "ninja.js"), ["-dev"]);
}
}
}
/**
*
* @param {string} eventType
* @param {string} filename
*/
function onSourceChange(eventType, filename) {
// console.log('event ', eventType,filename)
if (
filename.endsWith(".ml") ||
filename.endsWith(".mli") ||
filename.endsWith(".mll") ||
filename.endsWith(".mly") ||
filename.endsWith(".json") ||
filename.endsWith(".res") ||
filename.endsWith(".resi")
) {
rebuild();
}
}
sourceDirs.forEach(x => {
fs.watch(path.join(jscompDir, x), "utf8", onSourceChange);
});
fs.watch(path.join("..", "package.json"), "utf8", onSourceChange);
rebuild();
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;
cp.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;
cp.fork(path.join(__dirname, "ninja.js"), ["cleanbuild"]).on(
"close",
() => {
isBuilding = false;
}
);
}
break;
}
});