|
1 | 1 | import { field, logger } from "@coder/logger" |
| 2 | +import { promises as fs } from "fs" |
2 | 3 | import http from "http" |
3 | 4 | import * as os from "os" |
4 | 5 | import * as path from "path" |
5 | 6 | import { Disposable } from "../common/emitter" |
6 | | -import { plural } from "../common/util" |
| 7 | +import { generateUuid, plural } from "../common/util" |
7 | 8 | import { createApp, ensureAddress } from "./app" |
8 | 9 | import { AuthType, DefaultedArgs, Feature, toCodeArgs, UserProvidedArgs } from "./cli" |
9 | | -import { commit, version, vsRootPath } from "./constants" |
| 10 | +import { commit, tmpdir, version, vsRootPath } from "./constants" |
10 | 11 | import { loadCustomStrings } from "./i18n" |
11 | 12 | import { register } from "./routes" |
12 | 13 | import { VSCodeModule } from "./routes/vscode" |
13 | | -import { isDirectory, open } from "./util" |
| 14 | +import { isDirectory, isFile, isNodeJSErrnoException, open } from "./util" |
14 | 15 | import { wrapper } from "./wrapper" |
15 | 16 |
|
16 | 17 | /** |
@@ -100,23 +101,53 @@ export const openInExistingInstance = async (args: DefaultedArgs, socketPath: st |
100 | 101 | logger.error("Please specify at least one file or folder") |
101 | 102 | process.exit(1) |
102 | 103 | } |
103 | | - const vscode = http.request( |
104 | | - { |
105 | | - path: "/", |
106 | | - method: "POST", |
107 | | - socketPath, |
108 | | - }, |
109 | | - (response) => { |
110 | | - response.on("data", (message) => { |
111 | | - logger.debug("got message from Code", field("message", message.toString())) |
112 | | - }) |
113 | | - }, |
114 | | - ) |
115 | | - vscode.on("error", (error: unknown) => { |
116 | | - logger.error("got error from Code", field("error", error)) |
117 | | - }) |
118 | | - vscode.write(JSON.stringify(pipeArgs)) |
119 | | - vscode.end() |
| 104 | + let marker: string | undefined |
| 105 | + if (args.wait) { |
| 106 | + if (pipeArgs.fileURIs.length === 0) { |
| 107 | + logger.error("--wait requires at least one file") |
| 108 | + process.exit(1) |
| 109 | + } |
| 110 | + marker = path.join(tmpdir, generateUuid(16)) |
| 111 | + await fs.mkdir(tmpdir, { recursive: true }) |
| 112 | + await fs.writeFile(marker, "") |
| 113 | + pipeArgs.waitMarkerFilePath = marker |
| 114 | + } |
| 115 | + try { |
| 116 | + await new Promise<void>((resolve, reject) => { |
| 117 | + const vscode = http.request( |
| 118 | + { |
| 119 | + path: "/", |
| 120 | + method: "POST", |
| 121 | + socketPath, |
| 122 | + }, |
| 123 | + (response) => { |
| 124 | + response.on("data", (message) => { |
| 125 | + logger.debug("got message from Code", field("message", message.toString())) |
| 126 | + }) |
| 127 | + response.on("end", resolve) |
| 128 | + }, |
| 129 | + ) |
| 130 | + vscode.on("error", reject) |
| 131 | + vscode.write(JSON.stringify(pipeArgs)) |
| 132 | + vscode.end() |
| 133 | + }) |
| 134 | + } catch (error) { |
| 135 | + // Try to clean up the marker, but do not let removal errors override the original error. |
| 136 | + if (marker) { |
| 137 | + try { |
| 138 | + await fs.rm(marker) |
| 139 | + } catch (rmError) { |
| 140 | + if (!isNodeJSErrnoException(rmError) || rmError.code !== "ENOENT") { |
| 141 | + logger.error("failed to clean up wait marker", field("error", rmError)) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + throw error |
| 146 | + } |
| 147 | + // Code deletes the marker file once the opened files are closed. |
| 148 | + while (marker && (await isFile(marker))) { |
| 149 | + await new Promise((resolve) => setTimeout(resolve, 1000)) |
| 150 | + } |
120 | 151 | } |
121 | 152 |
|
122 | 153 | export const runCodeServer = async ( |
|
0 commit comments