dorfl 0.8.1 → 0.10.1

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.
Files changed (58) hide show
  1. package/dist/cli.d.ts.map +1 -1
  2. package/dist/cli.js +70 -26
  3. package/dist/cli.js.map +1 -1
  4. package/dist/config.d.ts +82 -7
  5. package/dist/config.d.ts.map +1 -1
  6. package/dist/config.js +14 -2
  7. package/dist/config.js.map +1 -1
  8. package/dist/do.d.ts +16 -1
  9. package/dist/do.d.ts.map +1 -1
  10. package/dist/do.js +14 -9
  11. package/dist/do.js.map +1 -1
  12. package/dist/env-config.d.ts.map +1 -1
  13. package/dist/env-config.js +25 -3
  14. package/dist/env-config.js.map +1 -1
  15. package/dist/gc.d.ts +17 -0
  16. package/dist/gc.d.ts.map +1 -1
  17. package/dist/gc.js +75 -2
  18. package/dist/gc.js.map +1 -1
  19. package/dist/git.d.ts +20 -0
  20. package/dist/git.d.ts.map +1 -1
  21. package/dist/git.js +155 -8
  22. package/dist/git.js.map +1 -1
  23. package/dist/intake-trigger-template.d.ts +91 -53
  24. package/dist/intake-trigger-template.d.ts.map +1 -1
  25. package/dist/intake-trigger-template.js +213 -114
  26. package/dist/intake-trigger-template.js.map +1 -1
  27. package/dist/intake.d.ts +53 -4
  28. package/dist/intake.d.ts.map +1 -1
  29. package/dist/intake.js +59 -15
  30. package/dist/intake.js.map +1 -1
  31. package/dist/placement.d.ts +42 -33
  32. package/dist/placement.d.ts.map +1 -1
  33. package/dist/placement.js +20 -17
  34. package/dist/placement.js.map +1 -1
  35. package/dist/repo-config.d.ts +1 -1
  36. package/dist/repo-config.d.ts.map +1 -1
  37. package/dist/repo-config.js +35 -0
  38. package/dist/repo-config.js.map +1 -1
  39. package/dist/tasking.d.ts +43 -0
  40. package/dist/tasking.d.ts.map +1 -1
  41. package/dist/tasking.js +39 -19
  42. package/dist/tasking.js.map +1 -1
  43. package/dist/workspace.d.ts.map +1 -1
  44. package/dist/workspace.js +64 -21
  45. package/dist/workspace.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/cli.ts +83 -29
  48. package/src/config.ts +96 -9
  49. package/src/do.ts +30 -10
  50. package/src/env-config.ts +25 -3
  51. package/src/gc.ts +95 -2
  52. package/src/git.ts +180 -8
  53. package/src/intake-trigger-template.ts +263 -143
  54. package/src/intake.ts +136 -18
  55. package/src/placement.ts +42 -36
  56. package/src/repo-config.ts +35 -0
  57. package/src/tasking.ts +66 -41
  58. package/src/workspace.ts +74 -20
package/src/git.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import {spawnSync, spawn} from 'node:child_process';
2
- import {mkdirSync} from 'node:fs';
3
- import {dirname} from 'node:path';
2
+ import {existsSync, mkdirSync, statSync} from 'node:fs';
3
+ import {delimiter, dirname, isAbsolute, join} from 'node:path';
4
4
 
5
5
  /** Result of running a git (or any) command. */
6
6
  export interface RunResult {
@@ -9,6 +9,176 @@ export interface RunResult {
9
9
  stderr: string;
10
10
  }
11
11
 
12
+ /**
13
+ * The standard system dirs core tools (`git`, `ssh`, `sh`) live in. Some callers
14
+ * launch `dorfl` with a CURATED `PATH` (a version-manager / MCP-agent env that
15
+ * lists only `~/.volta/bin`, `~/.cargo/bin`, `~/.local/bin`, …) that OMITS these.
16
+ * `dorfl` spawns bare `git` (and git in turn may shell out to `ssh`/`sh` for
17
+ * hooks and remote transport), so a `PATH` missing `/usr/bin` produces an opaque
18
+ * mid-run `spawn git ENOENT`. We UNION these onto whatever `PATH` we are given so
19
+ * core tools resolve even under a curated caller `PATH`, without discarding the
20
+ * caller's own entries (a project-pinned `git` earlier on `PATH` still wins).
21
+ */
22
+ const SYSTEM_PATH_DIRS = [
23
+ '/usr/local/bin',
24
+ '/usr/bin',
25
+ '/bin',
26
+ '/usr/sbin',
27
+ '/sbin',
28
+ ];
29
+
30
+ /**
31
+ * `PATH` with {@link SYSTEM_PATH_DIRS} APPENDED (caller entries kept FIRST, so a
32
+ * pinned tool earlier on `PATH` still wins; missing system dirs are added, not
33
+ * substituted). Deduplicated, preserving first-seen order.
34
+ */
35
+ function pathWithSystemDirs(path: string | undefined): string {
36
+ const seen = new Set<string>();
37
+ const out: string[] = [];
38
+ for (const dir of [
39
+ ...(path ? path.split(delimiter) : []),
40
+ ...SYSTEM_PATH_DIRS,
41
+ ]) {
42
+ if (dir !== '' && !seen.has(dir)) {
43
+ seen.add(dir);
44
+ out.push(dir);
45
+ }
46
+ }
47
+ return out.join(delimiter);
48
+ }
49
+
50
+ /**
51
+ * Return `env` with `PATH` hardened via {@link pathWithSystemDirs}, so the
52
+ * spawned tool (and any tool IT shells out to) can find core system binaries
53
+ * even when the caller's `PATH` omitted `/usr/bin`. Returns a COPY — never
54
+ * mutates the caller's env object. `PATH` is looked up case-insensitively so a
55
+ * Windows `Path`/`PATH` split is not silently missed.
56
+ */
57
+ function envWithSystemPath(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
58
+ const key =
59
+ Object.keys(env).find((k) => k.toUpperCase() === 'PATH') ?? 'PATH';
60
+ return {...env, [key]: pathWithSystemDirs(env[key])};
61
+ }
62
+
63
+ /**
64
+ * Per-effective-PATH cache of the resolved absolute `git` path. Keyed by the
65
+ * effective PATH (+ any `DORFL_GIT`/`GIT` override) so a DIFFERENT env — e.g. a
66
+ * test's PATH-prepended `git` shim, or a project that pins its own git — is
67
+ * resolved AGAINST ITS OWN PATH, never masked by a stale global memo. Bounded in
68
+ * practice (the process uses a handful of distinct envs) and reset in tests.
69
+ */
70
+ const gitBinaryCache = new Map<string, string>();
71
+
72
+ /** Clear the {@link resolveGitBinary} cache (tests only). */
73
+ export function resetResolvedGitBinaryForTest(): void {
74
+ gitBinaryCache.clear();
75
+ }
76
+
77
+ /**
78
+ * Resolve the `git` executable to an ABSOLUTE path for a given `env`, robustly —
79
+ * so a caller `PATH` that omits `/usr/bin` cannot produce a mid-run `spawn git
80
+ * ENOENT`, WITHOUT masking a git the caller deliberately put earlier on `PATH`
81
+ * (a project pin, or a test shim). Order:
82
+ *
83
+ * 1. an explicit `DORFL_GIT` / `GIT` env override (an absolute path to a git
84
+ * binary), for full operator control;
85
+ * 2. a probe of the env's OWN `PATH` FIRST (so a shim / pinned git earlier on
86
+ * `PATH` wins), then the standard system dirs APPENDED — so `/usr/bin/git`
87
+ * is still found when the caller dropped `/usr/bin`, but never AHEAD of the
88
+ * caller's own entries.
89
+ *
90
+ * Falls back to the bare name `'git'` when nothing resolves (git genuinely
91
+ * absent) so the spawn still runs and produces the diagnostic path. Cached PER
92
+ * effective PATH so distinct envs resolve independently.
93
+ */
94
+ export function resolveGitBinary(env: NodeJS.ProcessEnv = process.env): string {
95
+ const pathValue = pathWithSystemDirs(env.PATH);
96
+ const cacheKey = `${env.DORFL_GIT ?? ''}\u0000${env.GIT ?? ''}\u0000${pathValue}`;
97
+ const cached = gitBinaryCache.get(cacheKey);
98
+ if (cached !== undefined) {
99
+ return cached;
100
+ }
101
+ const resolved = resolveGitBinaryUncached(env, pathValue);
102
+ gitBinaryCache.set(cacheKey, resolved);
103
+ return resolved;
104
+ }
105
+
106
+ function resolveGitBinaryUncached(
107
+ env: NodeJS.ProcessEnv,
108
+ pathValue: string,
109
+ ): string {
110
+ for (const override of [env.DORFL_GIT, env.GIT]) {
111
+ if (override && isAbsolute(override) && isExecutableFile(override)) {
112
+ return override;
113
+ }
114
+ }
115
+ const exe = process.platform === 'win32' ? 'git.exe' : 'git';
116
+ for (const dir of pathValue.split(delimiter)) {
117
+ const candidate = join(dir, exe);
118
+ if (isExecutableFile(candidate)) {
119
+ return candidate;
120
+ }
121
+ }
122
+ return 'git'; // unresolved — let the spawn surface the ENOENT diagnostic
123
+ }
124
+
125
+ /** True iff `path` is a regular file (a symlink target is followed by statSync). */
126
+ function isExecutableFile(path: string): boolean {
127
+ try {
128
+ return existsSync(path) && statSync(path).isFile();
129
+ } catch {
130
+ return false;
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Resolve a command + harden its spawn env: bare `'git'` is replaced by the
136
+ * absolute {@link resolveGitBinary} path, and the spawn env's `PATH` is unioned
137
+ * with the standard system dirs so git's own child processes (hooks, `ssh`) also
138
+ * resolve. Any OTHER command is passed through unchanged (only its `PATH` is
139
+ * hardened). This is the single choke-point both {@link run} and
140
+ * {@link runAsync} funnel through, so every git spawn in the codebase inherits
141
+ * the robust resolution for free.
142
+ */
143
+ function resolveSpawn(
144
+ command: string,
145
+ env: NodeJS.ProcessEnv,
146
+ ): {command: string; env: NodeJS.ProcessEnv} {
147
+ const hardenedEnv = envWithSystemPath(env);
148
+ const resolved = command === 'git' ? resolveGitBinary(hardenedEnv) : command;
149
+ return {command: resolved, env: hardenedEnv};
150
+ }
151
+
152
+ /**
153
+ * Build the spawn-failure message. A plain `spawn git ENOENT` is OPAQUE (it does
154
+ * not say WHY git was not found); for the `ENOENT` case we surface the effective
155
+ * `PATH` and point at `DORFL_GIT`, so a curated caller `PATH` that omits the
156
+ * system dirs is diagnosable at a glance instead of mid-run. Other spawn errors
157
+ * pass through with the original message.
158
+ */
159
+ function spawnErrorMessage(
160
+ command: string,
161
+ resolved: string,
162
+ env: NodeJS.ProcessEnv,
163
+ err: Error & {code?: string},
164
+ ): string {
165
+ if (err.code === 'ENOENT') {
166
+ const path =
167
+ env[Object.keys(env).find((k) => k.toUpperCase() === 'PATH') ?? 'PATH'] ??
168
+ '';
169
+ const hint =
170
+ command === 'git'
171
+ ? `Is git installed and on PATH? Set DORFL_GIT to an absolute git path, or add its dir to PATH. `
172
+ : `Is it installed and on PATH? `;
173
+ return (
174
+ `failed to spawn '${command}': not found (tried '${resolved}'). ` +
175
+ hint +
176
+ `Effective PATH=${path}`
177
+ );
178
+ }
179
+ return `failed to spawn '${command}': ${err.message}`;
180
+ }
181
+
12
182
  /**
13
183
  * Run a command synchronously in `cwd`, capturing output. A non-zero exit is NOT
14
184
  * thrown here — callers inspect `status` (claim.sh's exit codes are meaningful).
@@ -19,15 +189,16 @@ export function run(
19
189
  cwd: string,
20
190
  options: {input?: string; env?: NodeJS.ProcessEnv} = {},
21
191
  ): RunResult {
22
- const result = spawnSync(command, args, {
192
+ const {command: exe, env} = resolveSpawn(command, options.env ?? process.env);
193
+ const result = spawnSync(exe, args, {
23
194
  cwd,
24
195
  encoding: 'utf8',
25
196
  input: options.input,
26
- env: options.env ?? process.env,
197
+ env,
27
198
  maxBuffer: 64 * 1024 * 1024,
28
199
  });
29
200
  if (result.error) {
30
- throw new Error(`failed to spawn '${command}': ${result.error.message}`);
201
+ throw new Error(spawnErrorMessage(command, exe, env, result.error));
31
202
  }
32
203
  return {
33
204
  status: result.status ?? -1,
@@ -47,10 +218,11 @@ export function runAsync(
47
218
  cwd: string,
48
219
  options: {input?: string; env?: NodeJS.ProcessEnv} = {},
49
220
  ): Promise<RunResult> {
221
+ const {command: exe, env} = resolveSpawn(command, options.env ?? process.env);
50
222
  return new Promise((resolvePromise, reject) => {
51
- const child = spawn(command, args, {
223
+ const child = spawn(exe, args, {
52
224
  cwd,
53
- env: options.env ?? process.env,
225
+ env,
54
226
  });
55
227
  let stdout = '';
56
228
  let stderr = '';
@@ -61,7 +233,7 @@ export function runAsync(
61
233
  stderr += chunk.toString('utf8');
62
234
  });
63
235
  child.on('error', (err) =>
64
- reject(new Error(`failed to spawn '${command}': ${err.message}`)),
236
+ reject(new Error(spawnErrorMessage(command, exe, env, err))),
65
237
  );
66
238
  child.on('close', (code) => {
67
239
  resolvePromise({status: code ?? -1, stdout, stderr});