@zuke-build/cli 2.9.1 → 2.11.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/README.md CHANGED
@@ -4,9 +4,10 @@ The `zuke` command-line tool — scaffold
4
4
  [Zuke](https://github.com/zuke-build/zuke#readme) into any project.
5
5
 
6
6
  ```sh
7
- deno install -A -g -n zuke jsr:@zuke/cli
8
- zuke setup
9
- zuke setup --runtime node # a Node project: package.json and Node launchers, no Deno
7
+ npm install -g @zuke-build/cli
8
+ # or, on Deno: deno install -A -g -n zuke jsr:@zuke/cli
9
+ zuke setup # a Node project (default)
10
+ zuke setup --runtime deno # a Deno project
10
11
  zuke setup --sdk python # a build.py, with launchers that run the zuke binary: no Deno
11
12
  zuke upgrade # later: move the installed CLI to the latest release
12
13
  zuke upgrade --engine # or move a Python project to the latest zuke release
@@ -38,7 +39,8 @@ scaffold Zuke into any project with `zuke setup`, then run its build from
38
39
  anywhere inside the project with `zuke <target>`: every command that is not
39
40
  the CLI's own (`setup`, `import`, `doc`, `upgrade`, and the project
40
41
  commands `add`, `remove`, `cache` and `relock`) is forwarded to the
41
- nearest `zuke.ts`, exactly as the `./zuke` launcher would run it. Update the
42
+ nearest `zuke.ts`, which it runs on Node or Deno, installing either when
43
+ the machine has none. Update the
42
44
  installed CLI itself with `zuke upgrade`, and manage a project's
43
45
  dependencies and lock with `zuke add`, `remove`, `cache` and `relock`.
44
46
  @module
@@ -66,12 +68,29 @@ const EMBEDDED_ENGINE_ENV: "ZUKE_EMBEDDED_ENGINE"
66
68
  and removes it, so nothing the build starts inherits it.
67
69
 
68
70
  const FORWARDED_ENV: "ZUKE_FORWARDED"
69
- Set to `1` in a launcher's environment, so a launcher that itself calls the
70
- global `zuke` gets the bare `deno run` instead of itself again, endlessly.
71
+ Set to `1` by the launchers `zuke setup` writes, in the environment of what
72
+ they start; this command does not read it.
73
+
74
+ const WorkerTasks: WorkerTasksApi
75
+ What starts a hosted build's worker: finding (or installing) its program.
71
76
 
72
77
  const defaultPrompter: Prompter
73
78
  The real {@link Prompter}, backed by Deno's `prompt`/`confirm`.
74
79
 
80
+ class UvNotFoundError extends Error
81
+ Thrown when a build's worker runs uv and none was found or installed.
82
+
83
+ constructor(tried: readonly string[])
84
+ Build the failure from the candidates that were tried, in order.
85
+
86
+ @param tried
87
+ The executables that were looked for and not found.
88
+
89
+ override name: string
90
+ The error's name, as reported by `String(error)` and stack traces.
91
+ readonly tried: readonly string[]
92
+ The executables that were looked for and not found, in the order tried.
93
+
75
94
  interface BinaryPin
76
95
  The pinned `zuke` release, the engine inside it, and each archive's digest.
77
96
 
@@ -89,10 +108,6 @@ interface BuildLocation
89
108
  The absolute repository root: the directory holding `zuke.json`.
90
109
  frozen: boolean
91
110
  Whether a `deno.lock` sits at the root, so the run passes `--frozen`.
92
- launcher: string | null
93
- The absolute path of the executable `zuke` launcher at the root, run in
94
- place of `deno run` — or `null` when there is none, or the platform
95
- reports no file mode to tell (Windows).
96
111
  build: string
97
112
  The build file's name: the one the root's `zuke.json` names in its
98
113
  `build` field, `zuke.ts` when it names none.
@@ -331,13 +346,30 @@ interface UpgradeHost extends InstallHost
331
346
  readText(path: string): Promise<string>
332
347
  Read a text file, rejecting when it cannot be read.
333
348
 
349
+ interface WorkerTasksApi
350
+ The shape of {@link WorkerTasks}.
351
+
352
+ resolveProgram(program: string): Promise<string>
353
+ The executable to start a worker whose `sdk.command` begins with
354
+ `program`: for `uv`, the one on `PATH`, else the pinned uv `zuke`
355
+ installed, else that uv installed now (see `runtime_install.ts` for when
356
+ it asks first); any
357
+ other program unchanged. The `zuke` binary hands this to the engine as its
358
+ `resolveProgram`.
359
+
360
+ @param program
361
+ The first word of the build's `sdk.command`.
362
+
363
+ @throws {UvNotFoundError}
364
+ when there is no uv and none may be installed.
365
+
334
366
  type BinaryTarget = "aarch64-apple-darwin" | "x86_64-apple-darwin" | "aarch64-unknown-linux-gnu" | "x86_64-unknown-linux-gnu" | "x86_64-pc-windows-msvc"
335
367
  A platform a `zuke` release ships a binary for, as its archive names it.
336
368
 
337
369
  type BuildRunner = (root: string, args: string[], program?: string, env?: Readonly<Record<string, string>>) => Promise<number>
338
- Runs `program <args>` from `root` — Deno when `program` is omitted, else the
339
- project's launcher at that path, or {@link NODE} for a build file on Node —
340
- and resolves to its exit code: the
370
+ Runs `program <args>` from `root` — Deno when `program` is omitted,
371
+ {@link NODE} for a build file on Node, else the program at that path (this
372
+ binary, to run its built-in engine) — and resolves to its exit code: the
341
373
  injectable subprocess seam, so the forwarding is testable without a build.
342
374
  `env` adds variables to the environment the child inherits.
343
375
 
package/mod.d.ts CHANGED
@@ -18,6 +18,7 @@ export type { ProjectHost } from "./src/project.ts";
18
18
  export type { ProjectRuntime } from "./src/runtime_field.ts";
19
19
  export type { BuildLocation, BuildProbe, BuildRunner, Ownership, } from "./src/dispatch.ts";
20
20
  export { FORWARDED_ENV } from "./src/dispatch.ts";
21
+ export { UvNotFoundError, WorkerTasks, type WorkerTasksApi, } from "./src/uv_path.ts";
21
22
  /**
22
23
  * The interactive surface, injectable so the wizard is testable without a TTY.
23
24
  */
package/mod.js CHANGED
@@ -11,7 +11,8 @@
11
11
  * anywhere inside the project with `zuke <target>`: every command that is not
12
12
  * the CLI's own (`setup`, `import`, `doc`, `upgrade`, and the project
13
13
  * commands `add`, `remove`, `cache` and `relock`) is forwarded to the
14
- * nearest `zuke.ts`, exactly as the `./zuke` launcher would run it. Update the
14
+ * nearest `zuke.ts`, which it runs on Node or Deno, installing either when
15
+ * the machine has none. Update the
15
16
  * installed CLI itself with `zuke upgrade`, and manage a project's
16
17
  * dependencies and lock with `zuke add`, `remove`, `cache` and `relock`.
17
18
  *
@@ -25,7 +26,7 @@ import { defaultStarActions, promptStar, } from "./src/star.js";
25
26
  import { VERSION } from "./src/version.js";
26
27
  import { installLabel, installSource, } from "./src/install_source.js";
27
28
  import { DENO_PIN } from "./src/deno_pin.js";
28
- import { buildLabel, defaultBuildProbe, defaultBuildRunner, DENO_SUPPORT_NOTICE, embeddedEngineNotice, locateBuild, NO_LOCK_NOTICE, owesDenoSupportNotice, runBuild, runningNotice, servingEngine, } from "./src/dispatch.js";
29
+ import { buildLabel, defaultBuildProbe, defaultBuildRunner, DENO_SUPPORT_NOTICE, locateBuild, NO_LOCK_NOTICE, owesDenoSupportNotice, runBuild, runningNotice, servingEngine, } from "./src/dispatch.js";
29
30
  import { absolutePath, logoLines, resolveDocSpec, RuntimeTasks, } from "@zuke/core";
30
31
  import { runDenoIsolated } from "./src/deno_isolated.js";
31
32
  import { defaultDenoHost, firstSet } from "./src/deno_path.js";
@@ -41,6 +42,7 @@ import { versionPanel } from "@zuke/core/render";
41
42
  import { homeRelative, lockedCoreVersions, projectConfigText, projectLockPath, VERSION_PATTERN, } from "./src/version_report.js";
42
43
  export { EMBEDDED_ENGINE_ENV, } from "./src/embedded_engine.js";
43
44
  export { FORWARDED_ENV } from "./src/dispatch.js";
45
+ export { UvNotFoundError, WorkerTasks, } from "./src/uv_path.js";
44
46
  /** The real {@link Prompter}, backed by Deno's `prompt`/`confirm`. */
45
47
  export const defaultPrompter = {
46
48
  interactive() {
@@ -291,7 +293,7 @@ function printBanner(host) {
291
293
  *
292
294
  * Headed so a reader can tell it apart from the build's half, which is printed
293
295
  * after it when there is a project here. The two are different surfaces — this
294
- * one is the installed `zuke`, that one is whatever `zuke.ts` declares — and
296
+ * one is the installed `zuke`, that one is whatever the build file declares — and
295
297
  * showing them unlabelled under one heading was what made them look like one
296
298
  * inconsistent command.
297
299
  */
@@ -305,12 +307,13 @@ Usage:
305
307
  Zuke commands (available anywhere):
306
308
  setup [options] Scaffold Zuke into a directory
307
309
  import [options] Generate a build from package.json scripts, a Makefile or pyproject.toml tasks
308
- doc <package> Show a @zuke/* package's API docs (isolated resolution)
309
- upgrade [<version>] Reinstall this CLI at the latest (or given) JSR release
310
- (Homebrew installs: brew upgrade zuke)
310
+ doc <package> Show a @zuke/* package's API docs (runs deno doc, so needs Deno)
311
+ upgrade [<version>] Reinstall a JSR-installed CLI at the latest (or given) release
312
+ (npm: npm install -g @zuke-build/cli; Homebrew: brew upgrade zuke)
311
313
  upgrade --engine [<v>] Move a Python project's launchers to the latest (or given) zuke release
312
- add <package...> Add packages to this project's deno.json (docker = @zuke/docker)
313
- remove <package...> Remove packages from this project's deno.json
314
+ add <package...> Add packages to this project's package.json, or deno.json on Deno
315
+ (docker = @zuke/docker)
316
+ remove <package...> Remove packages from this project's package.json, or deno.json on Deno
314
317
  cache [<entry>...] Fetch everything the build imports, keeping the lock
315
318
  relock [<entry>...] Regenerate this project's lock from scratch
316
319
  --help Show this help
@@ -328,7 +331,7 @@ Setup options:
328
331
  --mcp Also write .mcp.json registering the build's MCP server
329
332
  --allow-run …with --allow-run, so an agent may execute targets (implies --mcp)
330
333
  --bootstrap Launchers download their pinned, checksum-verified runtime when missing (default):
331
- Deno for a zuke.ts build, the zuke binary for a Python one
334
+ Node or Deno for a TypeScript build, the zuke binary for a Python one
332
335
  --no-bootstrap Launchers require that runtime on PATH and fail closed without it
333
336
  --force, -f Overwrite existing files
334
337
  --yes, -y Accept defaults without prompting
@@ -338,11 +341,11 @@ Import options:
338
341
  --name <Class> Build class name (default: MyBuild)
339
342
  --from <source> Force a source: package.json, makefile or pyproject (default: auto-detect)
340
343
  --zuke <version> The zuke release a pyproject.toml import's launchers run (default: this one)
341
- --runtime deno|node Runtime of a generated zuke.ts (default: node, or the project's own)
344
+ --runtime deno|node Runtime of a generated build file (default: node, or the project's own)
342
345
  --mcp Also write .mcp.json registering the build's MCP server
343
346
  --allow-run …with --allow-run, so an agent may execute targets (implies --mcp)
344
347
  --bootstrap Launchers download their pinned, checksum-verified runtime when missing (default):
345
- Deno for a zuke.ts build, the zuke binary for a Python one
348
+ Node or Deno for a TypeScript build, the zuke binary for a Python one
346
349
  --no-bootstrap Launchers require that runtime on PATH and fail closed without it
347
350
  --force, -f Overwrite existing files
348
351
  --yes, -y Accept defaults without prompting
@@ -385,7 +388,7 @@ function helpText(paint) {
385
388
  */
386
389
  function buildHelpHeading(location, paint, engine) {
387
390
  const title = `This project's build (${buildLabel(location, engine)}) — ` +
388
- `run as \`zuke <target>\` or \`./zuke <target>\`:`;
391
+ `run as \`zuke <target>\`:`;
389
392
  return `\n${paint.muted("─".repeat(72))}\n${paint.heading(title)}\n`;
390
393
  }
391
394
  /**
@@ -393,7 +396,7 @@ function buildHelpHeading(location, paint, engine) {
393
396
  * found but did not describe itself — it failed to spawn, or its own `--help`
394
397
  * exited non-zero. The heading is already printed by then (the build inherits
395
398
  * stdio, so there is nothing to buffer and inspect first), which would
396
- * otherwise leave a heading over an empty section with only Deno's own error
399
+ * otherwise leave a heading over an empty section with only the runtime's own error
397
400
  * to explain it. Named on stderr so stdout stays the help.
398
401
  */
399
402
  const BUILD_HELP_FAILED = "The build was found but could not describe itself, so the section above " +
@@ -401,7 +404,7 @@ const BUILD_HELP_FAILED = "The build was found but could not describe itself, so
401
404
  /** What to say when the build was found but could not report its version. */
402
405
  const BUILD_VERSION_FAILED = "The build was found but could not report its version: a build on " +
403
406
  "@zuke/core older than 1.60.0 cannot answer --version, and this project " +
404
- "has no deno.lock naming its core to read instead. The CLI's own version " +
407
+ "has no lockfile naming its core to read instead. The CLI's own version " +
405
408
  "above is unaffected.";
406
409
  /** What to say where the build's half would be, when there is no project. */
407
410
  const NO_BUILD_NOTICE = `\n${"─".repeat(72)}\nNo zuke.json in this directory or any parent, so ` +
@@ -630,7 +633,7 @@ async function commandDoc(args, host, runner) {
630
633
  */
631
634
  async function forwardToBuild(args, probe, runner, engine) {
632
635
  // The CLI's own stderr sink, not `ConsoleTasks`: `ZUKE_LOG_LEVEL` is a knob
633
- // for the build's output, and neither a security refusal nor the launchers'
636
+ // for the build's output, and neither a security refusal nor the
634
637
  // unverified-lockfile notice may be silenced by it. The sink neutralises the
635
638
  // line on an Actions runner — the root and the error message below carry
636
639
  // text from the filesystem and from a failed spawn, not from Zuke.
@@ -640,23 +643,17 @@ async function forwardToBuild(args, probe, runner, engine) {
640
643
  if (location === null)
641
644
  return null;
642
645
  // Discovery ran something the caller never named: say which, unless it
643
- // is the build right here, where `./zuke` would have been the same act.
646
+ // is the build right here, which the caller did name by standing in it.
644
647
  if (location.root !== absolutePath(cwd).path) {
645
648
  output.error(runningNotice(location, engine));
646
649
  }
647
- const fallback = embeddedEngineNotice(location, engine);
648
- if (fallback !== undefined)
649
- output.error(fallback);
650
- // A launcher prints its own notices; the CLI says where a Deno build
651
- // stands only when the launcher it goes through does not.
652
- if (await owesDenoSupportNotice(location, probe)) {
650
+ if (owesDenoSupportNotice(location)) {
653
651
  output.error(DENO_SUPPORT_NOTICE);
654
652
  }
655
- // Only the bare `deno run` a launcher stands in for is this notice's.
656
- // A hosted build has no Deno config for Deno to write a lock into, so
657
- // the launchers say nothing there, and neither does this.
658
- if (location.launcher === null && !location.frozen &&
659
- location.engine === null) {
653
+ // A hosted build has no Deno config for Deno to write a lock into, and a
654
+ // Node build never writes a deno.lock, so neither is told.
655
+ if (!location.frozen && location.engine === null &&
656
+ location.runtime === "deno") {
660
657
  output.error(NO_LOCK_NOTICE);
661
658
  }
662
659
  return await runBuild(runner, location, args, undefined, engine);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zuke-build/cli",
3
- "version": "2.9.1",
3
+ "version": "2.11.0",
4
4
  "description": "The zuke command-line tool — scaffold a Zuke project with `zuke setup`, including a starter build, launchers, and tasks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -15,8 +15,8 @@
15
15
  * - **plain** — require `zuke` on `PATH`, and when it is missing print where
16
16
  * to get it and exit 1, for a project that must not download its tools.
17
17
  *
18
- * Both set `ZUKE_FORWARDED`, so the `zuke` they start runs the build itself
19
- * rather than this launcher again, and `ZUKE_LAUNCHER_ROOT` to the
18
+ * Both set `ZUKE_FORWARDED`, which an older `zuke` read so as not to run this
19
+ * launcher again (today's never runs a launcher), and `ZUKE_LAUNCHER_ROOT` to the
20
20
  * launcher's directory: running a launcher is an explicit act on the
21
21
  * project it sits in, so the discovery trust gate does not judge that root.
22
22
  *
@@ -17,8 +17,8 @@
17
17
  * - **plain** — require `zuke` on `PATH`, and when it is missing print where
18
18
  * to get it and exit 1, for a project that must not download its tools.
19
19
  *
20
- * Both set `ZUKE_FORWARDED`, so the `zuke` they start runs the build itself
21
- * rather than this launcher again, and `ZUKE_LAUNCHER_ROOT` to the
20
+ * Both set `ZUKE_FORWARDED`, which an older `zuke` read so as not to run this
21
+ * launcher again (today's never runs a launcher), and `ZUKE_LAUNCHER_ROOT` to the
22
22
  * launcher's directory: running a launcher is an explicit act on the
23
23
  * project it sits in, so the discovery trust gate does not judge that root.
24
24
  *
@@ -17,7 +17,8 @@
17
17
  * @module
18
18
  */
19
19
  import { FileTasks } from "@zuke/core";
20
- import { spawnDeno } from "./deno_path.js";
20
+ import { denoCandidates, spawnDeno } from "./deno_path.js";
21
+ import { provisionDeno } from "./runtime_install.js";
21
22
  import { output } from "./output.js";
22
23
  /**
23
24
  * The config written into the isolated directory: a workspace root with no
@@ -48,7 +49,7 @@ export async function runDenoIsolated(denoArgs, prefix) {
48
49
  stdin: "null",
49
50
  stdout: "piped",
50
51
  stderr: "piped",
51
- });
52
+ }, denoCandidates(), provisionDeno());
52
53
  const [{ code }, stdout, stderr] = await Promise.all([
53
54
  child.status,
54
55
  new Response(child.stdout).text(),
@@ -27,7 +27,7 @@
27
27
  *
28
28
  * @module
29
29
  */
30
- import { type ChildProcess, type ProcessStdio } from "@zuke/core";
30
+ import { type AbsolutePath, type ChildProcess, type ProcessStdio } from "@zuke/core";
31
31
  /**
32
32
  * The environment questions Deno resolution asks, injectable so the ordering
33
33
  * is testable without a second machine.
@@ -44,12 +44,29 @@ export interface DenoHost {
44
44
  export declare const defaultDenoHost: DenoHost;
45
45
  /** The first of `names` with a non-empty value, or `undefined` if none has one. */
46
46
  export declare function firstSet(host: DenoHost, ...names: string[]): string | undefined;
47
+ /**
48
+ * Deno's install directory: `DENO_INSTALL`, or `~/.deno`, where Deno's own
49
+ * installer puts it and where `zuke` installs the pinned Deno (in `bin`).
50
+ * `USERPROFILE` stands in for `HOME` on Windows, where `HOME` is often unset
51
+ * outside PowerShell. `undefined` when the environment
52
+ * names no directory the candidate can be built from — none of the three set,
53
+ * or a value that is not an absolute path — which leaves `PATH` as the only
54
+ * candidate rather than a crash on the way to spawning anything.
55
+ */
56
+ export declare function denoInstallDir(host: DenoHost): AbsolutePath | undefined;
57
+ /**
58
+ * The directory `variable` names, else `home` under the user's home
59
+ * (`HOME`, or `USERPROFILE` on Windows). `undefined` when neither is set, or
60
+ * the value is not an absolute path.
61
+ */
62
+ export declare function installDirFrom(host: DenoHost, variable: string, ...home: string[]): AbsolutePath | undefined;
47
63
  /**
48
64
  * The Deno executables to try, in order. The first is whatever
49
65
  * {@link denoExecutable} says Deno is here — the running executable under
50
66
  * `deno run`, the bare name for the OS to resolve on `PATH` when compiled —
51
- * and the rest is this package's own: the launchers' bootstrap directory, for
52
- * the compiled case where `PATH` may have no Deno at all.
67
+ * and the rest is this package's own: Deno's install directory, for a `zuke`
68
+ * that is not Deno itself (compiled, or on Node), where `PATH` may have no
69
+ * Deno at all.
53
70
  */
54
71
  export declare function denoCandidates(host?: DenoHost): string[];
55
72
  /**
@@ -72,12 +89,12 @@ export declare const QUIET_DENO_ENV: Readonly<Record<string, string>>;
72
89
  export declare class DenoNotFoundError extends Error {
73
90
  /** The error's name, as reported by `String(error)` and stack traces. */
74
91
  name: string;
75
- /** The executables that were spawned and not found, in the order tried. */
92
+ /** The executables that were looked for and not found, in the order tried. */
76
93
  readonly tried: readonly string[];
77
94
  /**
78
95
  * Build the failure from the candidates that were tried, in order.
79
96
  *
80
- * @param tried The executables that were spawned and not found.
97
+ * @param tried The executables that were looked for and not found.
81
98
  */
82
99
  constructor(tried: readonly string[]);
83
100
  }
@@ -110,6 +127,31 @@ export interface DenoSpawnOptions {
110
127
  * is pure and takes a {@link DenoHost} instead, so each layer is injected at
111
128
  * the level it decides something.
112
129
  *
113
- * @throws {DenoNotFoundError} when every candidate is missing.
130
+ * With `provision`, a Deno that none of the candidates is gets installed (see
131
+ * `runtime_install.ts`) and run.
132
+ *
133
+ * @throws {DenoNotFoundError} when every candidate is missing and none was
134
+ * installed.
135
+ */
136
+ export declare function spawnDeno(args: string[], options?: DenoSpawnOptions, candidates?: readonly string[], provision?: Provision): Promise<ChildProcess>;
137
+ /**
138
+ * Install the runtime none of the candidates was and return its executable,
139
+ * or throw `notFound` — the error the caller would otherwise see — when it
140
+ * may not be installed.
141
+ */
142
+ export type Provision = (notFound: Error) => Promise<string>;
143
+ /**
144
+ * Spawn the first of `candidates` that is installed with `args`, each with
145
+ * {@link QUIET_DENO_ENV} and its own directory first on `PATH`; the loop
146
+ * behind {@link spawnDeno}, shared with the Node a build file runs on. Each
147
+ * candidate is a bare name for the OS to find on `PATH` or an absolute path,
148
+ * as every candidate list here builds them: {@link pathWithDeno} refuses a
149
+ * relative one.
150
+ *
151
+ * When none is installed, `provision` (if given) installs one, which is then
152
+ * spawned.
153
+ *
154
+ * @throws the error `notFound` builds from every candidate tried, when none
155
+ * is installed and none was provisioned.
114
156
  */
115
- export declare function spawnDeno(args: string[], options?: DenoSpawnOptions, candidates?: readonly string[]): Promise<ChildProcess>;
157
+ export declare function spawnFirst(args: string[], options: DenoSpawnOptions, candidates: readonly string[], notFound: (tried: readonly string[]) => Error, provision?: Provision): Promise<ChildProcess>;
package/src/deno_path.js CHANGED
@@ -31,6 +31,7 @@
31
31
  */
32
32
  import { absolutePath, denoExecutable, EnvTasks, FileTasks, ProcessTasks, RuntimeTasks, } from "@zuke/core";
33
33
  import { DENO_INSTALL_DOCS } from "./launcher.js";
34
+ import { DENO_PIN } from "./deno_pin.js";
34
35
  /** The real, `Deno`-backed {@link DenoHost}. */
35
36
  export const defaultDenoHost = {
36
37
  standalone: () => RuntimeTasks.facts().standalone,
@@ -47,25 +48,34 @@ export function firstSet(host, ...names) {
47
48
  return undefined;
48
49
  }
49
50
  /**
50
- * The launchers' bootstrap directory: `DENO_INSTALL`, or `~/.deno`. `HOME` is
51
- * what the bash launcher reads; `USERPROFILE` is its Windows equivalent, where
52
- * `HOME` is often unset outside PowerShell. `undefined` when the environment
51
+ * Deno's install directory: `DENO_INSTALL`, or `~/.deno`, where Deno's own
52
+ * installer puts it and where `zuke` installs the pinned Deno (in `bin`).
53
+ * `USERPROFILE` stands in for `HOME` on Windows, where `HOME` is often unset
54
+ * outside PowerShell. `undefined` when the environment
53
55
  * names no directory the candidate can be built from — none of the three set,
54
56
  * or a value that is not an absolute path — which leaves `PATH` as the only
55
57
  * candidate rather than a crash on the way to spawning anything.
56
58
  */
57
- function installDir(host) {
58
- const explicit = firstSet(host, "DENO_INSTALL");
59
- const home = firstSet(host, "HOME", "USERPROFILE");
59
+ export function denoInstallDir(host) {
60
+ return installDirFrom(host, "DENO_INSTALL", ".deno");
61
+ }
62
+ /**
63
+ * The directory `variable` names, else `home` under the user's home
64
+ * (`HOME`, or `USERPROFILE` on Windows). `undefined` when neither is set, or
65
+ * the value is not an absolute path.
66
+ */
67
+ export function installDirFrom(host, variable, ...home) {
68
+ const explicit = firstSet(host, variable);
69
+ const user = firstSet(host, "HOME", "USERPROFILE");
60
70
  try {
61
71
  if (explicit !== undefined)
62
72
  return absolutePath(explicit);
63
- if (home !== undefined)
64
- return absolutePath(home)(".deno");
73
+ if (user !== undefined)
74
+ return absolutePath(user)(...home);
65
75
  }
66
76
  catch {
67
- // A relative `DENO_INSTALL` or `HOME` resolves against nothing meaningful
68
- // here, and `absolutePath` refuses it rather than guessing a base.
77
+ // A relative value resolves against nothing meaningful here, and
78
+ // `absolutePath` refuses it rather than guessing a base.
69
79
  }
70
80
  return undefined;
71
81
  }
@@ -73,14 +83,16 @@ function installDir(host) {
73
83
  * The Deno executables to try, in order. The first is whatever
74
84
  * {@link denoExecutable} says Deno is here — the running executable under
75
85
  * `deno run`, the bare name for the OS to resolve on `PATH` when compiled —
76
- * and the rest is this package's own: the launchers' bootstrap directory, for
77
- * the compiled case where `PATH` may have no Deno at all.
86
+ * and the rest is this package's own: Deno's install directory, for a `zuke`
87
+ * that is not Deno itself (compiled, or on Node), where `PATH` may have no
88
+ * Deno at all.
78
89
  */
79
90
  export function denoCandidates(host = defaultDenoHost) {
80
91
  const candidates = [denoExecutable(host.standalone())];
81
- if (!host.standalone())
92
+ // Under `deno run` the first candidate is the running Deno itself.
93
+ if (candidates[0] !== "deno")
82
94
  return candidates;
83
- const install = installDir(host);
95
+ const install = denoInstallDir(host);
84
96
  if (install !== undefined) {
85
97
  candidates.push(install("bin", host.windows() ? "deno.exe" : "deno").path);
86
98
  }
@@ -123,17 +135,17 @@ export const QUIET_DENO_ENV = {
123
135
  export class DenoNotFoundError extends Error {
124
136
  /** The error's name, as reported by `String(error)` and stack traces. */
125
137
  name = "DenoNotFoundError";
126
- /** The executables that were spawned and not found, in the order tried. */
138
+ /** The executables that were looked for and not found, in the order tried. */
127
139
  tried;
128
140
  /**
129
141
  * Build the failure from the candidates that were tried, in order.
130
142
  *
131
- * @param tried The executables that were spawned and not found.
143
+ * @param tried The executables that were looked for and not found.
132
144
  */
133
145
  constructor(tried) {
134
- super(`zuke: running a build needs Deno, and none was found — tried ` +
135
- `${tried.join(", ")}. Install Deno (${DENO_INSTALL_DOCS}), or run the ` +
136
- `project's own ./zuke launcher, which installs a pinned Deno itself.`);
146
+ super(`zuke: this needs Deno, and none was found — tried ` +
147
+ `${tried.join(", ")}. Install Deno (${DENO_INSTALL_DOCS}) and put it on PATH, or run ` +
148
+ `zuke again and let it install Deno ${DENO_PIN.version}.`);
137
149
  this.tried = tried;
138
150
  }
139
151
  }
@@ -161,12 +173,38 @@ function cwdUsable(cwd) {
161
173
  * is pure and takes a {@link DenoHost} instead, so each layer is injected at
162
174
  * the level it decides something.
163
175
  *
164
- * @throws {DenoNotFoundError} when every candidate is missing.
176
+ * With `provision`, a Deno that none of the candidates is gets installed (see
177
+ * `runtime_install.ts`) and run.
178
+ *
179
+ * @throws {DenoNotFoundError} when every candidate is missing and none was
180
+ * installed.
181
+ */
182
+ export function spawnDeno(args, options = {}, candidates = denoCandidates(), provision) {
183
+ return spawnFirst(args, options, candidates, (tried) => new DenoNotFoundError(tried), provision);
184
+ }
185
+ /**
186
+ * Spawn the first of `candidates` that is installed with `args`, each with
187
+ * {@link QUIET_DENO_ENV} and its own directory first on `PATH`; the loop
188
+ * behind {@link spawnDeno}, shared with the Node a build file runs on. Each
189
+ * candidate is a bare name for the OS to find on `PATH` or an absolute path,
190
+ * as every candidate list here builds them: {@link pathWithDeno} refuses a
191
+ * relative one.
192
+ *
193
+ * When none is installed, `provision` (if given) installs one, which is then
194
+ * spawned.
195
+ *
196
+ * @throws the error `notFound` builds from every candidate tried, when none
197
+ * is installed and none was provisioned.
165
198
  */
166
- export async function spawnDeno(args, options = {}, candidates = denoCandidates()) {
199
+ export async function spawnFirst(args, options, candidates, notFound, provision) {
167
200
  const tried = [];
168
201
  for (const command of candidates) {
169
202
  tried.push(command);
203
+ // A path that is not there is skipped before spawning: Windows reports
204
+ // spawning one as an error that is not a plain "not found".
205
+ const isPath = command.includes("/") || command.includes("\\");
206
+ if (isPath && !(await FileTasks.exists(command)))
207
+ continue;
170
208
  try {
171
209
  const child = ProcessTasks.spawn(command, (s) => {
172
210
  s.args(...args);
@@ -197,14 +235,17 @@ export async function spawnDeno(args, options = {}, candidates = denoCandidates(
197
235
  // Not installed here; the next candidate may be. Anything else — a
198
236
  // permission denial, a directory where the binary should be — is the
199
237
  // caller's to see rather than a reason to keep looking. `spawn` reports
200
- // a `cwd` that is gone as `NotFound` as well, and no other Deno would
201
- // fix that: telling the user to install one they already have would send
202
- // them somewhere the problem is not.
238
+ // a `cwd` that is gone as `NotFound` as well, and no other runtime would
239
+ // fix that: telling the user to install one they already have would
240
+ // send them somewhere the problem is not.
203
241
  if (!FileTasks.isNotFound(error))
204
242
  throw error;
205
243
  if (!cwdUsable(options.cwd))
206
244
  throw error;
207
245
  }
208
246
  }
209
- throw new DenoNotFoundError(tried);
247
+ const missing = notFound(tried);
248
+ if (provision === undefined)
249
+ throw missing;
250
+ return spawnFirst(args, options, [await provision(missing)], notFound);
210
251
  }