@zuke-build/cli 2.13.1 → 2.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,9 +59,18 @@ async function main(args: string[], host: SetupHost, prompter: Prompter, docRunn
59
59
  function parseImportFlags(args: string[]): ImportFlags
60
60
  Parse the argument list following `zuke import`.
61
61
 
62
+ @throws {Error}
63
+ on an argument `import` does not take — `setup`'s
64
+ `--launcher-name` and `--sdk` among them — a flag missing its value, or a
65
+ `--from` naming no source.
66
+
62
67
  function parseSetupFlags(args: string[]): SetupFlags
63
68
  Parse the argument list following `zuke setup`.
64
69
 
70
+ @throws {Error}
71
+ on an argument `setup` does not take, or a flag missing its
72
+ value.
73
+
65
74
  const EMBEDDED_ENGINE_ENV: "ZUKE_EMBEDDED_ENGINE"
66
75
  Set to `1` in the environment of the process that runs a hosted build on
67
76
  the binary's own engine: the compiled entry reads it, before the CLI does,
package/mod.d.ts CHANGED
@@ -71,7 +71,12 @@ export interface SetupFlags {
71
71
  */
72
72
  zuke?: string;
73
73
  }
74
- /** Parse the argument list following `zuke setup`. */
74
+ /**
75
+ * Parse the argument list following `zuke setup`.
76
+ *
77
+ * @throws {Error} on an argument `setup` does not take, or a flag missing its
78
+ * value.
79
+ */
75
80
  export declare function parseSetupFlags(args: string[]): SetupFlags;
76
81
  /** Flags accepted by `zuke import` — the setup flags plus `--from`. */
77
82
  export interface ImportFlags extends SetupFlags {
@@ -81,7 +86,13 @@ export interface ImportFlags extends SetupFlags {
81
86
  */
82
87
  from?: ImportSource;
83
88
  }
84
- /** Parse the argument list following `zuke import`. */
89
+ /**
90
+ * Parse the argument list following `zuke import`.
91
+ *
92
+ * @throws {Error} on an argument `import` does not take — `setup`'s
93
+ * `--launcher-name` and `--sdk` among them — a flag missing its value, or a
94
+ * `--from` naming no source.
95
+ */
85
96
  export declare function parseImportFlags(args: string[]): ImportFlags;
86
97
  /**
87
98
  * Runs `deno doc <args>` — the injectable subprocess seam for
package/mod.js CHANGED
@@ -24,6 +24,7 @@ import { defaultHost, existingBuildFile, existingRuntime, runSetup, unrelatedBui
24
24
  import { runImport } from "./src/import.js";
25
25
  import { defaultStarActions, promptStar, } from "./src/star.js";
26
26
  import { VERSION } from "./src/version.js";
27
+ import { asksForHelp, commandUsage, HELP, isOwnCommand, paintHelp, TAGLINE, } from "./src/help.js";
27
28
  import { installLabel, installSource, } from "./src/install_source.js";
28
29
  import { DENO_PIN } from "./src/deno_pin.js";
29
30
  import { buildLabel, defaultBuildProbe, defaultBuildRunner, DENO_SUPPORT_NOTICE, locateBuild, NO_LOCK_NOTICE, owesDenoSupportNotice, runBuild, runningNotice, servingEngine, } from "./src/dispatch.js";
@@ -62,17 +63,74 @@ export const defaultPrompter = {
62
63
  return RuntimeTasks.confirm(question);
63
64
  },
64
65
  };
65
- /** Parse the argument list following `zuke setup`. */
66
- export function parseSetupFlags(args) {
66
+ /** The flags that take a value, with the field each sets. */
67
+ const VALUE_FLAGS = new Map([
68
+ ["--name", "name"],
69
+ ["--build-file", "buildFile"],
70
+ ["--dir", "dir"],
71
+ ["--launcher-name", "launcherName"],
72
+ ["--sdk", "sdk"],
73
+ ["--runtime", "runtime"],
74
+ ["--zuke", "zuke"],
75
+ ]);
76
+ /** The value flags `zuke import` does not take: they shape `setup` only. */
77
+ const SETUP_ONLY_FLAGS = new Set([
78
+ "--launcher-name",
79
+ "--sdk",
80
+ ]);
81
+ /**
82
+ * Parse the arguments following `zuke setup` or `zuke import`.
83
+ *
84
+ * Strict, as `upgrade`'s parser is: both commands write into a directory, so
85
+ * an argument they do not take — a misspelt flag, a stray word, a flag with no
86
+ * value — must stop them rather than be skipped on the way to a scaffold. A
87
+ * value may not start with `-`, so `--name --force` is a missing name, not a
88
+ * class called `--force`; `--name=-x` still says it outright.
89
+ *
90
+ * @throws {Error} naming the argument the command does not take.
91
+ */
92
+ function parseScaffoldFlags(command, args) {
67
93
  const flags = {
68
94
  force: false,
69
95
  yes: false,
70
96
  mcp: false,
71
97
  allowRun: false,
72
98
  };
99
+ const refuse = (what) => new Error(`zuke ${command}: ${what}. See \`zuke ${command} --help\` for the ` +
100
+ "options it takes.");
73
101
  for (let i = 0; i < args.length; i++) {
74
102
  const arg = args[i];
75
- if (arg === "--force" || arg === "-f") {
103
+ const equals = arg.indexOf("=");
104
+ const flag = arg.startsWith("--") && equals > 0
105
+ ? arg.slice(0, equals)
106
+ : arg;
107
+ const inline = flag === arg ? undefined : arg.slice(equals + 1);
108
+ const field = VALUE_FLAGS.get(flag);
109
+ const takes = command === "setup" || !SETUP_ONLY_FLAGS.has(flag);
110
+ const isFrom = flag === "--from" && command === "import";
111
+ if ((field !== undefined && takes) || isFrom) {
112
+ const value = inline ?? args[i + 1];
113
+ if (value === undefined || value === "" ||
114
+ (inline === undefined && value.startsWith("-"))) {
115
+ throw refuse(`${flag} needs a value`);
116
+ }
117
+ if (inline === undefined)
118
+ i++;
119
+ if (field !== undefined) {
120
+ flags[field] = value;
121
+ continue;
122
+ }
123
+ const source = parseSource(value);
124
+ if (source === undefined) {
125
+ throw refuse(`unknown --from "${value}" — name package.json, makefile or ` +
126
+ "pyproject");
127
+ }
128
+ flags.from = source;
129
+ }
130
+ else if (inline !== undefined) {
131
+ throw refuse(`unknown option ${arg}`);
132
+ }
133
+ else if (arg === "--force" || arg === "-f") {
76
134
  flags.force = true;
77
135
  }
78
136
  else if (arg === "--yes" || arg === "-y") {
@@ -95,72 +153,24 @@ export function parseSetupFlags(args) {
95
153
  else if (arg === "--no-bootstrap" || arg === "--no-bootstrap-deno") {
96
154
  flags.bootstrapDeno = false;
97
155
  }
98
- else if (arg === "--name") {
99
- if (i + 1 < args.length) {
100
- i++;
101
- flags.name = args[i];
102
- }
103
- }
104
- else if (arg.startsWith("--name=")) {
105
- flags.name = arg.slice("--name=".length);
106
- }
107
- else if (arg === "--build-file") {
108
- if (i + 1 < args.length) {
109
- i++;
110
- flags.buildFile = args[i];
111
- }
112
- }
113
- else if (arg.startsWith("--build-file=")) {
114
- flags.buildFile = arg.slice("--build-file=".length);
115
- }
116
- else if (arg === "--dir") {
117
- if (i + 1 < args.length) {
118
- i++;
119
- flags.dir = args[i];
120
- }
121
- }
122
- else if (arg.startsWith("--dir=")) {
123
- flags.dir = arg.slice("--dir=".length);
124
- }
125
- else if (arg === "--launcher-name") {
126
- if (i + 1 < args.length) {
127
- i++;
128
- flags.launcherName = args[i];
129
- }
130
- }
131
- else if (arg.startsWith("--launcher-name=")) {
132
- flags.launcherName = arg.slice("--launcher-name=".length);
133
- }
134
- else if (arg === "--sdk") {
135
- if (i + 1 < args.length) {
136
- i++;
137
- flags.sdk = args[i];
138
- }
139
- }
140
- else if (arg.startsWith("--sdk=")) {
141
- flags.sdk = arg.slice("--sdk=".length);
156
+ else if (arg.startsWith("-")) {
157
+ throw refuse(`unknown option ${arg}`);
142
158
  }
143
- else if (arg === "--runtime") {
144
- if (i + 1 < args.length) {
145
- i++;
146
- flags.runtime = args[i];
147
- }
148
- }
149
- else if (arg.startsWith("--runtime=")) {
150
- flags.runtime = arg.slice("--runtime=".length);
151
- }
152
- else if (arg === "--zuke") {
153
- if (i + 1 < args.length) {
154
- i++;
155
- flags.zuke = args[i];
156
- }
157
- }
158
- else if (arg.startsWith("--zuke=")) {
159
- flags.zuke = arg.slice("--zuke=".length);
159
+ else {
160
+ throw refuse(`unexpected argument "${arg}"`);
160
161
  }
161
162
  }
162
163
  return flags;
163
164
  }
165
+ /**
166
+ * Parse the argument list following `zuke setup`.
167
+ *
168
+ * @throws {Error} on an argument `setup` does not take, or a flag missing its
169
+ * value.
170
+ */
171
+ export function parseSetupFlags(args) {
172
+ return parseScaffoldFlags("setup", args);
173
+ }
164
174
  /** Whether `value` names a runtime `--runtime` accepts. */
165
175
  function isRuntime(value) {
166
176
  return value === "deno" || value === "node";
@@ -270,8 +280,6 @@ function enginePinArgs(args) {
270
280
  }
271
281
  /** The environment that asks a spawned build for plain output. */
272
282
  const PLAIN_ENV = { ZUKE_PLAIN: "1" };
273
- /** The one-line identity printed under the logo and atop `--help`. */
274
- const TAGLINE = `zuke ${VERSION} — code-first build automation`;
275
283
  /**
276
284
  * Print the Zuke logo through `host`, so scaffolding opens with the brand.
277
285
  * Colour and log-level detection are `ConsoleTasks`'s (a TTY with `NO_COLOR`
@@ -288,100 +296,6 @@ function printBanner(host) {
288
296
  ConsoleTasks.reset();
289
297
  host.log("");
290
298
  }
291
- /**
292
- * The CLI's own half of the help: the commands that work anywhere, with no
293
- * project needed.
294
- *
295
- * Headed so a reader can tell it apart from the build's half, which is printed
296
- * after it when there is a project here. The two are different surfaces — this
297
- * one is the installed `zuke`, that one is whatever the build file declares — and
298
- * showing them unlabelled under one heading was what made them look like one
299
- * inconsistent command.
300
- */
301
- const HELP = `${TAGLINE}
302
-
303
- Usage:
304
- zuke <command> [options] A zuke command, anywhere (below)
305
- zuke <target> A target of the build in this directory
306
- zuke -- <args> Pass args straight to the build, unread
307
-
308
- Zuke commands (available anywhere):
309
- setup [options] Scaffold Zuke into a directory
310
- import [options] Generate a build from package.json scripts, a Makefile or pyproject.toml tasks
311
- doc <package> Show a @zuke/* package's API docs (runs deno doc, so needs Deno)
312
- upgrade [<version>] Reinstall a JSR-installed CLI at the latest (or given) release
313
- (npm: npm install -g @zuke-build/cli; Homebrew: brew upgrade zuke)
314
- upgrade --engine [<v>] Move a Python project's launchers to the latest (or given) zuke release
315
- add <package...> Add packages to this project's package.json, or deno.json on Deno
316
- (docker = @zuke/docker)
317
- remove <package...> Remove packages from this project's package.json, or deno.json on Deno
318
- cache [<entry>...] Fetch everything the build imports, keeping the lock
319
- relock [<entry>...] Regenerate this project's lock from scratch
320
- --help Show this help
321
- --version Show the CLI's version, and this project's build's
322
-
323
- Setup options:
324
- --dir <path> Directory to scaffold into (default: .)
325
- --name <Class> Build class name (default: MyBuild)
326
- --build-file <name> TypeScript build file name (default: build.ts, or the project's own)
327
- --launcher-name <name> Launcher base name when a zuke/ directory is in the way
328
- --runtime deno|node Runtime of a TypeScript build (default: node, or the project's own); node scaffolds a
329
- package.json and Node launchers, and converts a Deno project in place
330
- --sdk python Scaffold a build.py and launchers that run the zuke binary (no Deno)
331
- --zuke <version> The zuke release those launchers run (default: this one)
332
- --mcp Also write .mcp.json registering the build's MCP server
333
- --allow-run …with --allow-run, so an agent may execute targets (implies --mcp)
334
- --bootstrap Launchers download their pinned, checksum-verified runtime when missing (default):
335
- Node or Deno for a TypeScript build, the zuke binary for a Python one
336
- --no-bootstrap Launchers require that runtime on PATH and fail closed without it
337
- --force, -f Overwrite existing files
338
- --yes, -y Accept defaults without prompting
339
-
340
- Import options:
341
- --dir <path> Directory to read from and scaffold into (default: .)
342
- --name <Class> Build class name (default: MyBuild)
343
- --from <source> Force a source: package.json, makefile or pyproject (default: auto-detect)
344
- --zuke <version> The zuke release a pyproject.toml import's launchers run (default: this one)
345
- --runtime deno|node Runtime of a generated build file (default: node, or the project's own)
346
- --mcp Also write .mcp.json registering the build's MCP server
347
- --allow-run …with --allow-run, so an agent may execute targets (implies --mcp)
348
- --bootstrap Launchers download their pinned, checksum-verified runtime when missing (default):
349
- Node or Deno for a TypeScript build, the zuke binary for a Python one
350
- --no-bootstrap Launchers require that runtime on PATH and fail closed without it
351
- --force, -f Overwrite existing files
352
- --yes, -y Accept defaults without prompting
353
-
354
- Upgrade options:
355
- --dry-run Say what would be installed, and install nothing
356
- --force, -f Reinstall even when already on that version
357
- --engine Move a Python project's launchers and engine pin instead (takes --dry-run)
358
-
359
- Cache and relock options:
360
- --min-dep-age <age> Only resolve releases at least this old (0 for the newest)
361
- --dry-run Say what would run, and change nothing
362
-
363
- Doc:
364
- zuke doc core API of @zuke/core
365
- zuke doc @scope/pkg API of a scoped package (or pass jsr:/npm:/https: as-is)`;
366
- /**
367
- * {@link HELP} as `paint` renders it: headings marked, each row's command or
368
- * flag painted, every word in place. Plain paint returns it unchanged.
369
- */
370
- function helpText(paint) {
371
- if (!paint.rich)
372
- return HELP;
373
- return HELP.split("\n").map((line) => {
374
- if (line === TAGLINE)
375
- return paint.target(line);
376
- if (/^\S.*:$/.test(line))
377
- return paint.heading(line);
378
- const row = /^( {2})(\S.*?)( {2,})(.*)$/.exec(line);
379
- if (row === null)
380
- return line;
381
- const [, indent, label, gap, description] = row;
382
- return `${indent}${paint.command(label)}${gap}${description}`;
383
- }).join("\n");
384
- }
385
299
  /**
386
300
  * The heading that introduces the build's own half of the merged help: a rule,
387
301
  * then a line naming the build. Painted as a section heading when output is
@@ -540,19 +454,15 @@ function parseSource(value) {
540
454
  }
541
455
  return undefined;
542
456
  }
543
- /** Parse the argument list following `zuke import`. */
457
+ /**
458
+ * Parse the argument list following `zuke import`.
459
+ *
460
+ * @throws {Error} on an argument `import` does not take — `setup`'s
461
+ * `--launcher-name` and `--sdk` among them — a flag missing its value, or a
462
+ * `--from` naming no source.
463
+ */
544
464
  export function parseImportFlags(args) {
545
- const flags = parseSetupFlags(args);
546
- for (let i = 0; i < args.length; i++) {
547
- const arg = args[i];
548
- if (arg === "--from" && i + 1 < args.length) {
549
- flags.from = parseSource(args[++i]);
550
- }
551
- else if (arg.startsWith("--from=")) {
552
- flags.from = parseSource(arg.slice("--from=".length));
553
- }
554
- }
555
- return flags;
465
+ return parseScaffoldFlags("import", args);
556
466
  }
557
467
  /** Run the `import` subcommand. */
558
468
  async function commandImport(args, host, prompter, paint, upgradeHost) {
@@ -690,7 +600,7 @@ async function commandHelp(host, probe, runner, paint, childEnv, engine) {
690
600
  if (paint.rich && paint.banner) {
691
601
  host.log(`${logoLines(paint.color).join("\n")}\n`);
692
602
  }
693
- host.log(helpText(paint));
603
+ host.log(paintHelp(HELP, paint));
694
604
  let location = null;
695
605
  try {
696
606
  location = await locateBuild(RuntimeTasks.cwd(), probe);
@@ -879,6 +789,12 @@ export async function main(args, host = defaultHost, prompter = defaultPrompter,
879
789
  if (command === "--version" || command === "-V") {
880
790
  return await commandVersion(host, buildProbe, buildRunner, paint, upgradeHost, engine);
881
791
  }
792
+ // A command's own help is answered before the command is: `--help` read as
793
+ // one of its flags is how `zuke import --help` came to run an import.
794
+ if (isOwnCommand(command) && asksForHelp(rest)) {
795
+ host.log(paintHelp(commandUsage(command), paint));
796
+ return 0;
797
+ }
882
798
  try {
883
799
  if (command === "setup") {
884
800
  return await commandSetup(rest, host, prompter, starActions, paint, upgradeHost);
@@ -928,12 +844,12 @@ export async function main(args, host = defaultHost, prompter = defaultPrompter,
928
844
  if (forwarded !== null)
929
845
  return forwarded;
930
846
  if (own.length === 0) {
931
- host.log(helpText(paint));
847
+ host.log(paintHelp(HELP, paint));
932
848
  return 0;
933
849
  }
934
850
  host.log(`Unknown command: ${command} — and no zuke.json was found in the current ` +
935
851
  `directory or any parent to forward it to.\n`);
936
- host.log(helpText(paint));
852
+ host.log(paintHelp(HELP, paint));
937
853
  return 1;
938
854
  }
939
855
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zuke-build/cli",
3
- "version": "2.13.1",
3
+ "version": "2.14.1",
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",
package/src/help.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The global CLI's help: `zuke --help`, which lists every command, and
3
+ * `zuke <command> --help`, which shows one of them.
4
+ *
5
+ * Both are rendered from one table, so a command's row and its options read
6
+ * the same wherever they are shown. A per-command help used to be missing
7
+ * altogether: `--help` after a command the CLI keeps for itself was read as
8
+ * one of that command's flags, and `setup` and `import` skipped what they did
9
+ * not recognise and went on to scaffold — so `zuke import --help`, typed to
10
+ * read the flags, wrote a build into whatever directory it was typed in.
11
+ *
12
+ * @module
13
+ */
14
+ import type { CliPaint } from "./paint.ts";
15
+ /** The one-line identity printed under the logo and atop `--help`. */
16
+ export declare const TAGLINE: string;
17
+ /** A command the global CLI answers itself, rather than forwarding to the build. */
18
+ export type OwnCommand = "setup" | "import" | "doc" | "upgrade" | "add" | "remove" | "cache" | "relock";
19
+ /**
20
+ * The CLI's own half of the help: the commands that work anywhere, with no
21
+ * project needed.
22
+ *
23
+ * Headed so a reader can tell it apart from the build's half, which is printed
24
+ * after it when there is a project here. The two are different surfaces — this
25
+ * one is the installed `zuke`, that one is whatever the build file declares — and
26
+ * showing them unlabelled under one heading was what made them look like one
27
+ * inconsistent command.
28
+ */
29
+ export declare const HELP: string;
30
+ /**
31
+ * `zuke <command> --help`: the command's usage, then the options it takes.
32
+ */
33
+ export declare function commandUsage(command: OwnCommand): string;
34
+ /** Whether `value` names a command the CLI answers itself. */
35
+ export declare function isOwnCommand(value: string | undefined): value is OwnCommand;
36
+ /**
37
+ * Whether a command's arguments ask for its help. Anywhere in them, not only
38
+ * first: `zuke import --dir app --help` is someone reading the flags as they
39
+ * type them, and must not run the import.
40
+ */
41
+ export declare function asksForHelp(args: readonly string[]): boolean;
42
+ /**
43
+ * `text` — {@link HELP} or a {@link commandUsage} — as `paint` renders it:
44
+ * headings marked, each row's command or flag painted, every word in place.
45
+ * Plain paint returns it unchanged.
46
+ */
47
+ export declare function paintHelp(text: string, paint: CliPaint): string;
package/src/help.js ADDED
@@ -0,0 +1,282 @@
1
+ // Copyright (c) 2026 the Zuke contributors
2
+ // SPDX-License-Identifier: MIT
3
+ import { VERSION } from "./version.js";
4
+ /** The one-line identity printed under the logo and atop `--help`. */
5
+ export const TAGLINE = `zuke ${VERSION} — code-first build automation`;
6
+ const BOOTSTRAP_ROWS = [
7
+ [
8
+ "--bootstrap",
9
+ "Launchers download their pinned, checksum-verified runtime when missing (default):",
10
+ "Node or Deno for a TypeScript build, the zuke binary for a Python one",
11
+ ],
12
+ [
13
+ "--no-bootstrap",
14
+ "Launchers require that runtime on PATH and fail closed without it",
15
+ ],
16
+ ];
17
+ const MCP_ROWS = [
18
+ ["--mcp", "Also write .mcp.json registering the build's MCP server"],
19
+ [
20
+ "--allow-run",
21
+ "…with --allow-run, so an agent may execute targets (implies --mcp)",
22
+ ],
23
+ ];
24
+ const OVERWRITE_ROWS = [
25
+ ["--force, -f", "Overwrite existing files"],
26
+ ["--yes, -y", "Accept defaults without prompting"],
27
+ ];
28
+ const SETUP_OPTIONS = {
29
+ title: "Setup options",
30
+ rows: [
31
+ ["--dir <path>", "Directory to scaffold into (default: .)"],
32
+ ["--name <Class>", "Build class name (default: MyBuild)"],
33
+ [
34
+ "--build-file <name>",
35
+ "TypeScript build file name (default: build.ts, or the project's own)",
36
+ ],
37
+ [
38
+ "--launcher-name <name>",
39
+ "Launcher base name when a zuke/ directory is in the way",
40
+ ],
41
+ [
42
+ "--runtime deno|node",
43
+ "Runtime of a TypeScript build (default: node, or the project's own); node scaffolds a",
44
+ "package.json and Node launchers, and converts a Deno project in place",
45
+ ],
46
+ [
47
+ "--sdk python",
48
+ "Scaffold a build.py and launchers that run the zuke binary (no Deno)",
49
+ ],
50
+ [
51
+ "--zuke <version>",
52
+ "The zuke release those launchers run (default: this one)",
53
+ ],
54
+ ...MCP_ROWS,
55
+ ...BOOTSTRAP_ROWS,
56
+ ...OVERWRITE_ROWS,
57
+ ],
58
+ };
59
+ const IMPORT_OPTIONS = {
60
+ title: "Import options",
61
+ rows: [
62
+ [
63
+ "--dir <path>",
64
+ "Directory to read from and scaffold into (default: .)",
65
+ ],
66
+ ["--name <Class>", "Build class name (default: MyBuild)"],
67
+ [
68
+ "--build-file <name>",
69
+ "TypeScript build file name (default: build.ts, or the project's own)",
70
+ ],
71
+ [
72
+ "--from <source>",
73
+ "Force a source: package.json, makefile or pyproject (default: auto-detect)",
74
+ ],
75
+ [
76
+ "--zuke <version>",
77
+ "The zuke release a pyproject.toml import's launchers run (default: this one)",
78
+ ],
79
+ [
80
+ "--runtime deno|node",
81
+ "Runtime of a generated build file (default: node, or the project's own)",
82
+ ],
83
+ ...MCP_ROWS,
84
+ ...BOOTSTRAP_ROWS,
85
+ ...OVERWRITE_ROWS,
86
+ ],
87
+ };
88
+ const UPGRADE_OPTIONS = {
89
+ title: "Upgrade options",
90
+ rows: [
91
+ ["--dry-run", "Say what would be installed, and install nothing"],
92
+ ["--force, -f", "Reinstall even when already on that version"],
93
+ [
94
+ "--engine",
95
+ "Move a Python project's launchers and engine pin instead (takes --dry-run)",
96
+ ],
97
+ ],
98
+ };
99
+ const DEPENDENCY_OPTIONS = {
100
+ title: "Add and remove options",
101
+ rows: [["--dry-run", "Say what would run, and change nothing"]],
102
+ };
103
+ const INSTALL_OPTIONS = {
104
+ title: "Cache and relock options",
105
+ rows: [
106
+ [
107
+ "--min-dep-age <age>",
108
+ "Only resolve releases at least this old (0 for the newest)",
109
+ ],
110
+ ["--dry-run", "Say what would run, and change nothing"],
111
+ ],
112
+ };
113
+ const DOC_EXAMPLES = {
114
+ title: "Doc",
115
+ rows: [
116
+ ["zuke doc core", "API of @zuke/core"],
117
+ [
118
+ "zuke doc @scope/pkg",
119
+ "API of a scoped package (or pass jsr:/npm:/https: as-is)",
120
+ ],
121
+ ],
122
+ };
123
+ /** What each command is typed as and does, and the options it takes. */
124
+ const COMMANDS = {
125
+ setup: {
126
+ rows: [["setup [options]", "Scaffold Zuke into a directory"]],
127
+ sections: [SETUP_OPTIONS],
128
+ },
129
+ import: {
130
+ rows: [[
131
+ "import [options]",
132
+ "Generate a build from package.json scripts, a Makefile or pyproject.toml tasks",
133
+ ]],
134
+ sections: [IMPORT_OPTIONS],
135
+ },
136
+ doc: {
137
+ rows: [[
138
+ "doc <package>",
139
+ "Show a @zuke/* package's API docs (runs deno doc, so needs Deno)",
140
+ ]],
141
+ sections: [DOC_EXAMPLES],
142
+ },
143
+ upgrade: {
144
+ rows: [
145
+ [
146
+ "upgrade [<version>]",
147
+ "Reinstall a JSR-installed CLI at the latest (or given) release",
148
+ "(npm: npm install -g @zuke-build/cli; Homebrew: brew upgrade zuke)",
149
+ ],
150
+ [
151
+ "upgrade --engine [<v>]",
152
+ "Move a Python project's launchers to the latest (or given) zuke release",
153
+ ],
154
+ ],
155
+ sections: [UPGRADE_OPTIONS],
156
+ },
157
+ add: {
158
+ rows: [[
159
+ "add <package...>",
160
+ "Add packages to this project's package.json, or deno.json on Deno",
161
+ "(docker = @zuke/docker)",
162
+ ]],
163
+ sections: [DEPENDENCY_OPTIONS],
164
+ },
165
+ remove: {
166
+ rows: [[
167
+ "remove <package...>",
168
+ "Remove packages from this project's package.json, or deno.json on Deno",
169
+ ]],
170
+ sections: [DEPENDENCY_OPTIONS],
171
+ },
172
+ cache: {
173
+ rows: [[
174
+ "cache [<entry>...]",
175
+ "Fetch everything the build imports, keeping the lock",
176
+ ]],
177
+ sections: [INSTALL_OPTIONS],
178
+ },
179
+ relock: {
180
+ rows: [[
181
+ "relock [<entry>...]",
182
+ "Regenerate this project's lock from scratch",
183
+ ]],
184
+ sections: [INSTALL_OPTIONS],
185
+ },
186
+ };
187
+ /** The column a row's description starts in, past the two-space indent. */
188
+ const LABEL_WIDTH = 22;
189
+ /** `rows` as indented, aligned lines: each label, then its description. */
190
+ function renderRows(rows) {
191
+ const width = Math.max(LABEL_WIDTH, ...rows.map(([label]) => label.length));
192
+ return rows.flatMap(([label, description, ...more]) => [
193
+ ` ${label.padEnd(width)} ${description}`,
194
+ ...more.map((line) => ` ${"".padEnd(width)} ${line}`),
195
+ ]);
196
+ }
197
+ /** `section` under its title. */
198
+ function renderSection(section) {
199
+ return [`${section.title}:`, ...renderRows(section.rows)].join("\n");
200
+ }
201
+ /** The option sections `zuke --help` shows, every command's among them. */
202
+ const HELP_SECTIONS = [
203
+ SETUP_OPTIONS,
204
+ IMPORT_OPTIONS,
205
+ UPGRADE_OPTIONS,
206
+ DEPENDENCY_OPTIONS,
207
+ INSTALL_OPTIONS,
208
+ DOC_EXAMPLES,
209
+ ];
210
+ /**
211
+ * The CLI's own half of the help: the commands that work anywhere, with no
212
+ * project needed.
213
+ *
214
+ * Headed so a reader can tell it apart from the build's half, which is printed
215
+ * after it when there is a project here. The two are different surfaces — this
216
+ * one is the installed `zuke`, that one is whatever the build file declares — and
217
+ * showing them unlabelled under one heading was what made them look like one
218
+ * inconsistent command.
219
+ */
220
+ export const HELP = [
221
+ TAGLINE,
222
+ "",
223
+ "Usage:",
224
+ ...renderRows([
225
+ ["zuke <command> [options]", "A zuke command, anywhere (below)"],
226
+ ["zuke <target>", "A target of the build in this directory"],
227
+ ["zuke -- <args>", "Pass args straight to the build, unread"],
228
+ ]),
229
+ "",
230
+ "Zuke commands (available anywhere):",
231
+ ...renderRows([
232
+ ...Object.values(COMMANDS).flatMap(({ rows }) => rows),
233
+ ["--help", "Show this help (after a command: that command's help)"],
234
+ ["--version", "Show the CLI's version, and this project's build's"],
235
+ ]),
236
+ ...HELP_SECTIONS.flatMap((section) => ["", renderSection(section)]),
237
+ ].join("\n");
238
+ /**
239
+ * `zuke <command> --help`: the command's usage, then the options it takes.
240
+ */
241
+ export function commandUsage(command) {
242
+ const { rows, sections } = COMMANDS[command];
243
+ return [
244
+ TAGLINE,
245
+ "",
246
+ "Usage:",
247
+ ...renderRows(rows.map(([label, ...rest]) => [`zuke ${label}`, ...rest])),
248
+ ...sections.flatMap((section) => ["", renderSection(section)]),
249
+ ].join("\n");
250
+ }
251
+ /** Whether `value` names a command the CLI answers itself. */
252
+ export function isOwnCommand(value) {
253
+ return value !== undefined && Object.hasOwn(COMMANDS, value);
254
+ }
255
+ /**
256
+ * Whether a command's arguments ask for its help. Anywhere in them, not only
257
+ * first: `zuke import --dir app --help` is someone reading the flags as they
258
+ * type them, and must not run the import.
259
+ */
260
+ export function asksForHelp(args) {
261
+ return args.some((arg) => arg === "--help" || arg === "-h");
262
+ }
263
+ /**
264
+ * `text` — {@link HELP} or a {@link commandUsage} — as `paint` renders it:
265
+ * headings marked, each row's command or flag painted, every word in place.
266
+ * Plain paint returns it unchanged.
267
+ */
268
+ export function paintHelp(text, paint) {
269
+ if (!paint.rich)
270
+ return text;
271
+ return text.split("\n").map((line) => {
272
+ if (line === TAGLINE)
273
+ return paint.target(line);
274
+ if (/^\S.*:$/.test(line))
275
+ return paint.heading(line);
276
+ const row = /^( {2})(\S.*?)( {2,})(.*)$/.exec(line);
277
+ if (row === null)
278
+ return line;
279
+ const [, indent, label, gap, description] = row;
280
+ return `${indent}${paint.command(label)}${gap}${description}`;
281
+ }).join("\n");
282
+ }
package/src/import.js CHANGED
@@ -138,7 +138,7 @@ function stripRecipePrefix(recipe) {
138
138
  * appears only where it changes something: the wrapper's own resolution is not
139
139
  * repeated, and a command with no arguments takes no `.args()`.
140
140
  */
141
- function runCall(argv, resolution) {
141
+ function runCall(argv, resolution, stdout) {
142
142
  const match = wrapperFor(argv);
143
143
  const [bin, ...args] = argv;
144
144
  const [callee, own, rest, head] = match === undefined
@@ -155,6 +155,10 @@ function runCall(argv, resolution) {
155
155
  }
156
156
  if (rest.length > 0)
157
157
  settings.push(`args(${rest.map(literal).join(", ")})`);
158
+ if (stdout !== undefined) {
159
+ const append = stdout.append ? ", (w) => w.append()" : "";
160
+ settings.push(`stdoutToFile(${literal(stdout.path)}${append})`);
161
+ }
158
162
  const params = settings.length === 0
159
163
  ? head
160
164
  : [...head, `(s) => s.${settings.join(".")}`];
@@ -175,13 +179,19 @@ function runCall(argv, resolution) {
175
179
  * comment preserving the raw text when it is too shell-specific to translate.
176
180
  */
177
181
  export function translateCommand(command, resolution) {
178
- return segments(command).map((segment) => "raw" in segment
179
- ? {
182
+ return segments(command).map((segment) => {
183
+ if ("argv" in segment)
184
+ return runCall(segment.argv, resolution);
185
+ if ("redirect" in segment) {
186
+ const { argv, path, append } = segment.redirect;
187
+ return runCall(argv, resolution, { path, append });
188
+ }
189
+ return {
180
190
  code: `// TODO: translate this shell command: ${oneLine(segment.raw)}`,
181
191
  runnable: false,
182
192
  uses: [],
183
- }
184
- : runCall(segment.argv, resolution));
193
+ };
194
+ });
185
195
  }
186
196
  // --- identifiers ----------------------------------------------------------
187
197
  /**
@@ -145,11 +145,12 @@ function bodyLines(steps) {
145
145
  run(step.argv);
146
146
  else {
147
147
  for (const segment of segments(step.command)) {
148
- if ("raw" in segment) {
149
- todo(`translate this shell command: ${segment.raw}`);
150
- }
151
- else
148
+ if ("argv" in segment)
152
149
  run(segment.argv);
150
+ else {
151
+ const raw = "raw" in segment ? segment.raw : segment.redirect.raw;
152
+ todo(`translate this shell command: ${raw}`);
153
+ }
153
154
  }
154
155
  }
155
156
  }
@@ -1,9 +1,25 @@
1
- /** One `&&`-separated segment of a command: argv to run, or text to keep verbatim. */
1
+ /**
2
+ * One `&&`-separated segment of a command: argv to run, argv whose stdout the
3
+ * shell sends to a file, or text to keep verbatim.
4
+ */
2
5
  export type Segment = {
3
6
  readonly argv: readonly string[];
7
+ } | {
8
+ readonly redirect: Redirect;
4
9
  } | {
5
10
  readonly raw: string;
6
11
  };
12
+ /** A segment of the form `cmd args > file`, or `>> file` to append. */
13
+ export interface Redirect {
14
+ /** The command, as argv. */
15
+ readonly argv: readonly string[];
16
+ /** The file its stdout goes to, as written. */
17
+ readonly path: string;
18
+ /** Whether stdout is added to the file (`>>`) rather than replacing it. */
19
+ readonly append: boolean;
20
+ /** The segment as written, for a generator that cannot express it. */
21
+ readonly raw: string;
22
+ }
7
23
  /** Whether `segment` uses a shell feature a single argv cannot faithfully express. */
8
24
  export declare function needsShell(segment: string): boolean;
9
25
  /**
package/src/translate.js CHANGED
@@ -74,11 +74,50 @@ export function segments(command) {
74
74
  const segment = raw.trim();
75
75
  if (segment === "")
76
76
  continue;
77
- const argv = needsShell(segment) ? undefined : tokenize(segment);
78
- out.push(argv === undefined ? { raw: segment } : { argv });
77
+ out.push(parseSegment(segment));
79
78
  }
80
79
  return out;
81
80
  }
81
+ /** One non-blank segment, as {@link segments} classifies it. */
82
+ function parseSegment(segment) {
83
+ if (!needsShell(segment)) {
84
+ const argv = tokenize(segment);
85
+ return argv === undefined ? { raw: segment } : { argv };
86
+ }
87
+ const redirect = redirected(segment);
88
+ return redirect === undefined ? { raw: segment } : { redirect };
89
+ }
90
+ /**
91
+ * `segment` as a command whose stdout goes to a file, or `undefined` unless
92
+ * that is all the shell does with it: exactly one unquoted `>` or `>>`, after
93
+ * whitespace (so neither `2>` nor `&>`), followed by a single plain word that
94
+ * is the last thing on the line, with a command before it that needs no shell
95
+ * itself. A target the shell would expand (`$`, `~`, a glob), an fd target
96
+ * (`>&2`), a clobber (`>|`) or a parenthesis anywhere (a subshell to `sh`)
97
+ * leaves the segment verbatim.
98
+ */
99
+ function redirected(segment) {
100
+ const at = unquotedIndexes(segment, ">");
101
+ const append = at.length === 2 && at[1] === at[0] + 1;
102
+ if (at.length !== 1 && !append)
103
+ return undefined;
104
+ const start = at[0];
105
+ if (start === 0 || !/\s/.test(segment[start - 1]))
106
+ return undefined;
107
+ const command = segment.slice(0, start);
108
+ const target = segment.slice(at[at.length - 1] + 1);
109
+ if (needsShell(command) || needsShell(target))
110
+ return undefined;
111
+ if (/[~*?[{&]/.test(target) || /[()]/.test(segment))
112
+ return undefined;
113
+ const argv = tokenize(command);
114
+ const path = tokenize(target);
115
+ if (argv === undefined)
116
+ return undefined;
117
+ if (path === undefined || path.length !== 1)
118
+ return undefined;
119
+ return { argv, path: path[0], append, raw: segment };
120
+ }
82
121
  /** Split a command on top-level `&&`, ignoring `&&` inside quotes. */
83
122
  export function splitChain(command) {
84
123
  const parts = [];
package/src/version.js CHANGED
@@ -10,4 +10,4 @@
10
10
  * reads it — the annotation is what keeps that from mattering if it is ever
11
11
  * exported.
12
12
  */
13
- export const VERSION = "2.13.1"; // x-release-please-version
13
+ export const VERSION = "2.14.1"; // x-release-please-version