-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d02790
commit ba237e6
Showing
3 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// @ts-check | ||
|
||
import child_process from "node:child_process"; | ||
import fs from "node:fs/promises"; | ||
import process from "node:process"; | ||
|
||
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
const obtainLock = async () => { | ||
try { | ||
await fs.mkdir("build-lock"); | ||
return true; | ||
} catch (e) { | ||
if (e.code === "EEXIST") { | ||
return false; | ||
} | ||
|
||
throw new Error(e.message, { cause: e }); | ||
} | ||
}; | ||
|
||
const releaseLock = async () => await fs.rmdir("build-lock"); | ||
|
||
const waitAnotherBuild = async () => { | ||
while (true) { | ||
await sleep(1000); | ||
|
||
try { | ||
await fs.stat("build-lock"); | ||
} catch (e) { | ||
if (e.code === "ENOENT") { | ||
return; | ||
} | ||
|
||
throw new Error(e.message, { cause: e }); | ||
} | ||
} | ||
}; | ||
|
||
const main = async () => { | ||
if (!(await obtainLock())) { | ||
// An another build task is running concurrently. | ||
// Wait until it done. | ||
await waitAnotherBuild(); | ||
process.exit(0); | ||
} | ||
|
||
try { | ||
const restore = child_process.spawnSync("npm", ["install"], { | ||
stdio: "inherit", | ||
}); | ||
if (restore.status !== 0) { | ||
process.exit(restore.status); | ||
} | ||
|
||
const build = child_process.spawnSync("npm", ["run", "build"], { | ||
stdio: "inherit", | ||
}); | ||
if (build.status !== 0) { | ||
process.exit(build.status); | ||
} | ||
} finally { | ||
await releaseLock(); | ||
} | ||
}; | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters