fauxnix-cli 1.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,197 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { createHash } from 'node:crypto';
3
+ import { copyFileSync, existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
4
+ import { join, resolve } from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { promisify } from 'node:util';
7
+ import { inject as postjectInject } from 'postject';
8
+ import { packageVersion } from './version.js';
9
+ const execFileAsync = promisify(execFile);
10
+ export function launcherDir(env = process.env) {
11
+ const override = env['FAUXNIX_FACADE_DIR'];
12
+ if (override)
13
+ return override;
14
+ const base = env['LOCALAPPDATA'] ?? env['USERPROFILE'] ?? '.';
15
+ return join(base, 'fauxnix', 'bin');
16
+ }
17
+ function markerPath(dir) {
18
+ return join(dir, 'bash.exe.launcher.json');
19
+ }
20
+ function launcherInputs(env) {
21
+ // src/ and dist/ are both one level below the package root.
22
+ const bundle = new URL('../dist/facade-sea.cjs', import.meta.url);
23
+ if (!existsSync(bundle)) {
24
+ throw new Error('fauxnix: dist/facade-sea.cjs is missing — run npm run build first');
25
+ }
26
+ const stubPath = env['FAUXNIX_FACADE_STUB'] ? resolve(env['FAUXNIX_FACADE_STUB']) : null;
27
+ if (stubPath && !existsSync(stubPath)) {
28
+ throw new Error(`fauxnix: FAUXNIX_FACADE_STUB does not exist: ${stubPath}`);
29
+ }
30
+ return {
31
+ formatVersion: 1,
32
+ packageVersion,
33
+ nodeVersion: process.version,
34
+ mode: stubPath ? 'stub' : 'sea',
35
+ nodePath: process.execPath,
36
+ bundlePath: fileURLToPath(bundle),
37
+ bundleHash: createHash('sha256').update(readFileSync(bundle)).digest('hex'),
38
+ stubPath,
39
+ stubHash: stubPath ? createHash('sha256').update(readFileSync(stubPath)).digest('hex') : null,
40
+ };
41
+ }
42
+ function readMarker(dir) {
43
+ try {
44
+ return JSON.parse(readFileSync(markerPath(dir), 'utf8'));
45
+ }
46
+ catch {
47
+ return null;
48
+ }
49
+ }
50
+ function launcherMatches(dir, inputs) {
51
+ const marker = readMarker(dir);
52
+ // Version-only markers predate input tracking and must be rebuilt once.
53
+ if (!Object.entries(inputs).every(([key, value]) => marker?.[key] === value))
54
+ return false;
55
+ if (inputs.mode === 'sea')
56
+ return true;
57
+ try {
58
+ const routing = JSON.parse(readFileSync(join(dir, 'fauxnix-launch.json'), 'utf8'));
59
+ return routing?.node === inputs.nodePath && routing?.bundle === inputs.bundlePath;
60
+ }
61
+ catch {
62
+ return false;
63
+ }
64
+ }
65
+ export function launcherStatus(env = process.env) {
66
+ const dir = launcherDir(env);
67
+ const exePath = join(dir, 'bash.exe');
68
+ const installed = existsSync(exePath);
69
+ let upToDate = false;
70
+ try {
71
+ upToDate = installed && launcherMatches(dir, launcherInputs(env));
72
+ }
73
+ catch {
74
+ // Doctor reports stale for missing/unreadable inputs; buildLauncher gives
75
+ // the actionable error rather than silently reusing or changing modes.
76
+ }
77
+ return {
78
+ dir,
79
+ exePath,
80
+ installed,
81
+ upToDate,
82
+ packageVersion,
83
+ nodeVersion: process.version,
84
+ };
85
+ }
86
+ /** Build (or rebuild when stale) the launcher; returns the bash.exe path. */
87
+ export async function buildLauncher(env = process.env) {
88
+ const dir = launcherDir(env);
89
+ const exePath = join(dir, 'bash.exe');
90
+ const inputs = launcherInputs(env);
91
+ if (existsSync(exePath) && launcherMatches(dir, inputs))
92
+ return exePath;
93
+ const { mkdirSync } = await import('node:fs');
94
+ mkdirSync(dir, { recursive: true });
95
+ // A failed rebuild can replace bash.exe. Never leave the previous marker
96
+ // claiming that partially replaced launcher is still current.
97
+ rmSync(markerPath(dir), { force: true });
98
+ // GA path (RFC #224 Option B): a small prebuilt stub that forwards argv to
99
+ // this installation's node + bundle. Its build/signing pipeline is tracked
100
+ // separately; when FAUXNIX_FACADE_STUB points at a stub binary, install it
101
+ // with a routing config instead of the ~85 MB SEA copy.
102
+ if (inputs.stubPath) {
103
+ rmSync(exePath, { force: true });
104
+ copyFileSync(inputs.stubPath, exePath);
105
+ writeFileSync(join(dir, 'fauxnix-launch.json'), JSON.stringify({ node: inputs.nodePath, bundle: inputs.bundlePath }, null, 2));
106
+ writeFileSync(markerPath(dir), JSON.stringify(inputs));
107
+ return exePath;
108
+ }
109
+ const configPath = join(dir, 'sea-config.json');
110
+ const blobPath = join(dir, 'sea-prep.blob');
111
+ writeFileSync(configPath, JSON.stringify({
112
+ main: inputs.bundlePath,
113
+ output: blobPath,
114
+ disableExperimentalSEAWarning: true,
115
+ }));
116
+ try {
117
+ await execFileAsync(inputs.nodePath, ['--experimental-sea-config', configPath]);
118
+ rmSync(exePath, { force: true });
119
+ copyFileSync(inputs.nodePath, exePath);
120
+ // postject rewrites the PE resource section; any original Authenticode
121
+ // signature is invalidated, which is expected for a local build.
122
+ await postjectInject(exePath, 'NODE_SEA_BLOB', readFileSync(blobPath), {
123
+ sentinelFuse: 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
124
+ });
125
+ }
126
+ finally {
127
+ rmSync(configPath, { force: true });
128
+ rmSync(blobPath, { force: true });
129
+ }
130
+ writeFileSync(markerPath(dir), JSON.stringify(inputs));
131
+ return exePath;
132
+ }
133
+ /** User-level CLAUDE_CODE_GIT_BASH_PATH management, with prior-value restore. */
134
+ export async function setUserEnv(name, value) {
135
+ const ps = value === null
136
+ ? `[Environment]::SetEnvironmentVariable('${name}', $null, 'User')`
137
+ : `[Environment]::SetEnvironmentVariable('${name}', ${JSON.stringify(value)}, 'User')`;
138
+ await execFileAsync('powershell.exe', ['-NoProfile', '-Command', ps]);
139
+ }
140
+ export async function getUserEnv(name) {
141
+ try {
142
+ const { stdout } = await execFileAsync('powershell.exe', [
143
+ '-NoProfile',
144
+ '-Command',
145
+ `[Environment]::GetEnvironmentVariable('${name}', 'User')`,
146
+ ]);
147
+ const v = stdout.trim();
148
+ return v === '' ? null : v;
149
+ }
150
+ catch {
151
+ return null;
152
+ }
153
+ }
154
+ const SHELL_ENV = 'CLAUDE_CODE_GIT_BASH_PATH';
155
+ const PREVIOUS_FILE = 'claude-shell.previous.txt';
156
+ function previousPath(dir) {
157
+ return join(dir, PREVIOUS_FILE);
158
+ }
159
+ /** `fauxnix install --claude-shell` — build the launcher and point Claude Code at it. */
160
+ export async function installClaudeShell(env = process.env) {
161
+ const lines = [];
162
+ const exePath = await buildLauncher(env);
163
+ lines.push(`launcher ready: ${exePath}`);
164
+ const prev = await getUserEnv(SHELL_ENV);
165
+ if (prev && prev !== exePath) {
166
+ const dir = launcherDir(env);
167
+ if (!existsSync(previousPath(dir))) {
168
+ writeFileSync(previousPath(dir), prev, 'utf8');
169
+ lines.push(`saved previous ${SHELL_ENV}: ${prev}`);
170
+ }
171
+ }
172
+ await setUserEnv(SHELL_ENV, exePath);
173
+ lines.push(`set user ${SHELL_ENV}=${exePath}`);
174
+ lines.push('restart Claude Code from a new terminal to pick it up');
175
+ lines.push('note: the harness approval flow for its built-in Bash tool now governs fauxnix runs');
176
+ lines.push('undo anytime with: fauxnix install --claude-shell-off');
177
+ return lines;
178
+ }
179
+ /** `fauxnix install --claude-shell-off` — restore the previous value and remove the launcher. */
180
+ export async function uninstallClaudeShell(env = process.env) {
181
+ const lines = [];
182
+ const dir = launcherDir(env);
183
+ let prev = null;
184
+ try {
185
+ prev = readFileSync(previousPath(dir), 'utf8').trim() || null;
186
+ }
187
+ catch {
188
+ /* no saved previous value */
189
+ }
190
+ await setUserEnv(SHELL_ENV, prev);
191
+ lines.push(prev ? `restored ${SHELL_ENV}=${prev}` : `removed user ${SHELL_ENV}`);
192
+ rmSync(join(dir, 'bash.exe'), { force: true });
193
+ rmSync(markerPath(dir), { force: true });
194
+ rmSync(previousPath(dir), { force: true });
195
+ lines.push(`removed launcher: ${join(dir, 'bash.exe')}`);
196
+ return lines;
197
+ }
@@ -0,0 +1 @@
1
+ import './commands/install-all.js';
@@ -0,0 +1,11 @@
1
+ // Self-contained entry for the experimental bash.exe launcher (RFC #224).
2
+ // The launcher build bundles this file with esbuild into a single CJS blob,
3
+ // injected into a copy of node.exe named bash.exe via Node SEA + postject.
4
+ // Claude Code then spawns `bash.exe -c "<cmd>"` / keeps a persistent stdin
5
+ // session — argv contract is handled by runFacade.
6
+ import './commands/install-all.js';
7
+ import { runFacade } from './facade.js';
8
+ void (async () => {
9
+ const code = await runFacade(process.argv.slice(2));
10
+ process.exit(code);
11
+ })();