Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fakeCli.ts

84 lines3.1 KB

1// @effect-diagnostics nodeBuiltinImport:off preferSchemaOverJson:off - synchronous fixture writer used from plain and Effect tests alike.
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4​
5import * as HostProcess from "@t3tools/shared/HostProcess";
6​
7export interface FakeCliOptions {
8 readonly directory: string;
9 readonly name: string;
10 readonly source: string;
11 readonly env?: Readonly<Record<string, string>>;
12 readonly platform?: NodeJS.Platform;
14​
15/**
16 * On posix the launcher is a `#!/bin/sh` script; on Windows it is a `.cmd` shim, since a
17 * shebang file is not executable there and `resolveSpawnCommand` routes `.cmd` through a shell.
18 */
19export function writeFakeCli(options: FakeCliOptions): string {
20 NodeFS.mkdirSync(options.directory, { recursive: true });
21 const stubPath = NodePath.join(options.directory, `${options.name}-stub.mjs`);
22 // The environment rides in a JSON sidecar the stub applies to itself, since
23 // neither sh nor cmd.exe can quote every value (newlines, quotes, `%`) safely.
24 const envPath = NodePath.join(options.directory, `${options.name}-env.json`);
25 NodeFS.writeFileSync(envPath, JSON.stringify(options.env ?? {}), "utf8");
26 NodeFS.writeFileSync(
27 stubPath,
28 [
29 'import { readFileSync as readFakeCliEnv } from "node:fs";',
30 `Object.assign(process.env, JSON.parse(readFakeCliEnv(${JSON.stringify(envPath)}, "utf8")));`,
31 options.source,
32 ].join("\n"),
33 "utf8",
34 );
35​
36 if ((options.platform ?? HostProcess.Platform.defaultValue()) === "win32") {
37 const launcherPath = NodePath.join(options.directory, `${options.name}.cmd`);
38 NodeFS.writeFileSync(
39 launcherPath,
40 ["@echo off", `node "%~dp0${options.name}-stub.mjs" %*`, "exit /b %ERRORLEVEL%", ""].join(
41 "\r\n",
42 ),
43 "utf8",
44 );
45 return launcherPath;
46 }
47​
48 const launcherPath = NodePath.join(options.directory, options.name);
49 NodeFS.writeFileSync(
50 launcherPath,
51 ["#!/bin/sh", `exec node "$(dirname "$0")/${options.name}-stub.mjs" "$@"`, ""].join("\n"),
52 "utf8",
53 );
54 NodeFS.chmodSync(launcherPath, 0o755);
55 return launcherPath;
57​
58export function execScriptSource(options: {
59 readonly scriptPath: string;
60 readonly expectedArgs?: ReadonlyArray<string>;
61 readonly argvLogPath?: string;
62 readonly delayMs?: number;
63}): string {
64 return [
65 'import { appendFileSync } from "node:fs";',
66 'import { pathToFileURL } from "node:url";',
67 "const args = process.argv.slice(2);",
68 ...(options.argvLogPath === undefined
69 ? []
70 : [
71 `appendFileSync(${JSON.stringify(options.argvLogPath)}, args.join(${JSON.stringify("\t")}) + ${JSON.stringify("\n")});`,
72 ]),
73 ...(options.delayMs === undefined
74 ? []
75 : [`Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ${options.delayMs});`]),
76 `const expected = ${JSON.stringify(options.expectedArgs ?? [])};`,
77 "if (expected.some((value, index) => args[index] !== value)) {",
78 ' process.stderr.write(`unexpected args: ${args.join(" ")}\\n`);',
79 " process.exit(11);",
80 "}",
81 `await import(pathToFileURL(${JSON.stringify(options.scriptPath)}).href);`,
82 "",
83 ].join("\n");