2021-11-15 00:18:51 +00:00
|
|
|
import { spawn, fork, ChildProcess } from "child_process"
|
|
|
|
import del from "del"
|
|
|
|
import { promises as fs } from "fs"
|
2020-04-30 11:52:54 +00:00
|
|
|
import * as path from "path"
|
2021-11-15 00:18:51 +00:00
|
|
|
import { CompilationStats, onLine, OnLineCallback, VSCodeCompileStatus } from "../../src/node/util"
|
|
|
|
|
|
|
|
interface DevelopmentCompilers {
|
|
|
|
[key: string]: ChildProcess | undefined
|
|
|
|
vscode: ChildProcess
|
|
|
|
vscodeWebExtensions: ChildProcess
|
|
|
|
codeServer: ChildProcess
|
|
|
|
plugins: ChildProcess | undefined
|
2020-04-30 11:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Watcher {
|
2021-11-15 00:18:51 +00:00
|
|
|
private rootPath = path.resolve(process.cwd())
|
|
|
|
private readonly paths = {
|
|
|
|
/** Path to uncompiled VS Code source. */
|
|
|
|
vscodeDir: path.join(this.rootPath, "vendor", "modules", "code-oss-dev"),
|
|
|
|
compilationStatsFile: path.join(this.rootPath, "out", "watcher.json"),
|
|
|
|
pluginDir: process.env.PLUGIN_DIR,
|
|
|
|
}
|
|
|
|
|
|
|
|
//#region Web Server
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
/** Development web server. */
|
|
|
|
private webServer: ChildProcess | undefined
|
|
|
|
|
|
|
|
private reloadWebServer = (): void => {
|
|
|
|
if (this.webServer) {
|
|
|
|
this.webServer.kill()
|
2020-04-30 11:52:54 +00:00
|
|
|
}
|
2021-11-15 00:18:51 +00:00
|
|
|
|
|
|
|
// Pass CLI args, save for `node` and the initial script name.
|
|
|
|
const args = process.argv.slice(2)
|
|
|
|
this.webServer = fork(path.join(this.rootPath, "out/node/entry.js"), args)
|
|
|
|
const { pid } = this.webServer
|
|
|
|
|
|
|
|
this.webServer.on("exit", () => console.log("[Code Server]", `Web process ${pid} exited`))
|
|
|
|
|
|
|
|
console.log("\n[Code Server]", `Spawned web server process ${pid}`)
|
2020-04-30 11:52:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
//#endregion
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
//#region Compilers
|
2021-09-30 03:14:56 +00:00
|
|
|
|
2021-11-15 00:18:51 +00: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 03:14:56 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
private vscodeCompileStatus = VSCodeCompileStatus.Loading
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
public async initialize(): Promise<void> {
|
|
|
|
for (const event of ["SIGINT", "SIGTERM"]) {
|
|
|
|
process.on(event, () => this.dispose(0))
|
|
|
|
}
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
if (!this.hasVerboseLogging) {
|
|
|
|
console.log("\n[Watcher]", "Compiler logs will be minimal. Pass --log to show all output.")
|
|
|
|
}
|
2021-09-30 03:14:56 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
this.cleanFiles()
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
for (const [processName, devProcess] of Object.entries(this.compilers)) {
|
|
|
|
if (!devProcess) continue
|
2020-07-31 21:27:32 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
devProcess.on("exit", (code) => {
|
|
|
|
this.log(`[${processName}]`, "Terminated unexpectedly")
|
|
|
|
this.dispose(code)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (devProcess.stderr) {
|
|
|
|
devProcess.stderr.on("data", (d: string | Uint8Array) => process.stderr.write(d))
|
2020-04-30 11:52:54 +00:00
|
|
|
}
|
2021-11-15 00:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onLine(this.compilers.vscode, this.parseVSCodeLine)
|
|
|
|
onLine(this.compilers.codeServer, this.parseCodeServerLine)
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
if (this.compilers.plugins) {
|
|
|
|
onLine(this.compilers.plugins, this.parsePluginLine)
|
2020-04-30 11:52:54 +00:00
|
|
|
}
|
2021-11-15 00:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//#endregion
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
//#region Line Parsers
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
private parseVSCodeLine: OnLineCallback = (strippedLine, originalLine) => {
|
|
|
|
if (!strippedLine.includes("watch-extensions") || this.hasVerboseLogging) {
|
|
|
|
console.log("[VS Code]", originalLine)
|
|
|
|
}
|
2021-09-30 03:14:56 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
switch (this.vscodeCompileStatus) {
|
|
|
|
case VSCodeCompileStatus.Loading:
|
|
|
|
// Wait for watch-client since "Finished compilation" will appear multiple
|
|
|
|
// times before the client starts building.
|
|
|
|
if (strippedLine.includes("Starting 'watch-client'")) {
|
|
|
|
console.log("[VS Code] 🚧 Compiling 🚧", "(This may take a moment!)")
|
|
|
|
this.vscodeCompileStatus = VSCodeCompileStatus.Compiling
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case VSCodeCompileStatus.Compiling:
|
|
|
|
if (strippedLine.includes("Finished compilation")) {
|
|
|
|
console.log("[VS Code] ✨ Finished compiling! ✨", "(Refresh your web browser ♻️)")
|
|
|
|
this.vscodeCompileStatus = VSCodeCompileStatus.Compiled
|
|
|
|
|
|
|
|
this.emitCompilationStats()
|
|
|
|
this.reloadWebServer()
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case VSCodeCompileStatus.Compiled:
|
|
|
|
console.log("[VS Code] 🔔 Finished recompiling! 🔔", "(Refresh your web browser ♻️)")
|
|
|
|
this.emitCompilationStats()
|
|
|
|
this.reloadWebServer()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 03:14:56 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
private parseCodeServerLine: OnLineCallback = (strippedLine, originalLine) => {
|
|
|
|
if (!strippedLine.length) return
|
2021-09-30 03:14:56 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
console.log("[Compiler][Code Server]", originalLine)
|
|
|
|
|
|
|
|
if (strippedLine.includes("Watching for file changes")) {
|
|
|
|
console.log("[Compiler][Code Server]", "Finished compiling!", "(Refresh your web browser ♻️)")
|
|
|
|
|
|
|
|
this.reloadWebServer()
|
2020-08-18 17:03:49 +00:00
|
|
|
}
|
2021-11-15 00:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private parsePluginLine: OnLineCallback = (strippedLine, originalLine) => {
|
|
|
|
if (!strippedLine.length) return
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
console.log("[Compiler][Plugin]", originalLine)
|
2021-09-30 03:14:56 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
if (strippedLine.includes("Watching for file changes...")) {
|
|
|
|
this.reloadWebServer()
|
2020-08-18 17:03:49 +00:00
|
|
|
}
|
2021-11-15 00:18:51 +00:00
|
|
|
}
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
//#endregion
|
2020-04-30 11:52:54 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
//#region Utilities
|
2020-07-31 21:27:32 +00:00
|
|
|
|
2021-11-15 00:18:51 +00:00
|
|
|
/**
|
|
|
|
* Cleans files from previous builds.
|
|
|
|
*/
|
|
|
|
private cleanFiles(): Promise<string[]> {
|
|
|
|
console.log("[Watcher]", "Cleaning files from previous builds...")
|
|
|
|
|
|
|
|
return del([
|
|
|
|
"out/**/*",
|
|
|
|
// Included because the cache can sometimes enter bad state when debugging compiled files.
|
|
|
|
".cache/**/*",
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emits a file containing compilation data.
|
|
|
|
* This is especially useful when Express needs to determine if VS Code is still compiling.
|
|
|
|
*/
|
|
|
|
private emitCompilationStats(): Promise<void> {
|
|
|
|
const stats: CompilationStats = {
|
|
|
|
status: this.vscodeCompileStatus,
|
|
|
|
lastCompiledAt: new Date(),
|
|
|
|
}
|
|
|
|
|
|
|
|
this.log("Writing watcher stats...")
|
|
|
|
return fs.writeFile(this.paths.compilationStatsFile, JSON.stringify(stats, null, 2))
|
|
|
|
}
|
|
|
|
|
|
|
|
private log(...entries: string[]) {
|
|
|
|
process.stdout.write(entries.join(" "))
|
|
|
|
}
|
|
|
|
|
|
|
|
private dispose(code: number | null): void {
|
|
|
|
for (const [processName, devProcess] of Object.entries(this.compilers)) {
|
|
|
|
this.log(`[${processName}]`, "Killing...\n")
|
|
|
|
devProcess?.removeAllListeners()
|
|
|
|
devProcess?.kill()
|
2020-08-18 17:03:49 +00:00
|
|
|
}
|
2021-11-15 00:18:51 +00:00
|
|
|
process.exit(typeof code === "number" ? code : 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
private get hasVerboseLogging() {
|
|
|
|
return process.argv.includes("--log")
|
|
|
|
}
|
|
|
|
|
|
|
|
//#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 11:52:54 +00:00
|
|
|
}
|
2021-06-23 17:19:50 +00:00
|
|
|
}
|
2020-04-30 11:52:54 +00:00
|
|
|
|
|
|
|
main()
|