@zuke-build/cli 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +353 -2
- package/mod.d.ts +102 -0
- package/mod.js +931 -0
- package/package.json +35 -4
- package/src/binary_launcher.d.ts +49 -0
- package/src/binary_launcher.js +325 -0
- package/src/binary_pin.d.ts +47 -0
- package/src/binary_pin.js +117 -0
- package/src/build_file.d.ts +25 -0
- package/src/build_file.js +58 -0
- package/src/cache.d.ts +23 -0
- package/src/cache.js +26 -0
- package/src/deno_isolated.d.ts +14 -0
- package/src/deno_isolated.js +70 -0
- package/src/deno_path.d.ts +115 -0
- package/src/deno_path.js +205 -0
- package/src/deno_pin.d.ts +37 -0
- package/src/deno_pin.js +32 -0
- package/src/dependencies.d.ts +48 -0
- package/src/dependencies.js +80 -0
- package/src/dispatch.d.ts +229 -0
- package/src/dispatch.js +549 -0
- package/src/embedded_engine.d.ts +28 -0
- package/src/embedded_engine.js +21 -0
- package/src/engine_pin.d.ts +56 -0
- package/src/engine_pin.js +196 -0
- package/src/fs.d.ts +14 -0
- package/src/fs.js +25 -0
- package/src/import.d.ts +132 -0
- package/src/import.js +289 -0
- package/src/install_source.d.ts +79 -0
- package/src/install_source.js +104 -0
- package/src/launcher.d.ts +37 -0
- package/src/launcher.js +405 -0
- package/src/launcher_shared.d.ts +25 -0
- package/src/launcher_shared.js +91 -0
- package/src/mcp_config.d.ts +71 -0
- package/src/mcp_config.js +96 -0
- package/src/node_launcher.d.ts +19 -0
- package/src/node_launcher.js +416 -0
- package/src/node_pin.d.ts +25 -0
- package/src/node_pin.js +30 -0
- package/src/output.d.ts +38 -0
- package/src/output.js +43 -0
- package/src/paint.d.ts +42 -0
- package/src/paint.js +38 -0
- package/src/project.d.ts +69 -0
- package/src/project.js +132 -0
- package/src/pyproject.d.ts +30 -0
- package/src/pyproject.js +156 -0
- package/src/python_build.d.ts +27 -0
- package/src/python_build.js +197 -0
- package/src/records.d.ts +18 -0
- package/src/records.js +31 -0
- package/src/relock.d.ts +11 -0
- package/src/relock.js +182 -0
- package/src/runtime_field.d.ts +15 -0
- package/src/runtime_field.js +29 -0
- package/src/setup.d.ts +259 -0
- package/src/setup.js +693 -0
- package/src/star.d.ts +44 -0
- package/src/star.js +100 -0
- package/src/starter.d.ts +64 -0
- package/src/starter.js +138 -0
- package/src/toml.d.ts +32 -0
- package/src/toml.js +354 -0
- package/src/translate.d.ts +69 -0
- package/src/translate.js +198 -0
- package/src/upgrade.d.ts +113 -0
- package/src/upgrade.js +344 -0
- package/src/version.d.ts +11 -0
- package/src/version.js +13 -0
- package/src/version_report.d.ts +55 -0
- package/src/version_report.js +187 -0
- package/zuke.js +8 -0
package/src/setup.js
ADDED
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
// Copyright (c) 2026 the Zuke contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* The scaffolding engine behind `zuke setup`. It writes a starter `zuke.ts`,
|
|
5
|
+
* the `./zuke` launchers (see {@link "./launcher.ts"} for the two variants),
|
|
6
|
+
* and a `deno.json` task into a target directory.
|
|
7
|
+
*
|
|
8
|
+
* All filesystem and console effects go through an injectable {@link SetupHost}
|
|
9
|
+
* so the logic stays pure and unit-testable; {@link defaultHost} is the real
|
|
10
|
+
* `Deno`-backed implementation used in production.
|
|
11
|
+
*/
|
|
12
|
+
import { binaryMcpEntry, MCP_CONFIG_FILE, mcpServerEntry, mergeMcpConfig, nodeMcpEntry, parseMcpConfig, } from "./mcp_config.js";
|
|
13
|
+
import { FileTasks, RuntimeTasks } from "@zuke/core";
|
|
14
|
+
import { isRecord } from "./records.js";
|
|
15
|
+
import { assertBuildFileName, BUILD_FILE, buildFileNamed, NEW_BUILD_FILE, } from "./build_file.js";
|
|
16
|
+
import { launcherBash, launcherPwsh } from "./launcher.js";
|
|
17
|
+
import { nodeLauncherBash, nodeLauncherPowerShell } from "./node_launcher.js";
|
|
18
|
+
import { binaryLauncherBash, binaryLauncherPwsh } from "./binary_launcher.js";
|
|
19
|
+
import { lstatOrNull } from "./fs.js";
|
|
20
|
+
import { output } from "./output.js";
|
|
21
|
+
import { PYTHON_BUILD_FILE, STARTER_IMPORTS, starterBuild, starterConfig, starterPythonBuild, starterSdkConfig, zukeNpmDependency, } from "./starter.js";
|
|
22
|
+
// Re-exported so the merge guard keeps its historical home for importers.
|
|
23
|
+
export { isRecord };
|
|
24
|
+
/**
|
|
25
|
+
* The task names `setup` writes into `deno.json`, with their commands, for a
|
|
26
|
+
* build whose file is `buildFile`.
|
|
27
|
+
*
|
|
28
|
+
* The `zuke` task deliberately omits `--frozen`, unlike the launchers: a task
|
|
29
|
+
* command runs in `deno task`'s own shell, which has no conditionals, so it
|
|
30
|
+
* cannot skip the flag on the first run when no `deno.lock` exists yet — and
|
|
31
|
+
* hard-coding it would make a fresh scaffold fail before it could ever write
|
|
32
|
+
* one. The launcher (`./zuke`, the advertised entry point) does the lockfile
|
|
33
|
+
* check and enforces `--frozen` from the second run onward.
|
|
34
|
+
*/
|
|
35
|
+
function defaultTasks(buildFile) {
|
|
36
|
+
return [
|
|
37
|
+
["zuke", `deno run -A ${buildFile}`],
|
|
38
|
+
["fmt", "deno fmt"],
|
|
39
|
+
["lint", "deno lint"],
|
|
40
|
+
["test", "deno test -A"],
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
/** The `zuke` task as setup itself writes it, for any build file. */
|
|
44
|
+
const SCAFFOLDED_ZUKE_TASK = /^deno run -A [A-Za-z0-9_.][A-Za-z0-9_.-]*\.ts$/;
|
|
45
|
+
/**
|
|
46
|
+
* Whether `tasks.zuke` is a task setup wrote for another build file than
|
|
47
|
+
* `buildFile`: the one thing in `tasks` a re-run replaces, so that a project
|
|
48
|
+
* whose `zuke.json` was pointed at a new build file does not keep a task
|
|
49
|
+
* running the old one. A task the project wrote itself is never touched.
|
|
50
|
+
*/
|
|
51
|
+
function staleZukeTask(tasks, buildFile) {
|
|
52
|
+
if (!isRecord(tasks) || typeof tasks.zuke !== "string")
|
|
53
|
+
return false;
|
|
54
|
+
return SCAFFOLDED_ZUKE_TASK.test(tasks.zuke) &&
|
|
55
|
+
tasks.zuke !== `deno run -A ${buildFile}`;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Add every entry of `defaults` that `root[key]` does not already declare,
|
|
59
|
+
* leaving the ones it does exactly as they are.
|
|
60
|
+
*
|
|
61
|
+
* Shared by the `imports` and `tasks` merges, which differ only in the
|
|
62
|
+
* collection they draw from: one implementation, so the "never overwrite what
|
|
63
|
+
* the project already declared" rule cannot come to mean two things.
|
|
64
|
+
*/
|
|
65
|
+
function mergeMissing(root, key, defaults) {
|
|
66
|
+
const existing = root[key];
|
|
67
|
+
const merged = isRecord(existing)
|
|
68
|
+
? { ...existing }
|
|
69
|
+
: {};
|
|
70
|
+
for (const [name, value] of defaults) {
|
|
71
|
+
if (!(name in merged))
|
|
72
|
+
merged[name] = value;
|
|
73
|
+
}
|
|
74
|
+
root[key] = merged;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Whether a `deno.json` document delegates its import map to a separate file
|
|
78
|
+
* and would be broken by growing an `imports` block.
|
|
79
|
+
*
|
|
80
|
+
* Deno ignores the `importMap` field the moment `imports` or `scopes` appears
|
|
81
|
+
* in the config, so writing `imports` beside a lone `importMap` silently
|
|
82
|
+
* disables the project's entire module resolution. A document that already has
|
|
83
|
+
* `imports` or `scopes` is not delegating — Deno is ignoring `importMap` there
|
|
84
|
+
* already — so merging into it changes nothing.
|
|
85
|
+
*/
|
|
86
|
+
export function delegatesImportMap(root) {
|
|
87
|
+
return typeof root.importMap === "string" &&
|
|
88
|
+
!isRecord(root.imports) && !isRecord(root.scopes);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Merge the default Zuke tasks and `imports` into a `deno.json` document,
|
|
92
|
+
* preserving the existing content. `existing` is the file text, or `null` to
|
|
93
|
+
* start fresh; `imports` are the bare-specifier mappings the scaffolded build
|
|
94
|
+
* resolves through, and `buildFile` is the file the `zuke` task runs.
|
|
95
|
+
*
|
|
96
|
+
* Merging is additive per key: a task or import the document already declares
|
|
97
|
+
* is left exactly as it is (but for a `zuke` task setup wrote for another
|
|
98
|
+
* build file, which follows the build file), so a project that has pinned `@zuke/core` to a
|
|
99
|
+
* specific version keeps that pin. A document that delegates to an external
|
|
100
|
+
* `importMap` keeps that delegation and grows no `imports` block — see
|
|
101
|
+
* {@link delegatesImportMap}; the caller reports the entry as a manual step
|
|
102
|
+
* instead.
|
|
103
|
+
*/
|
|
104
|
+
export function mergeDenoJson(existing, imports, buildFile) {
|
|
105
|
+
const root = {};
|
|
106
|
+
if (existing !== null) {
|
|
107
|
+
const parsed = JSON.parse(existing);
|
|
108
|
+
if (isRecord(parsed))
|
|
109
|
+
Object.assign(root, parsed);
|
|
110
|
+
}
|
|
111
|
+
// Seed `imports` before `tasks` so a file created from scratch lists the
|
|
112
|
+
// dependencies first, the order deno.json conventionally uses. On a file that
|
|
113
|
+
// already has either key, assigning it keeps its original position.
|
|
114
|
+
if (!delegatesImportMap(root)) {
|
|
115
|
+
mergeMissing(root, "imports", Object.entries(imports));
|
|
116
|
+
}
|
|
117
|
+
if (staleZukeTask(root.tasks, buildFile) && isRecord(root.tasks)) {
|
|
118
|
+
root.tasks = { ...root.tasks, zuke: `deno run -A ${buildFile}` };
|
|
119
|
+
}
|
|
120
|
+
mergeMissing(root, "tasks", defaultTasks(buildFile));
|
|
121
|
+
return `${JSON.stringify(root, null, 2)}\n`;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Classify a `deno.json` text by whether the scaffold has anything to add to
|
|
125
|
+
* it: the `zuke` task, or any of `imports`.
|
|
126
|
+
*
|
|
127
|
+
* The imports are part of the question because the build file is written by the
|
|
128
|
+
* same run and imports them by bare specifier. Treating the `zuke` task alone
|
|
129
|
+
* as "already set up" — as this did before the imports existed — would skip the
|
|
130
|
+
* file on a project being re-scaffolded and leave a `zuke.ts` whose specifiers
|
|
131
|
+
* nothing resolves.
|
|
132
|
+
*/
|
|
133
|
+
export function denoJsonState(text, imports, buildFile) {
|
|
134
|
+
let parsed;
|
|
135
|
+
try {
|
|
136
|
+
parsed = JSON.parse(text);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return "unparseable";
|
|
140
|
+
}
|
|
141
|
+
if (!isRecord(parsed))
|
|
142
|
+
return "absent";
|
|
143
|
+
const hasTask = isRecord(parsed.tasks) && "zuke" in parsed.tasks &&
|
|
144
|
+
!staleZukeTask(parsed.tasks, buildFile);
|
|
145
|
+
// A document delegating to an external `importMap` will not grow an
|
|
146
|
+
// `imports` block (it would disable the delegation), so the imports are not
|
|
147
|
+
// something this file is still missing — they are reported as a manual step.
|
|
148
|
+
const declared = isRecord(parsed.imports) ? parsed.imports : {};
|
|
149
|
+
const hasImports = delegatesImportMap(parsed) ||
|
|
150
|
+
Object.keys(imports).every((s) => s in declared);
|
|
151
|
+
return hasTask && hasImports ? "present" : "absent";
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The scaffold specifiers `root` maps somewhere other than their `jsr:@zuke/…`
|
|
155
|
+
* dependency, in declaration order.
|
|
156
|
+
*
|
|
157
|
+
* Keeping a mapping the project already declared is the right default — it is
|
|
158
|
+
* how a deliberate version pin, or a link to a local checkout, survives. But
|
|
159
|
+
* the run should not go on to report the dependency as simply "present", so the
|
|
160
|
+
* caller names what it found and leaves the decision to the reader.
|
|
161
|
+
*/
|
|
162
|
+
export function remappedImports(root, imports) {
|
|
163
|
+
const declared = isRecord(root.imports) ? root.imports : {};
|
|
164
|
+
return Object.keys(imports).filter((specifier) => {
|
|
165
|
+
const value = declared[specifier];
|
|
166
|
+
return typeof value === "string" &&
|
|
167
|
+
!value.startsWith(`jsr:${specifier}@`);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The real, `Deno`-backed {@link SetupHost}. Every probe reads the link itself
|
|
172
|
+
* rather than its target (see {@link lstatOrNull}), so a symlink is reported
|
|
173
|
+
* as a symlink instead of as whatever it points at.
|
|
174
|
+
*/
|
|
175
|
+
export const defaultHost = {
|
|
176
|
+
exists: (path) => FileTasks.exists(path),
|
|
177
|
+
async isDirectory(path) {
|
|
178
|
+
return (await lstatOrNull(path))?.isDirectory === true;
|
|
179
|
+
},
|
|
180
|
+
async isSymlink(path) {
|
|
181
|
+
return (await lstatOrNull(path))?.isSymlink === true;
|
|
182
|
+
},
|
|
183
|
+
readText(path) {
|
|
184
|
+
return FileTasks.readText(path);
|
|
185
|
+
},
|
|
186
|
+
async writeText(path, content) {
|
|
187
|
+
// Write beside the destination and rename into place, rather than writing
|
|
188
|
+
// to the destination directly. Renaming replaces a symbolic link sitting at
|
|
189
|
+
// the destination instead of following it, so the bytes cannot be
|
|
190
|
+
// redirected outside this directory even by a link planted after the
|
|
191
|
+
// pre-write check. The temporary name is created exclusively, so it cannot
|
|
192
|
+
// itself be a link someone left waiting.
|
|
193
|
+
const temp = `${path}.zuke-${crypto.randomUUID().slice(0, 8)}.tmp`;
|
|
194
|
+
try {
|
|
195
|
+
await FileTasks.writeText(temp, content, (s) => s.createNew());
|
|
196
|
+
await FileTasks.move(temp, path);
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
// Best effort: a failed write may not have created the file at all.
|
|
200
|
+
await FileTasks.remove(temp).catch(() => { });
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
chmod(path, mode) {
|
|
205
|
+
return FileTasks.chmod(path, mode);
|
|
206
|
+
},
|
|
207
|
+
log(message) {
|
|
208
|
+
output.info(message);
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
/** Join a directory and file name without pulling in path utilities. */
|
|
212
|
+
export function joinPath(dir, name) {
|
|
213
|
+
return dir === "." ? name : `${dir}/${name}`;
|
|
214
|
+
}
|
|
215
|
+
/** The default launcher base name. */
|
|
216
|
+
export const DEFAULT_LAUNCHER = "zuke";
|
|
217
|
+
/**
|
|
218
|
+
* Scaffold file names a launcher must not shadow (the config, and the Python
|
|
219
|
+
* build file; the TypeScript one is named per project).
|
|
220
|
+
*/
|
|
221
|
+
const RESERVED_NON_LAUNCHER = new Set([
|
|
222
|
+
PYTHON_BUILD_FILE,
|
|
223
|
+
"zuke.json",
|
|
224
|
+
"deno.json",
|
|
225
|
+
"package.json",
|
|
226
|
+
".gitignore",
|
|
227
|
+
MCP_CONFIG_FILE,
|
|
228
|
+
]);
|
|
229
|
+
/**
|
|
230
|
+
* Validate a `--launcher-name`: a single path segment (so the launcher can't be
|
|
231
|
+
* written outside the target directory) that doesn't shadow another file setup
|
|
232
|
+
* writes, `buildFile` among them. Throws a friendly error otherwise.
|
|
233
|
+
*/
|
|
234
|
+
function assertLauncherName(name, buildFile) {
|
|
235
|
+
// Reject path separators and a colon (a Windows drive/ADS separator), so the
|
|
236
|
+
// launcher can only be a plain file in the target directory.
|
|
237
|
+
if (name === "" || /[\\/:]/.test(name) || name === "." || name === "..") {
|
|
238
|
+
throw new Error(`zuke setup: invalid --launcher-name "${name}" — it must be a plain file ` +
|
|
239
|
+
`name (no path separators or ":").`);
|
|
240
|
+
}
|
|
241
|
+
// Compare case-insensitively: a case-insensitive filesystem (macOS, Windows)
|
|
242
|
+
// would let `Zuke.ts` shadow the `zuke.ts` setup writes.
|
|
243
|
+
const lower = name.toLowerCase();
|
|
244
|
+
if (RESERVED_NON_LAUNCHER.has(lower) || lower === buildFile.toLowerCase()) {
|
|
245
|
+
throw new Error(`zuke setup: --launcher-name "${name}" would overwrite a file setup ` +
|
|
246
|
+
`writes (the build file or config) — choose a different name.`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* The build file the TypeScript project in `dir` already has: the one its
|
|
251
|
+
* `zuke.json` names, `zuke.ts` when it names none, and `zuke.ts` too for a
|
|
252
|
+
* directory that holds one with no `zuke.json` yet. `undefined` for a
|
|
253
|
+
* directory with neither, which is a new project. A `zuke.json` that is a
|
|
254
|
+
* link is not read: scaffolding refuses it.
|
|
255
|
+
*
|
|
256
|
+
* @throws {Error} when `zuke.json` names a build file that is not a valid name.
|
|
257
|
+
*/
|
|
258
|
+
export async function existingBuildFile(dir, host) {
|
|
259
|
+
const config = joinPath(dir, "zuke.json");
|
|
260
|
+
if (await host.exists(config) && !(await host.isSymlink(config))) {
|
|
261
|
+
return buildFileNamed(await host.readText(config));
|
|
262
|
+
}
|
|
263
|
+
return await host.exists(joinPath(dir, BUILD_FILE)) ? BUILD_FILE : undefined;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Whether `name` in `dir` is a file of the project's own that is not a Zuke
|
|
267
|
+
* build: it exists and does not import `@zuke/core`. A new project must not
|
|
268
|
+
* take such a name, because its `zuke.json` would point at an unrelated
|
|
269
|
+
* script, and `--force` would overwrite it.
|
|
270
|
+
*/
|
|
271
|
+
export async function unrelatedBuildFile(dir, name, host) {
|
|
272
|
+
const path = joinPath(dir, name);
|
|
273
|
+
// A link or a directory there is refused by the collision check instead.
|
|
274
|
+
if (!(await host.exists(path)) || await host.isSymlink(path) ||
|
|
275
|
+
await host.isDirectory(path)) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
return !(await host.readText(path)).includes("@zuke/core");
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* The build file a TypeScript scaffold writes: the one asked for, else the
|
|
282
|
+
* project's own, else `build.ts`.
|
|
283
|
+
*
|
|
284
|
+
* @throws {Error} when the name asked for is not valid, differs from the one
|
|
285
|
+
* an existing project has, or would take over an unrelated file in a new
|
|
286
|
+
* project.
|
|
287
|
+
*/
|
|
288
|
+
async function scaffoldBuildFile(options, host) {
|
|
289
|
+
const existing = await existingBuildFile(options.dir, host);
|
|
290
|
+
const asked = options.buildFile === undefined
|
|
291
|
+
? undefined
|
|
292
|
+
: assertBuildFileName(options.buildFile);
|
|
293
|
+
if (existing !== undefined) {
|
|
294
|
+
// Setup does not move a build: it would have to overwrite whatever has
|
|
295
|
+
// the new name. The person renames the file, and setup follows zuke.json.
|
|
296
|
+
if (asked !== undefined && asked !== existing) {
|
|
297
|
+
throw new Error(`zuke setup: this project's build file is ${existing}, not ${asked}. ` +
|
|
298
|
+
`To rename it, rename the file, set "build": "${asked}" in ` +
|
|
299
|
+
"zuke.json, and re-run zuke setup --force to rewrite the launchers.");
|
|
300
|
+
}
|
|
301
|
+
return existing;
|
|
302
|
+
}
|
|
303
|
+
const name = asked ?? NEW_BUILD_FILE;
|
|
304
|
+
if (await unrelatedBuildFile(options.dir, name, host)) {
|
|
305
|
+
throw new Error(`zuke setup: ${joinPath(options.dir, name)} already exists and is not ` +
|
|
306
|
+
"a Zuke build, so it was left alone. Pass --build-file <name> to " +
|
|
307
|
+
"give the build another name.");
|
|
308
|
+
}
|
|
309
|
+
return name;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Scaffold Zuke into `options.dir`: a starter build file, the launcher scripts,
|
|
313
|
+
* and a `deno.json` task block. Existing files are skipped unless
|
|
314
|
+
* `options.force` is set; `deno.json` is always merged (never clobbered). Fails
|
|
315
|
+
* up front if a target name is occupied by a directory (see
|
|
316
|
+
* {@link SetupOptions.launcherName}).
|
|
317
|
+
*/
|
|
318
|
+
export async function runSetup(options, host = defaultHost) {
|
|
319
|
+
const files = [];
|
|
320
|
+
const manualSteps = [];
|
|
321
|
+
const notes = [];
|
|
322
|
+
const launcher = options.launcherName ?? DEFAULT_LAUNCHER;
|
|
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
|
+
}
|
|
329
|
+
if (sdk !== undefined && options.buildFile !== undefined) {
|
|
330
|
+
throw new Error("zuke setup: --build-file names a TypeScript build file; a Python " +
|
|
331
|
+
`build is always ${PYTHON_BUILD_FILE}.`);
|
|
332
|
+
}
|
|
333
|
+
// The build file, the launchers, the task, the MCP entry and zuke.json all
|
|
334
|
+
// carry the one name. An SDK build has no TypeScript file to name.
|
|
335
|
+
const buildFile = sdk === undefined
|
|
336
|
+
? await scaffoldBuildFile(options, host)
|
|
337
|
+
: BUILD_FILE;
|
|
338
|
+
assertLauncherName(launcher, buildFile);
|
|
339
|
+
const variant = { bootstrapDeno: options.bootstrapDeno ?? true, buildFile };
|
|
340
|
+
const scaffold = [
|
|
341
|
+
sdk === undefined
|
|
342
|
+
? {
|
|
343
|
+
name: buildFile,
|
|
344
|
+
content: options.buildContent ?? starterBuild(options.name),
|
|
345
|
+
}
|
|
346
|
+
: {
|
|
347
|
+
name: PYTHON_BUILD_FILE,
|
|
348
|
+
content: options.buildContent ?? starterPythonBuild(options.name),
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
name: launcher,
|
|
352
|
+
content: node
|
|
353
|
+
? nodeLauncherBash({ buildFile })
|
|
354
|
+
: sdk === undefined
|
|
355
|
+
? launcherBash(variant)
|
|
356
|
+
: binaryLauncherBash({ download: variant.bootstrapDeno }, sdk.pin),
|
|
357
|
+
mode: 0o755,
|
|
358
|
+
launcher: true,
|
|
359
|
+
},
|
|
360
|
+
{
|
|
361
|
+
name: `${launcher}.ps1`,
|
|
362
|
+
content: node
|
|
363
|
+
? nodeLauncherPowerShell({ buildFile })
|
|
364
|
+
: sdk === undefined
|
|
365
|
+
? launcherPwsh(variant)
|
|
366
|
+
: binaryLauncherPwsh({ download: variant.bootstrapDeno }, sdk.pin),
|
|
367
|
+
launcher: true,
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
name: "zuke.json",
|
|
371
|
+
content: sdk === undefined
|
|
372
|
+
? starterConfig(options.name, buildFile, options.runtime)
|
|
373
|
+
: starterSdkConfig(options.name, sdk.pin.engine),
|
|
374
|
+
},
|
|
375
|
+
];
|
|
376
|
+
// Fail before writing anything if a target name is occupied by a directory —
|
|
377
|
+
// a scaffolded file can't be written over a directory, and silently skipping
|
|
378
|
+
// it (the old behaviour) produced a launcher-less setup. Covers every name
|
|
379
|
+
// setup writes, including the separately-written deno.json and .gitignore.
|
|
380
|
+
const collisionTargets = [
|
|
381
|
+
...scaffold.map((s) => ({ name: s.name, launcher: s.launcher === true })),
|
|
382
|
+
...(sdk === undefined
|
|
383
|
+
? [{ name: node ? "package.json" : "deno.json", launcher: false }]
|
|
384
|
+
: []),
|
|
385
|
+
{ name: ".gitignore", launcher: false },
|
|
386
|
+
...(options.mcp ? [{ name: MCP_CONFIG_FILE, launcher: false }] : []),
|
|
387
|
+
];
|
|
388
|
+
for (const item of collisionTargets) {
|
|
389
|
+
const path = joinPath(options.dir, item.name);
|
|
390
|
+
// A symlink at a scaffold name is refused for the same reason a directory
|
|
391
|
+
// is — nothing can be written there safely — but the stakes differ. Writing
|
|
392
|
+
// follows the link, so a repository that ships one (setup is precisely what
|
|
393
|
+
// a developer runs inside a freshly cloned project) would redirect the
|
|
394
|
+
// write to whatever it names, outside this directory entirely. Checked
|
|
395
|
+
// before any write, so a refusal leaves the tree untouched.
|
|
396
|
+
if (await host.isSymlink(path)) {
|
|
397
|
+
throw new Error(`zuke setup: refusing to write "${item.name}" — ${path} is a symbolic ` +
|
|
398
|
+
`link, and writing through it would modify the file it points at, ` +
|
|
399
|
+
`outside this directory. Replace the link with a regular file (or ` +
|
|
400
|
+
`remove it), then re-run setup.`);
|
|
401
|
+
}
|
|
402
|
+
if (await host.isDirectory(path)) {
|
|
403
|
+
throw new Error(`zuke setup: cannot write "${item.name}" — a directory exists at ` +
|
|
404
|
+
`${path}. ` +
|
|
405
|
+
(item.launcher
|
|
406
|
+
? `Pass --launcher-name <name> to write the launcher under a ` +
|
|
407
|
+
`different name`
|
|
408
|
+
: `Move or rename that directory (${item.name} is reserved by ` +
|
|
409
|
+
`Zuke)`) +
|
|
410
|
+
`, then re-run setup.`);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
for (const item of scaffold) {
|
|
414
|
+
const path = joinPath(options.dir, item.name);
|
|
415
|
+
const existed = await host.exists(path);
|
|
416
|
+
if (existed && !options.force) {
|
|
417
|
+
host.log(` skip ${item.name} (already exists)`);
|
|
418
|
+
files.push({ path: item.name, status: "skipped" });
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
await host.writeText(path, item.content);
|
|
422
|
+
if (item.mode !== undefined) {
|
|
423
|
+
try {
|
|
424
|
+
await host.chmod(path, item.mode);
|
|
425
|
+
}
|
|
426
|
+
catch {
|
|
427
|
+
// chmod is a no-op / unsupported on some platforms (e.g. Windows).
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
const status = existed ? "overwritten" : "created";
|
|
431
|
+
host.log(` ${existed ? "update" : "create"} ${item.name}`);
|
|
432
|
+
files.push({ path: item.name, status });
|
|
433
|
+
}
|
|
434
|
+
if (node) {
|
|
435
|
+
files.push(await setupPackageJson(options.dir, host, manualSteps, notes));
|
|
436
|
+
}
|
|
437
|
+
else if (sdk === undefined) {
|
|
438
|
+
files.push(await setupDenoJson(options.dir, options.imports ?? STARTER_IMPORTS, buildFile, host, manualSteps, notes));
|
|
439
|
+
}
|
|
440
|
+
files.push(await setupGitignore(options.dir, host, node ? [GITIGNORE_ENTRY, "node_modules/"] : [GITIGNORE_ENTRY]));
|
|
441
|
+
if (options.mcp) {
|
|
442
|
+
files.push(await setupMcpConfig(options.dir, options.force, host, notes,
|
|
443
|
+
// A hosted build is served by the zuke binary, whichever CLI
|
|
444
|
+
// registered it: its project needs nothing from Deno's stack. A
|
|
445
|
+
// TypeScript build stays on Deno.
|
|
446
|
+
node
|
|
447
|
+
? nodeMcpEntry(options.mcp.allowRun, buildFile)
|
|
448
|
+
: sdk === undefined
|
|
449
|
+
? mcpServerEntry(options.mcp.allowRun, buildFile)
|
|
450
|
+
: binaryMcpEntry(options.mcp.allowRun)));
|
|
451
|
+
}
|
|
452
|
+
return { files, manualSteps, notes };
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Create or merge `.mcp.json` so the build's MCP server is registered as
|
|
456
|
+
* `entry`. An
|
|
457
|
+
* existing `zuke` entry is left alone unless `force` is set (it may carry a
|
|
458
|
+
* deliberate `--allow-run` choice); every other server in the file survives
|
|
459
|
+
* either way. An unparseable file is skipped, recorded in `notes` so the
|
|
460
|
+
* caller can report that `--mcp` did not take effect.
|
|
461
|
+
*/
|
|
462
|
+
async function setupMcpConfig(dir, force, host, notes, entry) {
|
|
463
|
+
const name = MCP_CONFIG_FILE;
|
|
464
|
+
const path = joinPath(dir, name);
|
|
465
|
+
if (!(await host.exists(path))) {
|
|
466
|
+
await host.writeText(path, mergeMcpConfig({}, entry));
|
|
467
|
+
host.log(` create ${name}`);
|
|
468
|
+
return { path: name, status: "created" };
|
|
469
|
+
}
|
|
470
|
+
const config = parseMcpConfig(await host.readText(path));
|
|
471
|
+
if (config.state === "unparseable") {
|
|
472
|
+
host.log(` skip ${name} (unparseable, edit by hand)`);
|
|
473
|
+
// `--mcp` asked for the server to be registered and it was not, so say so
|
|
474
|
+
// in the result rather than only in a line of progress output. A note, not
|
|
475
|
+
// a manual step: the build itself runs regardless, only an agent client
|
|
476
|
+
// will not find it.
|
|
477
|
+
notes.push(`${path} could not be parsed, so the zuke MCP server was not ` +
|
|
478
|
+
`registered. Fix the file and re-run with --mcp to add it.`);
|
|
479
|
+
return { path: name, status: "skipped" };
|
|
480
|
+
}
|
|
481
|
+
if (config.state === "present" && !force) {
|
|
482
|
+
host.log(` skip ${name} (zuke server already registered)`);
|
|
483
|
+
return { path: name, status: "skipped" };
|
|
484
|
+
}
|
|
485
|
+
await host.writeText(path, mergeMcpConfig(config.document, entry));
|
|
486
|
+
host.log(` update ${name}`);
|
|
487
|
+
return { path: name, status: "overwritten" };
|
|
488
|
+
}
|
|
489
|
+
/** The line `setup` ensures is present in `.gitignore` for generated output. */
|
|
490
|
+
const GITIGNORE_ENTRY = ".zuke/";
|
|
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) {
|
|
496
|
+
const name = ".gitignore";
|
|
497
|
+
const path = joinPath(dir, name);
|
|
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)`);
|
|
504
|
+
return { path: name, status: "skipped" };
|
|
505
|
+
}
|
|
506
|
+
const separator = before === "" || before.endsWith("\n") ? "" : "\n";
|
|
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" };
|
|
583
|
+
}
|
|
584
|
+
/** The `imports` entries `steps` tells the reader to add, as JSON lines. */
|
|
585
|
+
function importLines(imports) {
|
|
586
|
+
return Object.entries(imports)
|
|
587
|
+
.map(([specifier, dependency]) => `"${specifier}": "${dependency}"`)
|
|
588
|
+
.join(", ");
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Where an `importMap` field points, as a path inside the setup directory, or
|
|
592
|
+
* `null` when it points anywhere else.
|
|
593
|
+
*
|
|
594
|
+
* Only a plain relative path is resolved. An absolute path, a URL, or one
|
|
595
|
+
* climbing out with `..` names a file this directory does not own, and setup
|
|
596
|
+
* has no business reading it just to decide what to print — those report as
|
|
597
|
+
* unverified instead.
|
|
598
|
+
*/
|
|
599
|
+
function delegatedMapPath(dir, importMap) {
|
|
600
|
+
const rel = importMap.replace(/^\.[\\/]/, "");
|
|
601
|
+
const escapes = rel === "" || rel.startsWith("/") || rel.startsWith("\\") ||
|
|
602
|
+
/^[a-z][a-z0-9+.-]*:/i.test(rel) || rel.split(/[\\/]/).includes("..");
|
|
603
|
+
return escapes ? null : joinPath(dir, rel);
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Whether the import map at `path` already declares every scaffold specifier.
|
|
607
|
+
*
|
|
608
|
+
* Setup cannot write into a delegated import map — it is the project's file,
|
|
609
|
+
* in a shape setup did not choose — but it can read it, and must: without this
|
|
610
|
+
* a project that has already added the entry would be told to add it again on
|
|
611
|
+
* every run, and `setup` would never exit 0 on a correctly configured project.
|
|
612
|
+
* Anything unreadable or unparseable answers `false`, so an unverifiable map
|
|
613
|
+
* still gets the manual step rather than a silent pass.
|
|
614
|
+
*/
|
|
615
|
+
async function delegatedMapDeclares(path, imports, host) {
|
|
616
|
+
try {
|
|
617
|
+
if (!(await host.exists(path)))
|
|
618
|
+
return false;
|
|
619
|
+
const parsed = JSON.parse(await host.readText(path));
|
|
620
|
+
if (!isRecord(parsed) || !isRecord(parsed.imports))
|
|
621
|
+
return false;
|
|
622
|
+
const declared = parsed.imports;
|
|
623
|
+
return Object.keys(imports).every((specifier) => specifier in declared);
|
|
624
|
+
}
|
|
625
|
+
catch {
|
|
626
|
+
return false;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Create or merge `deno.json`, returning what happened to it. `imports` are the
|
|
631
|
+
* bare specifiers the `buildFile` written alongside it resolves through, so the
|
|
632
|
+
* file is completed whenever any of them is missing — not only when the `zuke`
|
|
633
|
+
* task is.
|
|
634
|
+
*
|
|
635
|
+
* Two shapes cannot be completed automatically, and both are fatal to the
|
|
636
|
+
* scaffold now that the build resolves through this file rather than carrying
|
|
637
|
+
* its own `jsr:` specifier. They go into `steps` so the run reports an
|
|
638
|
+
* incomplete scaffold instead of pointing at a `./zuke` that cannot start:
|
|
639
|
+
*
|
|
640
|
+
* - **Unparseable.** `deno.json` is JSONC — Deno accepts `//` comments and
|
|
641
|
+
* trailing commas that `JSON.parse` rejects. Such a file is perfectly valid
|
|
642
|
+
* and must not be rewritten: doing so would strip the reader's comments.
|
|
643
|
+
* - **Delegated.** The document points `importMap` at a separate file, and
|
|
644
|
+
* `imports` belongs in *that* file (see {@link delegatesImportMap}).
|
|
645
|
+
*/
|
|
646
|
+
async function setupDenoJson(dir, imports, buildFile, host, steps, notes) {
|
|
647
|
+
const name = "deno.json";
|
|
648
|
+
const path = joinPath(dir, name);
|
|
649
|
+
if (!(await host.exists(path))) {
|
|
650
|
+
await host.writeText(path, mergeDenoJson(null, imports, buildFile));
|
|
651
|
+
host.log(` create ${name}`);
|
|
652
|
+
return { path: name, status: "created" };
|
|
653
|
+
}
|
|
654
|
+
const before = await host.readText(path);
|
|
655
|
+
const state = denoJsonState(before, imports, buildFile);
|
|
656
|
+
if (state === "unparseable") {
|
|
657
|
+
host.log(` skip ${name} (not plain JSON, edit by hand)`);
|
|
658
|
+
steps.push(`${path} has comments or a trailing comma, so it could not be edited ` +
|
|
659
|
+
`without discarding them. Add to its "imports": ` +
|
|
660
|
+
`${importLines(imports)} — the scaffolded ${buildFile} will not run until ` +
|
|
661
|
+
`you do.`);
|
|
662
|
+
return { path: name, status: "skipped" };
|
|
663
|
+
}
|
|
664
|
+
const parsed = JSON.parse(before);
|
|
665
|
+
const root = isRecord(parsed) ? parsed : {};
|
|
666
|
+
if (delegatesImportMap(root)) {
|
|
667
|
+
// Writing `imports` here would make Deno ignore `importMap` and take the
|
|
668
|
+
// project's whole module resolution down with it. Ask for the entry only
|
|
669
|
+
// when the delegated map does not already carry it — otherwise a project
|
|
670
|
+
// that has done exactly this would be nagged on every run.
|
|
671
|
+
const delegated = String(root.importMap);
|
|
672
|
+
const target = delegatedMapPath(dir, delegated);
|
|
673
|
+
const already = target !== null &&
|
|
674
|
+
await delegatedMapDeclares(target, imports, host);
|
|
675
|
+
if (!already) {
|
|
676
|
+
steps.push(`${path} delegates its import map to ${delegated}, which Deno ignores ` +
|
|
677
|
+
`as soon as "imports" appears beside it. Add to that file's ` +
|
|
678
|
+
`"imports" instead: ${importLines(imports)} — the scaffolded ` +
|
|
679
|
+
`${buildFile} will not run until you do.`);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
for (const specifier of remappedImports(root, imports)) {
|
|
683
|
+
notes.push(`${path} already maps "${specifier}" to something other than its JSR ` +
|
|
684
|
+
`package; setup kept that mapping. Check it is what you intend.`);
|
|
685
|
+
}
|
|
686
|
+
if (state === "present") {
|
|
687
|
+
host.log(` skip ${name} (zuke task and imports already declared)`);
|
|
688
|
+
return { path: name, status: "skipped" };
|
|
689
|
+
}
|
|
690
|
+
await host.writeText(path, mergeDenoJson(before, imports, buildFile));
|
|
691
|
+
host.log(` update ${name}`);
|
|
692
|
+
return { path: name, status: "overwritten" };
|
|
693
|
+
}
|