@zuke-build/cli 2.7.0 → 2.9.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/project.d.ts CHANGED
@@ -18,10 +18,15 @@ export interface ProjectHost {
18
18
  remove(path: string): Promise<void>;
19
19
  /** Run `deno <denoArgs>` in `cwd`, returning its exit code. */
20
20
  deno(denoArgs: string[], cwd: string): Promise<number>;
21
+ /**
22
+ * Run `npm <npmArgs>` in `cwd`, a Node project's root, returning its exit
23
+ * code.
24
+ */
25
+ npm(npmArgs: string[], cwd: string): Promise<number>;
21
26
  /** The version of the Deno that runs the command. */
22
27
  denoVersion(): string;
23
28
  }
24
- /** The real {@link ProjectHost}: the file system, and `deno` in the root. */
29
+ /** The real {@link ProjectHost}: the file system, and `deno` or `npm` in the root. */
25
30
  export declare const defaultProjectHost: ProjectHost;
26
31
  /**
27
32
  * The project at or above `cwd`, through the trust gate: its root, and the
@@ -67,3 +72,11 @@ export declare function parseInstallFlags(command: string, args: readonly string
67
72
  * `denoVersion` does not accept the flag.
68
73
  */
69
74
  export declare function installArgs(command: string, flags: InstallFlags, denoVersion: string, build: string): string[];
75
+ /**
76
+ * Refuse what `flags` asks of npm that it cannot do: a Node project's
77
+ * dependencies are its `package.json`'s, so there are no further entrypoints
78
+ * to resolve, and `--min-dep-age` is Deno's.
79
+ *
80
+ * @throws {Error} naming `command` when either was given.
81
+ */
82
+ export declare function nodeInstallFlags(command: string, flags: InstallFlags): void;
package/src/project.js CHANGED
@@ -2,8 +2,9 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  /**
4
4
  * What the global CLI's project commands share: `relock`, `cache`, `add` and
5
- * `remove` each run a `deno` command in the project root, so that working on
6
- * a Zuke project needs only `zuke`.
5
+ * `remove` each run a `deno` command in the project root — or an `npm` one,
6
+ * in a project whose build runs on Node — so that working on a Zuke project
7
+ * needs only `zuke`.
7
8
  *
8
9
  * They are the CLI's own rather than the build's because they must work
9
10
  * whatever core the project is on — including one too old, or too broken, to
@@ -14,9 +15,10 @@
14
15
  */
15
16
  import { FileTasks, RuntimeTasks } from "@zuke/core";
16
17
  import { spawnDeno } from "./deno_path.js";
18
+ import { runNpm } from "./npm_path.js";
17
19
  import { locateBuild, } from "./dispatch.js";
18
20
  import { acceptsMinimumDependencyAge } from "./upgrade.js";
19
- /** The real {@link ProjectHost}: the file system, and `deno` in the root. */
21
+ /** The real {@link ProjectHost}: the file system, and `deno` or `npm` in the root. */
20
22
  export const defaultProjectHost = {
21
23
  entry: async (path) => {
22
24
  try {
@@ -34,7 +36,7 @@ export const defaultProjectHost = {
34
36
  await FileTasks.remove(path);
35
37
  },
36
38
  deno: async (denoArgs, cwd) => {
37
- const child = spawnDeno(denoArgs, {
39
+ const child = await spawnDeno(denoArgs, {
38
40
  cwd,
39
41
  stdin: "null",
40
42
  stdout: "inherit",
@@ -42,6 +44,7 @@ export const defaultProjectHost = {
42
44
  });
43
45
  return (await child.status).code;
44
46
  },
47
+ npm: (npmArgs, cwd) => runNpm(npmArgs, cwd),
45
48
  // The cli runs on Deno, so the runtime is the Deno that runs the command.
46
49
  denoVersion: () => RuntimeTasks.facts().version,
47
50
  };
@@ -130,3 +133,17 @@ export function installArgs(command, flags, denoVersion, build) {
130
133
  }
131
134
  return [...args, `--minimum-dependency-age=${flags.minDepAge}`];
132
135
  }
136
+ /**
137
+ * Refuse what `flags` asks of npm that it cannot do: a Node project's
138
+ * dependencies are its `package.json`'s, so there are no further entrypoints
139
+ * to resolve, and `--min-dep-age` is Deno's.
140
+ *
141
+ * @throws {Error} naming `command` when either was given.
142
+ */
143
+ export function nodeInstallFlags(command, flags) {
144
+ if (flags.entrypoints.length > 0 || flags.minDepAge !== undefined) {
145
+ throw new Error(`zuke ${command}: this project's build runs on Node, whose ` +
146
+ "dependencies are package.json's: it takes only --dry-run, not " +
147
+ "entrypoints or --min-dep-age.");
148
+ }
149
+ }
package/src/records.d.ts CHANGED
@@ -16,3 +16,5 @@ export declare function isRecord(value: unknown): value is Record<string, unknow
16
16
  * whether the value it gets is valid.
17
17
  */
18
18
  export declare function forgivingField(text: string, field: string): unknown;
19
+ /** `text` parsed, when it is a JSON object; `undefined` otherwise. */
20
+ export declare function parsedRecord(text: string): Record<string, unknown> | undefined;
package/src/records.js CHANGED
@@ -20,12 +20,16 @@ export function isRecord(value) {
20
20
  * whether the value it gets is valid.
21
21
  */
22
22
  export function forgivingField(text, field) {
23
- let config;
23
+ return parsedRecord(text)?.[field];
24
+ }
25
+ /** `text` parsed, when it is a JSON object; `undefined` otherwise. */
26
+ export function parsedRecord(text) {
27
+ let parsed;
24
28
  try {
25
- config = JSON.parse(text);
29
+ parsed = JSON.parse(text);
26
30
  }
27
31
  catch {
28
32
  return undefined;
29
33
  }
30
- return isRecord(config) ? config[field] : undefined;
34
+ return isRecord(parsed) ? parsed : undefined;
31
35
  }
package/src/relock.js CHANGED
@@ -27,10 +27,15 @@
27
27
  * matters on Deno 2.9, which by default skips releases from the last day or
28
28
  * so: `--min-dep-age 0` resolves the newest published versions.
29
29
  *
30
+ * In a project whose build runs on Node it regenerates `package-lock.json`
31
+ * instead, behind the same backup and emptied in place the same way, and
32
+ * `npm install --package-lock-only --ignore-scripts` resolves `package.json`
33
+ * afresh into it, installing nothing.
34
+ *
30
35
  * @module
31
36
  */
32
37
  import { lockedJsrSpecifiers } from "@zuke/core";
33
- import { installArgs, parseInstallFlags, projectLocation, } from "./project.js";
38
+ import { installArgs, nodeInstallFlags, parseInstallFlags, projectLocation, } from "./project.js";
34
39
  import { lockedCoreVersions, projectConfigText, projectLockPath, } from "./version_report.js";
35
40
  /** The suffix of the backup a run keeps while the lock is being rewritten. */
36
41
  const BACKUP_SUFFIX = ".relock-backup";
@@ -54,66 +59,59 @@ function olderThan(a, b) {
54
59
  }
55
60
  return false;
56
61
  }
57
- /** The emptied lock a run resolves into: `before`'s format version, no entries. */
58
- function emptied(before) {
59
- let version = "5";
62
+ /** The lock formats `relock` empties: Deno's `deno.lock`, npm's `package-lock.json`. */
63
+ const LOCK_FORMATS = {
64
+ deno: { key: "version", fallback: "5", entries: {} },
65
+ npm: { key: "lockfileVersion", fallback: 3, entries: { packages: {} } },
66
+ };
67
+ /**
68
+ * The emptied lock a run resolves into: `before`'s format version, no
69
+ * entries. It is written over the lock in place rather than the lock being
70
+ * removed, so a lock that is a symlink to a shared file stays one.
71
+ */
72
+ function emptied(before, kind) {
73
+ const format = LOCK_FORMATS[kind];
74
+ let version = format.fallback;
60
75
  try {
61
76
  const parsed = JSON.parse(before);
62
77
  if (parsed !== null && typeof parsed === "object") {
63
- version = Reflect.get(parsed, "version") ?? version;
78
+ version = Reflect.get(parsed, format.key) ?? version;
64
79
  }
65
80
  }
66
81
  catch {
67
- // An unreadable lock is still replaced; the default format is Deno 2's.
82
+ // An unreadable lock is still replaced, in the current default format.
68
83
  }
69
- return `${JSON.stringify({ version }, null, 2)}\n`;
70
- }
71
- /** The JSR package names `lock` resolves. */
72
- function jsrPackages(lock) {
73
- return new Set(lockedJsrSpecifiers(lock).map((entry) => entry.name));
84
+ return `${JSON.stringify({ [format.key]: version, ...format.entries }, null, 2)}\n`;
74
85
  }
75
86
  /**
76
- * Run `zuke relock <args>` from `cwd`, reporting through `log`, and return
77
- * the exit code.
87
+ * Refuse to start while `backup`, a lock an earlier run set aside, is still
88
+ * there: it is the only copy of the lock as it was.
78
89
  *
79
- * @throws {Error} with a message for the user when the request cannot be
80
- * carried out. `main` prints the message and exits 1.
90
+ * @throws {Error} saying how to restore or drop it.
81
91
  */
82
- export async function runRelock(args, log, host, paint, cwd, probe) {
83
- const flags = parseInstallFlags("relock", args);
84
- const { root, build } = await projectLocation(cwd, probe, "relock");
85
- const lockPath = projectLockPath(root, await projectConfigText((path) => host.readText(path), root));
86
- if (lockPath === null) {
87
- throw new Error("zuke relock: this project's deno.json disables its lock, or could " +
88
- "not be read as JSON, so there is no lock to regenerate.");
89
- }
90
- // Regenerating is the point, so a lock the config freezes is not a reason
91
- // to refuse the write.
92
- const denoArgs = [
93
- ...installArgs("relock", flags, host.denoVersion(), build),
94
- "--frozen=false",
95
- ];
96
- const name = lockPath.startsWith(`${root}/`)
97
- ? lockPath.slice(root.length + 1)
98
- : lockPath;
99
- if (flags.dryRun) {
100
- log(`Would regenerate ${name}: deno ${denoArgs.join(" ")}`);
101
- return 0;
102
- }
103
- const backup = `${lockPath}${BACKUP_SUFFIX}`;
104
- let leftover = true;
92
+ async function refuseLeftover(host, backup, name) {
105
93
  try {
106
94
  await host.readText(backup);
107
95
  }
108
96
  catch {
109
- leftover = false;
110
- }
111
- if (leftover) {
112
- throw new Error(`zuke relock: ${backup} is left from a relock that did not finish. ` +
113
- `It is the lock as it was before that run: move it back over ${name} ` +
114
- "to restore it, or delete it to keep the lock as it is, then run " +
115
- "zuke relock again.");
97
+ return;
116
98
  }
99
+ throw new Error(`zuke relock: ${backup} is left from a relock that did not finish. ` +
100
+ `It is the lock as it was before that run: move it back over ${name} ` +
101
+ "to restore it, or delete it to keep the lock as it is, then run " +
102
+ "zuke relock again.");
103
+ }
104
+ /**
105
+ * Regenerate the lock at `lockPath` behind a backup, the one sequence both
106
+ * runtimes share: refuse to start over a backup an earlier run left, set the
107
+ * lock aside, empty it in place in format `kind` (written through, so a link
108
+ * stays one), and run `regenerate`. Its result is kept only when it exits 0
109
+ * and writes a lock that is not still the emptied one; otherwise the old text
110
+ * goes back the same way. The backup is removed either way, and on a throw.
111
+ */
112
+ async function regenerateLock(host, lockPath, name, kind, log, regenerate) {
113
+ const backup = `${lockPath}${BACKUP_SUFFIX}`;
114
+ await refuseLeftover(host, backup, name);
117
115
  let before;
118
116
  try {
119
117
  before = await host.readText(lockPath);
@@ -123,21 +121,22 @@ export async function runRelock(args, log, host, paint, cwd, probe) {
123
121
  }
124
122
  if (before !== undefined) {
125
123
  await host.writeText(backup, before);
126
- await host.writeText(lockPath, emptied(before));
124
+ await host.writeText(lockPath, emptied(before, kind));
127
125
  }
128
- const restore = async () => {
129
- if (before !== undefined)
126
+ const settle = async (keep) => {
127
+ if (before === undefined)
128
+ return;
129
+ if (!keep)
130
130
  await host.writeText(lockPath, before);
131
+ await host.remove(backup);
131
132
  };
132
133
  log(`Regenerating ${name}…`);
133
134
  let code;
134
135
  try {
135
- code = await host.deno(denoArgs, root);
136
+ code = await regenerate();
136
137
  }
137
138
  catch (error) {
138
- await restore();
139
- if (before !== undefined)
140
- await host.remove(backup);
139
+ await settle(false);
141
140
  throw error;
142
141
  }
143
142
  let after;
@@ -146,21 +145,79 @@ export async function runRelock(args, log, host, paint, cwd, probe) {
146
145
  after = await host.readText(lockPath);
147
146
  }
148
147
  catch {
149
- // Reported below.
148
+ // Reported by the caller, as no lock written.
150
149
  }
151
150
  }
152
- if (code !== 0 || after === undefined || after === emptied(before ?? "")) {
153
- await restore();
154
- if (before !== undefined)
155
- await host.remove(backup);
151
+ const fresh = code === 0 && after !== undefined &&
152
+ after !== emptied(before ?? "", kind);
153
+ await settle(fresh);
154
+ return { code, before, after: fresh ? after : undefined };
155
+ }
156
+ /**
157
+ * Regenerate a Node project's `package-lock.json` in `root`, resolving
158
+ * `package.json` afresh with npm behind {@link regenerateLock}'s backup.
159
+ */
160
+ async function relockNode(flags, root, log, host, paint) {
161
+ nodeInstallFlags("relock", flags);
162
+ const name = "package-lock.json";
163
+ const npmArgs = ["install", "--package-lock-only", "--ignore-scripts"];
164
+ if (flags.dryRun) {
165
+ log(`Would regenerate ${name}: npm ${npmArgs.join(" ")}`);
166
+ return 0;
167
+ }
168
+ const { code, before, after } = await regenerateLock(host, `${root}/${name}`, name, "npm", log, () => host.npm(npmArgs, root));
169
+ if (after === undefined) {
170
+ log(paint.fail(code !== 0
171
+ ? `zuke relock: npm install exited ${code}; ${name} is unchanged.`
172
+ : `zuke relock: npm install wrote no ${name}${before === undefined ? "" : "; the old one is restored"}.`));
173
+ return code !== 0 ? code : 1;
174
+ }
175
+ log(paint.ok(`Regenerated ${name}.`));
176
+ return 0;
177
+ }
178
+ /** The JSR package names `lock` resolves. */
179
+ function jsrPackages(lock) {
180
+ return new Set(lockedJsrSpecifiers(lock).map((entry) => entry.name));
181
+ }
182
+ /**
183
+ * Run `zuke relock <args>` from `cwd`, reporting through `log`, and return
184
+ * the exit code.
185
+ *
186
+ * @throws {Error} with a message for the user when the request cannot be
187
+ * carried out. `main` prints the message and exits 1.
188
+ */
189
+ export async function runRelock(args, log, host, paint, cwd, probe) {
190
+ const flags = parseInstallFlags("relock", args);
191
+ const { root, build, runtime } = await projectLocation(cwd, probe, "relock");
192
+ if (runtime === "node") {
193
+ return await relockNode(flags, root, log, host, paint);
194
+ }
195
+ const lockPath = projectLockPath(root, await projectConfigText((path) => host.readText(path), root));
196
+ if (lockPath === null) {
197
+ throw new Error("zuke relock: this project's deno.json disables its lock, or could " +
198
+ "not be read as JSON, so there is no lock to regenerate.");
199
+ }
200
+ // Regenerating is the point, so a lock the config freezes is not a reason
201
+ // to refuse the write.
202
+ const denoArgs = [
203
+ ...installArgs("relock", flags, host.denoVersion(), build),
204
+ "--frozen=false",
205
+ ];
206
+ const name = lockPath.startsWith(`${root}/`)
207
+ ? lockPath.slice(root.length + 1)
208
+ : lockPath;
209
+ if (flags.dryRun) {
210
+ log(`Would regenerate ${name}: deno ${denoArgs.join(" ")}`);
211
+ return 0;
212
+ }
213
+ const { code, before, after } = await regenerateLock(host, lockPath, name, "deno", log, () => host.deno(denoArgs, root));
214
+ if (after === undefined) {
156
215
  log(paint.fail(code !== 0
157
216
  ? `zuke relock: deno install exited ${code}; ${name} is unchanged.`
158
217
  : `zuke relock: deno install wrote no ${name}${before === undefined ? "" : "; the old one is restored"}. In a workspace member, Deno keeps the lock at the workspace ` +
159
218
  "root: run zuke relock there."));
160
219
  return code !== 0 ? code : 1;
161
220
  }
162
- if (before !== undefined)
163
- await host.remove(backup);
164
221
  const was = lockedCoreVersions(before ?? "");
165
222
  const now = lockedCoreVersions(after);
166
223
  log(paint.ok(`Regenerated ${name}: @zuke/core ${shown(was) === shown(now) ? shown(now) : `${shown(was)} → ${shown(now)}`}`));
package/src/setup.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isRecord } from "./records.ts";
2
- import type { ProjectRuntime } from "./runtime_field.ts";
2
+ import { type ProjectRuntime } from "./runtime_field.ts";
3
3
  import type { BinaryPin } from "./binary_pin.ts";
4
4
  export { isRecord };
5
5
  /**
@@ -242,6 +242,14 @@ export declare const DEFAULT_LAUNCHER = "zuke";
242
242
  * @throws {Error} when `zuke.json` names a build file that is not a valid name.
243
243
  */
244
244
  export declare function existingBuildFile(dir: string, host: SetupHost): Promise<string | undefined>;
245
+ /**
246
+ * The runtime the TypeScript project in `dir` already runs on: the one its
247
+ * `zuke.json` names, Deno for one that names none or has a build file and no
248
+ * `zuke.json` yet. `undefined` for a new project.
249
+ *
250
+ * @throws {Error} when `zuke.json` names a runtime Zuke does not run on.
251
+ */
252
+ export declare function existingRuntime(dir: string, host: SetupHost): Promise<ProjectRuntime | undefined>;
245
253
  /**
246
254
  * Whether `name` in `dir` is a file of the project's own that is not a Zuke
247
255
  * build: it exists and does not import `@zuke/core`. A new project must not