|
1 | 1 | #!/usr/bin/env node |
2 | 2 | import { createHash } from "node:crypto"; |
3 | | -import { createWriteStream, existsSync } from "node:fs"; |
| 3 | +import { createWriteStream, existsSync, realpathSync } from "node:fs"; |
4 | 4 | import { chmod, mkdir, readFile, rm } from "node:fs/promises"; |
5 | 5 | import { get } from "node:https"; |
6 | 6 | import { homedir, platform as osPlatform, tmpdir } from "node:os"; |
@@ -183,21 +183,31 @@ export async function ensureBinary(command = commandName()) { |
183 | 183 | async function main() { |
184 | 184 | const command = commandName(); |
185 | 185 | const binary = await ensureBinary(command); |
186 | | - const child = spawn(binary, process.argv.slice(2), { stdio: "inherit" }); |
187 | | - child.on("exit", (code, signal) => { |
188 | | - if (signal) { |
189 | | - process.kill(process.pid, signal); |
190 | | - return; |
191 | | - } |
192 | | - process.exit(code ?? 1); |
193 | | - }); |
194 | | - child.on("error", (error) => { |
195 | | - console.error(`nightward launcher failed: ${error.message}`); |
196 | | - process.exit(1); |
| 186 | + const result = await new Promise((resolve, reject) => { |
| 187 | + const child = spawn(binary, process.argv.slice(2), { stdio: "inherit" }); |
| 188 | + child.on("error", reject); |
| 189 | + child.on("exit", (code, signal) => resolve({ code: code ?? 1, signal })); |
197 | 190 | }); |
| 191 | + |
| 192 | + if (result.signal) { |
| 193 | + process.kill(process.pid, result.signal); |
| 194 | + return; |
| 195 | + } |
| 196 | + process.exit(result.code); |
| 197 | +} |
| 198 | + |
| 199 | +function isMainModule() { |
| 200 | + if (!process.argv[1]) { |
| 201 | + return false; |
| 202 | + } |
| 203 | + try { |
| 204 | + return realpathSync(modulePath) === realpathSync(process.argv[1]); |
| 205 | + } catch { |
| 206 | + return import.meta.url === pathToFileURL(process.argv[1]).href; |
| 207 | + } |
198 | 208 | } |
199 | 209 |
|
200 | | -if (import.meta.url === pathToFileURL(process.argv[1] || "").href) { |
| 210 | +if (isMainModule()) { |
201 | 211 | main().catch((error) => { |
202 | 212 | console.error(`nightward launcher failed: ${error.message}`); |
203 | 213 | process.exit(1); |
|
0 commit comments