@zuke-build/cli 0.0.0-stage → 2.6.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.
- package/LICENSE +21 -0
- package/README.md +343 -2
- package/mod.d.ts +99 -0
- package/mod.js +893 -0
- package/package.json +35 -4
- package/src/binary_launcher.d.ts +49 -0
- package/src/binary_launcher.js +327 -0
- package/src/binary_pin.d.ts +47 -0
- package/src/binary_pin.js +117 -0
- package/src/build_file.d.ts +25 -0
- package/src/build_file.js +65 -0
- package/src/cache.d.ts +23 -0
- package/src/cache.js +26 -0
- package/src/deno_isolated.d.ts +14 -0
- package/src/deno_isolated.js +70 -0
- package/src/deno_path.d.ts +115 -0
- package/src/deno_path.js +205 -0
- package/src/deno_pin.d.ts +37 -0
- package/src/deno_pin.js +32 -0
- package/src/dependencies.d.ts +48 -0
- package/src/dependencies.js +80 -0
- package/src/dispatch.d.ts +220 -0
- package/src/dispatch.js +538 -0
- package/src/embedded_engine.d.ts +28 -0
- package/src/embedded_engine.js +21 -0
- package/src/engine_pin.d.ts +56 -0
- package/src/engine_pin.js +196 -0
- package/src/fs.d.ts +14 -0
- package/src/fs.js +25 -0
- package/src/import.d.ts +132 -0
- package/src/import.js +289 -0
- package/src/install_source.d.ts +79 -0
- package/src/install_source.js +104 -0
- package/src/launcher.d.ts +37 -0
- package/src/launcher.js +464 -0
- package/src/mcp_config.d.ts +63 -0
- package/src/mcp_config.js +86 -0
- package/src/output.d.ts +38 -0
- package/src/output.js +43 -0
- package/src/paint.d.ts +42 -0
- package/src/paint.js +38 -0
- package/src/project.d.ts +69 -0
- package/src/project.js +132 -0
- package/src/pyproject.d.ts +30 -0
- package/src/pyproject.js +156 -0
- package/src/python_build.d.ts +27 -0
- package/src/python_build.js +197 -0
- package/src/records.d.ts +9 -0
- package/src/records.js +13 -0
- package/src/relock.d.ts +11 -0
- package/src/relock.js +182 -0
- package/src/setup.d.ts +251 -0
- package/src/setup.js +601 -0
- package/src/star.d.ts +44 -0
- package/src/star.js +100 -0
- package/src/starter.d.ts +55 -0
- package/src/starter.js +126 -0
- package/src/toml.d.ts +32 -0
- package/src/toml.js +354 -0
- package/src/translate.d.ts +69 -0
- package/src/translate.js +198 -0
- package/src/upgrade.d.ts +113 -0
- package/src/upgrade.js +344 -0
- package/src/version.d.ts +11 -0
- package/src/version.js +13 -0
- package/src/version_report.d.ts +55 -0
- package/src/version_report.js +187 -0
- package/zuke.js +8 -0
package/src/dispatch.js
ADDED
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
// Copyright (c) 2026 the Zuke contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* Forwarding a build command from the global `zuke` to the project's build.
|
|
5
|
+
*
|
|
6
|
+
* The global command (`deno install -A -g -n zuke jsr:@zuke/cli`) owns only
|
|
7
|
+
* `setup`, `import`, `doc` and `upgrade`. Everything else — `zuke ci`, `zuke --list`,
|
|
8
|
+
* `zuke graph`, `zuke mcp` — belongs to the build in the current project, so
|
|
9
|
+
* `main` forwards it: it walks up from the working directory to the nearest
|
|
10
|
+
* `zuke.json` (the repository-root marker `zuke setup` writes), then runs the
|
|
11
|
+
* executable `./zuke` launcher beside it, from the root, stdio inherited.
|
|
12
|
+
* That is the delegation a global `gradle` makes to a checkout's wrapper: one
|
|
13
|
+
* global word, the project's own launcher, build and lock deciding what
|
|
14
|
+
* actually runs — a pinned Deno, an exported environment variable, a lock kept
|
|
15
|
+
* somewhere other than `./deno.lock` all apply to `zuke` exactly as to
|
|
16
|
+
* `./zuke`. A root with no executable launcher (and every root on Windows,
|
|
17
|
+
* whose files report no mode) gets the launcher's own invocation instead:
|
|
18
|
+
* `deno run -A zuke.ts`, with `--frozen` once a `deno.lock` exists.
|
|
19
|
+
*
|
|
20
|
+
* Discovery has a trust gate, because it runs code the caller never named:
|
|
21
|
+
* a `zuke.json` planted in a shared parent (`/tmp`, a shared checkout tree)
|
|
22
|
+
* would otherwise have `zuke ci` in any directory below it run a stranger's
|
|
23
|
+
* `zuke.ts` with `-A`. So, as git's `safe.directory` does, a build is refused
|
|
24
|
+
* when its root directory is owned by another user or is world-writable (so
|
|
25
|
+
* anyone could have planted the files in it). Deno then does a walk of its
|
|
26
|
+
* own — it discovers `deno.json` and `package.json` in the root's ancestors,
|
|
27
|
+
* and an import map planted there rewrites what `zuke.ts` imports — so the
|
|
28
|
+
* gate also refuses any such ancestor config file owned by another user. The
|
|
29
|
+
* error names what was refused and the two ways forward: run that project's
|
|
30
|
+
* own `./zuke` launcher — an explicit act on a file the caller names — or fix
|
|
31
|
+
* the ownership. Where the platform has no file ownership to compare
|
|
32
|
+
* (Windows), the gate is inert. A hosted build's launcher starts `zuke` in
|
|
33
|
+
* the root it sits in, so that root, named in {@link LAUNCHER_ROOT_ENV}, is
|
|
34
|
+
* not judged either: running the launcher was the explicit act.
|
|
35
|
+
*
|
|
36
|
+
* A build another SDK hosts (a `zuke.json` and no `zuke.ts`) needs nothing
|
|
37
|
+
* from Deno's stack. The standalone binary runs it on its built-in engine,
|
|
38
|
+
* or, for a pin that engine cannot serve, through the project's launcher,
|
|
39
|
+
* which downloads the `zuke` release it pins; it never falls back to Deno.
|
|
40
|
+
*
|
|
41
|
+
* Locating and planning are pure given a probe, so the routing is
|
|
42
|
+
* unit-testable; the spawn goes through the injectable {@link BuildRunner}.
|
|
43
|
+
*
|
|
44
|
+
* @module
|
|
45
|
+
*/
|
|
46
|
+
import { absolutePath, CONFIG_FILE, EnvTasks, FileTasks, ProcessTasks, RuntimeTasks, } from "@zuke/core";
|
|
47
|
+
import { noLockNotice } from "./launcher.js";
|
|
48
|
+
import { BINARY_INSTALL_DOCS } from "./binary_pin.js";
|
|
49
|
+
import { BUILD_FILE, buildFileNamed } from "./build_file.js";
|
|
50
|
+
import { QUIET_DENO_ENV, spawnDeno } from "./deno_path.js";
|
|
51
|
+
import { EMBEDDED_ENGINE_ENV } from "./embedded_engine.js";
|
|
52
|
+
/**
|
|
53
|
+
* Deno's own config files, in the order it prefers them: a `deno.json` wins
|
|
54
|
+
* over a `deno.jsonc` beside it.
|
|
55
|
+
*/
|
|
56
|
+
export const DENO_CONFIG_FILES = ["deno.json", "deno.jsonc"];
|
|
57
|
+
/** The lockfile whose presence turns on `--frozen`, as in the launchers. */
|
|
58
|
+
export const LOCK_FILE = "deno.lock";
|
|
59
|
+
/** The POSIX launcher a forwarded command runs, beside {@link CONFIG_FILE}. */
|
|
60
|
+
export const LAUNCHER_FILE = "zuke";
|
|
61
|
+
/**
|
|
62
|
+
* Set to `1` in a launcher's environment, so a launcher that itself calls the
|
|
63
|
+
* global `zuke` gets the bare `deno run` instead of itself again, endlessly.
|
|
64
|
+
*/
|
|
65
|
+
export const FORWARDED_ENV = "ZUKE_FORWARDED";
|
|
66
|
+
/**
|
|
67
|
+
* Set by a hosted build's launcher to its own directory, so the `zuke` it
|
|
68
|
+
* starts does not refuse that root at the trust gate: the caller named the
|
|
69
|
+
* project by running the launcher inside it.
|
|
70
|
+
*/
|
|
71
|
+
export const LAUNCHER_ROOT_ENV = "ZUKE_LAUNCHER_ROOT";
|
|
72
|
+
/**
|
|
73
|
+
* `FileTasks.stat` a path, or `null` when nothing is there. Follows links, since
|
|
74
|
+
* the gate judges the directory the build actually runs in. A `NotFound` maps
|
|
75
|
+
* to `null`; any other error is rethrown rather than masked as absence.
|
|
76
|
+
*/
|
|
77
|
+
async function statOrNull(path) {
|
|
78
|
+
try {
|
|
79
|
+
return await FileTasks.stat(path);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
if (FileTasks.isNotFound(error))
|
|
83
|
+
return null;
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** The real, `Deno`-backed {@link BuildProbe}. */
|
|
88
|
+
export const defaultBuildProbe = {
|
|
89
|
+
exists: (path) => FileTasks.exists(path),
|
|
90
|
+
async ownership(path) {
|
|
91
|
+
const info = await statOrNull(path);
|
|
92
|
+
return info === null ? null : { uid: info.uid, mode: info.mode };
|
|
93
|
+
},
|
|
94
|
+
realPath(path) {
|
|
95
|
+
return FileTasks.realPath(path);
|
|
96
|
+
},
|
|
97
|
+
async readLink(path) {
|
|
98
|
+
const info = await FileTasks.lstat(path);
|
|
99
|
+
return info.isSymlink ? await FileTasks.readLink(path) : null;
|
|
100
|
+
},
|
|
101
|
+
uid() {
|
|
102
|
+
return RuntimeTasks.userId();
|
|
103
|
+
},
|
|
104
|
+
readText(path) {
|
|
105
|
+
return FileTasks.readText(path);
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Thrown when the build discovery found cannot be trusted — see the module
|
|
110
|
+
* docs for why it is refused rather than run.
|
|
111
|
+
*/
|
|
112
|
+
export class UntrustedBuildError extends Error {
|
|
113
|
+
/** The error's name, as reported by `String(error)` and stack traces. */
|
|
114
|
+
name = "UntrustedBuildError";
|
|
115
|
+
/** The directory holding the `zuke.json` that was found. */
|
|
116
|
+
root;
|
|
117
|
+
/** What the gate refused, as a clause: "the directory … ". */
|
|
118
|
+
reason;
|
|
119
|
+
/**
|
|
120
|
+
* Build the refusal for `root`, explaining `reason` and the ways forward.
|
|
121
|
+
*
|
|
122
|
+
* @param root The directory holding the `zuke.json` that was found.
|
|
123
|
+
* @param reason What the gate refused, as a clause: "the directory … ".
|
|
124
|
+
*/
|
|
125
|
+
constructor(root, reason) {
|
|
126
|
+
// Prose only — never a rendered shell line to paste, since `root` is
|
|
127
|
+
// whatever the filesystem says and the launcher there belongs to whoever
|
|
128
|
+
// owns it.
|
|
129
|
+
super(`zuke: refusing to run the build at ${root}: ${reason}, so the files ` +
|
|
130
|
+
`there could have been planted by someone else. If you trust it, run ` +
|
|
131
|
+
`its own launcher from that directory (./zuke), or fix the ownership.`);
|
|
132
|
+
this.root = root;
|
|
133
|
+
this.reason = reason;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The config files Deno discovers in a root's ancestors — an import map in any
|
|
138
|
+
* of them rewrites what `zuke.ts` imports, so they are gated like the root.
|
|
139
|
+
*/
|
|
140
|
+
const ANCESTOR_CONFIG_FILES = [...DENO_CONFIG_FILES, "package.json"];
|
|
141
|
+
/**
|
|
142
|
+
* The files in the root itself that decide what runs: the marker, the default
|
|
143
|
+
* build file, the launcher, the lockfile, and the root's own config. A build
|
|
144
|
+
* file the marker names is judged on top of these, never in place of
|
|
145
|
+
* `zuke.ts`: a launcher written before the marker named one still runs it. Owning the directory
|
|
146
|
+
* is not owning these — a `chown` of the directory alone leaves them as they
|
|
147
|
+
* were. The probe follows links, so a symlinked launcher is judged by the
|
|
148
|
+
* file it points at.
|
|
149
|
+
*/
|
|
150
|
+
const ROOT_FILES = [
|
|
151
|
+
CONFIG_FILE,
|
|
152
|
+
BUILD_FILE,
|
|
153
|
+
LAUNCHER_FILE,
|
|
154
|
+
LOCK_FILE,
|
|
155
|
+
...ANCESTOR_CONFIG_FILES,
|
|
156
|
+
];
|
|
157
|
+
/** The world-writable permission bit. */
|
|
158
|
+
const WORLD_WRITABLE = 0o002;
|
|
159
|
+
/** The file-type bits of a mode, and the value that marks a regular file. */
|
|
160
|
+
const FILE_TYPE = 0o170000;
|
|
161
|
+
const REGULAR_FILE = 0o100000;
|
|
162
|
+
/** Any of the execute permission bits. */
|
|
163
|
+
const EXECUTABLE = 0o111;
|
|
164
|
+
/**
|
|
165
|
+
* The root's launcher, resolved through any link, when it is an executable
|
|
166
|
+
* regular file, else `null`. Where the gate is inert (no user id: Windows)
|
|
167
|
+
* there is no launcher either, so nothing runs that the gate did not judge.
|
|
168
|
+
* The file itself was judged with the root's files; a link's target
|
|
169
|
+
* directory is judged here, since whoever can write there can swap the file.
|
|
170
|
+
*
|
|
171
|
+
* @throws {UntrustedBuildError} when the target's directory fails the gate.
|
|
172
|
+
*/
|
|
173
|
+
async function launcherAt(root, probe) {
|
|
174
|
+
const uid = probe.uid();
|
|
175
|
+
if (uid === null)
|
|
176
|
+
return null;
|
|
177
|
+
const path = root(LAUNCHER_FILE).path;
|
|
178
|
+
const mode = (await probe.ownership(path))?.mode ?? null;
|
|
179
|
+
if (mode === null)
|
|
180
|
+
return null;
|
|
181
|
+
const runnable = (mode & FILE_TYPE) === REGULAR_FILE &&
|
|
182
|
+
(mode & EXECUTABLE) !== 0;
|
|
183
|
+
if (!runnable)
|
|
184
|
+
return null;
|
|
185
|
+
// Only the target's own directory, not its ancestors — the root's
|
|
186
|
+
// ancestors are not judged as directories either.
|
|
187
|
+
const real = await probe.realPath(path);
|
|
188
|
+
const home = absolutePath(real).parent();
|
|
189
|
+
assertEntry(await probe.ownership(home.path), home.path, uid, root);
|
|
190
|
+
// Run it by its path under the root, not the resolved one. A launcher finds
|
|
191
|
+
// the project it builds from the path it was called by (`dirname "$0"`), so
|
|
192
|
+
// a `zuke` symlinked to a launcher shared across repos, called by its
|
|
193
|
+
// resolved path, would build the shared launcher's own directory instead.
|
|
194
|
+
const link = await probe.readLink(path);
|
|
195
|
+
if (link === null)
|
|
196
|
+
return path;
|
|
197
|
+
// Running the link re-resolves it, so it is only the file that was judged
|
|
198
|
+
// if the link is one hop straight to it: re-pointing that hop needs write
|
|
199
|
+
// access to the root, which the gate refuses to anyone but you. A chain
|
|
200
|
+
// through another directory could be re-pointed by whoever writes there,
|
|
201
|
+
// after the check. Rather than judge every hop, a chain gets the launcher's
|
|
202
|
+
// own `deno run` — the right project, and only files the gate judged.
|
|
203
|
+
//
|
|
204
|
+
// A relative link is read against the root's real path, not the root as
|
|
205
|
+
// written: the root's own ancestors are not judged (macOS's `/var` is a link
|
|
206
|
+
// to `/private/var`, so every temp directory is reached through one), while
|
|
207
|
+
// any link inside the target — the hops that matter — still leaves the
|
|
208
|
+
// lexical path short of the real one, and falls back.
|
|
209
|
+
const target = link.startsWith("/")
|
|
210
|
+
? absolutePath(link)
|
|
211
|
+
: absolutePath(await probe.realPath(root.path))(link);
|
|
212
|
+
return target.path === real ? path : null;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Refuse an entry owned by someone other than `uid`, or writable by everyone;
|
|
216
|
+
* an absent entry, or a property the platform does not report, passes.
|
|
217
|
+
* `subject` names the entry in the refusal.
|
|
218
|
+
*/
|
|
219
|
+
function assertEntry(own, subject, uid, root) {
|
|
220
|
+
if (own === null)
|
|
221
|
+
return;
|
|
222
|
+
if (own.uid !== null && own.uid !== uid) {
|
|
223
|
+
throw new UntrustedBuildError(root.path, `${subject} is owned by user ${own.uid}, not you (${uid})`);
|
|
224
|
+
}
|
|
225
|
+
if (own.mode !== null && (own.mode & WORLD_WRITABLE) !== 0) {
|
|
226
|
+
throw new UntrustedBuildError(root.path, `${subject} is writable by everyone`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Walk up from `cwd` to the nearest directory holding {@link CONFIG_FILE},
|
|
231
|
+
* asking `probe`. Returns `null` when no ancestor has one — the caller is not
|
|
232
|
+
* inside a Zuke project. A root equal to `vouched`, the directory of the
|
|
233
|
+
* launcher that started this process ({@link LAUNCHER_ROOT_ENV}), is not
|
|
234
|
+
* held to the trust gate.
|
|
235
|
+
*
|
|
236
|
+
* @throws {UntrustedBuildError} when the root found fails the trust gate (see
|
|
237
|
+
* the module docs).
|
|
238
|
+
*/
|
|
239
|
+
export async function locateBuild(cwd, probe, vouched = EnvTasks.get(LAUNCHER_ROOT_ENV)) {
|
|
240
|
+
let dir = absolutePath(cwd);
|
|
241
|
+
while (true) {
|
|
242
|
+
if (await probe.exists(dir(CONFIG_FILE).path)) {
|
|
243
|
+
// The root a launcher started this from was named by running it, and
|
|
244
|
+
// that launcher is the one running, so it is neither judged nor rerun.
|
|
245
|
+
const named = vouched !== undefined && dir.path === vouched;
|
|
246
|
+
if (!named)
|
|
247
|
+
await assertTrusted(dir, probe);
|
|
248
|
+
// Read only once the marker passed the gate, and then hold the file it
|
|
249
|
+
// names to the same gate: it is the one that runs.
|
|
250
|
+
const build = buildFileNamed(await probe.readText(dir(CONFIG_FILE).path));
|
|
251
|
+
const uid = probe.uid();
|
|
252
|
+
if (!named && uid !== null && build !== BUILD_FILE) {
|
|
253
|
+
await assertFilesOwned(dir, [build], uid, probe, dir);
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
root: dir.path,
|
|
257
|
+
build,
|
|
258
|
+
frozen: await probe.exists(dir(LOCK_FILE).path),
|
|
259
|
+
launcher: named ? null : await launcherAt(dir, probe),
|
|
260
|
+
engine: await probe.exists(dir(build).path)
|
|
261
|
+
? null
|
|
262
|
+
: await hostedEngine(dir, build, probe),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
if (dir.isRoot)
|
|
266
|
+
return null;
|
|
267
|
+
dir = dir.parent();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The trust gate. Inert when the platform reports no user id; otherwise the
|
|
272
|
+
* root must be owned by the current user and not world-writable, the files in
|
|
273
|
+
* it that decide what runs must be the user's too, and no ancestor may hold a
|
|
274
|
+
* Deno- or npm-discovered config file owned by someone else. An entry the
|
|
275
|
+
* platform reports without an owner or mode is not judged on that property.
|
|
276
|
+
*/
|
|
277
|
+
async function assertTrusted(root, probe) {
|
|
278
|
+
const uid = probe.uid();
|
|
279
|
+
if (uid === null)
|
|
280
|
+
return;
|
|
281
|
+
assertEntry(await probe.ownership(root.path), "the directory", uid, root);
|
|
282
|
+
await assertFilesOwned(root, ROOT_FILES, uid, probe, root);
|
|
283
|
+
for (let dir = root; !dir.isRoot;) {
|
|
284
|
+
dir = dir.parent();
|
|
285
|
+
await assertFilesOwned(dir, ANCESTOR_CONFIG_FILES, uid, probe, root);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Refuse when any of `names` in `dir` exists and is owned by someone other
|
|
290
|
+
* than `uid` or writable by everyone — see {@link assertEntry}.
|
|
291
|
+
*/
|
|
292
|
+
async function assertFilesOwned(dir, names, uid, probe, root) {
|
|
293
|
+
for (const name of names) {
|
|
294
|
+
const file = dir(name).path;
|
|
295
|
+
assertEntry(await probe.ownership(file), file, uid, root);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/** The field of `zuke.json` naming the `@zuke/engine` release that runs a build with no `zuke.ts`. */
|
|
299
|
+
export const ENGINE_FIELD = "engine";
|
|
300
|
+
/** One exact release, since a range would float the engine between runs. */
|
|
301
|
+
const ENGINE_VERSION = /^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$/;
|
|
302
|
+
/**
|
|
303
|
+
* The `engine` a root with no build file pins: such a root holds a build
|
|
304
|
+
* another SDK hosts, and nothing could run it without one — in particular
|
|
305
|
+
* not Deno, which a hosted build never needs.
|
|
306
|
+
* @throws when `zuke.json` names none, or names one that is not exact.
|
|
307
|
+
*/
|
|
308
|
+
async function hostedEngine(root, build, probe) {
|
|
309
|
+
const engine = await engineVersion(root, probe);
|
|
310
|
+
if (engine === null) {
|
|
311
|
+
throw new Error(`zuke: ${root.path} has no ${build}, and its ${CONFIG_FILE} ` +
|
|
312
|
+
`names no '${ENGINE_FIELD}' release to run its 'sdk' build with`);
|
|
313
|
+
}
|
|
314
|
+
return engine;
|
|
315
|
+
}
|
|
316
|
+
/** The refusal of an `engine` value that is not an exact version. */
|
|
317
|
+
function badEngineNotice(value) {
|
|
318
|
+
return `zuke: zuke.json names '${ENGINE_FIELD}': '${value}', which is not ` +
|
|
319
|
+
`an exact version like 1.2.3`;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* The `engine` field of the root's `zuke.json`: the `@zuke/engine` release
|
|
323
|
+
* that runs a build with no `zuke.ts`, or `null` when the file names none.
|
|
324
|
+
*
|
|
325
|
+
* @throws when the file is not JSON or the version is not exact, saying which.
|
|
326
|
+
*/
|
|
327
|
+
async function engineVersion(root, probe) {
|
|
328
|
+
const path = root(CONFIG_FILE).path;
|
|
329
|
+
let config;
|
|
330
|
+
try {
|
|
331
|
+
config = JSON.parse(await probe.readText(path));
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
throw new Error(`zuke: ${path} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
335
|
+
}
|
|
336
|
+
const engine = typeof config === "object" && config !== null &&
|
|
337
|
+
ENGINE_FIELD in config
|
|
338
|
+
? config[ENGINE_FIELD]
|
|
339
|
+
: undefined;
|
|
340
|
+
if (typeof engine !== "string" || engine === "")
|
|
341
|
+
return null;
|
|
342
|
+
if (!ENGINE_VERSION.test(engine))
|
|
343
|
+
throw new Error(badEngineNotice(engine));
|
|
344
|
+
return engine;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* The build `location` runs, as the CLI names it to a person: the root's
|
|
348
|
+
* `zuke.ts`, or for a build another SDK hosts its `zuke.json` and the engine
|
|
349
|
+
* that runs it: the binary's own `engine` when it serves the pin, else the
|
|
350
|
+
* release the file pins.
|
|
351
|
+
*/
|
|
352
|
+
export function buildLabel(location, engine) {
|
|
353
|
+
if (location.engine === null)
|
|
354
|
+
return `${location.root}/${location.build}`;
|
|
355
|
+
const serving = servingEngine(location, engine);
|
|
356
|
+
return `${location.root}/${CONFIG_FILE}, on ${serving === undefined
|
|
357
|
+
? `@zuke/engine ${location.engine}`
|
|
358
|
+
: `the built-in @zuke/engine ${serving.version}`}`;
|
|
359
|
+
}
|
|
360
|
+
/** The module that runs a build another SDK hosts: `@zuke/engine` at `version`. */
|
|
361
|
+
export function engineSpecifier(version) {
|
|
362
|
+
return `jsr:@zuke/engine@${version}`;
|
|
363
|
+
}
|
|
364
|
+
/** What a forwarded command runs: the build file, or the engine release for a root without one. */
|
|
365
|
+
function buildModule(location) {
|
|
366
|
+
return location.engine === null
|
|
367
|
+
? location.build
|
|
368
|
+
: engineSpecifier(location.engine);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* The `deno` argv for a forwarded command — the launchers' invocation, with
|
|
372
|
+
* `--frozen` only when the location has a lockfile to verify against. Pure.
|
|
373
|
+
*/
|
|
374
|
+
export function buildRunArgs(location, args) {
|
|
375
|
+
// Deno finds a lock only beside a Deno config, which a hosted build has no
|
|
376
|
+
// reason to keep, so the engine route names it; the build file's lock sits
|
|
377
|
+
// beside its config as always.
|
|
378
|
+
const lock = location.engine === null ? [] : [`--lock=${LOCK_FILE}`];
|
|
379
|
+
return [
|
|
380
|
+
"run",
|
|
381
|
+
"-A",
|
|
382
|
+
...(location.frozen ? [...lock, "--frozen"] : []),
|
|
383
|
+
buildModule(location),
|
|
384
|
+
...args,
|
|
385
|
+
];
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* The binary's `engine`, when it is the one to run `location`: a hosted build
|
|
389
|
+
* whose pinned release it serves. `undefined` for a `zuke.ts` build, for a
|
|
390
|
+
* run without an embedded engine, and for a pin it cannot serve.
|
|
391
|
+
*/
|
|
392
|
+
export function servingEngine(location, engine) {
|
|
393
|
+
return location.engine !== null && engine?.serves(location.engine)
|
|
394
|
+
? engine
|
|
395
|
+
: undefined;
|
|
396
|
+
}
|
|
397
|
+
/** Whether a forwarded command goes to the root's launcher rather than running here. */
|
|
398
|
+
function viaLauncher(location) {
|
|
399
|
+
return location.launcher !== null && EnvTasks.get(FORWARDED_ENV) !== "1";
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Run a forwarded command through `runner`. A hosted build whose pin the
|
|
403
|
+
* binary's own `engine` serves runs on it: the binary again, in engine mode,
|
|
404
|
+
* with `args` as they are, launcher or not. Otherwise the root's launcher with
|
|
405
|
+
* `args` when it has one and this is not already a launcher's own `zuke` call
|
|
406
|
+
* (see {@link FORWARDED_ENV}), else Deno with {@link buildRunArgs} — except a
|
|
407
|
+
* hosted build in the binary or on Node, which never falls back to Deno: a
|
|
408
|
+
* project another SDK hosts needs nothing from Deno's stack. `runtime` is the
|
|
409
|
+
* one this CLI runs on.
|
|
410
|
+
* @throws {Error} for a hosted build the binary cannot serve, or a CLI on
|
|
411
|
+
* Node cannot run, when no launcher can run it either, naming how to get a
|
|
412
|
+
* zuke that does.
|
|
413
|
+
*/
|
|
414
|
+
export function runBuild(runner, location, args, env, engine, runtime = RuntimeTasks.facts().name) {
|
|
415
|
+
const serving = servingEngine(location, engine);
|
|
416
|
+
if (serving !== undefined) {
|
|
417
|
+
return runner(location.root, args, serving.program, {
|
|
418
|
+
...env,
|
|
419
|
+
[EMBEDDED_ENGINE_ENV]: "1",
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
if (location.launcher !== null && viaLauncher(location)) {
|
|
423
|
+
return runner(location.root, args, location.launcher, env);
|
|
424
|
+
}
|
|
425
|
+
if (location.engine !== null && runtime !== "deno") {
|
|
426
|
+
// Installed from npm and run by Node: the Deno route would make a project
|
|
427
|
+
// another SDK hosts need Deno, which it never does.
|
|
428
|
+
throw new Error(`zuke: this zuke runs on Node and has no @zuke/engine to run ` +
|
|
429
|
+
`${CONFIG_FILE}'s ${location.engine}. Run the project's download ` +
|
|
430
|
+
"launcher (./zuke, or .\\zuke.ps1 on Windows), which fetches the " +
|
|
431
|
+
`release it pins, or install the zuke binary (${BINARY_INSTALL_DOCS}).`);
|
|
432
|
+
}
|
|
433
|
+
if (engine !== undefined && location.engine !== null) {
|
|
434
|
+
throw new Error(`zuke: this zuke's built-in @zuke/engine ${engine.version} cannot run ` +
|
|
435
|
+
`${CONFIG_FILE}'s ${location.engine}. Install a zuke release that ` +
|
|
436
|
+
`carries it (${BINARY_INSTALL_DOCS}), run the project's download ` +
|
|
437
|
+
"launcher (./zuke, or .\\zuke.ps1 on Windows), which fetches the " +
|
|
438
|
+
"release it pins, or move the project with `zuke upgrade --engine`.");
|
|
439
|
+
}
|
|
440
|
+
return runner(location.root, buildRunArgs(location, args), undefined, env);
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* The stderr line for a hosted build the binary's own `engine` cannot serve,
|
|
444
|
+
* which then goes to the project's launcher; `undefined` when there is
|
|
445
|
+
* nothing to say: no engine compiled in, a `zuke.ts` build, a pin it serves,
|
|
446
|
+
* or no launcher to go to, which {@link runBuild} refuses instead.
|
|
447
|
+
*/
|
|
448
|
+
export function embeddedEngineNotice(location, engine) {
|
|
449
|
+
if (engine === undefined || location.engine === null ||
|
|
450
|
+
servingEngine(location, engine) !== undefined || !viaLauncher(location)) {
|
|
451
|
+
return undefined;
|
|
452
|
+
}
|
|
453
|
+
return `zuke: this zuke's built-in @zuke/engine ${engine.version} cannot ` +
|
|
454
|
+
`run ${CONFIG_FILE}'s ${location.engine}, so the project's launcher ` +
|
|
455
|
+
"runs it";
|
|
456
|
+
}
|
|
457
|
+
/** The launchers' notice for a run without a lockfile, verbatim. */
|
|
458
|
+
export const NO_LOCK_NOTICE = noLockNotice("—");
|
|
459
|
+
/**
|
|
460
|
+
* The stderr line naming the build a forwarded command runs, printed when
|
|
461
|
+
* that build is not in the working directory — so a run that discovery chose
|
|
462
|
+
* is never silent about what it chose.
|
|
463
|
+
*/
|
|
464
|
+
export function runningNotice(location, engine) {
|
|
465
|
+
if (location.engine === null) {
|
|
466
|
+
return `zuke: running ${location.root}/${location.build}`;
|
|
467
|
+
}
|
|
468
|
+
const serving = servingEngine(location, engine);
|
|
469
|
+
return `zuke: running ${location.root}/${CONFIG_FILE} with ${serving === undefined
|
|
470
|
+
? buildModule(location)
|
|
471
|
+
: `the built-in @zuke/engine ${serving.version}`}`;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* The signals the forwarding parent handles while the build runs, and how.
|
|
475
|
+
*
|
|
476
|
+
* The launcher `exec`s Deno, so there the shell waits on the build itself. A
|
|
477
|
+
* forwarding parent cannot exec, so it must not die first: a Ctrl-C reaches
|
|
478
|
+
* the whole foreground group, and the build's own handler cancels it
|
|
479
|
+
* gracefully (a second Ctrl-C force-exits), so the parent only **ignores**
|
|
480
|
+
* SIGINT — forwarding it would deliver that second signal — and stays to
|
|
481
|
+
* report the build's exit code. SIGTERM is addressed to one process, so it is
|
|
482
|
+
* **forwarded** to the build. Windows has no SIGTERM to listen for; its
|
|
483
|
+
* Ctrl-Break is ignored like Ctrl-C.
|
|
484
|
+
*/
|
|
485
|
+
function signalPlan(child) {
|
|
486
|
+
const ignore = () => { };
|
|
487
|
+
const forward = (signal) => () => {
|
|
488
|
+
try {
|
|
489
|
+
child.kill(signal);
|
|
490
|
+
}
|
|
491
|
+
catch {
|
|
492
|
+
// The build already exited; its status is what the caller awaits.
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
return RuntimeTasks.os() === "windows"
|
|
496
|
+
? [["SIGINT", ignore], ["SIGBREAK", ignore]]
|
|
497
|
+
: [["SIGINT", ignore], ["SIGTERM", forward("SIGTERM")]];
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* The default {@link BuildRunner}: spawn the launcher as-is, since choosing a
|
|
501
|
+
* Deno is its job, or else Deno — {@link spawnDeno} finds it, and puts it
|
|
502
|
+
* first on the child's `PATH` as the launchers do. Stdio is inherited, because
|
|
503
|
+
* `zuke mcp` speaks JSON-RPC over the same stdin/stdout and a build may
|
|
504
|
+
* prompt. For the build's lifetime the parent handles signals as
|
|
505
|
+
* {@link signalPlan} says, so an interrupted build is never orphaned with its
|
|
506
|
+
* exit code lost.
|
|
507
|
+
*/
|
|
508
|
+
export const defaultBuildRunner = async (root, args, program, env) => {
|
|
509
|
+
const child = program === undefined
|
|
510
|
+
? spawnDeno(args, {
|
|
511
|
+
cwd: root,
|
|
512
|
+
stdin: "inherit",
|
|
513
|
+
stdout: "inherit",
|
|
514
|
+
stderr: "inherit",
|
|
515
|
+
env,
|
|
516
|
+
})
|
|
517
|
+
: ProcessTasks.spawn(program, (s) => {
|
|
518
|
+
s.args(...args).cwd(root).stdin("inherit").stdout("inherit")
|
|
519
|
+
.stderr("inherit");
|
|
520
|
+
const childEnv = { ...env, ...QUIET_DENO_ENV, [FORWARDED_ENV]: "1" };
|
|
521
|
+
for (const [name, value] of Object.entries(childEnv))
|
|
522
|
+
s.env(name, value);
|
|
523
|
+
return s;
|
|
524
|
+
});
|
|
525
|
+
const plan = signalPlan(child);
|
|
526
|
+
for (const [signal, handler] of plan) {
|
|
527
|
+
ProcessTasks.addSignalListener(signal, handler);
|
|
528
|
+
}
|
|
529
|
+
try {
|
|
530
|
+
const { code } = await child.status;
|
|
531
|
+
return code;
|
|
532
|
+
}
|
|
533
|
+
finally {
|
|
534
|
+
for (const [signal, handler] of plan) {
|
|
535
|
+
ProcessTasks.removeSignalListener(signal, handler);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The engine the compiled `zuke` binary carries.
|
|
3
|
+
*
|
|
4
|
+
* A project with no `zuke.ts` names its worker and an engine release in
|
|
5
|
+
* `zuke.json`, and the CLI used to run that release with Deno. The binary is
|
|
6
|
+
* compiled with `@zuke/engine` and every wrapper release inside it, so it runs
|
|
7
|
+
* such a project itself: it starts its own executable again with
|
|
8
|
+
* {@link EMBEDDED_ENGINE_ENV} set, and that process is the engine. Nothing
|
|
9
|
+
* looks for Deno, and none need be installed. A run under `deno run`, or a
|
|
10
|
+
* binary whose engine cannot serve the pinned release, goes the old way.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Set to `1` in the environment of the process that runs a hosted build on
|
|
16
|
+
* the binary's own engine: the compiled entry reads it, before the CLI does,
|
|
17
|
+
* and removes it, so nothing the build starts inherits it.
|
|
18
|
+
*/
|
|
19
|
+
export declare const EMBEDDED_ENGINE_ENV = "ZUKE_EMBEDDED_ENGINE";
|
|
20
|
+
/** The engine a compiled `zuke` carries, as its entry point describes it. */
|
|
21
|
+
export interface EmbeddedEngine {
|
|
22
|
+
/** The `@zuke/engine` release compiled in, such as `1.3.0`. */
|
|
23
|
+
readonly version: string;
|
|
24
|
+
/** The executable to start in engine mode: the binary itself. */
|
|
25
|
+
readonly program: string;
|
|
26
|
+
/** Whether the compiled-in release serves the one `zuke.json` pins. */
|
|
27
|
+
serves(pin: string): boolean;
|
|
28
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright (c) 2026 the Zuke contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* The engine the compiled `zuke` binary carries.
|
|
5
|
+
*
|
|
6
|
+
* A project with no `zuke.ts` names its worker and an engine release in
|
|
7
|
+
* `zuke.json`, and the CLI used to run that release with Deno. The binary is
|
|
8
|
+
* compiled with `@zuke/engine` and every wrapper release inside it, so it runs
|
|
9
|
+
* such a project itself: it starts its own executable again with
|
|
10
|
+
* {@link EMBEDDED_ENGINE_ENV} set, and that process is the engine. Nothing
|
|
11
|
+
* looks for Deno, and none need be installed. A run under `deno run`, or a
|
|
12
|
+
* binary whose engine cannot serve the pinned release, goes the old way.
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Set to `1` in the environment of the process that runs a hosted build on
|
|
18
|
+
* the binary's own engine: the compiled entry reads it, before the CLI does,
|
|
19
|
+
* and removes it, so nothing the build starts inherits it.
|
|
20
|
+
*/
|
|
21
|
+
export const EMBEDDED_ENGINE_ENV = "ZUKE_EMBEDDED_ENGINE";
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `zuke upgrade --engine`: move a build another SDK hosts to another `zuke`
|
|
3
|
+
* release, and so to the `@zuke/engine` inside it.
|
|
4
|
+
*
|
|
5
|
+
* Such a project has no `zuke.ts` and nothing from Deno's stack: its
|
|
6
|
+
* launchers run a pinned `zuke` binary, which carries the engine and every
|
|
7
|
+
* tool wrapper, and its `zuke.json` names that engine. Moving it rewrites
|
|
8
|
+
* both, from the release's own `SHA256SUMS` and engine version. The project
|
|
9
|
+
* is found the way forwarding finds it, through the same trust gate, and
|
|
10
|
+
* every other field of `zuke.json` is kept.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import { type BuildProbe } from "./dispatch.ts";
|
|
15
|
+
import type { BinaryPin } from "./binary_pin.ts";
|
|
16
|
+
import type { CliPaint } from "./paint.ts";
|
|
17
|
+
/** What moving the pin reaches outside the process, injectable for tests. */
|
|
18
|
+
export interface EnginePinHost {
|
|
19
|
+
/**
|
|
20
|
+
* What is at `path`, not following a link: a regular `file`, nothing
|
|
21
|
+
* (`missing`), or `other` — a directory, or a link, which a write would
|
|
22
|
+
* follow out of the project.
|
|
23
|
+
*/
|
|
24
|
+
entry(path: string): Promise<"file" | "missing" | "other">;
|
|
25
|
+
/** Read a regular text file. */
|
|
26
|
+
readText(path: string): Promise<string>;
|
|
27
|
+
/** Write a text file, replacing what is there. */
|
|
28
|
+
writeText(path: string, text: string): Promise<void>;
|
|
29
|
+
/** Fetch and parse `@zuke/cli`'s JSR `meta.json`, whose latest is the newest zuke. */
|
|
30
|
+
cliMeta(): Promise<unknown>;
|
|
31
|
+
/** Resolve the pin of a `zuke` release. */
|
|
32
|
+
binaryPin(version: string): Promise<BinaryPin>;
|
|
33
|
+
}
|
|
34
|
+
/** Flags accepted by `zuke upgrade --engine`. */
|
|
35
|
+
export interface EnginePinFlags {
|
|
36
|
+
/** The exact `zuke` release to move to. When unset, the registry's latest. */
|
|
37
|
+
version?: string;
|
|
38
|
+
/** Report what would change, and change nothing. */
|
|
39
|
+
dryRun: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Parse the arguments following `zuke upgrade --engine`. Strict like
|
|
43
|
+
* `upgrade`'s own parser: this rewrites project files.
|
|
44
|
+
* @throws {Error} on an unknown flag, a second version, or a malformed one.
|
|
45
|
+
*/
|
|
46
|
+
export declare function parseEnginePinFlags(args: readonly string[]): EnginePinFlags;
|
|
47
|
+
/**
|
|
48
|
+
* Run `zuke upgrade --engine <args>` for the project at or above `cwd`: move
|
|
49
|
+
* a build another SDK hosts to the latest (or given) `zuke` release, by
|
|
50
|
+
* rewriting its launchers to run that release and its `zuke.json` to pin the
|
|
51
|
+
* engine it carries. A project still on the Deno launchers moves to the
|
|
52
|
+
* binary ones. Reports through `log` and returns the exit code.
|
|
53
|
+
* @throws {Error} with a message for the user when there is no project, when
|
|
54
|
+
* it has its own `zuke.ts`, or when the release cannot be resolved.
|
|
55
|
+
*/
|
|
56
|
+
export declare function runEnginePin(args: readonly string[], log: (line: string) => void, host: EnginePinHost, paint: CliPaint, cwd: string, probe: BuildProbe): Promise<number>;
|