| 11 | } |
| 12 | |
| 13 | class Watcher { |
| 14 | private rootPath = path.resolve(process.cwd()) |
| 15 | private readonly paths = { |
| 16 | /** Path to uncompiled VS Code source. */ |
| 17 | vscodeDir: path.join(this.rootPath, "lib/vscode"), |
| 18 | pluginDir: process.env.PLUGIN_DIR, |
| 19 | } |
| 20 | |
| 21 | //#region Web Server |
| 22 | |
| 23 | /** Development web server. */ |
| 24 | private webServer: ChildProcess | undefined |
| 25 | |
| 26 | private reloadWebServer = (): void => { |
| 27 | if (this.webServer) { |
| 28 | this.webServer.kill() |
| 29 | } |
| 30 | |
| 31 | // Pass CLI args, save for `node` and the initial script name. |
| 32 | const args = process.argv.slice(2) |
| 33 | this.webServer = spawn("node", [path.join(this.rootPath, "out/node/entry.js"), ...args]) |
| 34 | onLine(this.webServer, (line) => console.log("[code-server]", line)) |
| 35 | const { pid } = this.webServer |
| 36 | |
| 37 | this.webServer.on("exit", () => console.log("[code-server]", `Web process ${pid} exited`)) |
| 38 | |
| 39 | console.log("\n[code-server]", `Spawned web server process ${pid}`) |
| 40 | } |
| 41 | |
| 42 | //#endregion |
| 43 | |
| 44 | //#region Compilers |
| 45 | |
| 46 | private readonly compilers: DevelopmentCompilers = { |
| 47 | codeServer: spawn("tsc", ["--watch", "--pretty", "--preserveWatchOutput"], { cwd: this.rootPath }), |
| 48 | vscode: spawn("npm", ["run", "watch"], { cwd: this.paths.vscodeDir }), |
| 49 | vscodeWebExtensions: spawn("npm", ["run", "watch-web"], { cwd: this.paths.vscodeDir }), |
| 50 | plugins: this.paths.pluginDir |
| 51 | ? spawn("npm", ["run", "build", "--watch"], { cwd: this.paths.pluginDir }) |
| 52 | : undefined, |
| 53 | } |
| 54 | |
| 55 | public async initialize(): Promise<void> { |
| 56 | for (const event of ["SIGINT", "SIGTERM"]) { |
| 57 | process.on(event, () => this.dispose(0)) |
| 58 | } |
| 59 | |
| 60 | for (const [processName, devProcess] of Object.entries(this.compilers)) { |
| 61 | if (!devProcess) continue |
| 62 | |
| 63 | devProcess.on("exit", (code) => { |
| 64 | console.log(`[${processName}]`, "Terminated unexpectedly") |
| 65 | this.dispose(code) |
| 66 | }) |
| 67 | |
| 68 | if (devProcess.stderr) { |
| 69 | devProcess.stderr.on("data", (d: string | Uint8Array) => process.stderr.write(d)) |
| 70 | } |