Skip to content

Commit ee76c2f

Browse files
committed
how about this?
1 parent 5f0cd48 commit ee76c2f

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

scripts/rescript_bsb.js

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,47 @@ var os = require("os");
66
const child_process = require("child_process");
77
const rescript_exe = require("./bin_path").rescript_exe;
88

9-
/**
10-
* @typedef {Object} ProjectFiles
11-
* @property {Array<string>} dirs
12-
* @property {Array<string>} generated
13-
*/
14-
15-
/**
16-
* @typedef {Object} WatcherRef
17-
* @property {string} dir
18-
* @property {fs.FSWatcher} watcher
19-
*/
20-
219
const cwd = process.cwd();
2210
const lockFileName = path.join(cwd, ".bsb.lock");
2311

24-
let isBuilding = false;
12+
/**
13+
* @type {child_process.ChildProcess | null}
14+
*/
15+
let ownerProcess = null;
2516
function releaseBuild() {
26-
if (isBuilding) {
17+
if (ownerProcess) {
2718
try {
19+
ownerProcess.kill();
2820
fs.unlinkSync(lockFileName);
29-
} catch (err) {}
30-
isBuilding = false;
21+
} catch {}
22+
ownerProcess = null;
3123
}
3224
}
3325

34-
// We use [~perm:0o664] rather than our usual default perms, [0o666], because
35-
// lock files shouldn't rely on the umask to disallow tampering by other.
36-
function acquireBuild() {
37-
if (isBuilding) {
38-
return false;
26+
/**
27+
* We use [~perm:0o664] rather than our usual default perms, [0o666], because
28+
* lock files shouldn't rely on the umask to disallow tampering by other.
29+
*
30+
* @param {Array<string>} args
31+
*/
32+
function acquireBuild(args) {
33+
if (ownerProcess) {
34+
return null;
3935
} else {
4036
try {
41-
const fid = fs.openSync(lockFileName, "wx", 0o664);
42-
fs.closeSync(fid);
43-
isBuilding = true;
37+
ownerProcess = child_process.spawn(rescript_exe, args, {
38+
stdio: "inherit",
39+
});
40+
fs.writeFileSync(lockFileName, ownerProcess.pid.toString(), {
41+
encoding: "utf8",
42+
mode: 0o664,
43+
});
4444
} catch (err) {
4545
if (err.code === "EEXIST") {
4646
console.warn(lockFileName, "already exists, try later");
4747
} else console.log(err);
4848
}
49-
return isBuilding;
49+
return ownerProcess;
5050
}
5151
}
5252

@@ -55,23 +55,17 @@ function acquireBuild() {
5555
* @param {(code: number) => void} [maybeOnClose]
5656
*/
5757
function delegate(args, maybeOnClose) {
58-
if (acquireBuild()) {
59-
/**
60-
* @type {child_process.ChildProcess}
61-
*/
62-
const p = child_process.spawn(rescript_exe, args, {
63-
stdio: "inherit",
64-
});
65-
66-
p.on("error", e => {
58+
let p;
59+
if ((p = acquireBuild(args))) {
60+
p.once("error", e => {
6761
console.error(String(e));
6862
releaseBuild();
6963
process.exit(2);
7064
});
7165

7266
// The 'close' event will always emit after 'exit' was already emitted, or
7367
// 'error' if the child failed to spawn.
74-
p.on("close", code => {
68+
p.once("close", code => {
7569
releaseBuild();
7670
const exitCode = code === null ? 1 : code;
7771
if (maybeOnClose) {
@@ -376,7 +370,7 @@ Please pick a different one using the \`-ws [host:]port\` flag from bsb.`);
376370
} else {
377371
dlog(`Rebuilding since ${reasonsToRebuild}`);
378372
}
379-
if (acquireBuild()) {
373+
if (acquireBuild(args)) {
380374
logStartCompiling();
381375
child_process
382376
.spawn(rescript_exe, rescriptWatchBuildArgs, {

0 commit comments

Comments
 (0)