2023-02-07 23:22:06 +01:00
|
|
|
import { spawn, ChildProcess } from "child_process"
|
2020-04-30 13:52:54 +02:00
|
|
|
import * as path from "path"
|
2021-12-17 20:06:52 +01:00
|
|
|
import { onLine, OnLineCallback } from "../../src/node/util"
|
2021-11-19 22:03:40 +01:00
|
|
|
|
|
|
|
interface DevelopmentCompilers {
|
|
|
|
[key: string]: ChildProcess | undefined
|
|
|
|
vscode: ChildProcess
|
|
|
|
vscodeWebExtensions: ChildProcess
|
|
|
|
codeServer: ChildProcess
|
|
|
|
plugins: ChildProcess | undefined
|
2020-04-30 13:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Watcher {
|
2021-11-19 22:03:40 +01:00
|
|
|
private rootPath = path.resolve(process.cwd())
|
|
|
|
private readonly paths = {
|
|
|
|
/** Path to uncompiled VS Code source. */
|
2022-03-15 03:37:29 +01:00
|
|
|
vscodeDir: path.join(this.rootPath, "lib/vscode"),
|
2021-11-19 22:03:40 +01:00
|
|
|
pluginDir: process.env.PLUGIN_DIR,
|
|
|
|
}
|
|
|
|
|
|
|
|
//#region Web Server
|
2020-04-30 13:52:54 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
/** Development web server. */
|
|
|
|
private webServer: ChildProcess | undefined
|
|
|
|
|
|
|
|
private reloadWebServer = (): void => {
|
|
|
|
if (this.webServer) {
|
|
|
|
this.webServer.kill()
|
2020-04-30 13:52:54 +02:00
|
|
|
}
|
2021-11-19 22:03:40 +01:00
|
|
|
|
|
|
|
// Pass CLI args, save for `node` and the initial script name.
|
|
|
|
const args = process.argv.slice(2)
|
2023-02-07 23:22:06 +01:00
|
|
|
this.webServer = spawn("node", [path.join(this.rootPath, "out/node/entry.js"), ...args])
|
2023-02-08 20:05:28 +01:00
|
|
|
onLine(this.webServer, (line) => console.log("[code-server]", line))
|
2021-11-19 22:03:40 +01:00
|
|
|
const { pid } = this.webServer
|
|
|
|
|
2023-02-08 20:05:28 +01:00
|
|
|
this.webServer.on("exit", () => console.log("[code-server]", `Web process ${pid} exited`))
|
2021-11-19 22:03:40 +01:00
|
|
|
|
2023-02-08 20:05:28 +01:00
|
|
|
console.log("\n[code-server]", `Spawned web server process ${pid}`)
|
2020-04-30 13:52:54 +02:00
|
|
|
}
|
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
//#endregion
|
2020-04-30 13:52:54 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
//#region Compilers
|
2021-09-30 05:14:56 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
private readonly compilers: DevelopmentCompilers = {
|
|
|
|
codeServer: spawn("tsc", ["--watch", "--pretty", "--preserveWatchOutput"], { cwd: this.rootPath }),
|
|
|
|
vscode: spawn("yarn", ["watch"], { cwd: this.paths.vscodeDir }),
|
|
|
|
vscodeWebExtensions: spawn("yarn", ["watch-web"], { cwd: this.paths.vscodeDir }),
|
|
|
|
plugins: this.paths.pluginDir ? spawn("yarn", ["build", "--watch"], { cwd: this.paths.pluginDir }) : undefined,
|
|
|
|
}
|
2021-09-30 05:14:56 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
public async initialize(): Promise<void> {
|
|
|
|
for (const event of ["SIGINT", "SIGTERM"]) {
|
|
|
|
process.on(event, () => this.dispose(0))
|
|
|
|
}
|
2020-04-30 13:52:54 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
for (const [processName, devProcess] of Object.entries(this.compilers)) {
|
|
|
|
if (!devProcess) continue
|
2020-07-31 23:27:32 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
devProcess.on("exit", (code) => {
|
2021-12-08 00:38:03 +01:00
|
|
|
console.log(`[${processName}]`, "Terminated unexpectedly")
|
2021-11-19 22:03:40 +01:00
|
|
|
this.dispose(code)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (devProcess.stderr) {
|
|
|
|
devProcess.stderr.on("data", (d: string | Uint8Array) => process.stderr.write(d))
|
2020-04-30 13:52:54 +02:00
|
|
|
}
|
2021-11-19 22:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onLine(this.compilers.vscode, this.parseVSCodeLine)
|
|
|
|
onLine(this.compilers.codeServer, this.parseCodeServerLine)
|
2020-04-30 13:52:54 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
if (this.compilers.plugins) {
|
|
|
|
onLine(this.compilers.plugins, this.parsePluginLine)
|
2020-04-30 13:52:54 +02:00
|
|
|
}
|
2021-11-19 22:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//#endregion
|
2020-04-30 13:52:54 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
//#region Line Parsers
|
2020-04-30 13:52:54 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
private parseVSCodeLine: OnLineCallback = (strippedLine, originalLine) => {
|
2021-12-08 00:38:03 +01:00
|
|
|
if (!strippedLine.length) return
|
|
|
|
|
2023-02-08 20:05:28 +01:00
|
|
|
console.log("[Code OSS]", originalLine)
|
2021-09-30 05:14:56 +02:00
|
|
|
|
2021-12-08 00:38:03 +01:00
|
|
|
if (strippedLine.includes("Finished compilation with")) {
|
2023-02-08 20:05:28 +01:00
|
|
|
console.log("[Code OSS] ✨ Finished compiling! ✨", "(Refresh your web browser ♻️)")
|
2021-12-08 00:38:03 +01:00
|
|
|
this.reloadWebServer()
|
2021-11-19 22:03:40 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-30 05:14:56 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
private parseCodeServerLine: OnLineCallback = (strippedLine, originalLine) => {
|
|
|
|
if (!strippedLine.length) return
|
2021-09-30 05:14:56 +02:00
|
|
|
|
2023-02-08 20:05:28 +01:00
|
|
|
console.log("[Compiler][code-server]", originalLine)
|
2021-11-19 22:03:40 +01:00
|
|
|
|
|
|
|
if (strippedLine.includes("Watching for file changes")) {
|
2023-02-08 20:05:28 +01:00
|
|
|
console.log("[Compiler][code-server]", "Finished compiling!", "(Refresh your web browser ♻️)")
|
2021-11-19 22:03:40 +01:00
|
|
|
this.reloadWebServer()
|
2020-08-18 19:03:49 +02:00
|
|
|
}
|
2021-11-19 22:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private parsePluginLine: OnLineCallback = (strippedLine, originalLine) => {
|
|
|
|
if (!strippedLine.length) return
|
2020-04-30 13:52:54 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
console.log("[Compiler][Plugin]", originalLine)
|
2021-09-30 05:14:56 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
if (strippedLine.includes("Watching for file changes...")) {
|
|
|
|
this.reloadWebServer()
|
2020-08-18 19:03:49 +02:00
|
|
|
}
|
2021-11-19 22:03:40 +01:00
|
|
|
}
|
2020-04-30 13:52:54 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
//#endregion
|
2020-04-30 13:52:54 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
//#region Utilities
|
2020-07-31 23:27:32 +02:00
|
|
|
|
2021-11-19 22:03:40 +01:00
|
|
|
private dispose(code: number | null): void {
|
|
|
|
for (const [processName, devProcess] of Object.entries(this.compilers)) {
|
2021-12-08 00:38:03 +01:00
|
|
|
console.log(`[${processName}]`, "Killing...\n")
|
2021-11-19 22:03:40 +01:00
|
|
|
devProcess?.removeAllListeners()
|
|
|
|
devProcess?.kill()
|
2020-08-18 19:03:49 +02:00
|
|
|
}
|
2021-11-19 22:03:40 +01:00
|
|
|
process.exit(typeof code === "number" ? code : 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main(): Promise<void> {
|
|
|
|
try {
|
|
|
|
const watcher = new Watcher()
|
|
|
|
await watcher.initialize()
|
|
|
|
} catch (error: any) {
|
|
|
|
console.error(error.message)
|
|
|
|
process.exit(1)
|
2020-04-30 13:52:54 +02:00
|
|
|
}
|
2021-06-23 19:19:50 +02:00
|
|
|
}
|
2020-04-30 13:52:54 +02:00
|
|
|
|
|
|
|
main()
|