@sovereignfs/create 0.13.0 → 0.17.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/README.md +13 -1
- package/dist/create.mjs +910 -47
- package/package.json +1 -1
- package/template/app/AGENTS.md +49 -0
- package/template/app/CHANGELOG.md +11 -0
- package/template/app/CLAUDE.md +9 -0
- package/template/app/CONTRIBUTING.md +41 -27
- package/template/app/README.md +5 -1
- package/template/app/RELEASING.md +31 -0
- package/template/app/_github/repository.json +50 -0
- package/template/app/_github/workflows/ci.yml +11 -3
- package/template/app/_github/workflows/promotion.yml +24 -0
- package/template/app/_github/workflows/publish.yml +56 -7
- package/template/app/_github/workflows/release.yml +40 -0
- package/template/components/RELEASING.md +27 -0
- package/template/encryption/ENCRYPTION.md +30 -0
- package/template/encryption/app/actions.ts +20 -0
- package/template/encryption/app/api/me/keys/route.ts +21 -0
- package/template/encryption/app/api/notes/[id]/route.ts +39 -0
- package/template/encryption/app/api/notes/route.ts +36 -0
- package/template/encryption/app/api/people/[sub]/keys/route.ts +26 -0
- package/template/encryption/app/api/people/route.ts +22 -0
- package/template/encryption/app/keys/return/page.tsx +11 -0
- package/template/encryption/app/keys/return/received.tsx +36 -0
- package/template/encryption/app/notes.tsx +529 -0
- package/template/encryption/app/page.tsx +71 -0
- package/template/encryption/app/ui.tsx +14 -0
- package/template/encryption/lib/auth.ts +19 -0
- package/template/encryption/lib/keys.ts +14 -0
- package/template/encryption/lib/notes.ts +201 -0
- package/template/encryption/lib/sovereign.ts +21 -0
- package/template/encryption/migrations/001_init.sql +39 -0
- package/template/encryption/sovereign.manifest.json +21 -0
package/dist/create.mjs
CHANGED
|
@@ -7,7 +7,7 @@ import { createInterface } from "readline/promises";
|
|
|
7
7
|
// package.json
|
|
8
8
|
var package_default = {
|
|
9
9
|
name: "@sovereignfs/create",
|
|
10
|
-
version: "0.
|
|
10
|
+
version: "0.17.0",
|
|
11
11
|
type: "module",
|
|
12
12
|
description: "npm create @sovereignfs: makes an instance repository, pinned to the toolkit (sv) of the same version.",
|
|
13
13
|
license: "AGPL-3.0-or-later",
|
|
@@ -48,8 +48,7 @@ var package_default = {
|
|
|
48
48
|
|
|
49
49
|
// src/app.ts
|
|
50
50
|
import { mkdir as mkdir2, readFile, readdir as readdir2, writeFile as writeFile2 } from "fs/promises";
|
|
51
|
-
import
|
|
52
|
-
import { fileURLToPath } from "url";
|
|
51
|
+
import path3 from "path";
|
|
53
52
|
|
|
54
53
|
// src/create.ts
|
|
55
54
|
import { chmod, mkdir, readdir, writeFile } from "fs/promises";
|
|
@@ -153,6 +152,7 @@ function readRequest(argv, env) {
|
|
|
153
152
|
let kind;
|
|
154
153
|
let remote;
|
|
155
154
|
let port;
|
|
155
|
+
let encryption = false;
|
|
156
156
|
const words = [];
|
|
157
157
|
for (let i = 0; i < argv.length; i += 1) {
|
|
158
158
|
const word = argv[i];
|
|
@@ -162,11 +162,12 @@ function readRequest(argv, env) {
|
|
|
162
162
|
else if (word.startsWith("--remote=")) remote = word.slice("--remote=".length);
|
|
163
163
|
else if (word === "--port") port = argv[++i] ?? "";
|
|
164
164
|
else if (word.startsWith("--port=")) port = word.slice("--port=".length);
|
|
165
|
+
else if (word === "--encryption") encryption = true;
|
|
165
166
|
else if (word.startsWith("--"))
|
|
166
167
|
throw new CreateError(
|
|
167
168
|
`There is no option ${word}.`,
|
|
168
169
|
2,
|
|
169
|
-
"Use: npm create @sovereignfs instance <folder> [--remote URL], or app <folder> [--port N]"
|
|
170
|
+
"Use: npm create @sovereignfs instance <folder> [--remote URL], or app <folder> [--port N] [--encryption]"
|
|
170
171
|
);
|
|
171
172
|
else words.push(word);
|
|
172
173
|
}
|
|
@@ -184,7 +185,15 @@ function readRequest(argv, env) {
|
|
|
184
185
|
"Use: npm create @sovereignfs instance <folder>"
|
|
185
186
|
);
|
|
186
187
|
port ??= env.npm_config_port === void 0 || env.npm_config_port === "" ? void 0 : env.npm_config_port;
|
|
187
|
-
|
|
188
|
+
const flag = env.npm_config_encryption;
|
|
189
|
+
if (flag !== void 0 && flag !== "" && flag !== "false") encryption = true;
|
|
190
|
+
return {
|
|
191
|
+
kind,
|
|
192
|
+
dir: words[0],
|
|
193
|
+
remote,
|
|
194
|
+
port: port === void 0 ? void 0 : parsePort(port),
|
|
195
|
+
encryption
|
|
196
|
+
};
|
|
188
197
|
}
|
|
189
198
|
async function checkEmpty(dir) {
|
|
190
199
|
try {
|
|
@@ -263,6 +272,48 @@ async function createInstance(request, io2, exec2, cwd) {
|
|
|
263
272
|
./sv setup`);
|
|
264
273
|
}
|
|
265
274
|
|
|
275
|
+
// src/fill.ts
|
|
276
|
+
import path2 from "path";
|
|
277
|
+
import { fileURLToPath } from "url";
|
|
278
|
+
var TEMPLATE = fileURLToPath(new URL("../template/app", import.meta.url));
|
|
279
|
+
var ENCRYPTION_TEMPLATE = fileURLToPath(
|
|
280
|
+
new URL("../template/encryption", import.meta.url)
|
|
281
|
+
);
|
|
282
|
+
var RENAMED = {
|
|
283
|
+
_gitignore: ".gitignore",
|
|
284
|
+
_dockerignore: ".dockerignore",
|
|
285
|
+
"_env.example": ".env.example",
|
|
286
|
+
// A published package may not carry a file called tsconfig.json.
|
|
287
|
+
"_tsconfig.json": "tsconfig.json",
|
|
288
|
+
_github: ".github",
|
|
289
|
+
// Hooks are versioned here and turned on by the `prepare` script (CONTRIBUTING.md).
|
|
290
|
+
_githooks: ".githooks"
|
|
291
|
+
};
|
|
292
|
+
var targetOf = (file) => path2.join(...file.split(/[\\/]/).map((part) => RENAMED[part] ?? part));
|
|
293
|
+
function fill(text2, values) {
|
|
294
|
+
const { names } = values;
|
|
295
|
+
return text2.replaceAll("__PREFIX_LOWER__", names.prefix.toLowerCase()).replaceAll("__PREFIX__", names.prefix).replaceAll("__NAME__", names.name).replaceAll("__TITLE__", names.title).replaceAll("**TITLE**", names.title).replaceAll("__PORT__", String(values.port)).replaceAll("__WORKFLOWS_REF__", values.workflowsRef).replaceAll("__DIRECTORY__", values.directory).replaceAll("__TAG_PREFIX__", values.tagPrefix);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// src/commands.ts
|
|
299
|
+
var TOOL_COMMANDS = ["release", "commits", "sync", "settings"];
|
|
300
|
+
|
|
301
|
+
// src/settings-file.ts
|
|
302
|
+
var PROMOTION_CHECK = "Promotion / Promotion title";
|
|
303
|
+
var DEFAULT_REQUIRED_CHECKS = ["Check"];
|
|
304
|
+
function repositorySettings(text2, requiredChecks) {
|
|
305
|
+
const file = JSON.parse(text2);
|
|
306
|
+
for (const ruleset of file.rulesets) {
|
|
307
|
+
for (const rule of ruleset.rules) {
|
|
308
|
+
if (rule.type !== "required_status_checks" || !rule.parameters) continue;
|
|
309
|
+
const contexts = [.../* @__PURE__ */ new Set([PROMOTION_CHECK, ...requiredChecks])];
|
|
310
|
+
rule.parameters.required_status_checks = contexts.map((context) => ({ context }));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return `${JSON.stringify(file, null, 2)}
|
|
314
|
+
`;
|
|
315
|
+
}
|
|
316
|
+
|
|
266
317
|
// src/versions.ts
|
|
267
318
|
var APP_DEPENDENCIES = {
|
|
268
319
|
// `@sovereignfs/ui` is one bundle that imports these two, which it lists as optional peers: an app has to have them for the bundle to load.
|
|
@@ -271,44 +322,45 @@ var APP_DEPENDENCIES = {
|
|
|
271
322
|
"@sovereignfs/config": "^0.1.0",
|
|
272
323
|
"@sovereignfs/sdk": "^2.0.0",
|
|
273
324
|
"@sovereignfs/storage": "^0.1.0",
|
|
274
|
-
"@sovereignfs/ui": "^0.
|
|
325
|
+
"@sovereignfs/ui": "^0.98.0",
|
|
275
326
|
next: "^16.4.6",
|
|
276
327
|
pg: "^8.23.1",
|
|
277
328
|
react: "^19.3.1",
|
|
278
329
|
"react-dom": "^19.3.1"
|
|
279
330
|
};
|
|
331
|
+
var ENCRYPTION_DEPENDENCIES = {
|
|
332
|
+
"@sovereignfs/sdk": "^2.5.0"
|
|
333
|
+
};
|
|
280
334
|
var APP_DEV_DEPENDENCIES = {
|
|
281
335
|
// Makes the image's label and port from the manifest, and checks the manifest (`sovereign-manifest`).
|
|
282
|
-
"@sovereignfs/manifest": "^0.
|
|
336
|
+
"@sovereignfs/manifest": "^0.3.0",
|
|
283
337
|
"@types/node": "^24.19.2",
|
|
284
338
|
"@types/pg": "^8.23.1",
|
|
285
339
|
"@types/react": "^19.3.0",
|
|
286
340
|
"@types/react-dom": "^19.3.0",
|
|
287
341
|
typescript: "~6.0.3"
|
|
288
342
|
};
|
|
343
|
+
var CREATE_VERSION = package_default.version;
|
|
344
|
+
var SHARED_WORKFLOWS_REF = "ef1ddb5f623ba702fc34c8769b4b2f98c6f61ed8";
|
|
289
345
|
|
|
290
346
|
// src/app.ts
|
|
291
|
-
var TEMPLATE = fileURLToPath(new URL("../template/app", import.meta.url));
|
|
292
347
|
var NAME = /^[a-z][a-z0-9-]{0,30}[a-z0-9]$/;
|
|
293
|
-
var RENAMED = {
|
|
294
|
-
_gitignore: ".gitignore",
|
|
295
|
-
_dockerignore: ".dockerignore",
|
|
296
|
-
"_env.example": ".env.example",
|
|
297
|
-
// A published package may not carry a file called tsconfig.json.
|
|
298
|
-
"_tsconfig.json": "tsconfig.json",
|
|
299
|
-
_github: ".github",
|
|
300
|
-
// Hooks are versioned here and turned on by the `prepare` script (CONTRIBUTING.md).
|
|
301
|
-
_githooks: ".githooks"
|
|
302
|
-
};
|
|
303
348
|
function namesOf(dir) {
|
|
304
|
-
const name =
|
|
349
|
+
const name = path3.basename(path3.resolve(dir)).toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
305
350
|
if (!NAME.test(name)) {
|
|
306
351
|
throw new CreateError(
|
|
307
|
-
`\u201C${
|
|
352
|
+
`\u201C${path3.basename(dir)}\u201D is not a name an app can have.`,
|
|
308
353
|
2,
|
|
309
354
|
"Use 2 to 32 lower-case letters, digits and hyphens, starting with a letter. Nothing was written."
|
|
310
355
|
);
|
|
311
356
|
}
|
|
357
|
+
if (TOOL_COMMANDS.includes(name)) {
|
|
358
|
+
throw new CreateError(
|
|
359
|
+
`${name} is a command of npm create @sovereignfs app, so no app can be called that.`,
|
|
360
|
+
2,
|
|
361
|
+
"Choose another name."
|
|
362
|
+
);
|
|
363
|
+
}
|
|
312
364
|
const prefix = name.replace(/-/g, "").toUpperCase();
|
|
313
365
|
if (prefix === "SV") {
|
|
314
366
|
throw new CreateError(
|
|
@@ -322,12 +374,12 @@ function namesOf(dir) {
|
|
|
322
374
|
}
|
|
323
375
|
async function* walk(dir, base = dir) {
|
|
324
376
|
for (const entry of await readdir2(dir, { withFileTypes: true })) {
|
|
325
|
-
const full =
|
|
377
|
+
const full = path3.join(dir, entry.name);
|
|
326
378
|
if (entry.isDirectory()) yield* walk(full, base);
|
|
327
|
-
else yield
|
|
379
|
+
else yield path3.relative(base, full);
|
|
328
380
|
}
|
|
329
381
|
}
|
|
330
|
-
function appPackageJson(names, port = DEFAULT_APP_PORT) {
|
|
382
|
+
function appPackageJson(names, port = DEFAULT_APP_PORT, options = {}) {
|
|
331
383
|
return `${JSON.stringify(
|
|
332
384
|
{
|
|
333
385
|
// Every app of the suite is named in the @sovereignfs scope. It is private, so the name claims nothing on npm.
|
|
@@ -348,39 +400,49 @@ function appPackageJson(names, port = DEFAULT_APP_PORT) {
|
|
|
348
400
|
// Turns the versioned hooks on for whoever installs; harmless where there is no repository (the image's build).
|
|
349
401
|
prepare: "git config core.hooksPath .githooks || true"
|
|
350
402
|
},
|
|
351
|
-
dependencies: APP_DEPENDENCIES,
|
|
352
|
-
|
|
403
|
+
dependencies: options.encryption ? { ...APP_DEPENDENCIES, ...ENCRYPTION_DEPENDENCIES } : APP_DEPENDENCIES,
|
|
404
|
+
// `@sovereignfs/create` is pinned exactly: `npm create @sovereignfs sync` and the shared workflows run this version (research 0007).
|
|
405
|
+
devDependencies: { ...APP_DEV_DEPENDENCIES, "@sovereignfs/create": CREATE_VERSION },
|
|
406
|
+
// What the shared files need of this app: its tag prefix, the checks its main requires, and any shared file it keeps as its own.
|
|
407
|
+
sovereign: { tagPrefix: "", requiredChecks: [...DEFAULT_REQUIRED_CHECKS], unmanaged: [] }
|
|
353
408
|
},
|
|
354
409
|
null,
|
|
355
410
|
2
|
|
356
411
|
)}
|
|
357
412
|
`;
|
|
358
413
|
}
|
|
359
|
-
async function writeApp(target, names, port = DEFAULT_APP_PORT) {
|
|
414
|
+
async function writeApp(target, names, port = DEFAULT_APP_PORT, options = {}) {
|
|
360
415
|
await checkEmpty(target);
|
|
361
|
-
const written =
|
|
362
|
-
const
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
await
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
416
|
+
const written = /* @__PURE__ */ new Set();
|
|
417
|
+
const values = { names, port, workflowsRef: SHARED_WORKFLOWS_REF, directory: ".", tagPrefix: "" };
|
|
418
|
+
const replace = (text2) => fill(text2, values);
|
|
419
|
+
const sources = [TEMPLATE, ...options.encryption ? [ENCRYPTION_TEMPLATE] : []];
|
|
420
|
+
for (const source of sources)
|
|
421
|
+
for await (const file of walk(source)) {
|
|
422
|
+
const out = targetOf(file);
|
|
423
|
+
const target_ = path3.join(target, out);
|
|
424
|
+
await mkdir2(path3.dirname(target_), { recursive: true });
|
|
425
|
+
let text2 = replace(await readFile(path3.join(source, file), "utf8"));
|
|
426
|
+
if (out === "sovereign.manifest.json") text2 = text2.replace(/"port": \d+/, `"port": ${port}`);
|
|
427
|
+
if (out === path3.join(".github", "repository.json"))
|
|
428
|
+
text2 = repositorySettings(text2, DEFAULT_REQUIRED_CHECKS);
|
|
429
|
+
await writeFile2(target_, text2, {
|
|
430
|
+
mode: out.startsWith(`.githooks${path3.sep}`) ? 493 : 420
|
|
431
|
+
});
|
|
432
|
+
written.add(out);
|
|
433
|
+
}
|
|
434
|
+
await writeFile2(path3.join(target, "package.json"), appPackageJson(names, port, options));
|
|
435
|
+
written.add("package.json");
|
|
436
|
+
return [...written].sort();
|
|
377
437
|
}
|
|
378
438
|
async function createApp(request, io2, exec2, cwd) {
|
|
379
|
-
const target =
|
|
439
|
+
const target = path3.resolve(cwd, request.dir);
|
|
380
440
|
const names = namesOf(target);
|
|
381
441
|
const port = request.port ?? DEFAULT_APP_PORT;
|
|
382
|
-
const written = await writeApp(target, names, port);
|
|
383
|
-
io2.err(
|
|
442
|
+
const written = await writeApp(target, names, port, { encryption: request.encryption ?? false });
|
|
443
|
+
io2.err(
|
|
444
|
+
`Wrote the files of ${names.name}, on port ${port}${request.encryption ? ", with its notes encrypted" : ""}.`
|
|
445
|
+
);
|
|
384
446
|
const step = async (label, command, args, hint) => {
|
|
385
447
|
const done = await exec2(command, args, { cwd: target });
|
|
386
448
|
if (done.code !== 0) {
|
|
@@ -426,6 +488,795 @@ Then host it with ./sv app add ${names.name} --image <the image> in your instanc
|
|
|
426
488
|
);
|
|
427
489
|
}
|
|
428
490
|
|
|
491
|
+
// src/tools.ts
|
|
492
|
+
import { readFile as readFile5 } from "fs/promises";
|
|
493
|
+
import path7 from "path";
|
|
494
|
+
import { appendFile } from "fs/promises";
|
|
495
|
+
|
|
496
|
+
// src/release.ts
|
|
497
|
+
var PREFIX = /^(?:[a-z][a-z0-9-]*-)?$/;
|
|
498
|
+
var tagOf = (prefix, version) => `${prefix}v${version}`;
|
|
499
|
+
var isPrefix = (prefix) => PREFIX.test(prefix);
|
|
500
|
+
function changelogSection(text2, version) {
|
|
501
|
+
const lines = text2.split("\n");
|
|
502
|
+
const heading = `## [${version}]`;
|
|
503
|
+
const start = lines.findIndex((line) => line.startsWith(heading));
|
|
504
|
+
if (start === -1) return void 0;
|
|
505
|
+
const rest = lines.slice(start + 1);
|
|
506
|
+
const end = rest.findIndex((line) => /^## /.test(line));
|
|
507
|
+
const body = (end === -1 ? rest : rest.slice(0, end)).join("\n").trim();
|
|
508
|
+
return body === "" ? void 0 : body;
|
|
509
|
+
}
|
|
510
|
+
var AGENT_TRAILERS = [
|
|
511
|
+
"Co-Authored-By: Claude Code <noreply@anthropic.com>",
|
|
512
|
+
"Co-Authored-By: Codex <noreply@openai.com>"
|
|
513
|
+
];
|
|
514
|
+
var AGENT_ADDRESSES = ["noreply@anthropic.com", "noreply@openai.com"];
|
|
515
|
+
function trailerProblems(message) {
|
|
516
|
+
const problems = [];
|
|
517
|
+
for (const raw of message.split("\n")) {
|
|
518
|
+
if (raw.startsWith("#")) continue;
|
|
519
|
+
const line = raw.trim();
|
|
520
|
+
if (!/^co-authored-by:/i.test(line)) continue;
|
|
521
|
+
if (!AGENT_ADDRESSES.some((address) => line.toLowerCase().includes(address))) continue;
|
|
522
|
+
if (!AGENT_TRAILERS.includes(line)) {
|
|
523
|
+
problems.push(`"${line}" is not an agent trailer; use one of: ${AGENT_TRAILERS.join(", ")}`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return problems;
|
|
527
|
+
}
|
|
528
|
+
function checkComponentsTitle(title, components) {
|
|
529
|
+
const text2 = title.trim().replace(/\s+\(#\d+\)$/, "");
|
|
530
|
+
const release = /^Release(\s|$)/.test(text2);
|
|
531
|
+
const ids = (release ? text2.replace(/^Release\s*/, "") : text2).split(",").map((part) => part.trim()).filter((part) => part !== "");
|
|
532
|
+
const problems = [];
|
|
533
|
+
const tags = {};
|
|
534
|
+
const expected = components.map((c) => tagOf(c.tagPrefix, c.version)).join(", ");
|
|
535
|
+
if (ids.length === 0)
|
|
536
|
+
problems.push(`The title names no version; it should name one or more of: ${expected}.`);
|
|
537
|
+
const byPrefix = [...components].sort((a, b) => b.tagPrefix.length - a.tagPrefix.length);
|
|
538
|
+
const seen = /* @__PURE__ */ new Set();
|
|
539
|
+
for (const id of ids) {
|
|
540
|
+
const component = byPrefix.find(
|
|
541
|
+
(c) => id.startsWith(`${c.tagPrefix}v`) && /^v\d+\.\d+\.\d+$/.test(id.slice(c.tagPrefix.length))
|
|
542
|
+
);
|
|
543
|
+
if (!component) {
|
|
544
|
+
problems.push(`"${id}" is not the tag of a version here; it should be one of: ${expected}.`);
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
if (seen.has(component.name)) {
|
|
548
|
+
problems.push(`${component.name} is named twice.`);
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
seen.add(component.name);
|
|
552
|
+
const tag = tagOf(component.tagPrefix, component.version);
|
|
553
|
+
if (id !== tag) {
|
|
554
|
+
problems.push(`${id}: ${component.name} is at ${component.version}, so it is ${tag}.`);
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
if (release) {
|
|
558
|
+
if (component.tagged)
|
|
559
|
+
problems.push(
|
|
560
|
+
`${tag} is tagged already; Release names only versions this promotion will tag.`
|
|
561
|
+
);
|
|
562
|
+
else if (!component.hasNotes)
|
|
563
|
+
problems.push(
|
|
564
|
+
`${tag}: ${component.name}'s changelog has no section for ${component.version}, which would be the release's notes.`
|
|
565
|
+
);
|
|
566
|
+
else tags[component.name] = tag;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
return { release, tags: problems.length > 0 ? {} : tags, problems };
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// src/components.ts
|
|
573
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
574
|
+
import path4 from "path";
|
|
575
|
+
var COMPONENTS_FILE = "sovereign.json";
|
|
576
|
+
var strings = (value, what) => {
|
|
577
|
+
if (value === void 0) return [];
|
|
578
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
|
|
579
|
+
throw new CreateError(`${what} must be a list of strings.`, 1);
|
|
580
|
+
}
|
|
581
|
+
return value;
|
|
582
|
+
};
|
|
583
|
+
var text = (value, what) => {
|
|
584
|
+
if (typeof value !== "string" || value === "")
|
|
585
|
+
throw new CreateError(`${what} must be a non-empty string.`, 1);
|
|
586
|
+
return value;
|
|
587
|
+
};
|
|
588
|
+
async function readJson(file) {
|
|
589
|
+
try {
|
|
590
|
+
return JSON.parse(await readFile2(file, "utf8"));
|
|
591
|
+
} catch (error) {
|
|
592
|
+
if (error.code === "ENOENT") return void 0;
|
|
593
|
+
throw new CreateError(`${file} is not valid JSON.`, 1);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
async function readReleases(root, appDir = root, defaultChecks = []) {
|
|
597
|
+
const file = await readJson(path4.join(root, COMPONENTS_FILE));
|
|
598
|
+
if (file) {
|
|
599
|
+
const list = file.components;
|
|
600
|
+
if (!Array.isArray(list) || list.length === 0)
|
|
601
|
+
throw new CreateError(`${COMPONENTS_FILE} must list its components.`, 1);
|
|
602
|
+
const components = list.map((item, i) => {
|
|
603
|
+
const entry = item ?? {};
|
|
604
|
+
const where = `${COMPONENTS_FILE} components[${i}]`;
|
|
605
|
+
const tagPrefix2 = entry.tagPrefix === void 0 ? "" : entry.tagPrefix;
|
|
606
|
+
if (typeof tagPrefix2 !== "string" || !isPrefix(tagPrefix2)) {
|
|
607
|
+
throw new CreateError(`${where}.tagPrefix must be empty or end in "-", such as web-.`, 1);
|
|
608
|
+
}
|
|
609
|
+
return {
|
|
610
|
+
name: text(entry.name, `${where}.name`),
|
|
611
|
+
version: text(entry.version, `${where}.version`),
|
|
612
|
+
tagPrefix: tagPrefix2,
|
|
613
|
+
changelog: text(entry.changelog, `${where}.changelog`),
|
|
614
|
+
publish: text(entry.publish, `${where}.publish`)
|
|
615
|
+
};
|
|
616
|
+
});
|
|
617
|
+
const prefixes = components.map((component) => component.tagPrefix);
|
|
618
|
+
if (new Set(prefixes).size !== prefixes.length)
|
|
619
|
+
throw new CreateError(`Two components in ${COMPONENTS_FILE} share a tag prefix.`, 1);
|
|
620
|
+
return {
|
|
621
|
+
name: text(file.name, `${COMPONENTS_FILE} name`),
|
|
622
|
+
components,
|
|
623
|
+
requiredChecks: file.requiredChecks === void 0 ? [...defaultChecks] : strings(file.requiredChecks, `${COMPONENTS_FILE} requiredChecks`),
|
|
624
|
+
unmanaged: strings(file.unmanaged, `${COMPONENTS_FILE} unmanaged`),
|
|
625
|
+
create: typeof file.create === "string" ? file.create : void 0,
|
|
626
|
+
file: COMPONENTS_FILE
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
const manifest = await readJson(path4.join(appDir, "package.json"));
|
|
630
|
+
if (!manifest)
|
|
631
|
+
throw new CreateError(
|
|
632
|
+
`There is no ${COMPONENTS_FILE} in ${root}, nor a package.json in ${appDir}.`,
|
|
633
|
+
1
|
|
634
|
+
);
|
|
635
|
+
const own = manifest.sovereign ?? {};
|
|
636
|
+
const tagPrefix = own.tagPrefix === void 0 ? "" : own.tagPrefix;
|
|
637
|
+
if (typeof tagPrefix !== "string" || !isPrefix(tagPrefix)) {
|
|
638
|
+
throw new CreateError(
|
|
639
|
+
`package.json's sovereign.tagPrefix must be empty or end in "-", such as web-.`,
|
|
640
|
+
1
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
const name = typeof manifest.name === "string" ? manifest.name.replace(/^@sovereignfs\//, "") : "";
|
|
644
|
+
const dir = path4.relative(root, appDir);
|
|
645
|
+
const at = (file2) => dir === "" ? file2 : path4.join(dir, file2).split(path4.sep).join("/");
|
|
646
|
+
const dev = manifest.devDependencies ?? {};
|
|
647
|
+
return {
|
|
648
|
+
name,
|
|
649
|
+
components: [
|
|
650
|
+
{
|
|
651
|
+
name,
|
|
652
|
+
version: at("package.json"),
|
|
653
|
+
tagPrefix,
|
|
654
|
+
changelog: at("CHANGELOG.md"),
|
|
655
|
+
publish: "publish.yml"
|
|
656
|
+
}
|
|
657
|
+
],
|
|
658
|
+
requiredChecks: own.requiredChecks === void 0 ? [...defaultChecks] : strings(own.requiredChecks, "package.json's sovereign.requiredChecks"),
|
|
659
|
+
unmanaged: strings(own.unmanaged, "package.json's sovereign.unmanaged"),
|
|
660
|
+
create: typeof dev["@sovereignfs/create"] === "string" ? dev["@sovereignfs/create"] : void 0,
|
|
661
|
+
file: "package.json"
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
function versionIn(file, content) {
|
|
665
|
+
if (file.endsWith(".json")) {
|
|
666
|
+
try {
|
|
667
|
+
const { version } = JSON.parse(content);
|
|
668
|
+
return typeof version === "string" && version !== "" ? version : void 0;
|
|
669
|
+
} catch {
|
|
670
|
+
return void 0;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
if (file.endsWith("Cargo.toml")) {
|
|
674
|
+
for (const table of ["workspace.package", "package"]) {
|
|
675
|
+
let inside = false;
|
|
676
|
+
for (const line of content.split("\n")) {
|
|
677
|
+
const header = /^\s*\[([^\]]+)\]\s*$/.exec(line);
|
|
678
|
+
if (header) inside = header[1] === table;
|
|
679
|
+
else if (inside) {
|
|
680
|
+
const found = /^\s*version\s*=\s*"([^"]+)"/.exec(line);
|
|
681
|
+
if (found) return found[1];
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return void 0;
|
|
687
|
+
}
|
|
688
|
+
async function componentVersion(root, component) {
|
|
689
|
+
let content;
|
|
690
|
+
try {
|
|
691
|
+
content = await readFile2(path4.join(root, component.version), "utf8");
|
|
692
|
+
} catch {
|
|
693
|
+
throw new CreateError(`There is no ${component.version} for ${component.name}.`, 1);
|
|
694
|
+
}
|
|
695
|
+
const version = versionIn(component.version, content);
|
|
696
|
+
if (!version)
|
|
697
|
+
throw new CreateError(`${component.version} has no version for ${component.name}.`, 1);
|
|
698
|
+
return version;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// src/sync.ts
|
|
702
|
+
import { mkdir as mkdir3, readFile as readFile3, writeFile as writeFile3 } from "fs/promises";
|
|
703
|
+
import path5 from "path";
|
|
704
|
+
var BEGIN = "<!-- sovereign:shared:begin -->";
|
|
705
|
+
var END = "<!-- sovereign:shared:end -->";
|
|
706
|
+
var MANAGED = [
|
|
707
|
+
{ file: "_github/workflows/promotion.yml", at: "root", kind: "text" },
|
|
708
|
+
{ file: "_github/workflows/release.yml", at: "root", kind: "text" },
|
|
709
|
+
{ file: "_github/workflows/sync-develop.yml", at: "root", kind: "text" },
|
|
710
|
+
{ file: "_github/repository.json", at: "root", kind: "json" },
|
|
711
|
+
{ file: "CONTRIBUTING.md", at: "app", kind: "section" },
|
|
712
|
+
{ file: "RELEASING.md", at: "app", kind: "section" },
|
|
713
|
+
{ file: "CLAUDE.md", at: "app", kind: "text" },
|
|
714
|
+
{ file: "AGENTS.md", at: "app", kind: "section" }
|
|
715
|
+
];
|
|
716
|
+
var strings2 = (value, what) => {
|
|
717
|
+
if (value === void 0) return [];
|
|
718
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
|
|
719
|
+
throw new CreateError(`package.json's sovereign.${what} must be a list of strings.`, 1);
|
|
720
|
+
}
|
|
721
|
+
return value;
|
|
722
|
+
};
|
|
723
|
+
async function readAppSettings(appDir) {
|
|
724
|
+
let manifest;
|
|
725
|
+
try {
|
|
726
|
+
manifest = JSON.parse(await readFile3(path5.join(appDir, "package.json"), "utf8"));
|
|
727
|
+
} catch {
|
|
728
|
+
throw new CreateError(`There is no package.json in ${appDir}.`, 1);
|
|
729
|
+
}
|
|
730
|
+
const name = typeof manifest.name === "string" ? manifest.name.replace(/^@sovereignfs\//, "") : "";
|
|
731
|
+
const own = manifest.sovereign ?? {};
|
|
732
|
+
const tagPrefix = own.tagPrefix === void 0 ? "" : own.tagPrefix;
|
|
733
|
+
if (typeof tagPrefix !== "string" || !isPrefix(tagPrefix)) {
|
|
734
|
+
throw new CreateError(
|
|
735
|
+
`package.json's sovereign.tagPrefix must be empty or end in "-", such as web-.`,
|
|
736
|
+
1
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
let port = DEFAULT_APP_PORT;
|
|
740
|
+
try {
|
|
741
|
+
const app = JSON.parse(
|
|
742
|
+
await readFile3(path5.join(appDir, "sovereign.manifest.json"), "utf8")
|
|
743
|
+
);
|
|
744
|
+
if (typeof app.port === "number") port = app.port;
|
|
745
|
+
} catch {
|
|
746
|
+
}
|
|
747
|
+
return {
|
|
748
|
+
name,
|
|
749
|
+
tagPrefix,
|
|
750
|
+
requiredChecks: own.requiredChecks === void 0 ? [...DEFAULT_REQUIRED_CHECKS] : strings2(own.requiredChecks, "requiredChecks"),
|
|
751
|
+
unmanaged: strings2(own.unmanaged, "unmanaged"),
|
|
752
|
+
port
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
var canonical = (text2) => {
|
|
756
|
+
const sort = (value) => Array.isArray(value) ? value.map(sort) : value && typeof value === "object" ? Object.fromEntries(
|
|
757
|
+
Object.keys(value).sort().map((key) => [key, sort(value[key])])
|
|
758
|
+
) : value;
|
|
759
|
+
try {
|
|
760
|
+
return JSON.stringify(sort(JSON.parse(text2)));
|
|
761
|
+
} catch {
|
|
762
|
+
return `not json: ${text2}`;
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
function sharedSection(text2) {
|
|
766
|
+
const start = text2.indexOf(BEGIN);
|
|
767
|
+
const end = text2.indexOf(END);
|
|
768
|
+
if (start === -1 || end === -1 || end < start) return void 0;
|
|
769
|
+
return text2.slice(start, end + END.length);
|
|
770
|
+
}
|
|
771
|
+
var readText = async (file) => {
|
|
772
|
+
try {
|
|
773
|
+
return await readFile3(file, "utf8");
|
|
774
|
+
} catch {
|
|
775
|
+
return void 0;
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
async function syncApp(appDir, gitRoot2, options) {
|
|
779
|
+
const releases = await readReleases(gitRoot2, appDir, DEFAULT_REQUIRED_CHECKS);
|
|
780
|
+
const several = releases.file === "sovereign.json";
|
|
781
|
+
const docsDir = several ? gitRoot2 : appDir;
|
|
782
|
+
const settings2 = several ? { port: DEFAULT_APP_PORT, tagPrefix: "" } : await readAppSettings(appDir);
|
|
783
|
+
const names = namesOf(path5.join(path5.sep, releases.name || path5.basename(appDir)));
|
|
784
|
+
const directory = several ? "." : path5.relative(gitRoot2, appDir) || ".";
|
|
785
|
+
const values = {
|
|
786
|
+
names,
|
|
787
|
+
port: settings2.port,
|
|
788
|
+
workflowsRef: SHARED_WORKFLOWS_REF,
|
|
789
|
+
directory: directory.split(path5.sep).join("/"),
|
|
790
|
+
tagPrefix: settings2.tagPrefix
|
|
791
|
+
};
|
|
792
|
+
const results = [];
|
|
793
|
+
for (const entry of MANAGED) {
|
|
794
|
+
const base = entry.at === "root" ? gitRoot2 : docsDir;
|
|
795
|
+
const target = path5.join(base, targetOf(entry.file));
|
|
796
|
+
const shown = path5.relative(gitRoot2, target).split(path5.sep).join("/");
|
|
797
|
+
if (releases.unmanaged.includes(shown)) {
|
|
798
|
+
results.push({ path: shown, state: "unmanaged" });
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
let wanted = several && entry.file === "_github/workflows/release.yml" ? releaseWorkflowFor(releases) : fill(
|
|
802
|
+
await readFile3(
|
|
803
|
+
several && entry.file === "RELEASING.md" ? path5.join(TEMPLATE, "..", "components", "RELEASING.md") : path5.join(TEMPLATE, entry.file),
|
|
804
|
+
"utf8"
|
|
805
|
+
),
|
|
806
|
+
values
|
|
807
|
+
);
|
|
808
|
+
if (entry.kind === "json") wanted = repositorySettings(wanted, releases.requiredChecks);
|
|
809
|
+
const present = await readText(target);
|
|
810
|
+
let same2;
|
|
811
|
+
let next;
|
|
812
|
+
let reason;
|
|
813
|
+
if (entry.kind === "section" && present !== void 0) {
|
|
814
|
+
const section = sharedSection(wanted);
|
|
815
|
+
const current = sharedSection(present);
|
|
816
|
+
same2 = current === section;
|
|
817
|
+
next = current === void 0 ? `${present.trimEnd()}
|
|
818
|
+
|
|
819
|
+
${section}
|
|
820
|
+
` : present.replace(current, section);
|
|
821
|
+
if (current === void 0) reason = "has no shared section";
|
|
822
|
+
} else {
|
|
823
|
+
same2 = present !== void 0 && (entry.kind === "json" ? canonical(present) === canonical(wanted) : present === wanted);
|
|
824
|
+
next = wanted;
|
|
825
|
+
if (present === void 0) reason = "is missing";
|
|
826
|
+
}
|
|
827
|
+
if (same2) results.push({ path: shown, state: "same" });
|
|
828
|
+
else if (options.check)
|
|
829
|
+
results.push({ path: shown, state: "drifted", ...reason ? { reason } : {} });
|
|
830
|
+
else {
|
|
831
|
+
await mkdir3(path5.dirname(target), { recursive: true });
|
|
832
|
+
await writeFile3(target, next);
|
|
833
|
+
results.push({ path: shown, state: "written" });
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
return results;
|
|
837
|
+
}
|
|
838
|
+
function releaseWorkflowFor(releases) {
|
|
839
|
+
const lines = [
|
|
840
|
+
"name: Release",
|
|
841
|
+
"",
|
|
842
|
+
'# Written by `npm create @sovereignfs app sync` from sovereign.json; do not edit it here (CONTRIBUTING.md, "Shared files"). When a',
|
|
843
|
+
"# promotion pull request titled `Release \u2026` is merged, the workflow every repository of the suite shares checks the title against the",
|
|
844
|
+
"# merged code and tags the merge commit with each version it names; each component released is then published by its own workflow",
|
|
845
|
+
"# (RELEASING.md). A title without `Release` publishes nothing.",
|
|
846
|
+
"on:",
|
|
847
|
+
" pull_request:",
|
|
848
|
+
" branches: [main]",
|
|
849
|
+
" types: [closed]",
|
|
850
|
+
"",
|
|
851
|
+
"permissions:",
|
|
852
|
+
" contents: read",
|
|
853
|
+
"",
|
|
854
|
+
"concurrency:",
|
|
855
|
+
" group: release-${{ github.event.pull_request.number }}",
|
|
856
|
+
" cancel-in-progress: false",
|
|
857
|
+
"",
|
|
858
|
+
"jobs:",
|
|
859
|
+
" plan:",
|
|
860
|
+
" name: Plan",
|
|
861
|
+
" permissions:",
|
|
862
|
+
" contents: write",
|
|
863
|
+
` uses: sovereignfs/sovereign/.github/workflows/app-release.yml@${SHARED_WORKFLOWS_REF}`
|
|
864
|
+
];
|
|
865
|
+
for (const component of releases.components) {
|
|
866
|
+
const tag = `fromJSON(needs.plan.outputs.tags || '{}')['${component.name}']`;
|
|
867
|
+
lines.push(
|
|
868
|
+
"",
|
|
869
|
+
` publish-${component.name}:`,
|
|
870
|
+
` name: Publish ${component.name}`,
|
|
871
|
+
" needs: plan",
|
|
872
|
+
` if: \${{ ${tag} != null }}`,
|
|
873
|
+
" permissions:",
|
|
874
|
+
" contents: write",
|
|
875
|
+
" packages: write",
|
|
876
|
+
" id-token: write",
|
|
877
|
+
" attestations: write",
|
|
878
|
+
` uses: ./.github/workflows/${component.publish}`,
|
|
879
|
+
" with:",
|
|
880
|
+
` tag: \${{ ${tag} }}`,
|
|
881
|
+
" secrets: inherit"
|
|
882
|
+
);
|
|
883
|
+
}
|
|
884
|
+
return `${lines.join("\n")}
|
|
885
|
+
`;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// src/settings.ts
|
|
889
|
+
import { mkdtemp, readFile as readFile4, writeFile as writeFile4 } from "fs/promises";
|
|
890
|
+
import { tmpdir } from "os";
|
|
891
|
+
import path6 from "path";
|
|
892
|
+
var same = (a, b) => JSON.stringify(sortKeys(a)) === JSON.stringify(sortKeys(b));
|
|
893
|
+
function sortKeys(value) {
|
|
894
|
+
if (Array.isArray(value)) return value.map(sortKeys);
|
|
895
|
+
if (value && typeof value === "object") {
|
|
896
|
+
return Object.fromEntries(
|
|
897
|
+
Object.keys(value).sort().map((key) => [key, sortKeys(value[key])])
|
|
898
|
+
);
|
|
899
|
+
}
|
|
900
|
+
return value;
|
|
901
|
+
}
|
|
902
|
+
function ruleDifferences(name, want, have) {
|
|
903
|
+
const out = [];
|
|
904
|
+
const types = (rules) => rules.map((rule) => rule.type).sort();
|
|
905
|
+
if (!same(types(want), types(have)))
|
|
906
|
+
out.push({ what: `ruleset "${name}": rules`, want: types(want), have: types(have) });
|
|
907
|
+
for (const rule of want) {
|
|
908
|
+
const live = have.find((candidate) => candidate.type === rule.type);
|
|
909
|
+
if (!live || !rule.parameters) continue;
|
|
910
|
+
for (const [key, value] of Object.entries(rule.parameters)) {
|
|
911
|
+
const current = live.parameters?.[key];
|
|
912
|
+
const comparable = key === "required_status_checks" && Array.isArray(value) && Array.isArray(current) ? [value, current].map(
|
|
913
|
+
(list) => list.map((check) => check.context).sort()
|
|
914
|
+
) : [value, current];
|
|
915
|
+
if (!same(comparable[0], comparable[1])) {
|
|
916
|
+
out.push({ what: `ruleset "${name}": ${rule.type}.${key}`, want: value, have: current });
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
return out;
|
|
921
|
+
}
|
|
922
|
+
function differences(file, live) {
|
|
923
|
+
const out = [];
|
|
924
|
+
for (const [key, value] of Object.entries(file.settings)) {
|
|
925
|
+
if (live.unreadable.includes(`settings.${key}`)) continue;
|
|
926
|
+
if (!same(value, live.settings[key]))
|
|
927
|
+
out.push({ what: `settings.${key}`, want: value, have: live.settings[key] });
|
|
928
|
+
}
|
|
929
|
+
if (live.unreadable.includes("rulesets")) return out;
|
|
930
|
+
for (const ruleset of file.rulesets) {
|
|
931
|
+
const current = live.rulesets.find((candidate) => candidate.name === ruleset.name);
|
|
932
|
+
if (!current) {
|
|
933
|
+
out.push({ what: `ruleset "${ruleset.name}"`, want: "present", have: "missing" });
|
|
934
|
+
continue;
|
|
935
|
+
}
|
|
936
|
+
for (const key of ["target", "enforcement", "conditions", "bypass_actors"]) {
|
|
937
|
+
if (live.unreadable.includes(`ruleset "${ruleset.name}".${key}`)) continue;
|
|
938
|
+
if (!same(ruleset[key], current[key])) {
|
|
939
|
+
out.push({
|
|
940
|
+
what: `ruleset "${ruleset.name}": ${key}`,
|
|
941
|
+
want: ruleset[key],
|
|
942
|
+
have: current[key]
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
out.push(...ruleDifferences(ruleset.name, ruleset.rules, current.rules));
|
|
947
|
+
}
|
|
948
|
+
for (const current of live.rulesets) {
|
|
949
|
+
if (!file.rulesets.some((ruleset) => ruleset.name === current.name)) {
|
|
950
|
+
out.push({ what: `ruleset "${current.name}"`, want: "absent", have: "present" });
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
return out;
|
|
954
|
+
}
|
|
955
|
+
var gh = async (exec2, cwd, args) => {
|
|
956
|
+
const done = await exec2("gh", args, { cwd });
|
|
957
|
+
return { ...done, json: () => JSON.parse(done.stdout) };
|
|
958
|
+
};
|
|
959
|
+
async function readLive(exec2, cwd, repo, file) {
|
|
960
|
+
const unreadable = [];
|
|
961
|
+
const repoAnswer = await gh(exec2, cwd, ["api", `repos/${repo}`]);
|
|
962
|
+
if (repoAnswer.code !== 0)
|
|
963
|
+
throw new CreateError(`Reading ${repo} failed: ${repoAnswer.stderr.trim()}`, 1);
|
|
964
|
+
const settings2 = repoAnswer.json();
|
|
965
|
+
for (const key of Object.keys(file.settings))
|
|
966
|
+
if (settings2[key] === void 0) unreadable.push(`settings.${key}`);
|
|
967
|
+
const rulesets = [];
|
|
968
|
+
const list = await gh(exec2, cwd, ["api", `repos/${repo}/rulesets`]);
|
|
969
|
+
if (list.code !== 0) unreadable.push("rulesets");
|
|
970
|
+
else {
|
|
971
|
+
for (const { id } of list.json()) {
|
|
972
|
+
const one = await gh(exec2, cwd, ["api", `repos/${repo}/rulesets/${id}`]);
|
|
973
|
+
if (one.code !== 0) {
|
|
974
|
+
unreadable.push("rulesets");
|
|
975
|
+
break;
|
|
976
|
+
}
|
|
977
|
+
const ruleset = one.json();
|
|
978
|
+
if (ruleset.bypass_actors === void 0)
|
|
979
|
+
unreadable.push(`ruleset "${ruleset.name}".bypass_actors`);
|
|
980
|
+
rulesets.push(ruleset);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
return { settings: settings2, rulesets, unreadable };
|
|
984
|
+
}
|
|
985
|
+
async function readRepositoryFile(file) {
|
|
986
|
+
try {
|
|
987
|
+
return JSON.parse(await readFile4(file, "utf8"));
|
|
988
|
+
} catch {
|
|
989
|
+
throw new CreateError(`There is no readable ${file}.`, 1);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
async function apply(exec2, cwd, repo, file, live, prune) {
|
|
993
|
+
const done = [];
|
|
994
|
+
const work = await mkdtemp(path6.join(tmpdir(), "sv-settings-"));
|
|
995
|
+
const send = async (method, endpoint, body) => {
|
|
996
|
+
const input = path6.join(work, "body.json");
|
|
997
|
+
await writeFile4(input, JSON.stringify(body));
|
|
998
|
+
const answer = await gh(exec2, cwd, [
|
|
999
|
+
"api",
|
|
1000
|
+
"-X",
|
|
1001
|
+
method,
|
|
1002
|
+
endpoint,
|
|
1003
|
+
"--input",
|
|
1004
|
+
input,
|
|
1005
|
+
"--silent"
|
|
1006
|
+
]);
|
|
1007
|
+
if (answer.code !== 0)
|
|
1008
|
+
throw new CreateError(`${method} ${endpoint} failed: ${answer.stderr.trim()}`, 1);
|
|
1009
|
+
};
|
|
1010
|
+
const changed = Object.entries(file.settings).filter(
|
|
1011
|
+
([key, value]) => !same(value, live.settings[key])
|
|
1012
|
+
);
|
|
1013
|
+
if (changed.length > 0) {
|
|
1014
|
+
await send("PATCH", `repos/${repo}`, Object.fromEntries(changed));
|
|
1015
|
+
done.push(`settings: ${changed.map(([key]) => key).join(", ")}`);
|
|
1016
|
+
}
|
|
1017
|
+
for (const ruleset of file.rulesets) {
|
|
1018
|
+
const current = live.rulesets.find((candidate) => candidate.name === ruleset.name);
|
|
1019
|
+
if (!current) {
|
|
1020
|
+
await send("POST", `repos/${repo}/rulesets`, ruleset);
|
|
1021
|
+
done.push(`ruleset "${ruleset.name}": made`);
|
|
1022
|
+
} else if (differences(
|
|
1023
|
+
{ settings: {}, rulesets: [ruleset] },
|
|
1024
|
+
{ settings: {}, rulesets: [current], unreadable: [] }
|
|
1025
|
+
).length > 0) {
|
|
1026
|
+
await send("PUT", `repos/${repo}/rulesets/${current.id}`, ruleset);
|
|
1027
|
+
done.push(`ruleset "${ruleset.name}": replaced`);
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
for (const current of live.rulesets) {
|
|
1031
|
+
if (file.rulesets.some((ruleset) => ruleset.name === current.name)) continue;
|
|
1032
|
+
if (!prune) {
|
|
1033
|
+
done.push(
|
|
1034
|
+
`ruleset "${current.name}": on GitHub but not in the file; kept (apply --prune removes it)`
|
|
1035
|
+
);
|
|
1036
|
+
continue;
|
|
1037
|
+
}
|
|
1038
|
+
const answer = await gh(exec2, cwd, [
|
|
1039
|
+
"api",
|
|
1040
|
+
"-X",
|
|
1041
|
+
"DELETE",
|
|
1042
|
+
`repos/${repo}/rulesets/${current.id}`,
|
|
1043
|
+
"--silent"
|
|
1044
|
+
]);
|
|
1045
|
+
if (answer.code !== 0)
|
|
1046
|
+
throw new CreateError(
|
|
1047
|
+
`Removing ruleset "${current.name}" failed: ${answer.stderr.trim()}`,
|
|
1048
|
+
1
|
|
1049
|
+
);
|
|
1050
|
+
done.push(`ruleset "${current.name}": removed`);
|
|
1051
|
+
}
|
|
1052
|
+
return done;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
// src/tools.ts
|
|
1056
|
+
function optionsOf(argv, valued, switches = []) {
|
|
1057
|
+
const values = /* @__PURE__ */ new Map();
|
|
1058
|
+
const flags = /* @__PURE__ */ new Set();
|
|
1059
|
+
const words = [];
|
|
1060
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
1061
|
+
const word = argv[i];
|
|
1062
|
+
const eq = word.indexOf("=");
|
|
1063
|
+
const name = word.startsWith("--") ? eq === -1 ? word.slice(2) : word.slice(2, eq) : void 0;
|
|
1064
|
+
if (name !== void 0 && valued.includes(name)) {
|
|
1065
|
+
const value = eq === -1 ? argv[++i] : word.slice(eq + 1);
|
|
1066
|
+
if (value === void 0) throw new CreateError(`--${name} needs a value.`, 2);
|
|
1067
|
+
values.set(name, value);
|
|
1068
|
+
} else if (name !== void 0 && switches.includes(name) && eq === -1) flags.add(name);
|
|
1069
|
+
else if (name !== void 0) throw new CreateError(`There is no option ${word}.`, 2);
|
|
1070
|
+
else words.push(word);
|
|
1071
|
+
}
|
|
1072
|
+
return { values, flags, words };
|
|
1073
|
+
}
|
|
1074
|
+
async function runTool(argv, ctx) {
|
|
1075
|
+
const [command, verb, ...rest] = argv;
|
|
1076
|
+
if (command === "release" && verb === "check") return releaseCheck(rest, ctx);
|
|
1077
|
+
if (command === "release" && verb === "notes") return releaseNotes(rest, ctx);
|
|
1078
|
+
if (command === "commits" && verb === "check") return commitsCheck(rest, ctx);
|
|
1079
|
+
if (command === "settings" && (verb === "apply" || verb === "check"))
|
|
1080
|
+
return settings(verb, rest, ctx);
|
|
1081
|
+
if (command === "sync")
|
|
1082
|
+
return sync(
|
|
1083
|
+
[verb, ...rest].filter((word) => word !== void 0),
|
|
1084
|
+
ctx
|
|
1085
|
+
);
|
|
1086
|
+
throw new CreateError(
|
|
1087
|
+
`There is no command ${[command, verb].filter(Boolean).join(" ")}.`,
|
|
1088
|
+
2,
|
|
1089
|
+
"Use: npm create @sovereignfs app release check --title T, app release notes VERSION, app commits check BASE..HEAD"
|
|
1090
|
+
);
|
|
1091
|
+
}
|
|
1092
|
+
async function releaseCheck(argv, ctx) {
|
|
1093
|
+
const { values, flags, words } = optionsOf(argv, ["title", "dir", "prefix"], ["github-output"]);
|
|
1094
|
+
if (words.length > 0) throw new CreateError(`Too many arguments: ${words.join(" ")}.`, 2);
|
|
1095
|
+
const title = values.get("title") ?? ctx.env.PR_TITLE;
|
|
1096
|
+
if (title === void 0) throw new CreateError("Give the title with --title, or in PR_TITLE.", 2);
|
|
1097
|
+
const prefix = values.get("prefix");
|
|
1098
|
+
if (prefix !== void 0 && !isPrefix(prefix))
|
|
1099
|
+
throw new CreateError(`--prefix "${prefix}" must be empty or end in "-", such as web-.`, 2);
|
|
1100
|
+
const appDir = path7.resolve(ctx.cwd, values.get("dir") ?? ".");
|
|
1101
|
+
const root = await gitRoot(ctx, appDir);
|
|
1102
|
+
const releases = await readReleases(root, appDir);
|
|
1103
|
+
if (prefix && releases.file === "package.json" && releases.components[0])
|
|
1104
|
+
releases.components[0].tagPrefix = prefix;
|
|
1105
|
+
const facts = [];
|
|
1106
|
+
for (const component of releases.components) {
|
|
1107
|
+
const version = await componentVersion(root, component);
|
|
1108
|
+
const tag = `${component.tagPrefix}v${version}`;
|
|
1109
|
+
const listed = await ctx.exec("git", ["tag", "--list", tag], { cwd: root });
|
|
1110
|
+
facts.push({
|
|
1111
|
+
name: component.name,
|
|
1112
|
+
tagPrefix: component.tagPrefix,
|
|
1113
|
+
version,
|
|
1114
|
+
tagged: listed.code === 0 && listed.stdout.trim() !== "",
|
|
1115
|
+
hasNotes: changelogSection(await readText2(path7.join(root, component.changelog)), version) !== void 0
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
const checked = checkComponentsTitle(title, facts);
|
|
1119
|
+
if (checked.problems.length > 0) {
|
|
1120
|
+
ctx.io.err(`The title "${title}" does not match what this repository releases:`);
|
|
1121
|
+
for (const problem of checked.problems) ctx.io.err(` ${problem}`);
|
|
1122
|
+
return 1;
|
|
1123
|
+
}
|
|
1124
|
+
const released = Object.values(checked.tags);
|
|
1125
|
+
const single = facts.length === 1 ? facts[0] : void 0;
|
|
1126
|
+
ctx.io.out(
|
|
1127
|
+
JSON.stringify({
|
|
1128
|
+
release: checked.release,
|
|
1129
|
+
tags: checked.tags,
|
|
1130
|
+
...single ? { tag: `${single.tagPrefix}v${single.version}`, version: single.version } : {}
|
|
1131
|
+
})
|
|
1132
|
+
);
|
|
1133
|
+
if (flags.has("github-output")) {
|
|
1134
|
+
const file = ctx.env.GITHUB_OUTPUT;
|
|
1135
|
+
if (!file) throw new CreateError("--github-output needs GITHUB_OUTPUT.", 2);
|
|
1136
|
+
await appendFile(
|
|
1137
|
+
file,
|
|
1138
|
+
`release=${String(checked.release)}
|
|
1139
|
+
tags=${JSON.stringify(checked.tags)}
|
|
1140
|
+
tag=${released.length === 1 ? released[0] : ""}
|
|
1141
|
+
version=${single?.version ?? ""}
|
|
1142
|
+
`
|
|
1143
|
+
);
|
|
1144
|
+
}
|
|
1145
|
+
return 0;
|
|
1146
|
+
}
|
|
1147
|
+
async function releaseNotes(argv, ctx) {
|
|
1148
|
+
const { values, words } = optionsOf(argv, ["dir"]);
|
|
1149
|
+
const [asked, ...more] = words;
|
|
1150
|
+
if (asked === void 0 || more.length > 0)
|
|
1151
|
+
throw new CreateError("Give one version or tag: release notes 1.2.0.", 2);
|
|
1152
|
+
const appDir = path7.resolve(ctx.cwd, values.get("dir") ?? ".");
|
|
1153
|
+
const root = await gitRoot(ctx, appDir);
|
|
1154
|
+
const releases = await readReleases(root, appDir);
|
|
1155
|
+
const byPrefix = [...releases.components].sort((a, b) => b.tagPrefix.length - a.tagPrefix.length);
|
|
1156
|
+
const component = byPrefix.find(
|
|
1157
|
+
(c) => asked.startsWith(`${c.tagPrefix}v`) && /^v\d+\.\d+\.\d+$/.test(asked.slice(c.tagPrefix.length))
|
|
1158
|
+
) ?? (releases.components.length === 1 ? releases.components[0] : void 0);
|
|
1159
|
+
if (!component) throw new CreateError(`${asked} is not the tag of a component here.`, 1);
|
|
1160
|
+
const version = asked.replace(/^(?:[a-z][a-z0-9-]*-)?v/, "");
|
|
1161
|
+
const section = changelogSection(await readText2(path7.join(root, component.changelog)), version);
|
|
1162
|
+
if (section === void 0) {
|
|
1163
|
+
ctx.io.err(`${component.changelog} has no section for ${version}.`);
|
|
1164
|
+
return 1;
|
|
1165
|
+
}
|
|
1166
|
+
ctx.io.out(section);
|
|
1167
|
+
return 0;
|
|
1168
|
+
}
|
|
1169
|
+
async function readText2(file) {
|
|
1170
|
+
try {
|
|
1171
|
+
return await readFile5(file, "utf8");
|
|
1172
|
+
} catch {
|
|
1173
|
+
return "";
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
async function gitRoot(ctx, dir) {
|
|
1177
|
+
const top = await ctx.exec("git", ["rev-parse", "--show-toplevel"], { cwd: dir });
|
|
1178
|
+
return top.code === 0 && top.stdout.trim() !== "" ? top.stdout.trim() : dir;
|
|
1179
|
+
}
|
|
1180
|
+
async function commitsCheck(argv, ctx) {
|
|
1181
|
+
const { words } = optionsOf(argv, []);
|
|
1182
|
+
const [range, ...more] = words;
|
|
1183
|
+
if (range === void 0 || !range.includes("..") || more.length > 0) {
|
|
1184
|
+
throw new CreateError("Give one range: commits check origin/main..HEAD.", 2);
|
|
1185
|
+
}
|
|
1186
|
+
const log = await ctx.exec("git", ["log", "--no-merges", "--format=%H%x1f%B%x1e", range], {
|
|
1187
|
+
cwd: ctx.cwd
|
|
1188
|
+
});
|
|
1189
|
+
if (log.code !== 0) throw new CreateError(`git log ${range} failed: ${log.stderr.trim()}`, 1);
|
|
1190
|
+
const refused = [];
|
|
1191
|
+
for (const record of log.stdout.split("")) {
|
|
1192
|
+
const [sha, message] = record.trim().split("");
|
|
1193
|
+
if (!sha || message === void 0) continue;
|
|
1194
|
+
for (const problem of trailerProblems(message)) refused.push(`${sha.slice(0, 7)}: ${problem}`);
|
|
1195
|
+
}
|
|
1196
|
+
if (refused.length > 0) {
|
|
1197
|
+
ctx.io.err(`${refused.length} commit(s) in ${range} carry a trailer that is not allowed:`);
|
|
1198
|
+
for (const line of refused) ctx.io.err(` ${line}`);
|
|
1199
|
+
return 1;
|
|
1200
|
+
}
|
|
1201
|
+
ctx.io.out(`Every commit in ${range} has allowed trailers.`);
|
|
1202
|
+
return 0;
|
|
1203
|
+
}
|
|
1204
|
+
async function sync(argv, ctx) {
|
|
1205
|
+
const { values, flags, words } = optionsOf(argv, ["dir"], ["check"]);
|
|
1206
|
+
if (words.length > 0) throw new CreateError(`Too many arguments: ${words.join(" ")}.`, 2);
|
|
1207
|
+
const appDir = path7.resolve(ctx.cwd, values.get("dir") ?? ".");
|
|
1208
|
+
const top = await ctx.exec("git", ["rev-parse", "--show-toplevel"], { cwd: appDir });
|
|
1209
|
+
const gitRoot2 = top.code === 0 && top.stdout.trim() !== "" ? top.stdout.trim() : appDir;
|
|
1210
|
+
const check = flags.has("check");
|
|
1211
|
+
const results = await syncApp(appDir, gitRoot2, { check });
|
|
1212
|
+
for (const result of results) {
|
|
1213
|
+
const said = {
|
|
1214
|
+
same: "the same",
|
|
1215
|
+
written: "written",
|
|
1216
|
+
drifted: "differs from the template",
|
|
1217
|
+
unmanaged: "kept as the app's own"
|
|
1218
|
+
}[result.state];
|
|
1219
|
+
const line = ` ${result.path}: ${said}${result.reason ? ` (${result.reason})` : ""}`;
|
|
1220
|
+
if (result.state === "drifted") ctx.io.err(line);
|
|
1221
|
+
else ctx.io.out(line);
|
|
1222
|
+
}
|
|
1223
|
+
const drifted = results.filter((result) => result.state === "drifted");
|
|
1224
|
+
if (drifted.length > 0) {
|
|
1225
|
+
ctx.io.err(
|
|
1226
|
+
`${drifted.length} shared file(s) differ from the template. Run npm create @sovereignfs app sync, or list a file under sovereign.unmanaged in package.json if this app keeps it as its own.`
|
|
1227
|
+
);
|
|
1228
|
+
return 1;
|
|
1229
|
+
}
|
|
1230
|
+
return 0;
|
|
1231
|
+
}
|
|
1232
|
+
async function settings(verb, argv, ctx) {
|
|
1233
|
+
const { values, flags, words } = optionsOf(argv, ["file", "repo"], ["prune"]);
|
|
1234
|
+
if (words.length > 0) throw new CreateError(`Too many arguments: ${words.join(" ")}.`, 2);
|
|
1235
|
+
const top = await ctx.exec("git", ["rev-parse", "--show-toplevel"], { cwd: ctx.cwd });
|
|
1236
|
+
const root = top.code === 0 && top.stdout.trim() !== "" ? top.stdout.trim() : ctx.cwd;
|
|
1237
|
+
const file = await readRepositoryFile(
|
|
1238
|
+
path7.resolve(root, values.get("file") ?? ".github/repository.json")
|
|
1239
|
+
);
|
|
1240
|
+
let repo = values.get("repo") ?? ctx.env.GITHUB_REPOSITORY;
|
|
1241
|
+
if (!repo) {
|
|
1242
|
+
const seen = await ctx.exec(
|
|
1243
|
+
"gh",
|
|
1244
|
+
["repo", "view", "--json", "nameWithOwner", "-q", ".nameWithOwner"],
|
|
1245
|
+
{ cwd: root }
|
|
1246
|
+
);
|
|
1247
|
+
repo = seen.code === 0 ? seen.stdout.trim() : "";
|
|
1248
|
+
}
|
|
1249
|
+
if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo)) {
|
|
1250
|
+
throw new CreateError(
|
|
1251
|
+
"Say which repository: --repo OWNER/NAME, or run it where gh knows the repository.",
|
|
1252
|
+
2
|
|
1253
|
+
);
|
|
1254
|
+
}
|
|
1255
|
+
const live = await readLive(ctx.exec, root, repo, file);
|
|
1256
|
+
for (const what of live.unreadable)
|
|
1257
|
+
ctx.io.err(` ${what}: not checked; this token cannot read it (an owner's apply can).`);
|
|
1258
|
+
if (verb === "apply") {
|
|
1259
|
+
const done = await apply(ctx.exec, root, repo, file, live, flags.has("prune"));
|
|
1260
|
+
for (const line of done.length > 0 ? done : ["nothing to change"]) ctx.io.out(` ${line}`);
|
|
1261
|
+
return 0;
|
|
1262
|
+
}
|
|
1263
|
+
const found = differences(file, live);
|
|
1264
|
+
if (found.length === 0) {
|
|
1265
|
+
ctx.io.out(`${repo} has the settings in the file.`);
|
|
1266
|
+
return 0;
|
|
1267
|
+
}
|
|
1268
|
+
ctx.io.err(`${repo} differs from the file in ${found.length} place(s):`);
|
|
1269
|
+
for (const { what, want, have } of found) {
|
|
1270
|
+
ctx.io.err(
|
|
1271
|
+
` ${what}: the file says ${JSON.stringify(want)}, GitHub has ${JSON.stringify(have)}`
|
|
1272
|
+
);
|
|
1273
|
+
}
|
|
1274
|
+
ctx.io.err(
|
|
1275
|
+
"Change the file and apply it (npm create @sovereignfs app settings apply); never the interface."
|
|
1276
|
+
);
|
|
1277
|
+
return 1;
|
|
1278
|
+
}
|
|
1279
|
+
|
|
429
1280
|
// src/main.ts
|
|
430
1281
|
var exec = (command, args, options) => new Promise((resolve) => {
|
|
431
1282
|
execFile(
|
|
@@ -456,7 +1307,17 @@ var io = {
|
|
|
456
1307
|
};
|
|
457
1308
|
async function main() {
|
|
458
1309
|
try {
|
|
459
|
-
const
|
|
1310
|
+
const argv = process.argv.slice(2);
|
|
1311
|
+
if (argv[0] === "app" && TOOL_COMMANDS.includes(argv[1] ?? "")) {
|
|
1312
|
+
return await runTool(argv.slice(1), {
|
|
1313
|
+
io: { ...io, out: (line) => process.stdout.write(`${line}
|
|
1314
|
+
`) },
|
|
1315
|
+
exec,
|
|
1316
|
+
cwd: process.cwd(),
|
|
1317
|
+
env: process.env
|
|
1318
|
+
});
|
|
1319
|
+
}
|
|
1320
|
+
const request = readRequest(argv, process.env);
|
|
460
1321
|
let { kind, dir } = request;
|
|
461
1322
|
if (kind === void 0) {
|
|
462
1323
|
if (!io.interactive)
|
|
@@ -489,8 +1350,10 @@ async function main() {
|
|
|
489
1350
|
const answer = await io.ask(`Which port should it listen on? (${DEFAULT_APP_PORT})`);
|
|
490
1351
|
port = answer === "" ? DEFAULT_APP_PORT : parsePort(answer);
|
|
491
1352
|
}
|
|
492
|
-
await createApp({ dir, port }, io, exec, process.cwd());
|
|
1353
|
+
await createApp({ dir, port, encryption: request.encryption }, io, exec, process.cwd());
|
|
493
1354
|
} else {
|
|
1355
|
+
if (request.encryption)
|
|
1356
|
+
throw new CreateError("--encryption is for an app.", 2, "Make an app with it instead.");
|
|
494
1357
|
if (request.port !== void 0)
|
|
495
1358
|
throw new CreateError(
|
|
496
1359
|
"--port is for an app.",
|