Fix packaged node entrypoint env preservation
This commit is contained in:
@@ -1,18 +1,66 @@
|
|||||||
|
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||||
|
import { tmpdir } from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { sanitizeNodeEntrypointEnv } from "./node-entrypoint-runner";
|
import { main } from "./node-entrypoint-runner";
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line no-var
|
||||||
|
var __PASEO_NODE_ENTRYPOINT_RUNNER_FIXTURE__:
|
||||||
|
| {
|
||||||
|
argv: string[];
|
||||||
|
electronRunAsNode: string | undefined;
|
||||||
|
electronNoAttachConsole: string | undefined;
|
||||||
|
}
|
||||||
|
| undefined;
|
||||||
|
}
|
||||||
|
|
||||||
describe("node-entrypoint-runner", () => {
|
describe("node-entrypoint-runner", () => {
|
||||||
it("removes Electron node-mode env before loading the target entrypoint", () => {
|
it("preserves Electron node-mode env before loading the target entrypoint", async () => {
|
||||||
const env = {
|
const originalArgv = process.argv;
|
||||||
ELECTRON_RUN_AS_NODE: "1",
|
const originalRunAsNode = process.env.ELECTRON_RUN_AS_NODE;
|
||||||
ELECTRON_NO_ATTACH_CONSOLE: "1",
|
const originalNoAttachConsole = process.env.ELECTRON_NO_ATTACH_CONSOLE;
|
||||||
PATH: "/usr/bin",
|
const originalFixture = globalThis.__PASEO_NODE_ENTRYPOINT_RUNNER_FIXTURE__;
|
||||||
};
|
const fixtureDir = mkdtempSync(path.join(tmpdir(), "paseo-node-entrypoint-runner-"));
|
||||||
|
const fixturePath = path.join(fixtureDir, "fixture.mjs");
|
||||||
|
writeFileSync(
|
||||||
|
fixturePath,
|
||||||
|
`
|
||||||
|
globalThis.__PASEO_NODE_ENTRYPOINT_RUNNER_FIXTURE__ = {
|
||||||
|
argv: process.argv,
|
||||||
|
electronRunAsNode: process.env.ELECTRON_RUN_AS_NODE,
|
||||||
|
electronNoAttachConsole: process.env.ELECTRON_NO_ATTACH_CONSOLE,
|
||||||
|
};
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
sanitizeNodeEntrypointEnv(env);
|
process.argv = ["Paseo", "runner", "node-script", fixturePath, "daemon", "start"];
|
||||||
|
process.env.ELECTRON_RUN_AS_NODE = "1";
|
||||||
|
process.env.ELECTRON_NO_ATTACH_CONSOLE = "1";
|
||||||
|
delete globalThis.__PASEO_NODE_ENTRYPOINT_RUNNER_FIXTURE__;
|
||||||
|
|
||||||
expect(env).toEqual({
|
try {
|
||||||
PATH: "/usr/bin",
|
await main();
|
||||||
});
|
|
||||||
|
expect(globalThis.__PASEO_NODE_ENTRYPOINT_RUNNER_FIXTURE__).toEqual({
|
||||||
|
argv: ["Paseo", fixturePath, "daemon", "start"],
|
||||||
|
electronRunAsNode: "1",
|
||||||
|
electronNoAttachConsole: "1",
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
process.argv = originalArgv;
|
||||||
|
if (originalRunAsNode === undefined) {
|
||||||
|
delete process.env.ELECTRON_RUN_AS_NODE;
|
||||||
|
} else {
|
||||||
|
process.env.ELECTRON_RUN_AS_NODE = originalRunAsNode;
|
||||||
|
}
|
||||||
|
if (originalNoAttachConsole === undefined) {
|
||||||
|
delete process.env.ELECTRON_NO_ATTACH_CONSOLE;
|
||||||
|
} else {
|
||||||
|
process.env.ELECTRON_NO_ATTACH_CONSOLE = originalNoAttachConsole;
|
||||||
|
}
|
||||||
|
globalThis.__PASEO_NODE_ENTRYPOINT_RUNNER_FIXTURE__ = originalFixture;
|
||||||
|
rmSync(fixtureDir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
import { pathToFileURL } from "node:url";
|
import { pathToFileURL } from "node:url";
|
||||||
|
|
||||||
export function sanitizeNodeEntrypointEnv(env: NodeJS.ProcessEnv = process.env): void {
|
|
||||||
delete env.ELECTRON_RUN_AS_NODE;
|
|
||||||
delete env.ELECTRON_NO_ATTACH_CONSOLE;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function main(): Promise<void> {
|
export async function main(): Promise<void> {
|
||||||
const [argvMode, entryPath, ...args] = process.argv.slice(2);
|
const [argvMode, entryPath, ...args] = process.argv.slice(2);
|
||||||
if (argvMode !== "bare" && argvMode !== "node-script") {
|
if (argvMode !== "bare" && argvMode !== "node-script") {
|
||||||
@@ -18,7 +13,6 @@ export async function main(): Promise<void> {
|
|||||||
argvMode === "bare"
|
argvMode === "bare"
|
||||||
? [process.argv[0] ?? "node", ...args]
|
? [process.argv[0] ?? "node", ...args]
|
||||||
: [process.argv[0] ?? "node", entryPath, ...args];
|
: [process.argv[0] ?? "node", entryPath, ...args];
|
||||||
sanitizeNodeEntrypointEnv();
|
|
||||||
await import(pathToFileURL(entryPath).href);
|
await import(pathToFileURL(entryPath).href);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user