@zuke-build/cli 2.6.0 → 2.7.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/src/setup.js CHANGED
@@ -9,15 +9,16 @@
9
9
  * so the logic stays pure and unit-testable; {@link defaultHost} is the real
10
10
  * `Deno`-backed implementation used in production.
11
11
  */
12
- import { binaryMcpEntry, MCP_CONFIG_FILE, mcpServerEntry, mergeMcpConfig, parseMcpConfig, } from "./mcp_config.js";
13
- import { FileTasks } from "@zuke/core";
12
+ import { binaryMcpEntry, MCP_CONFIG_FILE, mcpServerEntry, mergeMcpConfig, nodeMcpEntry, parseMcpConfig, } from "./mcp_config.js";
13
+ import { FileTasks, RuntimeTasks } from "@zuke/core";
14
14
  import { isRecord } from "./records.js";
15
15
  import { assertBuildFileName, BUILD_FILE, buildFileNamed, NEW_BUILD_FILE, } from "./build_file.js";
16
16
  import { launcherBash, launcherPwsh } from "./launcher.js";
17
+ import { nodeLauncherBash, nodeLauncherPowerShell } from "./node_launcher.js";
17
18
  import { binaryLauncherBash, binaryLauncherPwsh } from "./binary_launcher.js";
18
19
  import { lstatOrNull } from "./fs.js";
19
20
  import { output } from "./output.js";
20
- import { PYTHON_BUILD_FILE, STARTER_IMPORTS, starterBuild, starterConfig, starterPythonBuild, starterSdkConfig, } from "./starter.js";
21
+ import { PYTHON_BUILD_FILE, STARTER_IMPORTS, starterBuild, starterConfig, starterPythonBuild, starterSdkConfig, zukeNpmDependency, } from "./starter.js";
21
22
  // Re-exported so the merge guard keeps its historical home for importers.
22
23
  export { isRecord };
23
24
  /**
@@ -221,6 +222,7 @@ const RESERVED_NON_LAUNCHER = new Set([
221
222
  PYTHON_BUILD_FILE,
222
223
  "zuke.json",
223
224
  "deno.json",
225
+ "package.json",
224
226
  ".gitignore",
225
227
  MCP_CONFIG_FILE,
226
228
  ]);
@@ -319,6 +321,11 @@ export async function runSetup(options, host = defaultHost) {
319
321
  const notes = [];
320
322
  const launcher = options.launcherName ?? DEFAULT_LAUNCHER;
321
323
  const sdk = options.sdk;
324
+ const node = options.runtime === "node";
325
+ if (sdk !== undefined && node) {
326
+ throw new Error("zuke setup: --runtime picks the runtime of a TypeScript build; " +
327
+ "--sdk python builds run on the zuke binary.");
328
+ }
322
329
  if (sdk !== undefined && options.buildFile !== undefined) {
323
330
  throw new Error("zuke setup: --build-file names a TypeScript build file; a Python " +
324
331
  `build is always ${PYTHON_BUILD_FILE}.`);
@@ -342,23 +349,27 @@ export async function runSetup(options, host = defaultHost) {
342
349
  },
343
350
  {
344
351
  name: launcher,
345
- content: sdk === undefined
346
- ? launcherBash(variant)
347
- : binaryLauncherBash({ download: variant.bootstrapDeno }, sdk.pin),
352
+ content: node
353
+ ? nodeLauncherBash({ buildFile })
354
+ : sdk === undefined
355
+ ? launcherBash(variant)
356
+ : binaryLauncherBash({ download: variant.bootstrapDeno }, sdk.pin),
348
357
  mode: 0o755,
349
358
  launcher: true,
350
359
  },
351
360
  {
352
361
  name: `${launcher}.ps1`,
353
- content: sdk === undefined
354
- ? launcherPwsh(variant)
355
- : binaryLauncherPwsh({ download: variant.bootstrapDeno }, sdk.pin),
362
+ content: node
363
+ ? nodeLauncherPowerShell({ buildFile })
364
+ : sdk === undefined
365
+ ? launcherPwsh(variant)
366
+ : binaryLauncherPwsh({ download: variant.bootstrapDeno }, sdk.pin),
356
367
  launcher: true,
357
368
  },
358
369
  {
359
370
  name: "zuke.json",
360
371
  content: sdk === undefined
361
- ? starterConfig(options.name, buildFile)
372
+ ? starterConfig(options.name, buildFile, options.runtime)
362
373
  : starterSdkConfig(options.name, sdk.pin.engine),
363
374
  },
364
375
  ];
@@ -368,7 +379,9 @@ export async function runSetup(options, host = defaultHost) {
368
379
  // setup writes, including the separately-written deno.json and .gitignore.
369
380
  const collisionTargets = [
370
381
  ...scaffold.map((s) => ({ name: s.name, launcher: s.launcher === true })),
371
- ...(sdk === undefined ? [{ name: "deno.json", launcher: false }] : []),
382
+ ...(sdk === undefined
383
+ ? [{ name: node ? "package.json" : "deno.json", launcher: false }]
384
+ : []),
372
385
  { name: ".gitignore", launcher: false },
373
386
  ...(options.mcp ? [{ name: MCP_CONFIG_FILE, launcher: false }] : []),
374
387
  ];
@@ -418,18 +431,23 @@ export async function runSetup(options, host = defaultHost) {
418
431
  host.log(` ${existed ? "update" : "create"} ${item.name}`);
419
432
  files.push({ path: item.name, status });
420
433
  }
421
- if (sdk === undefined) {
434
+ if (node) {
435
+ files.push(await setupPackageJson(options.dir, host, manualSteps, notes));
436
+ }
437
+ else if (sdk === undefined) {
422
438
  files.push(await setupDenoJson(options.dir, options.imports ?? STARTER_IMPORTS, buildFile, host, manualSteps, notes));
423
439
  }
424
- files.push(await setupGitignore(options.dir, host));
440
+ files.push(await setupGitignore(options.dir, host, node ? [GITIGNORE_ENTRY, "node_modules/"] : [GITIGNORE_ENTRY]));
425
441
  if (options.mcp) {
426
442
  files.push(await setupMcpConfig(options.dir, options.force, host, notes,
427
443
  // A hosted build is served by the zuke binary, whichever CLI
428
444
  // registered it: its project needs nothing from Deno's stack. A
429
445
  // TypeScript build stays on Deno.
430
- sdk === undefined
431
- ? mcpServerEntry(options.mcp.allowRun, buildFile)
432
- : binaryMcpEntry(options.mcp.allowRun)));
446
+ node
447
+ ? nodeMcpEntry(options.mcp.allowRun, buildFile)
448
+ : sdk === undefined
449
+ ? mcpServerEntry(options.mcp.allowRun, buildFile)
450
+ : binaryMcpEntry(options.mcp.allowRun)));
433
451
  }
434
452
  return { files, manualSteps, notes };
435
453
  }
@@ -470,24 +488,98 @@ async function setupMcpConfig(dir, force, host, notes, entry) {
470
488
  }
471
489
  /** The line `setup` ensures is present in `.gitignore` for generated output. */
472
490
  const GITIGNORE_ENTRY = ".zuke/";
473
- /** Create or update `.gitignore` so the generated `.zuke/` folder is ignored. */
474
- async function setupGitignore(dir, host) {
491
+ /**
492
+ * Create or update `.gitignore` so every one of `entries` is ignored, adding
493
+ * only the lines it does not already have.
494
+ */
495
+ async function setupGitignore(dir, host, entries) {
475
496
  const name = ".gitignore";
476
497
  const path = joinPath(dir, name);
477
- if (!(await host.exists(path))) {
478
- await host.writeText(path, `${GITIGNORE_ENTRY}\n`);
479
- host.log(` create ${name}`);
480
- return { path: name, status: "created" };
481
- }
482
- const before = await host.readText(path);
483
- if (before.split(/\r?\n/).some((line) => line.trim() === GITIGNORE_ENTRY)) {
484
- host.log(` skip ${name} (${GITIGNORE_ENTRY} already ignored)`);
498
+ const exists = await host.exists(path);
499
+ const before = exists ? await host.readText(path) : "";
500
+ const lines = before.split(/\r?\n/).map((line) => line.trim());
501
+ const missing = entries.filter((entry) => !lines.includes(entry));
502
+ if (missing.length === 0) {
503
+ host.log(` skip ${name} (${entries.join(", ")} already ignored)`);
485
504
  return { path: name, status: "skipped" };
486
505
  }
487
506
  const separator = before === "" || before.endsWith("\n") ? "" : "\n";
488
- await host.writeText(path, `${before}${separator}${GITIGNORE_ENTRY}\n`);
489
- host.log(` update ${name}`);
490
- return { path: name, status: "overwritten" };
507
+ await host.writeText(path, `${before}${separator}${missing.join("\n")}\n`);
508
+ host.log(` ${exists ? "update" : "create"} ${name}`);
509
+ return { path: name, status: exists ? "overwritten" : "created" };
510
+ }
511
+ /** The name a new `package.json` takes: the project directory's, as npm spells one. */
512
+ function packageNameFor(dir) {
513
+ const last = (dir === "." ? RuntimeTasks.cwd() : dir).split(/[\\/]/).filter(Boolean)
514
+ .at(-1) ?? "";
515
+ const name = last.toLowerCase().replace(/[^a-z0-9._-]+/g, "-")
516
+ .replace(/^[._-]+/, "");
517
+ return name === "" ? "zuke-build" : name;
518
+ }
519
+ /**
520
+ * Create or merge `package.json` for a Node project: `type: module` and the
521
+ * `@zuke/core` dependency, each only when absent. A different `type` is kept
522
+ * and noted, since a CommonJS package cannot load the starter build; a file
523
+ * that is not JSON is left alone and reported as a manual step.
524
+ */
525
+ async function setupPackageJson(dir, host, steps, notes) {
526
+ const name = "package.json";
527
+ const path = joinPath(dir, name);
528
+ const dependency = [
529
+ ["@zuke/core", zukeNpmDependency("core")],
530
+ ];
531
+ const exists = await host.exists(path);
532
+ const root = {};
533
+ let original = "";
534
+ if (exists) {
535
+ let parsed;
536
+ try {
537
+ parsed = JSON.parse(await host.readText(path));
538
+ }
539
+ catch {
540
+ parsed = undefined;
541
+ }
542
+ // Not JSON, or JSON that is not an object: either way not a manifest setup
543
+ // can merge into, so it is left exactly as it is.
544
+ if (!isRecord(parsed)) {
545
+ host.log(` skip ${name} (not a JSON object, edit by hand)`);
546
+ steps.push(`${path} is not a JSON object. Add "type": "module" and a dependency ` +
547
+ `"@zuke/core": "${dependency[0][1]}" — the scaffolded build will ` +
548
+ "not run until you do.");
549
+ return { path: name, status: "skipped" };
550
+ }
551
+ Object.assign(root, parsed);
552
+ original = JSON.stringify(root);
553
+ }
554
+ else {
555
+ root.name = packageNameFor(dir);
556
+ root.private = true;
557
+ }
558
+ if (root.type === undefined)
559
+ root.type = "module";
560
+ else if (root.type !== "module") {
561
+ notes.push(`${path} has "type": ${JSON.stringify(root.type)}; setup kept it, but ` +
562
+ 'the scaffolded build uses import syntax and needs "type": "module".');
563
+ }
564
+ const declared = root.dependencies;
565
+ const added = !(isRecord(declared) && dependency[0][0] in declared);
566
+ mergeMissing(root, "dependencies", dependency);
567
+ // The launcher runs npm ci, which refuses a lockfile that no longer matches
568
+ // package.json, so a dependency added beside an existing lock needs one
569
+ // npm install to record it.
570
+ const lock = joinPath(dir, "package-lock.json");
571
+ if (added && await host.exists(lock)) {
572
+ steps.push(`Run \`npm install --ignore-scripts\` in ${dir} once to add ` +
573
+ `${dependency[0][0]} to package-lock.json: ./zuke installs with ` +
574
+ "npm ci, which refuses a lockfile that does not match package.json.");
575
+ }
576
+ if (exists && JSON.stringify(root) === original) {
577
+ host.log(` skip ${name} (type and @zuke/core already declared)`);
578
+ return { path: name, status: "skipped" };
579
+ }
580
+ await host.writeText(path, `${JSON.stringify(root, null, 2)}\n`);
581
+ host.log(` ${exists ? "update" : "create"} ${name}`);
582
+ return { path: name, status: exists ? "overwritten" : "created" };
491
583
  }
492
584
  /** The `imports` entries `steps` tells the reader to add, as JSON lines. */
493
585
  function importLines(imports) {
package/src/starter.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type ProjectRuntime } from "./runtime_field.ts";
1
2
  /**
2
3
  * The `jsr:` dependency a scaffolded `deno.json` maps `@zuke/<name>` to.
3
4
  *
@@ -7,6 +8,13 @@
7
8
  * build and `zuke import`'s generated one cannot drift apart on it.
8
9
  */
9
10
  export declare function zukeDependency(name: string): string;
11
+ /**
12
+ * The `package.json` dependency a Node project maps `@zuke/<name>` to: the npm
13
+ * package every Zuke package is also published as, behind an alias so the build
14
+ * imports the same bare specifier. The caret major is the one
15
+ * {@link zukeDependency} pins.
16
+ */
17
+ export declare function zukeNpmDependency(name: string): string;
10
18
  /**
11
19
  * The `deno.json` import map entries the starter build resolves through.
12
20
  *
@@ -24,9 +32,10 @@ export declare function starterBuild(name: string): string;
24
32
  * The starter `zuke.json` config. Its presence at the repository root is what
25
33
  * `@zuke/core`'s `repoRoot()` walks up to find; the recorded `name` is the
26
34
  * build class for reference, and `build` names the build file when it is not
27
- * the default `zuke.ts`.
35
+ * the default `zuke.ts`. A `runtime` other than Deno is recorded as the
36
+ * `runtime` field.
28
37
  */
29
- export declare function starterConfig(name: string, buildFile: string): string;
38
+ export declare function starterConfig(name: string, buildFile: string, runtime?: ProjectRuntime): string;
30
39
  /** The build file `zuke setup --sdk python` writes, which `sdk.command` runs. */
31
40
  export declare const PYTHON_BUILD_FILE = "build.py";
32
41
  /**
package/src/starter.js CHANGED
@@ -11,6 +11,7 @@
11
11
  * @module
12
12
  */
13
13
  import { BUILD_FIELD, BUILD_FILE } from "./build_file.js";
14
+ import { RUNTIME_FIELD } from "./runtime_field.js";
14
15
  /**
15
16
  * The `jsr:` dependency a scaffolded `deno.json` maps `@zuke/<name>` to.
16
17
  *
@@ -22,6 +23,15 @@ import { BUILD_FIELD, BUILD_FILE } from "./build_file.js";
22
23
  export function zukeDependency(name) {
23
24
  return `jsr:@zuke/${name}@^1`;
24
25
  }
26
+ /**
27
+ * The `package.json` dependency a Node project maps `@zuke/<name>` to: the npm
28
+ * package every Zuke package is also published as, behind an alias so the build
29
+ * imports the same bare specifier. The caret major is the one
30
+ * {@link zukeDependency} pins.
31
+ */
32
+ export function zukeNpmDependency(name) {
33
+ return `npm:@zuke-build/${name}@^1`;
34
+ }
25
35
  /**
26
36
  * The `deno.json` import map entries the starter build resolves through.
27
37
  *
@@ -61,11 +71,13 @@ await run(${name});
61
71
  * The starter `zuke.json` config. Its presence at the repository root is what
62
72
  * `@zuke/core`'s `repoRoot()` walks up to find; the recorded `name` is the
63
73
  * build class for reference, and `build` names the build file when it is not
64
- * the default `zuke.ts`.
74
+ * the default `zuke.ts`. A `runtime` other than Deno is recorded as the
75
+ * `runtime` field.
65
76
  */
66
- export function starterConfig(name, buildFile) {
77
+ export function starterConfig(name, buildFile, runtime = "deno") {
67
78
  const build = buildFile === BUILD_FILE ? {} : { [BUILD_FIELD]: buildFile };
68
- return `${JSON.stringify({ name, ...build }, null, 2)}\n`;
79
+ const on = runtime === "deno" ? {} : { [RUNTIME_FIELD]: runtime };
80
+ return `${JSON.stringify({ name, ...build, ...on }, null, 2)}\n`;
69
81
  }
70
82
  /** The build file `zuke setup --sdk python` writes, which `sdk.command` runs. */
71
83
  export const PYTHON_BUILD_FILE = "build.py";
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.6.0"; // x-release-please-version
13
+ export const VERSION = "2.7.0"; // x-release-please-version