@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.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +343 -2
  3. package/mod.d.ts +99 -0
  4. package/mod.js +893 -0
  5. package/package.json +35 -4
  6. package/src/binary_launcher.d.ts +49 -0
  7. package/src/binary_launcher.js +327 -0
  8. package/src/binary_pin.d.ts +47 -0
  9. package/src/binary_pin.js +117 -0
  10. package/src/build_file.d.ts +25 -0
  11. package/src/build_file.js +65 -0
  12. package/src/cache.d.ts +23 -0
  13. package/src/cache.js +26 -0
  14. package/src/deno_isolated.d.ts +14 -0
  15. package/src/deno_isolated.js +70 -0
  16. package/src/deno_path.d.ts +115 -0
  17. package/src/deno_path.js +205 -0
  18. package/src/deno_pin.d.ts +37 -0
  19. package/src/deno_pin.js +32 -0
  20. package/src/dependencies.d.ts +48 -0
  21. package/src/dependencies.js +80 -0
  22. package/src/dispatch.d.ts +220 -0
  23. package/src/dispatch.js +538 -0
  24. package/src/embedded_engine.d.ts +28 -0
  25. package/src/embedded_engine.js +21 -0
  26. package/src/engine_pin.d.ts +56 -0
  27. package/src/engine_pin.js +196 -0
  28. package/src/fs.d.ts +14 -0
  29. package/src/fs.js +25 -0
  30. package/src/import.d.ts +132 -0
  31. package/src/import.js +289 -0
  32. package/src/install_source.d.ts +79 -0
  33. package/src/install_source.js +104 -0
  34. package/src/launcher.d.ts +37 -0
  35. package/src/launcher.js +464 -0
  36. package/src/mcp_config.d.ts +63 -0
  37. package/src/mcp_config.js +86 -0
  38. package/src/output.d.ts +38 -0
  39. package/src/output.js +43 -0
  40. package/src/paint.d.ts +42 -0
  41. package/src/paint.js +38 -0
  42. package/src/project.d.ts +69 -0
  43. package/src/project.js +132 -0
  44. package/src/pyproject.d.ts +30 -0
  45. package/src/pyproject.js +156 -0
  46. package/src/python_build.d.ts +27 -0
  47. package/src/python_build.js +197 -0
  48. package/src/records.d.ts +9 -0
  49. package/src/records.js +13 -0
  50. package/src/relock.d.ts +11 -0
  51. package/src/relock.js +182 -0
  52. package/src/setup.d.ts +251 -0
  53. package/src/setup.js +601 -0
  54. package/src/star.d.ts +44 -0
  55. package/src/star.js +100 -0
  56. package/src/starter.d.ts +55 -0
  57. package/src/starter.js +126 -0
  58. package/src/toml.d.ts +32 -0
  59. package/src/toml.js +354 -0
  60. package/src/translate.d.ts +69 -0
  61. package/src/translate.js +198 -0
  62. package/src/upgrade.d.ts +113 -0
  63. package/src/upgrade.js +344 -0
  64. package/src/version.d.ts +11 -0
  65. package/src/version.js +13 -0
  66. package/src/version_report.d.ts +55 -0
  67. package/src/version_report.js +187 -0
  68. package/zuke.js +8 -0
package/src/upgrade.js ADDED
@@ -0,0 +1,344 @@
1
+ // Copyright (c) 2026 the Zuke contributors
2
+ // SPDX-License-Identifier: MIT
3
+ /**
4
+ * `zuke upgrade`: replace the globally installed CLI with another release
5
+ * from JSR.
6
+ *
7
+ * The CLI cannot drift forward by itself. Since Deno 2, `deno install -g`
8
+ * writes a lockfile beside the shim (`<root>/bin/.zuke/deno.lock`), which pins
9
+ * `jsr:@zuke/cli@*` to the version resolved on install day, so the installed
10
+ * `zuke` stays on that version until someone re-runs the install with
11
+ * `--force`. This command does that re-run.
12
+ *
13
+ * The reinstall keeps the shim byte-for-byte identical whenever it targets
14
+ * the latest release. It passes the same unversioned specifier as the
15
+ * documented install, plus `--reload` so Deno re-reads the registry's
16
+ * metadata, and only the lock moves. That matters on Windows, where the shim
17
+ * is a `.cmd` file and is rewritten *while it is executing*: `cmd.exe`
18
+ * resumes reading the batch file at its old byte offset once `deno` returns,
19
+ * so a longer file would run the tail of its new last line as a command. Only
20
+ * an explicit non-latest version pins a specifier into the shim, which grows
21
+ * the file, so on Windows that one case is refused with the command to run
22
+ * from another shell. Moving back to latest shrinks the file, which is safe.
23
+ *
24
+ * On Deno 2.6 and later the reinstall turns the minimum dependency age off
25
+ * (see {@link installArgs}), and on a Deno that writes the install lock (2.7
26
+ * and later) success is reported only once that lock resolves `@zuke/cli` to
27
+ * the release that was asked for: Deno exiting 0 means it installed *a*
28
+ * version, not necessarily that one.
29
+ *
30
+ * It only ever replaces an install that is there: the shim's own
31
+ * `<root>/bin/.zuke/deno.json` must exist, so a one-off
32
+ * `deno run jsr:@zuke/cli upgrade`, or an install under another `--root`,
33
+ * is told so instead of gaining a second `zuke` in the default root.
34
+ *
35
+ * The command and its flags mirror `deno upgrade` (`[VERSION]`, `--dry-run`,
36
+ * `--force`).
37
+ *
38
+ * @module
39
+ */
40
+ import { absolutePath, FileTasks, httpJson, lockedJsrSpecifiers, RuntimeTasks, } from "@zuke/core";
41
+ import { resolveBinaryPin } from "./binary_pin.js";
42
+ import { runDenoIsolated } from "./deno_isolated.js";
43
+ import { VERSION_PATTERN } from "./version_report.js";
44
+ import { defaultDenoHost, firstSet } from "./deno_path.js";
45
+ import { VERSION } from "./version.js";
46
+ import { ZUKE_REPO_URL } from "./star.js";
47
+ import { installSource, JSR_PACKAGE as PACKAGE, } from "./install_source.js";
48
+ /** Where the compiled binaries are published. */
49
+ const RELEASES_URL = `${ZUKE_REPO_URL}/releases`;
50
+ /** The registry document listing every published version of `pkg` and the latest. */
51
+ function metaUrl(pkg) {
52
+ return `https://jsr.io/${pkg}/meta.json`;
53
+ }
54
+ /** The CLI's JSR package, whose releases are the `zuke` releases. */
55
+ export const CLI_PACKAGE = "@zuke/cli";
56
+ /** The real {@link UpgradeHost}: `Deno.mainModule`, JSR, and `deno install`. */
57
+ export const defaultUpgradeHost = {
58
+ standalone: () => RuntimeTasks.facts().standalone,
59
+ mainModule: () => RuntimeTasks.mainModule(),
60
+ execPath: () => RuntimeTasks.execPath(),
61
+ realPath: (path) => FileTasks.realPath(path),
62
+ binDir: () => {
63
+ const explicit = firstSet(defaultDenoHost, "DENO_INSTALL_ROOT");
64
+ const home = firstSet(defaultDenoHost, "HOME", "USERPROFILE");
65
+ try {
66
+ if (explicit !== undefined)
67
+ return absolutePath(explicit)("bin").path;
68
+ if (home !== undefined)
69
+ return absolutePath(home)(".deno", "bin").path;
70
+ }
71
+ catch {
72
+ // A relative value names no directory to look in.
73
+ }
74
+ return undefined;
75
+ },
76
+ exists: async (path) => {
77
+ try {
78
+ await FileTasks.stat(path);
79
+ return true;
80
+ }
81
+ catch {
82
+ return false;
83
+ }
84
+ },
85
+ windows: () => defaultDenoHost.windows(),
86
+ meta: () => httpJson(metaUrl(CLI_PACKAGE)),
87
+ binaryPin: (version) => resolveBinaryPin(version, fetch),
88
+ install: (denoArgs) => runDenoIsolated(denoArgs, "zuke-upgrade-"),
89
+ // The cli runs on Deno, so the runtime is the Deno that runs the command.
90
+ denoVersion: () => RuntimeTasks.facts().version,
91
+ readText: (path) => FileTasks.readText(path),
92
+ };
93
+ /**
94
+ * Parse the argument list following `zuke upgrade`.
95
+ *
96
+ * This is stricter than `setup`'s parser: this command replaces the installed
97
+ * executable, so a typo of `--dry-run` must stop it rather than be ignored on
98
+ * the way to a real reinstall.
99
+ *
100
+ * @throws {Error} on an unknown flag, a second version, or a malformed one.
101
+ */
102
+ export function parseUpgradeFlags(args) {
103
+ const flags = { dryRun: false, force: false };
104
+ for (const arg of args) {
105
+ if (arg === "--dry-run") {
106
+ flags.dryRun = true;
107
+ }
108
+ else if (arg === "--force" || arg === "-f") {
109
+ flags.force = true;
110
+ }
111
+ else if (arg.startsWith("-")) {
112
+ throw new Error(`zuke upgrade: unknown option ${arg}. Usage: zuke upgrade ` +
113
+ "[<version>] [--dry-run] [--force]");
114
+ }
115
+ else if (flags.version !== undefined) {
116
+ throw new Error(`zuke upgrade: name one version, not ${flags.version} and ${arg}.`);
117
+ }
118
+ else if (!VERSION_PATTERN.test(arg)) {
119
+ throw new Error(`zuke upgrade: "${arg}" is not a version — name a release such as ` +
120
+ "1.8.0.");
121
+ }
122
+ else {
123
+ flags.version = arg;
124
+ }
125
+ }
126
+ return flags;
127
+ }
128
+ /** A field of a parsed JSON value, or `undefined` when it is not an object. */
129
+ function field(value, name) {
130
+ return value !== null && typeof value === "object"
131
+ ? Reflect.get(value, name)
132
+ : undefined;
133
+ }
134
+ /**
135
+ * The release of `pkg` that `version` asks for, or its latest when none is
136
+ * named, checked against the registry's `meta`. Errors open with `command`,
137
+ * the `zuke` command asking.
138
+ *
139
+ * @throws {Error} when the document has no latest, or the requested version
140
+ * is unpublished or yanked.
141
+ */
142
+ export function resolveRelease(meta, version, pkg, command) {
143
+ const latest = field(meta, "latest");
144
+ if (typeof latest !== "string") {
145
+ throw new Error(`${command}: JSR's metadata for ${pkg} names no latest version.`);
146
+ }
147
+ if (version === undefined)
148
+ return latest;
149
+ const entry = field(field(meta, "versions"), version);
150
+ if (entry === null || typeof entry !== "object") {
151
+ throw new Error(`${command}: ${pkg} ${version} is not published on JSR ` +
152
+ `(latest is ${latest}).`);
153
+ }
154
+ if (field(entry, "yanked") === true) {
155
+ throw new Error(`${command}: ${pkg} ${version} has been yanked from JSR ` +
156
+ `(latest is ${latest}).`);
157
+ }
158
+ return version;
159
+ }
160
+ /**
161
+ * `meta` read through `read`, with a failure to reach the registry reported
162
+ * as the `zuke` `command`'s own error.
163
+ */
164
+ export async function registryMeta(read, pkg, command) {
165
+ try {
166
+ return await read();
167
+ }
168
+ catch (error) {
169
+ throw new Error(`${command}: could not read ${pkg}'s versions from JSR: ` +
170
+ (error instanceof Error ? error.message : String(error)));
171
+ }
172
+ }
173
+ /**
174
+ * Whether Deno `version` is 2.`minor` or later. A version this cannot read is
175
+ * treated as older: each caller gates something that misbehaves on a Deno
176
+ * that lacks it.
177
+ */
178
+ function denoAtLeast(version, minor) {
179
+ const match = /^(\d+)\.(\d+)\./.exec(version);
180
+ if (match === null)
181
+ return false;
182
+ const major = Number(match[1]);
183
+ return major > 2 || (major === 2 && Number(match[2]) >= minor);
184
+ }
185
+ /**
186
+ * Whether the reinstall passes `--minimum-dependency-age=0` on Deno
187
+ * `version`: 2.6.0 and later. The flag arrived during 2.5 (2.5.0 rejects it
188
+ * as an unknown argument, which fails the whole install), and no Deno before
189
+ * 2.9 applies a default age, so gating on a whole minor that has it loses
190
+ * nothing.
191
+ */
192
+ export function acceptsMinimumDependencyAge(version) {
193
+ return denoAtLeast(version, 6);
194
+ }
195
+ /**
196
+ * Whether `deno install -g` on Deno `version` writes the lock beside the shim
197
+ * (`<root>/bin/.zuke/deno.lock`): 2.7.0 and later. An older Deno installs a
198
+ * `--no-config` shim and leaves any lock a newer Deno wrote untouched, so
199
+ * that lock says nothing about what it just installed and is not checked.
200
+ */
201
+ export function writesInstallLock(version) {
202
+ return denoAtLeast(version, 7);
203
+ }
204
+ /**
205
+ * The `deno install` argv that reinstalls the global command at `target`.
206
+ * When `target` is the latest release, the specifier is unversioned (see the
207
+ * module docs for why the shim must not change).
208
+ *
209
+ * On a Deno that has one, the minimum dependency age is turned off. Deno 2.9
210
+ * applies a cooling-off window to registry resolution, which skips releases
211
+ * from the last few days: an unversioned install then resolves to an older
212
+ * release, and a pinned one fails to resolve at all. Upgrading is an explicit
213
+ * choice of a specific published release, so the window does not apply. The
214
+ * flag only steers resolution and is not written into the shim.
215
+ *
216
+ * `--no-config` keeps any discovered config out of the install, the isolated
217
+ * directory's own included (which would otherwise draw a "discovered config
218
+ * file will be ignored" warning); the install still writes its lock.
219
+ */
220
+ export function installArgs(target, latest, denoVersion) {
221
+ const args = ["install", "--global", "--force", "--allow-all", "--no-config"];
222
+ if (acceptsMinimumDependencyAge(denoVersion)) {
223
+ args.push("--minimum-dependency-age=0");
224
+ }
225
+ args.push("--name", "zuke");
226
+ if (latest)
227
+ return [...args, `--reload=${PACKAGE}`, PACKAGE];
228
+ return [...args, `${PACKAGE}@${target}`];
229
+ }
230
+ /**
231
+ * The version the install's lock resolves `spec` to (the specifier the
232
+ * install was given), or `undefined` when the lock cannot be read or does not
233
+ * name it: what was actually installed, whatever was asked for. The exact
234
+ * specifier is matched, so another `@zuke/cli` entry in the lock cannot stand
235
+ * in for it.
236
+ */
237
+ async function installedVersion(host, lockPath, spec) {
238
+ let text;
239
+ try {
240
+ text = await host.readText(lockPath);
241
+ }
242
+ catch {
243
+ return undefined;
244
+ }
245
+ // Deno records an unversioned specifier as the `*` range.
246
+ const key = spec === PACKAGE ? `${PACKAGE}@*` : spec;
247
+ return lockedJsrSpecifiers(text).find((entry) => entry.specifier === key)
248
+ ?.resolved;
249
+ }
250
+ /**
251
+ * Run `zuke upgrade <args>`, reporting through `log`, and return the exit
252
+ * code.
253
+ *
254
+ * @throws {Error} with a message for the user when the request cannot be
255
+ * carried out. `main` prints the message and exits 1.
256
+ */
257
+ export async function runUpgrade(args, log, host, paint) {
258
+ const flags = parseUpgradeFlags(args);
259
+ // Anything but a JSR install is refused: reinstalling from JSR would put a
260
+ // different executable on PATH and leave this one, the one actually
261
+ // running, exactly as it was. A compiled binary is classified before its
262
+ // main module is read, because one compiled from JSR still reports
263
+ // `jsr:@zuke/cli` and would otherwise upgrade a second, unrelated install.
264
+ const source = await installSource(host);
265
+ if (source.kind === "homebrew") {
266
+ throw new Error(`zuke upgrade: this zuke was installed with Homebrew ` +
267
+ `(${source.location}), so reinstalling from JSR would not update ` +
268
+ "it. Update it with: brew upgrade zuke");
269
+ }
270
+ if (source.kind === "chocolatey") {
271
+ throw new Error(`zuke upgrade: this zuke was installed with Chocolatey ` +
272
+ `(${source.location}), so reinstalling from JSR would not update ` +
273
+ "it. Update it with: choco upgrade zuke");
274
+ }
275
+ if (source.kind === "binary") {
276
+ throw new Error(`zuke upgrade: this zuke is a standalone binary (${source.location}), ` +
277
+ "so reinstalling from JSR would not update it. Replace it with the " +
278
+ `binary from ${RELEASES_URL}, or install with Homebrew (brew install ` +
279
+ "zuke-build/tap/zuke), with Chocolatey (choco install zuke) or from " +
280
+ `JSR (deno install -A -g -n zuke ${PACKAGE}).`);
281
+ }
282
+ if (source.kind === "npm") {
283
+ log("This zuke was installed from npm. Update it with: " +
284
+ "npm install -g @zuke-build/cli@latest");
285
+ return 0;
286
+ }
287
+ if (source.kind === "source") {
288
+ throw new Error(`zuke upgrade: this zuke runs ${source.location}, not ${PACKAGE}, so ` +
289
+ "reinstalling from JSR would not update it. Update it where it came " +
290
+ "from.");
291
+ }
292
+ const binDir = host.binDir();
293
+ const marker = binDir === undefined
294
+ ? undefined
295
+ : absolutePath(binDir)(".zuke", "deno.json").path;
296
+ if (marker === undefined || !(await host.exists(marker))) {
297
+ throw new Error("zuke upgrade: found no global install of zuke to replace" +
298
+ (marker === undefined
299
+ ? " (neither DENO_INSTALL_ROOT nor a home directory is set)"
300
+ : ` (no ${marker})`) +
301
+ ". Install it with: deno install -A -g -n zuke " + PACKAGE);
302
+ }
303
+ const meta = await registryMeta(() => host.meta(), CLI_PACKAGE, "zuke upgrade");
304
+ const target = resolveRelease(meta, flags.version, CLI_PACKAGE, "zuke upgrade");
305
+ if (target === VERSION && !flags.force) {
306
+ log(paint.ok(`zuke ${VERSION} is already ${flags.version === undefined ? "the latest release" : "installed"}. Pass --force to reinstall it.`));
307
+ return 0;
308
+ }
309
+ const latest = target === field(meta, "latest");
310
+ const denoArgs = installArgs(target, latest, host.denoVersion());
311
+ if (!latest && host.windows()) {
312
+ throw new Error(`zuke upgrade: on Windows, installing ${target} would rewrite the ` +
313
+ "zuke.cmd running this command into a longer file, and cmd.exe " +
314
+ "would then run a fragment of it. Run this from another shell " +
315
+ `instead: deno ${denoArgs.join(" ")}`);
316
+ }
317
+ if (flags.dryRun) {
318
+ log(`Would replace zuke ${VERSION} with ${target}: deno ${denoArgs.join(" ")}`);
319
+ return 0;
320
+ }
321
+ log(`Replacing zuke ${VERSION} with ${target}…`);
322
+ const code = await host.install(denoArgs);
323
+ if (code !== 0) {
324
+ // Not "still on the old version": a forced install that failed part-way
325
+ // may have replaced some of it, and Deno's own output above says how far.
326
+ log(paint.fail(`zuke upgrade: deno install exited ${code}; see its output above.`));
327
+ return code;
328
+ }
329
+ // A zero exit says Deno installed *something*. Confirm it is the target
330
+ // before saying so: a resolver that picks another release (Deno 2.9's
331
+ // dependency-age window did) would otherwise be reported as a success.
332
+ const deno = host.denoVersion();
333
+ const installed = writesInstallLock(deno)
334
+ ? await installedVersion(host, absolutePath(marker).parent()("deno.lock").path, denoArgs[denoArgs.length - 1])
335
+ : target;
336
+ if (installed !== target) {
337
+ log(paint.fail(`zuke upgrade: deno install finished, but its lock resolves ` +
338
+ `@zuke/cli to ${installed ?? "nothing"}, not ${target}. ` +
339
+ "Check with: zuke --version"));
340
+ return 1;
341
+ }
342
+ log(paint.ok(`Done — zuke ${target} is installed. Check with: zuke --version`));
343
+ return 0;
344
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The `@zuke/cli` version. Kept in sync with deno.json by release-please.
3
+ *
4
+ * Annotated `string` for the reason `@zuke/core`'s copy is: an inferred
5
+ * literal type is what `deno doc` would record, and a generated doc that
6
+ * pins the version goes stale the moment release-please bumps the marked
7
+ * line. This constant is internal to the package today, so nothing generated
8
+ * reads it — the annotation is what keeps that from mattering if it is ever
9
+ * exported.
10
+ */
11
+ export declare const VERSION: string;
package/src/version.js ADDED
@@ -0,0 +1,13 @@
1
+ // Copyright (c) 2026 the Zuke contributors
2
+ // SPDX-License-Identifier: MIT
3
+ /**
4
+ * The `@zuke/cli` version. Kept in sync with deno.json by release-please.
5
+ *
6
+ * Annotated `string` for the reason `@zuke/core`'s copy is: an inferred
7
+ * literal type is what `deno doc` would record, and a generated doc that
8
+ * pins the version goes stale the moment release-please bumps the marked
9
+ * line. This constant is internal to the package today, so nothing generated
10
+ * reads it — the annotation is what keeps that from mattering if it is ever
11
+ * exported.
12
+ */
13
+ export const VERSION = "2.6.0"; // x-release-please-version
@@ -0,0 +1,55 @@
1
+ /**
2
+ * The text of the first of Deno's config files at `root` that can be read, in
3
+ * the order Deno prefers them (`deno.json`, then `deno.jsonc`), or
4
+ * `undefined` when there is none: the input {@link projectLockPath} takes.
5
+ */
6
+ export declare function projectConfigText(readText: (path: string) => Promise<string>, root: string): Promise<string | undefined>;
7
+ /**
8
+ * The lockfile the project at `root` resolves through, from the text of its
9
+ * config (the first of `DENO_CONFIG_FILES` that exists, or `undefined` when
10
+ * there is none), or `null` when no lock can be read for it.
11
+ *
12
+ * It follows Deno's `lock` setting:
13
+ * - `false` disables the lock, and Deno then ignores any `deno.lock` left
14
+ * behind, so reading that file would report a version the build does not
15
+ * run. That is `null`.
16
+ * - A string, or an object's `path`, names the lockfile, relative to `root`
17
+ * unless it is absolute.
18
+ * - No setting, `true`, or an object without a `path` keeps `deno.lock`.
19
+ *
20
+ * Comments and trailing commas are read as Deno reads them. A config that is
21
+ * still not valid is a setting this cannot read, so it is also `null` rather
22
+ * than a guess; the caller then asks the build, as it does for a project with
23
+ * no lock.
24
+ */
25
+ export declare function projectLockPath(root: string, configText: string | undefined): string | null;
26
+ /**
27
+ * A release version: a numeric core with an optional prerelease and build.
28
+ * `zuke upgrade` checks the version it is asked for against it, and a lock's
29
+ * resolved core version must match it before it is printed.
30
+ */
31
+ export declare const VERSION_PATTERN: RegExp;
32
+ /**
33
+ * The `@zuke/core` versions a lock resolves, from its text: one entry per
34
+ * distinct version (a lock normally holds one, but several ranges can each
35
+ * resolve their own), in the order the lock lists them. Empty when the lock
36
+ * names no core, or is not a lock this can read.
37
+ *
38
+ * A resolved value that is not a release version is dropped rather than
39
+ * printed. The lock is the project's own file, and a value carrying a newline
40
+ * or a terminal escape sequence would otherwise reach the terminal as written,
41
+ * from a command that promises only to report a number.
42
+ */
43
+ export declare function lockedCoreVersions(lockText: string): string[];
44
+ /**
45
+ * `path` with the home directory written `~`, for display: a project path is
46
+ * usually under home, and the prefix is the least informative part of it.
47
+ * Only a whole leading directory is replaced, so `/home/me2` stays as it is
48
+ * under `/home/me`.
49
+ *
50
+ * `path` is an {@link absolutePath} one (forward slashes, also on Windows), so
51
+ * `home` is normalised the same way before comparing, and compared without
52
+ * regard to case on Windows, whose paths are case-insensitive. A `home` that
53
+ * is not absolute names no prefix to strip.
54
+ */
55
+ export declare function homeRelative(path: string, home: string | undefined, windows: boolean): string;
@@ -0,0 +1,187 @@
1
+ // Copyright (c) 2026 the Zuke contributors
2
+ // SPDX-License-Identifier: MIT
3
+ /**
4
+ * What `zuke --version` reports about the project it was run in. The rich
5
+ * panel it prints on a terminal is core's `versionPanel`, so it matches the
6
+ * build's own.
7
+ *
8
+ * The project's Zuke version is read from its `deno.lock` — the version the
9
+ * build actually resolves — rather than by running the build's own
10
+ * `--version`. Asking the build only works on a build whose core answers that
11
+ * flag, which is core 1.60.0 and later; on anything older it printed
12
+ * `Unknown flag "--version"`. The lock answers for every version, without
13
+ * spawning a process. Asking the build is kept only as the fallback for a
14
+ * project with no lock, or a lock that names no core.
15
+ *
16
+ * @module
17
+ */
18
+ import { absolutePath, lockedJsrSpecifiers } from "@zuke/core";
19
+ import { isRecord } from "./records.js";
20
+ import { DENO_CONFIG_FILES, LOCK_FILE } from "./dispatch.js";
21
+ /**
22
+ * `text` with its JSONC comments and trailing commas removed, so `JSON.parse`
23
+ * reads it. Deno reads `deno.json` and `deno.jsonc` alike as JSONC, and a
24
+ * comment is the usual reason for a `deno.jsonc`. Text inside a string is left
25
+ * alone, escapes included; anything this does not make valid stays invalid.
26
+ */
27
+ function stripJsonc(text) {
28
+ let out = "";
29
+ // A comma is held back until the next token shows whether it trails.
30
+ let comma = false;
31
+ let i = 0;
32
+ const emit = (token) => {
33
+ if (comma && token !== "}" && token !== "]")
34
+ out += ",";
35
+ comma = false;
36
+ out += token;
37
+ };
38
+ while (i < text.length) {
39
+ const ch = text[i];
40
+ if (ch === '"') {
41
+ let end = i + 1;
42
+ while (end < text.length && text[end] !== '"') {
43
+ end += text[end] === "\\" ? 2 : 1;
44
+ }
45
+ emit(text.slice(i, end + 1));
46
+ i = end + 1;
47
+ }
48
+ else if (text.startsWith("//", i)) {
49
+ const end = text.indexOf("\n", i);
50
+ i = end === -1 ? text.length : end;
51
+ }
52
+ else if (text.startsWith("/*", i)) {
53
+ const end = text.indexOf("*/", i + 2);
54
+ i = end === -1 ? text.length : end + 2;
55
+ }
56
+ else if (ch === ",") {
57
+ if (comma)
58
+ out += ",";
59
+ comma = true;
60
+ i++;
61
+ }
62
+ else if (/\s/.test(ch)) {
63
+ out += ch;
64
+ i++;
65
+ }
66
+ else {
67
+ emit(ch);
68
+ i++;
69
+ }
70
+ }
71
+ return comma ? `${out},` : out;
72
+ }
73
+ /**
74
+ * The text of the first of Deno's config files at `root` that can be read, in
75
+ * the order Deno prefers them (`deno.json`, then `deno.jsonc`), or
76
+ * `undefined` when there is none: the input {@link projectLockPath} takes.
77
+ */
78
+ export async function projectConfigText(readText, root) {
79
+ for (const name of DENO_CONFIG_FILES) {
80
+ try {
81
+ return await readText(`${root}/${name}`);
82
+ }
83
+ catch {
84
+ // Not there: try the next one Deno would.
85
+ }
86
+ }
87
+ return undefined;
88
+ }
89
+ /**
90
+ * The lockfile the project at `root` resolves through, from the text of its
91
+ * config (the first of `DENO_CONFIG_FILES` that exists, or `undefined` when
92
+ * there is none), or `null` when no lock can be read for it.
93
+ *
94
+ * It follows Deno's `lock` setting:
95
+ * - `false` disables the lock, and Deno then ignores any `deno.lock` left
96
+ * behind, so reading that file would report a version the build does not
97
+ * run. That is `null`.
98
+ * - A string, or an object's `path`, names the lockfile, relative to `root`
99
+ * unless it is absolute.
100
+ * - No setting, `true`, or an object without a `path` keeps `deno.lock`.
101
+ *
102
+ * Comments and trailing commas are read as Deno reads them. A config that is
103
+ * still not valid is a setting this cannot read, so it is also `null` rather
104
+ * than a guess; the caller then asks the build, as it does for a project with
105
+ * no lock.
106
+ */
107
+ export function projectLockPath(root, configText) {
108
+ if (configText === undefined)
109
+ return `${root}/${LOCK_FILE}`;
110
+ let config;
111
+ try {
112
+ config = JSON.parse(stripJsonc(configText));
113
+ }
114
+ catch {
115
+ return null;
116
+ }
117
+ const lock = isRecord(config) ? config.lock : undefined;
118
+ if (lock === false)
119
+ return null;
120
+ const path = typeof lock === "string"
121
+ ? lock
122
+ : isRecord(lock) && typeof lock.path === "string"
123
+ ? lock.path
124
+ : LOCK_FILE;
125
+ try {
126
+ return absolutePath(path).path;
127
+ }
128
+ catch {
129
+ return absolutePath(root, path).path;
130
+ }
131
+ }
132
+ /** The core package whose resolved version a project's lock is read for. */
133
+ const CORE_PACKAGE = "@zuke/core";
134
+ /**
135
+ * A release version: a numeric core with an optional prerelease and build.
136
+ * `zuke upgrade` checks the version it is asked for against it, and a lock's
137
+ * resolved core version must match it before it is printed.
138
+ */
139
+ export const VERSION_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;
140
+ /**
141
+ * The `@zuke/core` versions a lock resolves, from its text: one entry per
142
+ * distinct version (a lock normally holds one, but several ranges can each
143
+ * resolve their own), in the order the lock lists them. Empty when the lock
144
+ * names no core, or is not a lock this can read.
145
+ *
146
+ * A resolved value that is not a release version is dropped rather than
147
+ * printed. The lock is the project's own file, and a value carrying a newline
148
+ * or a terminal escape sequence would otherwise reach the terminal as written,
149
+ * from a command that promises only to report a number.
150
+ */
151
+ export function lockedCoreVersions(lockText) {
152
+ const versions = lockedJsrSpecifiers(lockText)
153
+ .filter((entry) => entry.name === CORE_PACKAGE)
154
+ .map((entry) => entry.resolved)
155
+ .filter((version) => VERSION_PATTERN.test(version));
156
+ return [...new Set(versions)];
157
+ }
158
+ /**
159
+ * `path` with the home directory written `~`, for display: a project path is
160
+ * usually under home, and the prefix is the least informative part of it.
161
+ * Only a whole leading directory is replaced, so `/home/me2` stays as it is
162
+ * under `/home/me`.
163
+ *
164
+ * `path` is an {@link absolutePath} one (forward slashes, also on Windows), so
165
+ * `home` is normalised the same way before comparing, and compared without
166
+ * regard to case on Windows, whose paths are case-insensitive. A `home` that
167
+ * is not absolute names no prefix to strip.
168
+ */
169
+ export function homeRelative(path, home, windows) {
170
+ if (home === undefined)
171
+ return path;
172
+ let base;
173
+ try {
174
+ base = absolutePath(home).path;
175
+ }
176
+ catch {
177
+ return path;
178
+ }
179
+ if (base === "/" || /^[A-Za-z]:\/?$/.test(base))
180
+ return path;
181
+ const fold = (text) => windows ? text.toLowerCase() : text;
182
+ if (fold(path) === fold(base))
183
+ return "~";
184
+ return fold(path).startsWith(`${fold(base)}/`)
185
+ ? `~${path.slice(base.length)}`
186
+ : path;
187
+ }
package/zuke.js ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ // Copyright (c) 2026 the Zuke contributors
3
+ // SPDX-License-Identifier: MIT
4
+
5
+ import { RuntimeTasks } from "@zuke/core";
6
+ import { main } from "./mod.js";
7
+
8
+ RuntimeTasks.exit(await main(RuntimeTasks.args()));