zappie 0.0.0 → 0.1.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/CHANGELOG.md +18 -0
- package/LICENSE +21 -0
- package/README.md +38 -2
- package/bin/zappie.js +2 -0
- package/dist/cli.js +1657 -0
- package/package.json +40 -5
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1657 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
import path15 from "path";
|
|
5
|
+
|
|
6
|
+
// src/args.ts
|
|
7
|
+
import { parseArgs } from "util";
|
|
8
|
+
|
|
9
|
+
// src/config.ts
|
|
10
|
+
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
11
|
+
import path from "path";
|
|
12
|
+
|
|
13
|
+
// src/errors.ts
|
|
14
|
+
var CliError = class extends Error {
|
|
15
|
+
constructor(message, exitCode = 1) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "CliError";
|
|
18
|
+
this.exitCode = exitCode;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/config.ts
|
|
23
|
+
var CONFIG_FILE = "zappie.json";
|
|
24
|
+
function normaliseApi(api) {
|
|
25
|
+
return api.replace(/\/+$/, "");
|
|
26
|
+
}
|
|
27
|
+
function readProjectConfig(dir) {
|
|
28
|
+
const file = path.join(dir, CONFIG_FILE);
|
|
29
|
+
if (!existsSync(file)) {
|
|
30
|
+
return void 0;
|
|
31
|
+
}
|
|
32
|
+
let source2;
|
|
33
|
+
try {
|
|
34
|
+
source2 = readFileSync(file, "utf8");
|
|
35
|
+
} catch {
|
|
36
|
+
throw new CliError(`We could not read ${file}.`);
|
|
37
|
+
}
|
|
38
|
+
let parsed;
|
|
39
|
+
try {
|
|
40
|
+
parsed = JSON.parse(source2);
|
|
41
|
+
} catch {
|
|
42
|
+
throw new CliError(`${file} is not valid JSON.`);
|
|
43
|
+
}
|
|
44
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
45
|
+
throw new CliError(`Put an object in ${file}.`);
|
|
46
|
+
}
|
|
47
|
+
const value = parsed;
|
|
48
|
+
if (value.api !== void 0 && typeof value.api !== "string") {
|
|
49
|
+
throw new CliError(`Use a string for "api" in ${file} when you set it.`);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
...value,
|
|
53
|
+
...value.api !== void 0 ? { api: normaliseApi(value.api) } : {}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function writeProjectConfig(dir, config) {
|
|
57
|
+
writeFileSync(
|
|
58
|
+
path.join(dir, CONFIG_FILE),
|
|
59
|
+
`${JSON.stringify(config, null, 2)}
|
|
60
|
+
`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
var DEFAULT_REGISTRY = "https://registry.zappie.ai/v1";
|
|
64
|
+
function registrySource(flag, configured) {
|
|
65
|
+
return flag ?? configured ?? DEFAULT_REGISTRY;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/args.ts
|
|
69
|
+
var registryFlag = "--registry <url|dir>";
|
|
70
|
+
var usage = `Usage: zappie <command>
|
|
71
|
+
|
|
72
|
+
Commands:
|
|
73
|
+
add <name...> Add components from a registry
|
|
74
|
+
list Show registry or installed components
|
|
75
|
+
diff [name] Compare your installed components with the registry
|
|
76
|
+
doctor Check your project and installed kit components
|
|
77
|
+
check [dir] Check your Zappie storefront project
|
|
78
|
+
verify [url] Test your storefront through customer journeys
|
|
79
|
+
|
|
80
|
+
Options:
|
|
81
|
+
${registryFlag} Use a registry URL or folder. Default: zappie.json, then ${DEFAULT_REGISTRY}
|
|
82
|
+
--json Print machine-readable output for add, list, diff, doctor, check or verify
|
|
83
|
+
--all Keep running check after a step fails
|
|
84
|
+
--suite <name> Run the standard or extended verify suite
|
|
85
|
+
--report <file> Save your verification report to a file
|
|
86
|
+
--key <pk_test_...> Use this test publishable key for verify
|
|
87
|
+
--store <slug> Choose the store the runner verifies
|
|
88
|
+
--overwrite Replace files you changed locally during add
|
|
89
|
+
--dry-run Show an add or diff --apply plan without writing
|
|
90
|
+
--apply Apply registry updates with a 3-way merge
|
|
91
|
+
--no-install Skip installing missing npm dependencies
|
|
92
|
+
--installed Show your installed components
|
|
93
|
+
-h, --help Show this help`;
|
|
94
|
+
function noArguments(name2, rest) {
|
|
95
|
+
if (rest.length > 0) {
|
|
96
|
+
throw new CliError(`Run ${name2} without arguments.
|
|
97
|
+
|
|
98
|
+
${usage}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function parseCommand(argv) {
|
|
102
|
+
let parsed;
|
|
103
|
+
try {
|
|
104
|
+
parsed = parseArgs({
|
|
105
|
+
args: argv,
|
|
106
|
+
options: {
|
|
107
|
+
registry: { type: "string" },
|
|
108
|
+
json: { type: "boolean" },
|
|
109
|
+
overwrite: { type: "boolean" },
|
|
110
|
+
"dry-run": { type: "boolean" },
|
|
111
|
+
"no-install": { type: "boolean" },
|
|
112
|
+
installed: { type: "boolean" },
|
|
113
|
+
apply: { type: "boolean" },
|
|
114
|
+
all: { type: "boolean" },
|
|
115
|
+
suite: { type: "string" },
|
|
116
|
+
report: { type: "string" },
|
|
117
|
+
key: { type: "string" },
|
|
118
|
+
store: { type: "string" },
|
|
119
|
+
help: { type: "boolean", short: "h" }
|
|
120
|
+
},
|
|
121
|
+
allowPositionals: true,
|
|
122
|
+
strict: true
|
|
123
|
+
});
|
|
124
|
+
} catch (error) {
|
|
125
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
126
|
+
throw new CliError(`${reason}
|
|
127
|
+
|
|
128
|
+
${usage}`);
|
|
129
|
+
}
|
|
130
|
+
const { values, positionals } = parsed;
|
|
131
|
+
const registry = typeof values.registry === "string" ? values.registry : void 0;
|
|
132
|
+
const json4 = values.json === true;
|
|
133
|
+
const [name2, ...rest] = positionals;
|
|
134
|
+
if (values.help === true || name2 === void 0 || name2 === "help") {
|
|
135
|
+
return { name: "help" };
|
|
136
|
+
}
|
|
137
|
+
if (name2 === "add") {
|
|
138
|
+
if (rest.length === 0) {
|
|
139
|
+
throw new CliError(`Name at least one component to add.
|
|
140
|
+
|
|
141
|
+
${usage}`);
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
name: name2,
|
|
145
|
+
items: rest,
|
|
146
|
+
registry,
|
|
147
|
+
json: json4,
|
|
148
|
+
overwrite: values.overwrite === true,
|
|
149
|
+
dryRun: values["dry-run"] === true,
|
|
150
|
+
install: values["no-install"] !== true
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (name2 === "list") {
|
|
154
|
+
noArguments(name2, rest);
|
|
155
|
+
return { name: name2, registry, json: json4, installed: values.installed === true };
|
|
156
|
+
}
|
|
157
|
+
if (name2 === "diff") {
|
|
158
|
+
if (rest.length > 1) {
|
|
159
|
+
throw new CliError(`Name at most one component to diff.
|
|
160
|
+
|
|
161
|
+
${usage}`);
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
name: name2,
|
|
165
|
+
item: rest[0],
|
|
166
|
+
registry,
|
|
167
|
+
json: json4,
|
|
168
|
+
apply: values.apply === true,
|
|
169
|
+
dryRun: values["dry-run"] === true
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (name2 === "doctor") {
|
|
173
|
+
noArguments(name2, rest);
|
|
174
|
+
return { name: name2, registry, json: json4 };
|
|
175
|
+
}
|
|
176
|
+
if (name2 === "check") {
|
|
177
|
+
if (rest.length > 1) {
|
|
178
|
+
throw new CliError(`Pass check at most one directory.
|
|
179
|
+
|
|
180
|
+
${usage}`);
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
name: name2,
|
|
184
|
+
directory: rest[0],
|
|
185
|
+
all: values.all === true,
|
|
186
|
+
json: json4
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
if (name2 === "verify") {
|
|
190
|
+
if (rest.length > 1) {
|
|
191
|
+
throw new CliError(`Pass verify at most one URL.
|
|
192
|
+
|
|
193
|
+
${usage}`);
|
|
194
|
+
}
|
|
195
|
+
const suite = values.suite ?? "standard";
|
|
196
|
+
if (suite !== "standard" && suite !== "extended") {
|
|
197
|
+
throw new CliError("Choose standard or extended for --suite.");
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
name: name2,
|
|
201
|
+
url: rest[0],
|
|
202
|
+
suite,
|
|
203
|
+
report: typeof values.report === "string" ? values.report : void 0,
|
|
204
|
+
key: typeof values.key === "string" ? values.key : void 0,
|
|
205
|
+
store: typeof values.store === "string" ? values.store : void 0,
|
|
206
|
+
json: json4
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
throw new CliError(`Command "${name2}" is unknown.
|
|
210
|
+
|
|
211
|
+
${usage}`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// src/project-package.ts
|
|
215
|
+
import { createRequire } from "module";
|
|
216
|
+
import path2 from "path";
|
|
217
|
+
import { pathToFileURL } from "url";
|
|
218
|
+
async function loadProjectPackage(cwd, name2, manager) {
|
|
219
|
+
const require2 = createRequire(path2.join(cwd, "package.json"));
|
|
220
|
+
let entry;
|
|
221
|
+
try {
|
|
222
|
+
entry = require2.resolve(name2);
|
|
223
|
+
} catch {
|
|
224
|
+
throw new CliError(
|
|
225
|
+
`You have not installed ${name2}. Install it with: ${installCommand(manager, name2)}`
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
return await import(pathToFileURL(entry).href);
|
|
229
|
+
}
|
|
230
|
+
function installCommand(manager, name2) {
|
|
231
|
+
if (manager === "npm") {
|
|
232
|
+
return `npm install --save-dev ${name2}`;
|
|
233
|
+
}
|
|
234
|
+
if (manager === "yarn") {
|
|
235
|
+
return `yarn add --dev ${name2}`;
|
|
236
|
+
}
|
|
237
|
+
if (manager === "bun") {
|
|
238
|
+
return `bun add --dev ${name2}`;
|
|
239
|
+
}
|
|
240
|
+
return `pnpm add --save-dev ${name2}`;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// src/check-command.ts
|
|
244
|
+
async function runProjectCheck(options) {
|
|
245
|
+
const loaded = await (options.load ?? loadProjectCheck)(
|
|
246
|
+
options.cwd,
|
|
247
|
+
options.packageManager
|
|
248
|
+
);
|
|
249
|
+
const result = await loaded.runCheck({
|
|
250
|
+
cwd: options.cwd,
|
|
251
|
+
all: options.all,
|
|
252
|
+
packageManager: options.packageManager,
|
|
253
|
+
...options.signal ? { signal: options.signal } : {}
|
|
254
|
+
});
|
|
255
|
+
const print = options.print ?? console.log;
|
|
256
|
+
print(options.json ? JSON.stringify(result) : humanOutput(result));
|
|
257
|
+
return result;
|
|
258
|
+
}
|
|
259
|
+
async function loadProjectCheck(cwd, manager) {
|
|
260
|
+
const loaded = await loadProjectPackage(
|
|
261
|
+
cwd,
|
|
262
|
+
"@zappie/check",
|
|
263
|
+
manager
|
|
264
|
+
);
|
|
265
|
+
if (typeof loaded.runCheck !== "function") {
|
|
266
|
+
throw new CliError(
|
|
267
|
+
"Your installed @zappie/check does not export runCheck."
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
return loaded;
|
|
271
|
+
}
|
|
272
|
+
function humanOutput(result) {
|
|
273
|
+
const lines2 = [];
|
|
274
|
+
for (const checked of result.steps) {
|
|
275
|
+
lines2.push(`${checked.name}: ${checked.result}`);
|
|
276
|
+
for (const issue of checked.problems) {
|
|
277
|
+
const location = issue.file ? `${issue.file}${issue.line ? `:${issue.line}` : ""}` : "project";
|
|
278
|
+
lines2.push(
|
|
279
|
+
` ${location}${issue.rule ? ` [${issue.rule}]` : ""} ${issue.message}`
|
|
280
|
+
);
|
|
281
|
+
if (issue.fix) {
|
|
282
|
+
lines2.push(` Fix: ${issue.fix}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return lines2.join("\n");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/doctor.ts
|
|
290
|
+
import path7 from "path";
|
|
291
|
+
import { fileURLToPath } from "url";
|
|
292
|
+
|
|
293
|
+
// src/doctor-checks.ts
|
|
294
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
295
|
+
import path4 from "path";
|
|
296
|
+
import { satisfies } from "semver";
|
|
297
|
+
|
|
298
|
+
// src/hash.ts
|
|
299
|
+
import { createHash } from "crypto";
|
|
300
|
+
function sha256(content) {
|
|
301
|
+
return createHash("sha256").update(content).digest("hex");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// src/kit-project.ts
|
|
305
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
306
|
+
import path3 from "path";
|
|
307
|
+
function json(file) {
|
|
308
|
+
let source2;
|
|
309
|
+
try {
|
|
310
|
+
source2 = readFileSync2(file, "utf8");
|
|
311
|
+
} catch {
|
|
312
|
+
throw new CliError(`We could not read ${file}.`);
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
return JSON.parse(source2);
|
|
316
|
+
} catch {
|
|
317
|
+
throw new CliError(`${file} is not valid JSON.`);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
function packageJson(cwd) {
|
|
321
|
+
const file = path3.join(cwd, "package.json");
|
|
322
|
+
if (!existsSync2(file)) {
|
|
323
|
+
throw new CliError(`There is no package.json in ${cwd}.`);
|
|
324
|
+
}
|
|
325
|
+
return json(file);
|
|
326
|
+
}
|
|
327
|
+
function detectFramework(cwd) {
|
|
328
|
+
const value = packageJson(cwd);
|
|
329
|
+
const dependencies = {
|
|
330
|
+
...value.dependencies,
|
|
331
|
+
...value.devDependencies,
|
|
332
|
+
...value.peerDependencies
|
|
333
|
+
};
|
|
334
|
+
if (dependencies.next) {
|
|
335
|
+
return "next";
|
|
336
|
+
}
|
|
337
|
+
if (dependencies["react-router"] || dependencies["react-router-dom"]) {
|
|
338
|
+
return "react-router";
|
|
339
|
+
}
|
|
340
|
+
if (dependencies.vite) {
|
|
341
|
+
return "vite";
|
|
342
|
+
}
|
|
343
|
+
throw new CliError("We could not detect React Router, Next.js or Vite.");
|
|
344
|
+
}
|
|
345
|
+
function defaultDirectory(cwd, framework) {
|
|
346
|
+
if (framework === "react-router") {
|
|
347
|
+
return "app/components/zappie";
|
|
348
|
+
}
|
|
349
|
+
if (framework === "next" && !existsSync2(path3.join(cwd, "src"))) {
|
|
350
|
+
return "components/zappie";
|
|
351
|
+
}
|
|
352
|
+
return "src/components/zappie";
|
|
353
|
+
}
|
|
354
|
+
function parseTsconfig(cwd) {
|
|
355
|
+
const file = path3.join(cwd, "tsconfig.json");
|
|
356
|
+
if (!existsSync2(file)) {
|
|
357
|
+
return void 0;
|
|
358
|
+
}
|
|
359
|
+
const source2 = readFileSync2(file, "utf8").replace(/\/\*[\s\S]*?\*\//g, "").replace(/(^|\s)\/\/.*$/gm, "$1").replace(/,\s*([}\]])/g, "$1");
|
|
360
|
+
try {
|
|
361
|
+
return JSON.parse(source2);
|
|
362
|
+
} catch {
|
|
363
|
+
throw new CliError(`${file} is not valid JSON or JSONC.`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function detectAlias(cwd, directory) {
|
|
367
|
+
const config = parseTsconfig(cwd);
|
|
368
|
+
const compiler = config?.compilerOptions;
|
|
369
|
+
if (typeof compiler !== "object" || compiler === null) {
|
|
370
|
+
return void 0;
|
|
371
|
+
}
|
|
372
|
+
const paths = compiler.paths;
|
|
373
|
+
if (typeof paths !== "object" || paths === null) {
|
|
374
|
+
return void 0;
|
|
375
|
+
}
|
|
376
|
+
const target = directory.replaceAll("\\", "/");
|
|
377
|
+
for (const [alias, entries] of Object.entries(paths)) {
|
|
378
|
+
if (!Array.isArray(entries)) {
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
for (const entry of entries) {
|
|
382
|
+
if (typeof entry !== "string") {
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const normalized = entry.replace(/^\.\//, "").replaceAll("\\", "/");
|
|
386
|
+
const star = normalized.indexOf("*");
|
|
387
|
+
const prefix = star < 0 ? normalized : normalized.slice(0, star);
|
|
388
|
+
if (!target.startsWith(prefix)) {
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const remainder = target.slice(prefix.length);
|
|
392
|
+
return star < 0 ? alias : alias.replace("*", remainder);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return void 0;
|
|
396
|
+
}
|
|
397
|
+
function kitProject(cwd) {
|
|
398
|
+
const current = readProjectConfig(cwd) ?? {};
|
|
399
|
+
const { alias: configuredAlias, ...rest } = current;
|
|
400
|
+
const framework = current.framework ?? detectFramework(cwd);
|
|
401
|
+
const componentsDir = current.componentsDir ?? defaultDirectory(cwd, framework);
|
|
402
|
+
const alias = configuredAlias ?? detectAlias(cwd, componentsDir);
|
|
403
|
+
return {
|
|
404
|
+
...rest,
|
|
405
|
+
framework,
|
|
406
|
+
componentsDir: componentsDir.replaceAll("\\", "/"),
|
|
407
|
+
...alias !== void 0 ? { alias } : {},
|
|
408
|
+
tsx: current.tsx ?? true,
|
|
409
|
+
installed: current.installed ?? {}
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// src/doctor-checks.ts
|
|
414
|
+
function check(name2, status, message, fix) {
|
|
415
|
+
return { name: name2, status, message, ...fix ? { fix } : {} };
|
|
416
|
+
}
|
|
417
|
+
function json2(file) {
|
|
418
|
+
try {
|
|
419
|
+
return JSON.parse(readFileSync3(file, "utf8"));
|
|
420
|
+
} catch {
|
|
421
|
+
return void 0;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
function configCheck(file, raw, errors) {
|
|
425
|
+
if (errors.length === 0) {
|
|
426
|
+
return check("config", "ok", "zappie.json matches the kit project schema.");
|
|
427
|
+
}
|
|
428
|
+
if (!existsSync3(file)) {
|
|
429
|
+
return check(
|
|
430
|
+
"config",
|
|
431
|
+
"fail",
|
|
432
|
+
"There is no zappie.json in this project.",
|
|
433
|
+
"Run zappie add to create it."
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
if (raw === void 0) {
|
|
437
|
+
return check(
|
|
438
|
+
"config",
|
|
439
|
+
"fail",
|
|
440
|
+
"We could not read zappie.json as JSON.",
|
|
441
|
+
"Fix the JSON in zappie.json."
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
return check(
|
|
445
|
+
"config",
|
|
446
|
+
"fail",
|
|
447
|
+
errors.join(" "),
|
|
448
|
+
"Fix these fields in zappie.json."
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
function nodeCheck(version2, range) {
|
|
452
|
+
return satisfies(version2, range) ? check("node", "ok", `Your Node ${version2} meets ${range}.`) : check(
|
|
453
|
+
"node",
|
|
454
|
+
"fail",
|
|
455
|
+
`Your Node ${version2} does not meet ${range}.`,
|
|
456
|
+
`Install Node ${range}.`
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
function frameworkCheck(cwd, config) {
|
|
460
|
+
try {
|
|
461
|
+
const detected = detectFramework(cwd);
|
|
462
|
+
if (config?.framework && config.framework !== detected) {
|
|
463
|
+
return check(
|
|
464
|
+
"framework",
|
|
465
|
+
"fail",
|
|
466
|
+
`Found ${detected}, but zappie.json says ${config.framework}.`,
|
|
467
|
+
"Update zappie.json to match your project."
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
const directory = config?.componentsDir;
|
|
471
|
+
if (!directory || !existsSync3(path4.join(cwd, directory))) {
|
|
472
|
+
return check(
|
|
473
|
+
"framework",
|
|
474
|
+
"fail",
|
|
475
|
+
`Found ${detected}, but the components directory is missing.`,
|
|
476
|
+
"Create your configured componentsDir or run zappie add."
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
return check("framework", "ok", `Found ${detected}. ${directory} exists.`);
|
|
480
|
+
} catch (error) {
|
|
481
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
482
|
+
return check(
|
|
483
|
+
"framework",
|
|
484
|
+
"fail",
|
|
485
|
+
message,
|
|
486
|
+
"Add React Router, Next.js or Vite and configure componentsDir."
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
function filesCheck(cwd, config) {
|
|
491
|
+
const modified2 = Object.entries(config?.installed ?? {}).flatMap(
|
|
492
|
+
([name2, item2]) => Object.entries(item2.files).filter(([file, expected]) => {
|
|
493
|
+
const target = path4.join(cwd, file);
|
|
494
|
+
return !existsSync3(target) || sha256(readFileSync3(target, "utf8")) !== expected;
|
|
495
|
+
}).map(([file]) => `${name2}:${file}`)
|
|
496
|
+
);
|
|
497
|
+
return modified2.length === 0 ? check(
|
|
498
|
+
"files",
|
|
499
|
+
"ok",
|
|
500
|
+
"Your installed component files match their recorded hashes."
|
|
501
|
+
) : check(
|
|
502
|
+
"files",
|
|
503
|
+
"warn",
|
|
504
|
+
`Changed or missing files: ${modified2.join(", ")}.`,
|
|
505
|
+
"Run zappie diff before you apply an update."
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
function doctorText(checks) {
|
|
509
|
+
return checks.flatMap((entry) => [
|
|
510
|
+
`[${entry.status}] ${entry.name}: ${entry.message}`,
|
|
511
|
+
...entry.fix ? [` Fix: ${entry.fix}`] : []
|
|
512
|
+
]).join("\n");
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// src/doctor-registry.ts
|
|
516
|
+
import path6 from "path";
|
|
517
|
+
import { satisfies as satisfies2 } from "semver";
|
|
518
|
+
|
|
519
|
+
// src/registry.ts
|
|
520
|
+
import { readFile } from "fs/promises";
|
|
521
|
+
import path5 from "path";
|
|
522
|
+
function object(value, label) {
|
|
523
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
524
|
+
throw new CliError(`Registry ${label} is invalid.`);
|
|
525
|
+
}
|
|
526
|
+
return value;
|
|
527
|
+
}
|
|
528
|
+
function string(value, label) {
|
|
529
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
530
|
+
throw new CliError(`Registry ${label} is invalid.`);
|
|
531
|
+
}
|
|
532
|
+
return value;
|
|
533
|
+
}
|
|
534
|
+
function record(value, label) {
|
|
535
|
+
const input = object(value, label);
|
|
536
|
+
return Object.fromEntries(
|
|
537
|
+
Object.entries(input).map(([key, entry]) => [key, string(entry, label)])
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
function item(value) {
|
|
541
|
+
const input = object(value, "item");
|
|
542
|
+
const type = string(input.type, "item type");
|
|
543
|
+
if (type !== "component" && type !== "block") {
|
|
544
|
+
throw new CliError("Registry item type is invalid.");
|
|
545
|
+
}
|
|
546
|
+
if (!Array.isArray(input.files) || !Array.isArray(input.registryDependencies)) {
|
|
547
|
+
throw new CliError("Registry item files or dependencies are invalid.");
|
|
548
|
+
}
|
|
549
|
+
return {
|
|
550
|
+
name: string(input.name, "item name"),
|
|
551
|
+
type,
|
|
552
|
+
version: string(input.version, "item version"),
|
|
553
|
+
title: string(input.title, "item title"),
|
|
554
|
+
description: string(input.description, "item description"),
|
|
555
|
+
requires: record(input.requires, "item requirements"),
|
|
556
|
+
dependencies: record(input.dependencies, "item dependencies"),
|
|
557
|
+
registryDependencies: input.registryDependencies.map(
|
|
558
|
+
(entry) => string(entry, "registry dependency")
|
|
559
|
+
),
|
|
560
|
+
files: input.files.map((entry) => {
|
|
561
|
+
const file = object(entry, "item file");
|
|
562
|
+
return {
|
|
563
|
+
path: string(file.path, "file path"),
|
|
564
|
+
target: string(file.target, "file target"),
|
|
565
|
+
sha256: string(file.sha256, "file hash"),
|
|
566
|
+
content: string(file.content, "file content")
|
|
567
|
+
};
|
|
568
|
+
})
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
function index(value) {
|
|
572
|
+
const input = object(value, "index");
|
|
573
|
+
if (!Array.isArray(input.items)) {
|
|
574
|
+
throw new CliError("Registry index items are invalid.");
|
|
575
|
+
}
|
|
576
|
+
return {
|
|
577
|
+
version: string(input.version, "index version"),
|
|
578
|
+
items: input.items.map((entry) => {
|
|
579
|
+
const value2 = item({
|
|
580
|
+
...object(entry, "index item"),
|
|
581
|
+
requires: {},
|
|
582
|
+
dependencies: {},
|
|
583
|
+
registryDependencies: [],
|
|
584
|
+
files: [
|
|
585
|
+
{ path: "index", target: "index", sha256: "index", content: "index" }
|
|
586
|
+
]
|
|
587
|
+
});
|
|
588
|
+
return {
|
|
589
|
+
name: value2.name,
|
|
590
|
+
type: value2.type,
|
|
591
|
+
version: value2.version,
|
|
592
|
+
title: value2.title,
|
|
593
|
+
description: value2.description
|
|
594
|
+
};
|
|
595
|
+
})
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
var Registry = class {
|
|
599
|
+
constructor(source2, cwd) {
|
|
600
|
+
this.source = source2;
|
|
601
|
+
this.cwd = cwd;
|
|
602
|
+
}
|
|
603
|
+
async read(file) {
|
|
604
|
+
try {
|
|
605
|
+
if (/^https?:\/\//.test(this.source)) {
|
|
606
|
+
const response = await fetch(
|
|
607
|
+
`${this.source.replace(/\/+$/, "")}/${file}`
|
|
608
|
+
);
|
|
609
|
+
if (!response.ok) {
|
|
610
|
+
throw new Error(`${response.status} ${response.statusText}`.trim());
|
|
611
|
+
}
|
|
612
|
+
return await response.json();
|
|
613
|
+
}
|
|
614
|
+
const root = path5.resolve(this.cwd, this.source);
|
|
615
|
+
return JSON.parse(await readFile(path5.join(root, file), "utf8"));
|
|
616
|
+
} catch (error) {
|
|
617
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
618
|
+
throw new CliError(`We could not read registry ${file}: ${message}`, 3);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
async index() {
|
|
622
|
+
return index(await this.read("index.json"));
|
|
623
|
+
}
|
|
624
|
+
async item(name2, version2) {
|
|
625
|
+
const suffix = version2 === void 0 ? name2 : `${name2}@${version2}`;
|
|
626
|
+
return item(await this.read(`items/${suffix}.json`));
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
// src/doctor-registry.ts
|
|
631
|
+
function packageVersion(cwd, name2) {
|
|
632
|
+
const file = path6.join(
|
|
633
|
+
cwd,
|
|
634
|
+
"node_modules",
|
|
635
|
+
...name2.split("/"),
|
|
636
|
+
"package.json"
|
|
637
|
+
);
|
|
638
|
+
const value = json2(file);
|
|
639
|
+
if (typeof value !== "object" || value === null) {
|
|
640
|
+
return void 0;
|
|
641
|
+
}
|
|
642
|
+
return typeof value.version === "string" ? value.version : void 0;
|
|
643
|
+
}
|
|
644
|
+
function dependencyChecks(cwd, manifests) {
|
|
645
|
+
const required = Object.assign(
|
|
646
|
+
{},
|
|
647
|
+
...manifests.map((item2) => item2.requires)
|
|
648
|
+
);
|
|
649
|
+
const dependencies = Object.assign(
|
|
650
|
+
{},
|
|
651
|
+
...manifests.map((item2) => item2.dependencies)
|
|
652
|
+
);
|
|
653
|
+
const bad = Object.entries(required).filter(([name2, range]) => {
|
|
654
|
+
const version2 = packageVersion(cwd, name2);
|
|
655
|
+
return version2 === void 0 || !satisfies2(version2, range);
|
|
656
|
+
});
|
|
657
|
+
const missing = Object.keys(dependencies).filter(
|
|
658
|
+
(name2) => packageVersion(cwd, name2) === void 0
|
|
659
|
+
);
|
|
660
|
+
return [
|
|
661
|
+
bad.length === 0 ? check(
|
|
662
|
+
"requires",
|
|
663
|
+
"ok",
|
|
664
|
+
"Your installed Zappie package versions meet every item requirement."
|
|
665
|
+
) : check(
|
|
666
|
+
"requires",
|
|
667
|
+
"fail",
|
|
668
|
+
`Unmet requirements: ${bad.map(([name2, range]) => `${name2} ${range}`).join(", ")}.`,
|
|
669
|
+
"Install versions that meet each registry item's requires field."
|
|
670
|
+
),
|
|
671
|
+
missing.length === 0 ? check(
|
|
672
|
+
"dependencies",
|
|
673
|
+
"ok",
|
|
674
|
+
"You have every registry npm dependency installed."
|
|
675
|
+
) : check(
|
|
676
|
+
"dependencies",
|
|
677
|
+
"fail",
|
|
678
|
+
`Missing dependencies: ${missing.join(", ")}.`,
|
|
679
|
+
"Install the missing npm dependencies."
|
|
680
|
+
)
|
|
681
|
+
];
|
|
682
|
+
}
|
|
683
|
+
function unavailable() {
|
|
684
|
+
const fix = "Restore registry access and run zappie doctor again.";
|
|
685
|
+
return [
|
|
686
|
+
check(
|
|
687
|
+
"requires",
|
|
688
|
+
"warn",
|
|
689
|
+
"Without the registry, we could not check package requirements.",
|
|
690
|
+
fix
|
|
691
|
+
),
|
|
692
|
+
check(
|
|
693
|
+
"dependencies",
|
|
694
|
+
"warn",
|
|
695
|
+
"Without the registry, we could not check registry dependencies.",
|
|
696
|
+
fix
|
|
697
|
+
)
|
|
698
|
+
];
|
|
699
|
+
}
|
|
700
|
+
async function registryChecks(cwd, source2, config) {
|
|
701
|
+
try {
|
|
702
|
+
const registry = new Registry(registrySource(source2), cwd);
|
|
703
|
+
await registry.index();
|
|
704
|
+
const manifests = await Promise.all(
|
|
705
|
+
Object.entries(config?.installed ?? {}).map(
|
|
706
|
+
([name2, item2]) => registry.item(name2, item2.version)
|
|
707
|
+
)
|
|
708
|
+
);
|
|
709
|
+
return [
|
|
710
|
+
check("registry", "ok", "We can reach your registry."),
|
|
711
|
+
...dependencyChecks(cwd, manifests)
|
|
712
|
+
];
|
|
713
|
+
} catch (error) {
|
|
714
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
715
|
+
return [
|
|
716
|
+
check(
|
|
717
|
+
"registry",
|
|
718
|
+
"warn",
|
|
719
|
+
message,
|
|
720
|
+
"Check the registry URL or try again when you are online."
|
|
721
|
+
),
|
|
722
|
+
...unavailable()
|
|
723
|
+
];
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// src/doctor-schema.ts
|
|
728
|
+
var frameworks = /* @__PURE__ */ new Set(["react-router", "next", "vite"]);
|
|
729
|
+
var allowed = /* @__PURE__ */ new Set([
|
|
730
|
+
"$schema",
|
|
731
|
+
"framework",
|
|
732
|
+
"componentsDir",
|
|
733
|
+
"alias",
|
|
734
|
+
"tsx",
|
|
735
|
+
"registry",
|
|
736
|
+
"installed"
|
|
737
|
+
]);
|
|
738
|
+
var name = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
739
|
+
var version = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
740
|
+
var hash = /^[a-f0-9]{64}$/;
|
|
741
|
+
function object2(value) {
|
|
742
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
743
|
+
}
|
|
744
|
+
function relative(value) {
|
|
745
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
746
|
+
return false;
|
|
747
|
+
}
|
|
748
|
+
const normalized = value.replaceAll("\\", "/");
|
|
749
|
+
return !normalized.startsWith("/") && !/^[A-Za-z]:\//.test(normalized) && !normalized.split("/").includes("..");
|
|
750
|
+
}
|
|
751
|
+
function validateProject(value) {
|
|
752
|
+
const errors = [];
|
|
753
|
+
if (!object2(value)) {
|
|
754
|
+
return ["Put an object in zappie.json."];
|
|
755
|
+
}
|
|
756
|
+
for (const key of Object.keys(value)) {
|
|
757
|
+
if (!allowed.has(key)) {
|
|
758
|
+
errors.push(`Field "${key}" is unknown.`);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
if (typeof value.$schema !== "string" || !URL.canParse(value.$schema)) {
|
|
762
|
+
errors.push("Use a URL for $schema.");
|
|
763
|
+
}
|
|
764
|
+
if (typeof value.framework !== "string" || !frameworks.has(value.framework)) {
|
|
765
|
+
errors.push("Set framework to react-router, next or vite.");
|
|
766
|
+
}
|
|
767
|
+
if (!relative(value.componentsDir)) {
|
|
768
|
+
errors.push("Keep componentsDir inside your project.");
|
|
769
|
+
}
|
|
770
|
+
if (value.alias !== void 0 && (typeof value.alias !== "string" || value.alias.length === 0)) {
|
|
771
|
+
errors.push("Use a non-empty string for alias.");
|
|
772
|
+
}
|
|
773
|
+
if (typeof value.tsx !== "boolean") {
|
|
774
|
+
errors.push("Use a boolean for tsx.");
|
|
775
|
+
}
|
|
776
|
+
if (typeof value.registry !== "string" || value.registry.length === 0) {
|
|
777
|
+
errors.push("Set registry to a URL or local directory.");
|
|
778
|
+
}
|
|
779
|
+
if (!object2(value.installed)) {
|
|
780
|
+
errors.push("Use an object for installed.");
|
|
781
|
+
return errors;
|
|
782
|
+
}
|
|
783
|
+
for (const [itemName, item2] of Object.entries(value.installed)) {
|
|
784
|
+
if (!name.test(itemName) || !object2(item2)) {
|
|
785
|
+
errors.push(`installed.${itemName} is invalid.`);
|
|
786
|
+
continue;
|
|
787
|
+
}
|
|
788
|
+
if (typeof item2.version !== "string" || !version.test(item2.version)) {
|
|
789
|
+
errors.push(`installed.${itemName}.version is invalid.`);
|
|
790
|
+
}
|
|
791
|
+
if (!object2(item2.files)) {
|
|
792
|
+
errors.push(`installed.${itemName}.files is invalid.`);
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
for (const [file, fileHash] of Object.entries(item2.files)) {
|
|
796
|
+
if (!relative(file) || typeof fileHash !== "string" || !hash.test(fileHash)) {
|
|
797
|
+
errors.push(`installed.${itemName}.files.${file} is invalid.`);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
return errors;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// src/doctor.ts
|
|
805
|
+
async function doctor(options) {
|
|
806
|
+
const print = options.print ?? console.log;
|
|
807
|
+
const file = path7.join(options.cwd, CONFIG_FILE);
|
|
808
|
+
const raw = json2(file);
|
|
809
|
+
const errors = validateProject(raw);
|
|
810
|
+
const config = errors.length === 0 ? raw : void 0;
|
|
811
|
+
const packageFile = json2(
|
|
812
|
+
fileURLToPath(new URL("../package.json", import.meta.url))
|
|
813
|
+
);
|
|
814
|
+
const range = options.engineRange ?? packageFile?.engines?.node ?? ">=20";
|
|
815
|
+
const checks = [
|
|
816
|
+
nodeCheck(options.nodeVersion ?? process.versions.node, range),
|
|
817
|
+
configCheck(file, raw, errors),
|
|
818
|
+
frameworkCheck(options.cwd, config),
|
|
819
|
+
filesCheck(options.cwd, config),
|
|
820
|
+
...await registryChecks(
|
|
821
|
+
options.cwd,
|
|
822
|
+
options.source ?? config?.registry,
|
|
823
|
+
config
|
|
824
|
+
)
|
|
825
|
+
];
|
|
826
|
+
const result = { command: "doctor", checks };
|
|
827
|
+
print(options.json ? JSON.stringify(result) : doctorText(checks));
|
|
828
|
+
return { ...result, failed: checks.some((entry) => entry.status === "fail") };
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// src/kit-add.ts
|
|
832
|
+
import { existsSync as existsSync5, mkdirSync, readFileSync as readFileSync5, writeFileSync as writeFileSync2 } from "fs";
|
|
833
|
+
import path11 from "path";
|
|
834
|
+
|
|
835
|
+
// src/kit-files.ts
|
|
836
|
+
import path8 from "path";
|
|
837
|
+
function safeTarget(cwd, target) {
|
|
838
|
+
const normalized = target.replaceAll("\\", "/");
|
|
839
|
+
if (normalized.startsWith("/") || /^[A-Za-z]:\//.test(normalized) || normalized.split("/").includes("..")) {
|
|
840
|
+
throw new CliError(
|
|
841
|
+
`Registry target points outside your project root: ${target}`
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
const absolute = path8.resolve(cwd, normalized);
|
|
845
|
+
if (absolute !== cwd && !absolute.startsWith(`${cwd}${path8.sep}`)) {
|
|
846
|
+
throw new CliError(
|
|
847
|
+
`Registry target points outside your project root: ${target}`
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
return { normalized, absolute };
|
|
851
|
+
}
|
|
852
|
+
function verifySource(item2, file) {
|
|
853
|
+
if (sha256(file.content) !== file.sha256) {
|
|
854
|
+
throw new CliError(`The registry hash does not match ${item2}.`);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// src/kit-imports.ts
|
|
859
|
+
import path9 from "path";
|
|
860
|
+
var IMPORT = /(\bfrom\s+|\bimport\s*)(["'])(@\/components\/zappie\/([^"']+))\2/g;
|
|
861
|
+
function posixRelative(from, to) {
|
|
862
|
+
const value = path9.posix.relative(path9.posix.dirname(from), to);
|
|
863
|
+
return value.startsWith(".") ? value : `./${value}`;
|
|
864
|
+
}
|
|
865
|
+
function rewriteKitImports(content, target, componentsDir, alias) {
|
|
866
|
+
return content.replace(
|
|
867
|
+
IMPORT,
|
|
868
|
+
(match, keyword, quote, specifier, sibling) => {
|
|
869
|
+
const replacement = alias ? `${alias.replace(/\/$/, "")}/${sibling}` : posixRelative(target, `${componentsDir}/${sibling}`);
|
|
870
|
+
return `${keyword}${quote}${replacement}${quote}`;
|
|
871
|
+
}
|
|
872
|
+
);
|
|
873
|
+
}
|
|
874
|
+
function installTarget(target, componentsDir) {
|
|
875
|
+
const normalized = target.replaceAll("\\", "/");
|
|
876
|
+
return normalized.startsWith("components/zappie/") ? `${componentsDir}/${normalized.slice("components/zappie/".length)}` : normalized;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// src/package-manager.ts
|
|
880
|
+
import { spawnSync } from "child_process";
|
|
881
|
+
import { existsSync as existsSync4, readFileSync as readFileSync4 } from "fs";
|
|
882
|
+
import path10 from "path";
|
|
883
|
+
function detectPackageManager(cwd) {
|
|
884
|
+
for (let directory = cwd; ; directory = path10.dirname(directory)) {
|
|
885
|
+
if (existsSync4(path10.join(directory, "pnpm-lock.yaml"))) {
|
|
886
|
+
return "pnpm";
|
|
887
|
+
}
|
|
888
|
+
if (existsSync4(path10.join(directory, "package-lock.json"))) {
|
|
889
|
+
return "npm";
|
|
890
|
+
}
|
|
891
|
+
if (existsSync4(path10.join(directory, "yarn.lock"))) {
|
|
892
|
+
return "yarn";
|
|
893
|
+
}
|
|
894
|
+
if (existsSync4(path10.join(directory, "bun.lock")) || existsSync4(path10.join(directory, "bun.lockb"))) {
|
|
895
|
+
return "bun";
|
|
896
|
+
}
|
|
897
|
+
if (path10.dirname(directory) === directory) {
|
|
898
|
+
break;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
return "npm";
|
|
902
|
+
}
|
|
903
|
+
function missingDependencies(cwd, requested) {
|
|
904
|
+
const manifest = JSON.parse(
|
|
905
|
+
readFileSync4(path10.join(cwd, "package.json"), "utf8")
|
|
906
|
+
);
|
|
907
|
+
const present = {
|
|
908
|
+
...manifest.dependencies,
|
|
909
|
+
...manifest.devDependencies,
|
|
910
|
+
...manifest.peerDependencies
|
|
911
|
+
};
|
|
912
|
+
return Object.entries(requested).filter(([name2]) => !(name2 in present)).map(([name2, version2]) => `${name2}@${version2}`);
|
|
913
|
+
}
|
|
914
|
+
function installDependencies(cwd, dependencies, quiet = false) {
|
|
915
|
+
if (dependencies.length === 0) {
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
const manager = detectPackageManager(cwd);
|
|
919
|
+
const command = manager === "npm" ? "install" : "add";
|
|
920
|
+
const result = spawnSync(manager, [command, ...dependencies], {
|
|
921
|
+
cwd,
|
|
922
|
+
stdio: quiet ? ["ignore", "ignore", "inherit"] : "inherit"
|
|
923
|
+
});
|
|
924
|
+
if (result.status !== 0) {
|
|
925
|
+
throw new CliError(`${manager} could not install your dependencies.`);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
// src/registry-graph.ts
|
|
930
|
+
async function batches(values, size, read) {
|
|
931
|
+
const output = [];
|
|
932
|
+
for (let index2 = 0; index2 < values.length; index2 += size) {
|
|
933
|
+
output.push(
|
|
934
|
+
...await Promise.all(values.slice(index2, index2 + size).map(read))
|
|
935
|
+
);
|
|
936
|
+
}
|
|
937
|
+
return output;
|
|
938
|
+
}
|
|
939
|
+
async function resolveRegistryItems(registry, requested) {
|
|
940
|
+
const index2 = await registry.index();
|
|
941
|
+
const known = new Set(index2.items.map((entry) => entry.name));
|
|
942
|
+
for (const name2 of requested) {
|
|
943
|
+
if (!known.has(name2)) {
|
|
944
|
+
throw new CliError(`Registry item "${name2}" is unknown.`);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
const items = /* @__PURE__ */ new Map();
|
|
948
|
+
let pending = [...new Set(requested)];
|
|
949
|
+
while (pending.length > 0) {
|
|
950
|
+
const loaded = await batches(pending, 6, (name2) => registry.item(name2));
|
|
951
|
+
for (const value of loaded) {
|
|
952
|
+
if (!known.has(value.name)) {
|
|
953
|
+
throw new CliError(`Registry item "${value.name}" is unknown.`);
|
|
954
|
+
}
|
|
955
|
+
items.set(value.name, value);
|
|
956
|
+
}
|
|
957
|
+
const dependencies = [];
|
|
958
|
+
for (const value of loaded) {
|
|
959
|
+
for (const dependency of value.registryDependencies) {
|
|
960
|
+
if (!known.has(dependency)) {
|
|
961
|
+
throw new CliError(
|
|
962
|
+
`${valueName(value)} needs unknown registry item "${dependency}".`
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
if (!items.has(dependency)) {
|
|
966
|
+
dependencies.push(dependency);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
pending = [...new Set(dependencies)];
|
|
971
|
+
}
|
|
972
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
973
|
+
const visited = /* @__PURE__ */ new Set();
|
|
974
|
+
const ordered = [];
|
|
975
|
+
const visit = (name2, trail) => {
|
|
976
|
+
if (visiting.has(name2)) {
|
|
977
|
+
throw new CliError(
|
|
978
|
+
`Registry dependencies form a cycle: ${[...trail, name2].join(" -> ")}.`
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
if (visited.has(name2)) {
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
const value = items.get(name2);
|
|
985
|
+
if (!value) {
|
|
986
|
+
throw new CliError(`Registry item "${name2}" is unknown.`);
|
|
987
|
+
}
|
|
988
|
+
visiting.add(name2);
|
|
989
|
+
for (const dependency of value.registryDependencies) {
|
|
990
|
+
visit(dependency, [...trail, name2]);
|
|
991
|
+
}
|
|
992
|
+
visiting.delete(name2);
|
|
993
|
+
visited.add(name2);
|
|
994
|
+
ordered.push(value);
|
|
995
|
+
};
|
|
996
|
+
for (const name2 of requested) {
|
|
997
|
+
visit(name2, []);
|
|
998
|
+
}
|
|
999
|
+
return ordered;
|
|
1000
|
+
}
|
|
1001
|
+
function valueName(value) {
|
|
1002
|
+
return `Registry item "${value.name}"`;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
// src/kit-add.ts
|
|
1006
|
+
async function addItems(options) {
|
|
1007
|
+
const print = options.print ?? console.log;
|
|
1008
|
+
const project2 = kitProject(options.cwd);
|
|
1009
|
+
const registry = new Registry(options.source, options.cwd);
|
|
1010
|
+
const items = await resolveRegistryItems(registry, options.names);
|
|
1011
|
+
const planned = [];
|
|
1012
|
+
for (const item2 of items) {
|
|
1013
|
+
for (const file of item2.files) {
|
|
1014
|
+
verifySource(`${item2.name}/${file.path}`, file);
|
|
1015
|
+
const target = installTarget(file.target, project2.componentsDir);
|
|
1016
|
+
const safe = safeTarget(options.cwd, target);
|
|
1017
|
+
const content = rewriteKitImports(
|
|
1018
|
+
file.content,
|
|
1019
|
+
safe.normalized,
|
|
1020
|
+
project2.componentsDir,
|
|
1021
|
+
project2.alias
|
|
1022
|
+
);
|
|
1023
|
+
const installedHash = project2.installed[item2.name]?.files[safe.normalized];
|
|
1024
|
+
if (existsSync5(safe.absolute)) {
|
|
1025
|
+
const currentHash = sha256(readFileSync5(safe.absolute, "utf8"));
|
|
1026
|
+
if (currentHash !== installedHash && !options.overwrite) {
|
|
1027
|
+
throw new CliError(
|
|
1028
|
+
`${safe.normalized} has local changes. Pass --overwrite to replace it.`
|
|
1029
|
+
);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
planned.push({
|
|
1033
|
+
item: item2.name,
|
|
1034
|
+
target: safe.normalized,
|
|
1035
|
+
content,
|
|
1036
|
+
hash: sha256(content)
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
const dependencies = missingDependencies(
|
|
1041
|
+
options.cwd,
|
|
1042
|
+
Object.assign({}, ...items.map((item2) => item2.dependencies))
|
|
1043
|
+
);
|
|
1044
|
+
const result = {
|
|
1045
|
+
command: "add",
|
|
1046
|
+
dryRun: options.dryRun,
|
|
1047
|
+
items: items.map((item2) => item2.name),
|
|
1048
|
+
files: planned.map((file) => file.target),
|
|
1049
|
+
dependencies
|
|
1050
|
+
};
|
|
1051
|
+
if (options.dryRun) {
|
|
1052
|
+
print(options.json ? JSON.stringify(result) : planText(result));
|
|
1053
|
+
return result;
|
|
1054
|
+
}
|
|
1055
|
+
if (options.install) {
|
|
1056
|
+
installDependencies(options.cwd, dependencies, options.json);
|
|
1057
|
+
}
|
|
1058
|
+
for (const file of planned) {
|
|
1059
|
+
const absolute = path11.join(options.cwd, file.target);
|
|
1060
|
+
mkdirSync(path11.dirname(absolute), { recursive: true });
|
|
1061
|
+
writeFileSync2(absolute, file.content);
|
|
1062
|
+
}
|
|
1063
|
+
const installed = { ...project2.installed };
|
|
1064
|
+
for (const item2 of items) {
|
|
1065
|
+
const files2 = Object.fromEntries(
|
|
1066
|
+
planned.filter((file) => file.item === item2.name).map((file) => [file.target, file.hash])
|
|
1067
|
+
);
|
|
1068
|
+
installed[item2.name] = {
|
|
1069
|
+
version: item2.version,
|
|
1070
|
+
files: files2
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
writeProjectConfig(options.cwd, {
|
|
1074
|
+
...project2,
|
|
1075
|
+
$schema: "https://registry.zappie.ai/v1/schema/project.json",
|
|
1076
|
+
registry: options.source,
|
|
1077
|
+
installed
|
|
1078
|
+
});
|
|
1079
|
+
print(options.json ? JSON.stringify(result) : planText(result));
|
|
1080
|
+
return result;
|
|
1081
|
+
}
|
|
1082
|
+
function planText(result) {
|
|
1083
|
+
const heading = result.dryRun ? "Plan to add" : "Added";
|
|
1084
|
+
const lines2 = [`${heading}: ${result.items.join(", ")}`, ...result.files];
|
|
1085
|
+
if (result.dependencies.length > 0) {
|
|
1086
|
+
lines2.push(`Dependencies: ${result.dependencies.join(", ")}`);
|
|
1087
|
+
}
|
|
1088
|
+
return lines2.join("\n");
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
// src/kit-apply.ts
|
|
1092
|
+
import { mkdirSync as mkdirSync2, rmSync, writeFileSync as writeFileSync3 } from "fs";
|
|
1093
|
+
import path12 from "path";
|
|
1094
|
+
|
|
1095
|
+
// src/kit-apply-plan.ts
|
|
1096
|
+
import { existsSync as existsSync6, readFileSync as readFileSync6 } from "fs";
|
|
1097
|
+
|
|
1098
|
+
// src/three-way.ts
|
|
1099
|
+
import { diff3Merge } from "node-diff3";
|
|
1100
|
+
function lines(content) {
|
|
1101
|
+
return content.match(/[^\n]*\n|[^\n]+$/g) ?? [];
|
|
1102
|
+
}
|
|
1103
|
+
function mergeText(ours, base, theirs) {
|
|
1104
|
+
if (ours === base) {
|
|
1105
|
+
return { content: theirs, conflicts: [] };
|
|
1106
|
+
}
|
|
1107
|
+
if (theirs === base || ours === theirs) {
|
|
1108
|
+
return { content: ours, conflicts: [] };
|
|
1109
|
+
}
|
|
1110
|
+
const regions = diff3Merge(lines(ours), lines(base), lines(theirs), {
|
|
1111
|
+
excludeFalseConflicts: true
|
|
1112
|
+
});
|
|
1113
|
+
const conflicts = [];
|
|
1114
|
+
const merged = [];
|
|
1115
|
+
for (const region of regions) {
|
|
1116
|
+
if (region.ok) {
|
|
1117
|
+
merged.push(...region.ok);
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
if (region.conflict) {
|
|
1121
|
+
conflicts.push({
|
|
1122
|
+
hunk: conflicts.length + 1,
|
|
1123
|
+
oursLine: region.conflict.aIndex + 1,
|
|
1124
|
+
baseLine: region.conflict.oIndex + 1,
|
|
1125
|
+
theirsLine: region.conflict.bIndex + 1,
|
|
1126
|
+
ours: region.conflict.a.join(""),
|
|
1127
|
+
base: region.conflict.o.join(""),
|
|
1128
|
+
theirs: region.conflict.b.join("")
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
return {
|
|
1133
|
+
...conflicts.length === 0 ? { content: merged.join("") } : {},
|
|
1134
|
+
conflicts
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
// src/kit-apply-plan.ts
|
|
1139
|
+
function files(cwd, project2, item2) {
|
|
1140
|
+
return new Map(
|
|
1141
|
+
item2.files.map((file) => {
|
|
1142
|
+
verifySource(`${item2.name}/${file.path}`, file);
|
|
1143
|
+
const target = installTarget(file.target, project2.componentsDir);
|
|
1144
|
+
const safe = safeTarget(cwd, target);
|
|
1145
|
+
return [
|
|
1146
|
+
safe.normalized,
|
|
1147
|
+
rewriteKitImports(
|
|
1148
|
+
file.content,
|
|
1149
|
+
safe.normalized,
|
|
1150
|
+
project2.componentsDir,
|
|
1151
|
+
project2.alias
|
|
1152
|
+
)
|
|
1153
|
+
];
|
|
1154
|
+
})
|
|
1155
|
+
);
|
|
1156
|
+
}
|
|
1157
|
+
function conflict(file, reason, hunks = []) {
|
|
1158
|
+
return { file, reason, hunks };
|
|
1159
|
+
}
|
|
1160
|
+
function planFile(cwd, target, base, theirs, installedHash) {
|
|
1161
|
+
const absolute = safeTarget(cwd, target).absolute;
|
|
1162
|
+
const ours = existsSync6(absolute) ? readFileSync6(absolute, "utf8") : void 0;
|
|
1163
|
+
if (base === void 0 && theirs !== void 0) {
|
|
1164
|
+
if (ours !== void 0 && ours !== theirs) {
|
|
1165
|
+
return {
|
|
1166
|
+
conflict: conflict(target, "The file you are adding already exists.")
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
1169
|
+
return ours === theirs ? { content: theirs } : {
|
|
1170
|
+
action: { kind: "write", target, content: theirs },
|
|
1171
|
+
content: theirs
|
|
1172
|
+
};
|
|
1173
|
+
}
|
|
1174
|
+
if (base !== void 0 && theirs === void 0) {
|
|
1175
|
+
if (ours === void 0) {
|
|
1176
|
+
return {};
|
|
1177
|
+
}
|
|
1178
|
+
if (installedHash !== void 0 && sha256(ours) === installedHash) {
|
|
1179
|
+
return { action: { kind: "delete", target } };
|
|
1180
|
+
}
|
|
1181
|
+
return {
|
|
1182
|
+
conflict: conflict(
|
|
1183
|
+
target,
|
|
1184
|
+
"The upstream file was removed but you have local changes."
|
|
1185
|
+
)
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
if (base === void 0 || theirs === void 0) {
|
|
1189
|
+
return {};
|
|
1190
|
+
}
|
|
1191
|
+
if (ours === void 0) {
|
|
1192
|
+
return { conflict: conflict(target, "Your local file is missing.") };
|
|
1193
|
+
}
|
|
1194
|
+
const merged = mergeText(ours, base, theirs);
|
|
1195
|
+
if (merged.conflicts.length > 0 || merged.content === void 0) {
|
|
1196
|
+
return { conflict: conflict(target, "Changes overlap.", merged.conflicts) };
|
|
1197
|
+
}
|
|
1198
|
+
return merged.content === ours ? { content: ours } : {
|
|
1199
|
+
action: { kind: "write", target, content: merged.content },
|
|
1200
|
+
content: merged.content
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
async function planItem(cwd, project2, registry, name2, latestVersion) {
|
|
1204
|
+
const installed = project2.installed[name2];
|
|
1205
|
+
if (!installed) {
|
|
1206
|
+
throw new CliError(`You have not installed item "${name2}".`);
|
|
1207
|
+
}
|
|
1208
|
+
if (installed.version === latestVersion) {
|
|
1209
|
+
return {
|
|
1210
|
+
result: {
|
|
1211
|
+
name: name2,
|
|
1212
|
+
from: installed.version,
|
|
1213
|
+
to: latestVersion,
|
|
1214
|
+
status: "skipped",
|
|
1215
|
+
files: [],
|
|
1216
|
+
conflicts: []
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1219
|
+
}
|
|
1220
|
+
const [baseItem, latestItem] = await Promise.all([
|
|
1221
|
+
registry.item(name2, installed.version),
|
|
1222
|
+
registry.item(name2, latestVersion)
|
|
1223
|
+
]);
|
|
1224
|
+
if (baseItem.name !== name2 || baseItem.version !== installed.version || latestItem.name !== name2 || latestItem.version !== latestVersion) {
|
|
1225
|
+
throw new CliError(
|
|
1226
|
+
`Registry entries for "${name2}" do not match the index.`
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
const base = files(cwd, project2, baseItem);
|
|
1230
|
+
const latest = files(cwd, project2, latestItem);
|
|
1231
|
+
const actions = [];
|
|
1232
|
+
const conflicts = [];
|
|
1233
|
+
const contents = /* @__PURE__ */ new Map();
|
|
1234
|
+
for (const target of /* @__PURE__ */ new Set([...base.keys(), ...latest.keys()])) {
|
|
1235
|
+
const plan = planFile(
|
|
1236
|
+
cwd,
|
|
1237
|
+
target,
|
|
1238
|
+
base.get(target),
|
|
1239
|
+
latest.get(target),
|
|
1240
|
+
installed.files[target]
|
|
1241
|
+
);
|
|
1242
|
+
if (plan.action) {
|
|
1243
|
+
actions.push(plan.action);
|
|
1244
|
+
}
|
|
1245
|
+
if (plan.conflict) {
|
|
1246
|
+
conflicts.push(plan.conflict);
|
|
1247
|
+
}
|
|
1248
|
+
if (plan.content !== void 0) {
|
|
1249
|
+
contents.set(target, plan.content);
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
const result = {
|
|
1253
|
+
name: name2,
|
|
1254
|
+
from: installed.version,
|
|
1255
|
+
to: latestVersion,
|
|
1256
|
+
status: conflicts.length > 0 ? "conflict" : "applied",
|
|
1257
|
+
files: conflicts.length > 0 ? [] : actions,
|
|
1258
|
+
conflicts
|
|
1259
|
+
};
|
|
1260
|
+
const next = {
|
|
1261
|
+
version: latestVersion,
|
|
1262
|
+
files: Object.fromEntries(
|
|
1263
|
+
[...latest.keys()].map((target) => [
|
|
1264
|
+
target,
|
|
1265
|
+
sha256(contents.get(target) ?? latest.get(target) ?? "")
|
|
1266
|
+
])
|
|
1267
|
+
)
|
|
1268
|
+
};
|
|
1269
|
+
return { result, next };
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
// src/kit-apply.ts
|
|
1273
|
+
async function applyDiff(options) {
|
|
1274
|
+
const print = options.print ?? console.log;
|
|
1275
|
+
if (!readProjectConfig(options.cwd)?.installed) {
|
|
1276
|
+
throw new CliError("No kit components are installed in zappie.json.");
|
|
1277
|
+
}
|
|
1278
|
+
const project2 = kitProject(options.cwd);
|
|
1279
|
+
const source2 = registrySource(options.source, project2.registry);
|
|
1280
|
+
const registry = new Registry(source2, options.cwd);
|
|
1281
|
+
const index2 = await registry.index();
|
|
1282
|
+
const names = options.name ? [options.name] : Object.keys(project2.installed);
|
|
1283
|
+
const installed = { ...project2.installed };
|
|
1284
|
+
const results = [];
|
|
1285
|
+
let changed = false;
|
|
1286
|
+
for (const name2 of names) {
|
|
1287
|
+
const latest = index2.items.find((item2) => item2.name === name2);
|
|
1288
|
+
if (!latest) {
|
|
1289
|
+
throw new CliError(
|
|
1290
|
+
`The registry is missing your installed item "${name2}".`
|
|
1291
|
+
);
|
|
1292
|
+
}
|
|
1293
|
+
const planned = await planItem(
|
|
1294
|
+
options.cwd,
|
|
1295
|
+
project2,
|
|
1296
|
+
registry,
|
|
1297
|
+
name2,
|
|
1298
|
+
latest.version
|
|
1299
|
+
);
|
|
1300
|
+
const result = options.dryRun && planned.result.status === "applied" ? { ...planned.result, status: "would-apply" } : planned.result;
|
|
1301
|
+
results.push(result);
|
|
1302
|
+
if (planned.next && planned.result.status === "applied" && !options.dryRun) {
|
|
1303
|
+
for (const action of planned.result.files) {
|
|
1304
|
+
const absolute = safeTarget(options.cwd, action.target).absolute;
|
|
1305
|
+
if (action.kind === "delete") {
|
|
1306
|
+
rmSync(absolute, { force: true });
|
|
1307
|
+
} else {
|
|
1308
|
+
mkdirSync2(path12.dirname(absolute), { recursive: true });
|
|
1309
|
+
writeFileSync3(absolute, action.content);
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
installed[name2] = planned.next;
|
|
1313
|
+
changed = true;
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
if (!options.dryRun && changed) {
|
|
1317
|
+
writeProjectConfig(options.cwd, {
|
|
1318
|
+
...project2,
|
|
1319
|
+
registry: source2,
|
|
1320
|
+
installed
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
1323
|
+
const output = {
|
|
1324
|
+
command: "diff",
|
|
1325
|
+
apply: true,
|
|
1326
|
+
dryRun: options.dryRun,
|
|
1327
|
+
items: results
|
|
1328
|
+
};
|
|
1329
|
+
print(options.json ? JSON.stringify(output) : applyText(results));
|
|
1330
|
+
return {
|
|
1331
|
+
...output,
|
|
1332
|
+
failed: results.some((item2) => item2.status === "conflict")
|
|
1333
|
+
};
|
|
1334
|
+
}
|
|
1335
|
+
function applyText(items) {
|
|
1336
|
+
return items.flatMap((item2) => [
|
|
1337
|
+
`${item2.name} ${item2.status} ${item2.from} -> ${item2.to}`,
|
|
1338
|
+
...item2.files.map((file) => `${file.kind} ${file.target}`),
|
|
1339
|
+
...item2.conflicts.flatMap((entry) => [
|
|
1340
|
+
`conflict ${entry.file} ${entry.reason}`,
|
|
1341
|
+
...entry.hunks.map(
|
|
1342
|
+
(hunk) => `hunk ${hunk.hunk} ours:${hunk.oursLine} base:${hunk.baseLine} theirs:${hunk.theirsLine}`
|
|
1343
|
+
)
|
|
1344
|
+
])
|
|
1345
|
+
]).join("\n");
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
// src/kit-status.ts
|
|
1349
|
+
import { existsSync as existsSync7, readFileSync as readFileSync7 } from "fs";
|
|
1350
|
+
import path13 from "path";
|
|
1351
|
+
|
|
1352
|
+
// src/unified-diff.ts
|
|
1353
|
+
function unifiedDiff(file, before, after) {
|
|
1354
|
+
if (before === after) {
|
|
1355
|
+
return "";
|
|
1356
|
+
}
|
|
1357
|
+
const oldLines = before.replace(/\n$/, "").split("\n");
|
|
1358
|
+
const newLines = after.replace(/\n$/, "").split("\n");
|
|
1359
|
+
return [
|
|
1360
|
+
`--- ${file}`,
|
|
1361
|
+
`+++ ${file}`,
|
|
1362
|
+
`@@ -1,${oldLines.length} +1,${newLines.length} @@`,
|
|
1363
|
+
...oldLines.map((line) => `-${line}`),
|
|
1364
|
+
...newLines.map((line) => `+${line}`)
|
|
1365
|
+
].join("\n");
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
// src/kit-status.ts
|
|
1369
|
+
function project(cwd) {
|
|
1370
|
+
const config = readProjectConfig(cwd);
|
|
1371
|
+
if (!config?.installed) {
|
|
1372
|
+
throw new CliError("No kit components are installed in zappie.json.");
|
|
1373
|
+
}
|
|
1374
|
+
return { ...config, installed: config.installed };
|
|
1375
|
+
}
|
|
1376
|
+
function modified(cwd, files2) {
|
|
1377
|
+
return Object.entries(files2).some(([file, installedHash]) => {
|
|
1378
|
+
const target = path13.join(cwd, file);
|
|
1379
|
+
return !existsSync7(target) || sha256(readFileSync7(target, "utf8")) !== installedHash;
|
|
1380
|
+
});
|
|
1381
|
+
}
|
|
1382
|
+
function state(local, update) {
|
|
1383
|
+
if (local && update) {
|
|
1384
|
+
return "modified-with-update";
|
|
1385
|
+
}
|
|
1386
|
+
if (local) {
|
|
1387
|
+
return "modified";
|
|
1388
|
+
}
|
|
1389
|
+
if (update) {
|
|
1390
|
+
return "update-available";
|
|
1391
|
+
}
|
|
1392
|
+
return "up-to-date";
|
|
1393
|
+
}
|
|
1394
|
+
function source(flag, configured, cwd) {
|
|
1395
|
+
const value = registrySource(flag, configured);
|
|
1396
|
+
return new Registry(value, cwd);
|
|
1397
|
+
}
|
|
1398
|
+
async function listItems(options) {
|
|
1399
|
+
const print = options.print ?? console.log;
|
|
1400
|
+
const config = readProjectConfig(options.cwd);
|
|
1401
|
+
const registry = source(options.source, config?.registry, options.cwd);
|
|
1402
|
+
const index2 = await registry.index();
|
|
1403
|
+
if (!options.installed) {
|
|
1404
|
+
print(
|
|
1405
|
+
options.json ? JSON.stringify({ command: "list", items: index2.items }) : index2.items.map((item2) => `${item2.name} ${item2.version} ${item2.title}`).join("\n")
|
|
1406
|
+
);
|
|
1407
|
+
return index2.items;
|
|
1408
|
+
}
|
|
1409
|
+
const current = project(options.cwd);
|
|
1410
|
+
const versions = new Map(
|
|
1411
|
+
index2.items.map((item2) => [item2.name, item2.version])
|
|
1412
|
+
);
|
|
1413
|
+
const items = Object.entries(current.installed).map(
|
|
1414
|
+
([name2, value]) => {
|
|
1415
|
+
const local = modified(options.cwd, value.files);
|
|
1416
|
+
const update = versions.get(name2) !== value.version;
|
|
1417
|
+
return {
|
|
1418
|
+
name: name2,
|
|
1419
|
+
version: value.version,
|
|
1420
|
+
status: local ? "modified" : update ? "update-available" : "up-to-date"
|
|
1421
|
+
};
|
|
1422
|
+
}
|
|
1423
|
+
);
|
|
1424
|
+
print(
|
|
1425
|
+
options.json ? JSON.stringify({ command: "list", items }) : items.map((item2) => `${item2.name} ${item2.version} ${item2.status}`).join("\n")
|
|
1426
|
+
);
|
|
1427
|
+
return items;
|
|
1428
|
+
}
|
|
1429
|
+
async function diffItems(options) {
|
|
1430
|
+
const print = options.print ?? console.log;
|
|
1431
|
+
const config = project(options.cwd);
|
|
1432
|
+
const registry = source(options.source, config.registry, options.cwd);
|
|
1433
|
+
const index2 = await registry.index();
|
|
1434
|
+
const selected = selectInstalled(config.installed, options.name);
|
|
1435
|
+
const results = [];
|
|
1436
|
+
for (const [name2, installed] of selected) {
|
|
1437
|
+
const latest = index2.items.find((item2) => item2.name === name2);
|
|
1438
|
+
if (!latest) {
|
|
1439
|
+
throw new CliError(
|
|
1440
|
+
`The registry is missing your installed item "${name2}".`
|
|
1441
|
+
);
|
|
1442
|
+
}
|
|
1443
|
+
const local = modified(options.cwd, installed.files);
|
|
1444
|
+
const update = latest.version !== installed.version;
|
|
1445
|
+
const diffs = update ? await upstreamDiffs(registry, name2, installed.version, latest.version) : [];
|
|
1446
|
+
results.push({ name: name2, status: state(local, update), diffs });
|
|
1447
|
+
}
|
|
1448
|
+
print(
|
|
1449
|
+
options.json ? JSON.stringify({ command: "diff", items: results }) : results.flatMap((item2) => [`${item2.name} ${item2.status}`, ...item2.diffs]).join("\n")
|
|
1450
|
+
);
|
|
1451
|
+
return results;
|
|
1452
|
+
}
|
|
1453
|
+
function selectInstalled(installed, name2) {
|
|
1454
|
+
if (name2 && !installed[name2]) {
|
|
1455
|
+
throw new CliError(`You have not installed item "${name2}".`);
|
|
1456
|
+
}
|
|
1457
|
+
const names = name2 ? [name2] : Object.keys(installed);
|
|
1458
|
+
return names.map((entry) => {
|
|
1459
|
+
const value = installed[entry];
|
|
1460
|
+
if (!value) {
|
|
1461
|
+
throw new CliError(`You have not installed item "${entry}".`);
|
|
1462
|
+
}
|
|
1463
|
+
return [entry, value];
|
|
1464
|
+
});
|
|
1465
|
+
}
|
|
1466
|
+
async function upstreamDiffs(registry, name2, installedVersion, latestVersion) {
|
|
1467
|
+
const [before, after] = await Promise.all([
|
|
1468
|
+
registry.item(name2, installedVersion),
|
|
1469
|
+
registry.item(name2, latestVersion)
|
|
1470
|
+
]);
|
|
1471
|
+
const oldFiles = new Map(
|
|
1472
|
+
before.files.map((file) => [file.target, file.content])
|
|
1473
|
+
);
|
|
1474
|
+
return after.files.map(
|
|
1475
|
+
(file) => unifiedDiff(file.target, oldFiles.get(file.target) ?? "", file.content)
|
|
1476
|
+
).filter(Boolean);
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
// src/verify-command.ts
|
|
1480
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
1481
|
+
import path14 from "path";
|
|
1482
|
+
async function runProjectVerify(options) {
|
|
1483
|
+
const key = options.key ?? process.env.ZAPPIE_KEY;
|
|
1484
|
+
if (key?.startsWith("pk_live_")) {
|
|
1485
|
+
throw new CliError(
|
|
1486
|
+
"live_key_refused: Use only pk_test_ keys with verify. Create a test publishable key and run verify again."
|
|
1487
|
+
);
|
|
1488
|
+
}
|
|
1489
|
+
if (!key?.startsWith("pk_test_")) {
|
|
1490
|
+
throw new CliError("Set your pk_test_ key with --key or ZAPPIE_KEY.");
|
|
1491
|
+
}
|
|
1492
|
+
const loaded = await (options.load ?? loadProjectVerify)(
|
|
1493
|
+
options.cwd,
|
|
1494
|
+
options.packageManager
|
|
1495
|
+
);
|
|
1496
|
+
try {
|
|
1497
|
+
const result = await loaded.verifyProject({
|
|
1498
|
+
cwd: options.cwd,
|
|
1499
|
+
key,
|
|
1500
|
+
suite: options.suite,
|
|
1501
|
+
packageManager: options.packageManager,
|
|
1502
|
+
...options.url ? { url: options.url } : {},
|
|
1503
|
+
...options.store ? { store: options.store } : {},
|
|
1504
|
+
...options.signal ? { signal: options.signal } : {}
|
|
1505
|
+
});
|
|
1506
|
+
if (options.report) {
|
|
1507
|
+
const file = path14.resolve(options.cwd, options.report);
|
|
1508
|
+
await mkdir(path14.dirname(file), { recursive: true });
|
|
1509
|
+
await writeFile(file, `${JSON.stringify(result, null, 2)}
|
|
1510
|
+
`);
|
|
1511
|
+
}
|
|
1512
|
+
(options.print ?? console.log)(
|
|
1513
|
+
options.json ? JSON.stringify(result) : loaded.printSummary(result)
|
|
1514
|
+
);
|
|
1515
|
+
return result;
|
|
1516
|
+
} catch (error) {
|
|
1517
|
+
if (isVerifyError(error)) {
|
|
1518
|
+
throw new CliError(`${error.code}: ${error.message}`);
|
|
1519
|
+
}
|
|
1520
|
+
throw error;
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
async function loadProjectVerify(cwd, manager) {
|
|
1524
|
+
const loaded = await loadProjectPackage(
|
|
1525
|
+
cwd,
|
|
1526
|
+
"@zappie/verify",
|
|
1527
|
+
manager
|
|
1528
|
+
);
|
|
1529
|
+
if (typeof loaded.verifyProject !== "function" || typeof loaded.printSummary !== "function") {
|
|
1530
|
+
throw new CliError(
|
|
1531
|
+
"Your installed @zappie/verify does not export verifyProject and printSummary."
|
|
1532
|
+
);
|
|
1533
|
+
}
|
|
1534
|
+
return loaded;
|
|
1535
|
+
}
|
|
1536
|
+
function isVerifyError(error) {
|
|
1537
|
+
return Boolean(
|
|
1538
|
+
error && typeof error === "object" && "code" in error && typeof error.code === "string" && "message" in error && typeof error.message === "string"
|
|
1539
|
+
);
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
// src/cli.ts
|
|
1543
|
+
async function main(argv) {
|
|
1544
|
+
const command = parseCommand(argv);
|
|
1545
|
+
const cwd = process.cwd();
|
|
1546
|
+
switch (command.name) {
|
|
1547
|
+
case "help": {
|
|
1548
|
+
console.log(usage);
|
|
1549
|
+
return 0;
|
|
1550
|
+
}
|
|
1551
|
+
case "add": {
|
|
1552
|
+
const configured = readProjectConfig(cwd)?.registry;
|
|
1553
|
+
const source2 = registrySource(command.registry, configured);
|
|
1554
|
+
await addItems({
|
|
1555
|
+
cwd,
|
|
1556
|
+
names: command.items,
|
|
1557
|
+
source: source2,
|
|
1558
|
+
overwrite: command.overwrite,
|
|
1559
|
+
dryRun: command.dryRun,
|
|
1560
|
+
install: command.install,
|
|
1561
|
+
json: command.json
|
|
1562
|
+
});
|
|
1563
|
+
return 0;
|
|
1564
|
+
}
|
|
1565
|
+
case "list": {
|
|
1566
|
+
await listItems({
|
|
1567
|
+
cwd,
|
|
1568
|
+
...command.registry !== void 0 ? { source: command.registry } : {},
|
|
1569
|
+
installed: command.installed,
|
|
1570
|
+
json: command.json
|
|
1571
|
+
});
|
|
1572
|
+
return 0;
|
|
1573
|
+
}
|
|
1574
|
+
case "diff": {
|
|
1575
|
+
if (command.apply) {
|
|
1576
|
+
const result = await applyDiff({
|
|
1577
|
+
cwd,
|
|
1578
|
+
...command.registry !== void 0 ? { source: command.registry } : {},
|
|
1579
|
+
...command.item !== void 0 ? { name: command.item } : {},
|
|
1580
|
+
dryRun: command.dryRun,
|
|
1581
|
+
json: command.json
|
|
1582
|
+
});
|
|
1583
|
+
return result.failed ? 1 : 0;
|
|
1584
|
+
}
|
|
1585
|
+
await diffItems({
|
|
1586
|
+
cwd,
|
|
1587
|
+
...command.registry !== void 0 ? { source: command.registry } : {},
|
|
1588
|
+
...command.item !== void 0 ? { name: command.item } : {},
|
|
1589
|
+
json: command.json
|
|
1590
|
+
});
|
|
1591
|
+
return 0;
|
|
1592
|
+
}
|
|
1593
|
+
case "doctor": {
|
|
1594
|
+
const result = await doctor({
|
|
1595
|
+
cwd,
|
|
1596
|
+
...command.registry !== void 0 ? { source: command.registry } : {},
|
|
1597
|
+
json: command.json
|
|
1598
|
+
});
|
|
1599
|
+
return result.failed ? 1 : 0;
|
|
1600
|
+
}
|
|
1601
|
+
case "check": {
|
|
1602
|
+
const directory = path15.resolve(cwd, command.directory ?? ".");
|
|
1603
|
+
const controller = new AbortController();
|
|
1604
|
+
const abort = () => controller.abort(new Error("Check cancelled."));
|
|
1605
|
+
process.once("SIGINT", abort);
|
|
1606
|
+
try {
|
|
1607
|
+
const result = await runProjectCheck({
|
|
1608
|
+
cwd: directory,
|
|
1609
|
+
all: command.all,
|
|
1610
|
+
json: command.json,
|
|
1611
|
+
packageManager: detectPackageManager(directory),
|
|
1612
|
+
signal: controller.signal
|
|
1613
|
+
});
|
|
1614
|
+
return result.result === "passed" ? 0 : 1;
|
|
1615
|
+
} finally {
|
|
1616
|
+
process.removeListener("SIGINT", abort);
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
case "verify": {
|
|
1620
|
+
const controller = new AbortController();
|
|
1621
|
+
const abort = () => controller.abort(new Error("Verify cancelled."));
|
|
1622
|
+
process.once("SIGINT", abort);
|
|
1623
|
+
try {
|
|
1624
|
+
const result = await runProjectVerify({
|
|
1625
|
+
cwd,
|
|
1626
|
+
suite: command.suite,
|
|
1627
|
+
json: command.json,
|
|
1628
|
+
...command.url ? { url: command.url } : {},
|
|
1629
|
+
...command.report ? { report: command.report } : {},
|
|
1630
|
+
...command.key ? { key: command.key } : {},
|
|
1631
|
+
...command.store ? { store: command.store } : {},
|
|
1632
|
+
packageManager: detectPackageManager(cwd),
|
|
1633
|
+
signal: controller.signal
|
|
1634
|
+
});
|
|
1635
|
+
return result.result === "passed" ? 0 : 2;
|
|
1636
|
+
} finally {
|
|
1637
|
+
process.removeListener("SIGINT", abort);
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
var json3 = process.argv.slice(2).includes("--json");
|
|
1643
|
+
main(process.argv.slice(2)).then(
|
|
1644
|
+
(code) => {
|
|
1645
|
+
process.exitCode = code;
|
|
1646
|
+
},
|
|
1647
|
+
(error) => {
|
|
1648
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1649
|
+
const exitCode = error instanceof CliError ? error.exitCode : 1;
|
|
1650
|
+
if (json3) {
|
|
1651
|
+
console.log(JSON.stringify({ error: { message, exitCode } }));
|
|
1652
|
+
} else {
|
|
1653
|
+
console.error(message);
|
|
1654
|
+
}
|
|
1655
|
+
process.exitCode = exitCode;
|
|
1656
|
+
}
|
|
1657
|
+
);
|