fix: support symlinked CLI entrypoint guard

This commit is contained in:
Federico Jaramillo Martinez
2026-06-25 14:41:58 +02:00
parent 47c9b66819
commit 5269a7a7d7
2 changed files with 42 additions and 3 deletions
+30 -1
View File
@@ -1,5 +1,8 @@
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest"; import { afterEach, describe, expect, it } from "vitest";
import { commandWithVersionCheck } from "./cli.js"; import { commandWithVersionCheck, isCliEntrypoint } from "./cli.js";
const originalShell = process.env["SHELL"]; const originalShell = process.env["SHELL"];
@@ -29,3 +32,29 @@ describe("commandWithVersionCheck", () => {
expect(command).not.toContain("("); expect(command).not.toContain("(");
}); });
}); });
describe("isCliEntrypoint", () => {
it("matches direct execution paths", () => {
expect(isCliEntrypoint("/tmp/pi-web-cli.js", "/tmp/pi-web-cli.js")).toBe(true);
});
it("matches npm-style symlinked bin entrypoints", () => {
const dir = mkdtempSync(join(tmpdir(), "pi-web-cli-test-"));
try {
const target = join(dir, "dist", "cli.js");
const symlink = join(dir, "bin", "pi-web");
mkdirSync(join(dir, "dist"));
mkdirSync(join(dir, "bin"));
writeFileSync(target, "#!/usr/bin/env node\n", { mode: 0o755 });
symlinkSync(target, symlink);
expect(isCliEntrypoint(symlink, target)).toBe(true);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("does not match unrelated paths", () => {
expect(isCliEntrypoint("/tmp/pi-web", "/tmp/other-pi-web")).toBe(false);
});
});
+12 -2
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
import { spawnSync } from "node:child_process"; import { spawnSync } from "node:child_process";
import { existsSync, readFileSync } from "node:fs"; import { existsSync, readFileSync, realpathSync } from "node:fs";
import { mkdir, rm, writeFile } from "node:fs/promises"; import { mkdir, rm, writeFile } from "node:fs/promises";
import { homedir, userInfo } from "node:os"; import { homedir, userInfo } from "node:os";
import { basename, dirname, join, resolve } from "node:path"; import { basename, dirname, join, resolve } from "node:path";
@@ -1092,7 +1092,17 @@ async function main(): Promise<void> {
else throw new Error(`Unknown command: ${command}`); else throw new Error(`Unknown command: ${command}`);
} }
if (process.argv[1] === fileURLToPath(import.meta.url)) { export function isCliEntrypoint(entrypoint: string | undefined = process.argv[1], modulePath: string = fileURLToPath(import.meta.url)): boolean {
if (entrypoint === undefined) return false;
if (entrypoint === modulePath) return true;
try {
return realpathSync(entrypoint) === realpathSync(modulePath);
} catch {
return false;
}
}
if (isCliEntrypoint()) {
main().catch((error: unknown) => { main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : String(error)); console.error(error instanceof Error ? error.message : String(error));
process.exit(1); process.exit(1);