@intellectual-frontiers/libraries 0.4.0 → 0.5.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.
Files changed (64) hide show
  1. package/LICENSE +75 -21
  2. package/dist/affected.d.ts +1 -1
  3. package/dist/dev.d.ts +2 -0
  4. package/dist/dev.js +14 -2
  5. package/dist/family.js +1 -0
  6. package/dist/generate-ontology.js +4 -0
  7. package/dist/imports/nist-sp-800-53.auto.prov.ttl +22 -0
  8. package/dist/imports/schema-org.auto.prov.ttl +22 -0
  9. package/dist/install-bun.js +19 -14
  10. package/dist/libraries.d.ts +39 -2
  11. package/dist/libraries.js +80 -9
  12. package/dist/license/PolyForm-Noncommercial-1.0.0.md +73 -0
  13. package/dist/license/standard.d.ts +46 -0
  14. package/dist/license/standard.js +43 -0
  15. package/dist/license/third-party.d.ts +25 -0
  16. package/dist/license/third-party.js +71 -0
  17. package/dist/license.d.ts +61 -0
  18. package/dist/license.js +204 -0
  19. package/dist/link.d.ts +1 -1
  20. package/dist/link.js +4 -4
  21. package/dist/metapackages.d.ts +62 -0
  22. package/dist/metapackages.js +190 -0
  23. package/dist/mod.d.ts +1 -1
  24. package/dist/mod.js +1 -1
  25. package/dist/pack-library.d.ts +9 -0
  26. package/dist/pack-library.js +46 -58
  27. package/dist/pack-metapackage.d.ts +22 -0
  28. package/dist/pack-metapackage.js +147 -0
  29. package/dist/pins.d.ts +34 -7
  30. package/dist/pins.js +36 -9
  31. package/dist/readme.d.ts +71 -0
  32. package/dist/readme.js +310 -0
  33. package/dist/releasing.d.ts +8 -1
  34. package/dist/releasing.js +44 -14
  35. package/dist/workflows/definitions.d.ts +2 -2
  36. package/dist/workflows/definitions.js +32 -114
  37. package/dist/workflows/model.d.ts +6 -0
  38. package/dist/workflows/render.js +2 -0
  39. package/dist/workflows/schema/github-workflow.schema.json +2290 -0
  40. package/package.json +39 -14
  41. package/src/affected.ts +1 -1
  42. package/src/dev.ts +13 -2
  43. package/src/family.ts +1 -0
  44. package/src/generate-ontology.ts +4 -0
  45. package/src/imports/nist-sp-800-53.auto.prov.ttl +22 -0
  46. package/src/imports/schema-org.auto.prov.ttl +22 -0
  47. package/src/install-bun.ts +16 -13
  48. package/src/libraries.ts +100 -8
  49. package/src/license/PolyForm-Noncommercial-1.0.0.md +73 -0
  50. package/src/license/standard.ts +67 -0
  51. package/src/license/third-party.ts +87 -0
  52. package/src/license.ts +222 -0
  53. package/src/link.ts +4 -4
  54. package/src/metapackages.ts +206 -0
  55. package/src/mod.ts +1 -1
  56. package/src/pack-library.ts +52 -55
  57. package/src/pack-metapackage.ts +137 -0
  58. package/src/pins.ts +55 -10
  59. package/src/readme.ts +320 -0
  60. package/src/releasing.ts +47 -16
  61. package/src/workflows/definitions.ts +36 -133
  62. package/src/workflows/model.ts +6 -0
  63. package/src/workflows/render.ts +1 -0
  64. package/src/workflows/schema/github-workflow.schema.json +2290 -0
@@ -1,5 +1,5 @@
1
1
  /**
2
- * The package of a library of commons (commons 0032-lifting-libraries FR-005, FR-013): the library's sources compiled by the TypeScript 7 compiler into ECMAScript modules and their declarations, the
2
+ * The package of a library of commons (commons 0032-libraries-by-concern FR-005, FR-013): the library's sources compiled by the TypeScript 7 compiler into ECMAScript modules and their declarations, the
3
3
  * `package.json` that is published (each `exports` entry naming its `types`, the source for Bun and the compiled file for everything else), and the tarball. The same inspection that the packer makes before it
4
4
  * packs is `inspectPackage`, which a test runs again on the tarball it unpacks.
5
5
  *
@@ -24,6 +24,8 @@
24
24
  */
25
25
  import { chmod, readTextFile, writeTextFile } from "@intellectual-frontiers/system/fs";
26
26
  import { existsSync, mkdirSync, readDirSync, readTextFileSync, removeSync, writeTextFileSync } from "@intellectual-frontiers/system/fs";
27
+ import { decodeLibrarySource, type Library, LIBRARY_FILE } from "./libraries.ts";
28
+ import { COMMAND as LICENSE_COMMAND, licenseOf } from "./license.ts";
27
29
  import { basename, dirname, join, relative, resolve, toPosix } from "@intellectual-frontiers/system/path";
28
30
  import { env, run, which } from "@intellectual-frontiers/system/proc";
29
31
 
@@ -35,7 +37,6 @@ export type Findings = readonly string[];
35
37
 
36
38
  type Json = Readonly<Record<string, unknown>>;
37
39
 
38
- const obj = (v: unknown): Json => (isObject(v) ? v : {});
39
40
  const isObject = (v: unknown): v is Json => typeof v === "object" && v !== null && !Array.isArray(v);
40
41
  const text = (v: unknown): string => (typeof v === "string" ? v : "");
41
42
  /** A text file copied: every file of a package is text. */
@@ -150,7 +151,7 @@ export function inspectPackage(stage: string): Findings {
150
151
  for (const s of specifiersOf(readTextFileSync(join(stage, d)))) {
151
152
  const pkg = packageOf(s);
152
153
  if (pkg === undefined) continue;
153
- if (pkg === `${SCOPE}/commons`) found.push(`${d} imports ${s}: the package ${pkg} is not on npm`);
154
+ if (pkg === `${SCOPE}/commons`) found.push(`${d} imports ${s}: the package ${pkg} is a metapackage with no code: import the library that holds what is used`);
154
155
  else if (pkg !== name && !declared.has(pkg)) found.push(`${d} imports ${pkg}, which is not a dependency of ${name}`);
155
156
  }
156
157
  }
@@ -167,7 +168,7 @@ export function inspectPackage(stage: string): Findings {
167
168
  for (const s of specifiersOf(source)) {
168
169
  const pkg = packageOf(s);
169
170
  if (pkg === undefined) continue;
170
- if (pkg === `${SCOPE}/commons`) found.push(`${f} imports ${s}: the package ${pkg} is not on npm`);
171
+ if (pkg === `${SCOPE}/commons`) found.push(`${f} imports ${s}: the package ${pkg} is a metapackage with no code: import the library that holds what is used`);
171
172
  else if (pkg !== name && !declared.has(pkg)) found.push(`${f} imports ${pkg}, which is not a dependency of ${name}`);
172
173
  }
173
174
  }
@@ -187,24 +188,24 @@ export function rewriteDeclarationSpecifiers(source: string): string {
187
188
  export const tarballName = (name: string, version: string): string => `${name.replace(/^@/, "").replace("/", "-")}-${version}.tgz`;
188
189
 
189
190
  /** The version of a library of commons, from its `package.json`, and whether it is published to npm (its manifest names that). */
190
- function libraryOf(commons: string, name: string): { readonly version: string; readonly npm: boolean } | undefined {
191
+ function libraryOf(commons: string, name: string): { readonly version: string; readonly npm: boolean; readonly held: boolean } | undefined {
191
192
  const pkg = join(commons, name, "package.json");
192
193
  if (!existsSync(pkg)) return undefined;
193
194
  const lib = join(commons, name, "library.ts");
194
- const npm = existsSync(lib) && /distribution:\s*\[[^\]]*"npm"/.test(readTextFileSync(lib));
195
- return { version: text((JSON.parse(readTextFileSync(pkg)) as Json)["version"]), npm };
195
+ const source = existsSync(lib) ? readTextFileSync(lib) : "";
196
+ const npm = /distribution:\s*\[[^\]]*"npm"/.test(source);
197
+ const held = /\bheld:\s*"/.test(source);
198
+ return { version: text((JSON.parse(readTextFileSync(pkg)) as Json)["version"]), npm, held };
196
199
  }
197
200
 
198
201
  /**
199
- * The declarations of a bundle: from the entries' declarations in the compiler's output, every declaration reached by a relative import is published, and so is every declaration that a package of commons that is not
200
- * on npm (`@intellectual-frontiers/commons/ts/...`) gives to them, vendored under `dist/vendor/` with the specifiers that name it rewritten to relative ones. It is the way for a package to publish its API while what
201
- * its types are made of is still lifted out of `commons/ts` a library at a time (commons 0032-lifting-libraries); a library that has been lifted is a dependency and is not vendored.
202
+ * The declarations of a bundle: from the entries' declarations in the compiler's output, every declaration reached by a relative import is published, those of a file outside the library's own `src/` under `dist/vendor/`
203
+ * with the specifiers that name it rewritten to relative ones. A library of commons that a bundle uses is a dependency and is not vendored.
202
204
  */
203
205
  function vendorDeclarations(
204
206
  outRoot: string,
205
207
  lib: string,
206
208
  entries: readonly string[],
207
- commonsPkg: { readonly folder: string; readonly exports: ReadonlyMap<string, string> },
208
209
  stageDist: string,
209
210
  ): Findings {
210
211
  const found: string[] = [];
@@ -226,26 +227,10 @@ function vendorDeclarations(
226
227
  placed.set(file, dest);
227
228
  for (const s of specifiersOf(readTextFileSync(file))) {
228
229
  if (s.startsWith(".")) todo.push(join(dirname(file), s.replace(/\.(?:js|ts|mjs)$/, "") + ".d.ts"));
229
- else if (s.startsWith(`${SCOPE}/commons/`)) {
230
- const target = commonsPkg.exports.get(`./${s.slice(`${SCOPE}/commons/`.length)}`);
231
- if (target === undefined) found.push(`${dest} imports ${s}, which the exports of commons do not name`);
232
- else todo.push(join(outRoot, commonsPkg.folder, target.replace(/^\.\//, "").replace(/\.ts$/, "") + ".d.ts"));
233
- }
234
230
  }
235
231
  }
236
232
  for (const [file, dest] of placed) {
237
233
  let source = rewriteDeclarationSpecifiers(readTextFileSync(file));
238
- source = source.replace(
239
- new RegExp(`((?:from\\s+|import\\(\\s*|import\\s+)["'])${SCOPE}/commons/([^"']+)(["'])`, "g"),
240
- (whole, pre: string, sub: string, post: string) => {
241
- const target = commonsPkg.exports.get(`./${sub}`);
242
- if (target === undefined) return whole;
243
- const there = place(join(outRoot, commonsPkg.folder, target.replace(/^\.\//, "").replace(/\.ts$/, "") + ".d.ts"));
244
- let rel = toPosix(relative(dirname(dest), there)).replace(/\.d\.ts$/, ".js");
245
- if (!rel.startsWith(".")) rel = `./${rel}`;
246
- return `${pre}${rel}${post}`;
247
- },
248
- );
249
234
  // a relative import that leaves the library's own tree (a type the compiler named by its path in commons) goes to where that declaration was placed
250
235
  source = source.replace(
251
236
  /((?:from\s+|import\(\s*|import\s+)["'])(\.[^"']+)(["'])/g,
@@ -265,22 +250,11 @@ function vendorDeclarations(
265
250
  return found;
266
251
  }
267
252
 
268
- /** The compiler's `paths` for the packages of commons that are not on npm: each entry of `commons/ts/package.json` (`./ts/kernel`) names the source that the specifier `@intellectual-frontiers/commons/ts/kernel` is. */
269
- function commonsPaths(commons: string, dist: string): Record<string, readonly string[]> {
270
- const file = join(commons, "ts", "package.json");
271
- const out: Record<string, readonly string[]> = {};
272
- if (!existsSync(file)) return out;
273
- for (const [k, v] of Object.entries(obj(obj(JSON.parse(readTextFileSync(file)) as unknown)["exports"]))) {
274
- if (typeof v === "string" && v.endsWith(".ts"))
275
- out[`${SCOPE}/commons/${k.replace(/^\.\//, "")}`] = [toPosix(relative(dist, join(commons, "ts", v)))];
276
- }
277
- return out;
278
- }
279
-
253
+ /** What packing a library made: the staging folder, the tarball, the findings and the manifest. */
280
254
  export type Packed = { readonly stage: string; readonly tarball: string | undefined; readonly findings: Findings; readonly manifest: Json };
281
255
 
282
256
  /** What a package adds to the common form. */
283
- /** Whether a declaration of the package names a `node:` module: a package whose types name Node's needs `@types/node` where it is type-checked (a peer dependency, FR-013 of 0032-lifting-libraries). */
257
+ /** Whether a declaration of the package names a `node:` module: a package whose types name Node's needs `@types/node` where it is type-checked (a peer dependency, FR-013 of 0032-libraries-by-concern). */
284
258
  function namesNodeTypes(folder: string): boolean {
285
259
  if (!existsSync(folder)) return false;
286
260
  for (const e of readDirSync(folder)) {
@@ -292,9 +266,17 @@ function namesNodeTypes(folder: string): boolean {
292
266
  return false;
293
267
  }
294
268
 
269
+ /** What packing a library is told: its folder, where to stage it and whether to make the tarball. */
295
270
  export type PackOptions = {
296
271
  /** Types the compiler is given (`["node"]` for a library that names Node's). */
297
272
  readonly types?: readonly string[];
273
+ /**
274
+ * Commands of a package that is not a bundle: each command's name and the module, in `src/`, whose `main(argv)` gives the exit status; the package gets a one-line launcher for each (`<command>.mjs`, which Node and Bun run), made
275
+ * from the compiled module. The files under `src/` that are not TypeScript (a stylesheet a module reads beside itself) are published beside the sources and beside the compiled modules.
276
+ */
277
+ readonly launchers?: Readonly<Record<string, string>>;
278
+ /** Packages the published package may use where they are installed beside it and does not need (loaded by name at run time): written as optional peer dependencies, name and range. */
279
+ readonly optionalPeers?: Readonly<Record<string, string>>;
298
280
  /** A bundled package: the code of each entry and of each command is one file (`bun build`), and only the declarations the entries reach are published. */
299
281
  readonly bundle?: {
300
282
  /** The commands: their name, and the source that is the command's entry from the library's folder. */
@@ -307,6 +289,14 @@ export type PackOptions = {
307
289
  };
308
290
  };
309
291
 
292
+ /** The manifest of the library at `root`, where it has one that can be read. */
293
+ function manifestOf(root: string): Library | undefined {
294
+ const file = join(root, LIBRARY_FILE);
295
+ if (!existsSync(file)) return undefined;
296
+ const r = decodeLibrarySource(readTextFileSync(file));
297
+ return r.kind === "ok" ? r.value : undefined;
298
+ }
299
+
310
300
  /** Builds and stages the package of the library at `root` (a folder `commons/<name>/` with a `package.json` and sources in `src/`), inspects it and, where nothing is wrong, packs it. */
311
301
  export async function packLibrary(root: string, options: PackOptions = {}): Promise<Packed> {
312
302
  const own = JSON.parse(readTextFileSync(join(root, "package.json"))) as Json;
@@ -319,7 +309,7 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
319
309
  if (existsSync(dist)) removeSync(dist, { recursive: true });
320
310
  mkdirSync(stage, { recursive: true });
321
311
  // the compiler's configuration for this build: the base of commons, emitting modules and declarations of src/ and nothing else
322
- const base = relative(dist, join(commons, "ts", "tsconfig.json"));
312
+ const base = relative(dist, join(commons, "tsconfig.base.json"));
323
313
  writeTextFileSync(
324
314
  join(dist, "tsconfig.build.json"),
325
315
  JSON.stringify(
@@ -335,8 +325,6 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
335
325
  skipLibCheck: true,
336
326
  // another library of commons is read where the package manager linked it (under node_modules), so that a declaration names it by its package and its entry and never by the path of its sources
337
327
  preserveSymlinks: true,
338
- // a bundle's declarations are made from the sources of the packages of commons that are not on npm, which the compiler emits only when it reads them as sources and not through node_modules
339
- ...(bundle === undefined ? {} : { paths: commonsPaths(commons, dist) }),
340
328
  },
341
329
  include: [toPosix(relative(dist, join(root, "src"))) + "/**/*.ts"],
342
330
  },
@@ -360,12 +348,6 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
360
348
  else if (f.endsWith(".js")) copyFileSync(join(out, f), target);
361
349
  }
362
350
  } else {
363
- const commonsPkgFile = join(commons, "ts", "package.json");
364
- const commonsExports = new Map<string, string>();
365
- if (existsSync(commonsPkgFile)) {
366
- const declared = obj(obj(JSON.parse(readTextFileSync(commonsPkgFile)) as unknown)["exports"]);
367
- for (const [k, v] of Object.entries(declared)) if (typeof v === "string") commonsExports.set(k, v);
368
- }
369
351
  const entryFiles = Object.values(ownExports).map((e) =>
370
352
  join(out, (targetsOf(e)["default"] ?? text(e)).replace(/^\.\/src\//, "").replace(/\.ts$/, "") + ".d.ts"),
371
353
  );
@@ -374,7 +356,6 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
374
356
  join(dist, "out"),
375
357
  basename(resolve(root)),
376
358
  entryFiles,
377
- { folder: "ts", exports: commonsExports },
378
359
  join(stage, "dist"),
379
360
  ),
380
361
  );
@@ -382,10 +363,15 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
382
363
  // the sources, for the `bun` condition (a bundle has no source to offer: what it imports is not on npm)
383
364
  if (bundle === undefined) {
384
365
  for (const f of filesUnder(join(root, "src"))) {
385
- if (!f.endsWith(".ts")) continue;
386
366
  const target = join(stage, "src", f);
387
367
  mkdirSync(dirname(target), { recursive: true });
388
368
  copyFileSync(join(root, "src", f), target);
369
+ if (!f.endsWith(".ts")) {
370
+ // a file that is not TypeScript is read beside the module that reads it, from the sources and from the compiled modules alike
371
+ const compiled = join(stage, "dist", f);
372
+ mkdirSync(dirname(compiled), { recursive: true });
373
+ copyFileSync(join(root, "src", f), compiled);
374
+ }
389
375
  }
390
376
  }
391
377
  // the entries: each names its declarations, its source (Bun) and its compiled file
@@ -409,6 +395,13 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
409
395
  }
410
396
  // the commands of a bundle: one file each, with the line that runs it under Node
411
397
  const binOut: Record<string, string> = {};
398
+ for (const [cmd, source] of Object.entries(options.launchers ?? {})) {
399
+ const rel = toPosix(source).replace(/^\.?\/?(src\/)?/, "").replace(/\.ts$/, "");
400
+ const file = join(stage, `${cmd}.mjs`);
401
+ await writeTextFile(file, `#!/usr/bin/env node\nimport { main } from "./dist/${rel}.js";\n\nprocess.exit(await main(process.argv.slice(2)));\n`);
402
+ await chmod(file, 0o755);
403
+ binOut[cmd] = `./${cmd}.mjs`;
404
+ }
412
405
  for (const [cmd, source] of Object.entries(bundle?.bin ?? {})) {
413
406
  const file = join(stage, `${cmd}.mjs`);
414
407
  const bundled = await run([bun, "build", join(root, source), "--target=node", "--format=esm", "--outfile", file], { cwd: root });
@@ -446,7 +439,7 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
446
439
  if (pkg.startsWith(`${SCOPE}/`) && pkg !== `${SCOPE}/commons`) {
447
440
  const lib = libraryOf(commons, pkg.slice(SCOPE.length + 1));
448
441
  if (lib === undefined) findings.push(`${file} imports ${pkg}, which is no library of commons`);
449
- else if (!lib.npm) findings.push(`${file} imports ${pkg}, which is not published to npm`);
442
+ else if (!lib.npm && !(libraryOf(commons, short)?.held === true && lib.held)) findings.push(`${file} imports ${pkg}, which is not published to npm`);
450
443
  else dependencies[pkg] = lib.version;
451
444
  } else if (pkg !== `${SCOPE}/commons`) {
452
445
  const v = declaredDeps[pkg];
@@ -461,7 +454,7 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
461
454
  name,
462
455
  version: text(own["version"]),
463
456
  description: text(own["description"]),
464
- license: "MIT",
457
+ license: licenseOf(manifestOf(root)),
465
458
  type: "module",
466
459
  engines: { node: ">=26" },
467
460
  sideEffects: false,
@@ -471,7 +464,10 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
471
464
  ...(Object.keys(dependencies).length === 0
472
465
  ? {}
473
466
  : { dependencies: Object.fromEntries(Object.entries(dependencies).sort(([a], [b]) => (a < b ? -1 : 1))) }),
474
- ...(namesNodeTypes(join(stage, "dist")) ? { peerDependencies: { "@types/node": ">=26" } } : {}),
467
+ ...(namesNodeTypes(join(stage, "dist")) || options.optionalPeers !== undefined
468
+ ? { peerDependencies: { ...(namesNodeTypes(join(stage, "dist")) ? { "@types/node": ">=26" } : {}), ...(options.optionalPeers ?? {}) } }
469
+ : {}),
470
+ ...(options.optionalPeers === undefined ? {} : { peerDependenciesMeta: Object.fromEntries(Object.keys(options.optionalPeers).map((n) => [n, { optional: true }])) }),
475
471
  files: [
476
472
  ...(Object.keys(exportsOut).length === 0 ? [] : ["dist"]),
477
473
  ...(bundle === undefined ? ["src"] : []),
@@ -503,7 +499,8 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
503
499
  "",
504
500
  ].join("\n"),
505
501
  );
506
- copyFileSync(existsSync(join(root, "LICENSE")) ? join(root, "LICENSE") : join(commons, "ergonex", "LICENSE"), join(stage, "LICENSE"));
502
+ if (existsSync(join(root, "LICENSE"))) copyFileSync(join(root, "LICENSE"), join(stage, "LICENSE"));
503
+ else findings.push(`commons/${short} has no LICENSE; run: ${LICENSE_COMMAND}`);
507
504
  findings.push(...inspectPackage(stage));
508
505
  if (findings.length > 0) return { stage, tarball: undefined, findings, manifest };
509
506
  const packed = await run([bun, "pm", "pack", "--destination", dist], { cwd: stage });
@@ -0,0 +1,137 @@
1
+ /**
2
+ * The generator and the packer of the metapackages (commons 0036-distribution FR-007, FR-008): the file of each set that records its exact dependencies, and the tarball of its package.
3
+ *
4
+ * bun commons/libraries/src/pack-metapackage.ts write the file `dependencies.auto.json` of every metapackage again from the manifests
5
+ * bun commons/libraries/src/pack-metapackage.ts --check write nothing and exit 1 where a file is not what the manifests make
6
+ * bun tools/pack.ts [--check] (in the folder of a metapackage) stage, inspect and pack its package
7
+ * other flags: --repo DIR (the repository, the working folder by default)
8
+ *
9
+ * @remarks
10
+ * A metapackage is a folder `commons/<set>/` whose manifest names `bundle`; the folder holds the manifest, a private `package.json`, the `LICENSE` and `tools/pack.ts`, and nothing that is code. Its members come from the manifests
11
+ * ({@link membersOf}) and are written to `dependencies.auto.json` in its folder, so that a change of the set is a change of the folder, which the release sees. The packer writes the staged package (`package.json` with the
12
+ * exact dependencies, an empty `index.js` and its declaration, the README, the `LICENSE`), inspects it with the same inspection as every library's and packs it with `bun pm pack`.
13
+ *
14
+ * @limits
15
+ * It writes the files named above and runs `bun pm pack`; it asks no registry.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { synchronize } from "./pack-metapackage.ts";
20
+ * synchronize(".", false).problems; // []
21
+ * ```
22
+ *
23
+ * Spec: 0001-libraries FR-023
24
+ *
25
+ * @module
26
+ */
27
+ import process from "node:process";
28
+ import { err, ok, type Result } from "@intellectual-frontiers/core";
29
+ import { stdio } from "@intellectual-frontiers/system";
30
+ import { existsSync, mkdirSync, readTextFileSync, removeSync, writeTextFileSync } from "@intellectual-frontiers/system/fs";
31
+ import { join, resolve } from "@intellectual-frontiers/system/path";
32
+ import { env, run, which } from "@intellectual-frontiers/system/proc";
33
+ import { decodeLibrarySource, type Library, LIBRARY_FILE, readLibraries } from "./libraries.ts";
34
+ import { licenseOf } from "./license.ts";
35
+ import { ENTRY_DTS, ENTRY_JS, membersOf, publishedManifest, readmeOf, readSetFile, SET_FILE, setFileText } from "./metapackages.ts";
36
+ import { inspectPackage, tarballName } from "./pack-library.ts";
37
+
38
+ /** The metapackages of a repository: the libraries whose manifest names a set. */
39
+ export function metapackagesOf(repo: string): readonly (Library & { readonly bundle: NonNullable<Library["bundle"]> })[] {
40
+ return readLibraries(repo).flatMap((f) => {
41
+ if (!("library" in f)) return [];
42
+ const l = f.library;
43
+ return l.bundle === undefined ? [] : [{ ...l, bundle: l.bundle }];
44
+ });
45
+ }
46
+
47
+ /** The files that are not what the manifests make (from the repository's root), and what is wrong with reading them; with `write`, the files are written. */
48
+ export function synchronize(repo: string, write: boolean): { readonly stale: readonly string[]; readonly problems: readonly string[] } {
49
+ const root = repo.replace(/[\\/]$/, "");
50
+ const libraries = readLibraries(root).flatMap((f) => ("library" in f ? [f.library] : []));
51
+ const stale: string[] = [];
52
+ const problems: string[] = [];
53
+ for (const m of metapackagesOf(root)) {
54
+ const members = membersOf(m.bundle, libraries);
55
+ if (members.kind === "err") {
56
+ problems.push(members.error);
57
+ continue;
58
+ }
59
+ const path = `commons/${m.name}/${SET_FILE}`;
60
+ const file = join(root, path);
61
+ const want = setFileText(m.bundle, members.value);
62
+ if (existsSync(file) && readTextFileSync(file) === want) continue;
63
+ stale.push(path);
64
+ if (write) writeTextFileSync(file, want);
65
+ }
66
+ return { stale, problems };
67
+ }
68
+
69
+ /** What packing a metapackage gave: the staged folder, the tarball where nothing was wrong, and what was wrong. */
70
+ export type PackedSet = { readonly stage: string; readonly tarball: string | undefined; readonly findings: readonly string[] };
71
+
72
+ /** The manifest of the metapackage at `root`, or why it is none. */
73
+ function manifestAt(root: string): Result<Library, string> {
74
+ const file = join(root, LIBRARY_FILE);
75
+ if (!existsSync(file)) return err(`${file} is missing`);
76
+ const r = decodeLibrarySource(readTextFileSync(file));
77
+ if (r.kind === "err") return err(r.error);
78
+ return r.value.bundle === undefined ? err(`${file} names no bundle: it is not a metapackage`) : ok(r.value);
79
+ }
80
+
81
+ /** Stages, inspects and packs the metapackage in the folder `root` (`commons/<set>/`). */
82
+ export async function packMetapackage(root: string): Promise<PackedSet> {
83
+ const own = manifestAt(root);
84
+ const dist = join(root, "dist");
85
+ const stage = join(dist, "package");
86
+ if (own.kind === "err") return { stage, tarball: undefined, findings: [own.error] };
87
+ const l = own.value;
88
+ const bundle = l.bundle;
89
+ if (bundle === undefined) return { stage, tarball: undefined, findings: [`${l.name} names no bundle`] };
90
+ const setFile = join(root, SET_FILE);
91
+ if (!existsSync(setFile)) return { stage, tarball: undefined, findings: [`${SET_FILE} is missing: run bun commons/libraries/src/pack-metapackage.ts`] };
92
+ const deps = readSetFile(readTextFileSync(setFile));
93
+ if (deps.kind === "err") return { stage, tarball: undefined, findings: [deps.error] };
94
+ if (existsSync(dist)) removeSync(dist, { recursive: true });
95
+ mkdirSync(stage, { recursive: true });
96
+ const members = Object.entries(deps.value).map(([pkg, version]) => ({ library: pkg.slice(pkg.indexOf("/") + 1), package: pkg, version }));
97
+ const manifest = publishedManifest(bundle, l.version, deps.value, licenseOf(l));
98
+ writeTextFileSync(join(stage, "package.json"), `${JSON.stringify(manifest, null, 2)}\n`);
99
+ writeTextFileSync(join(stage, "index.js"), ENTRY_JS);
100
+ writeTextFileSync(join(stage, "index.d.ts"), ENTRY_DTS);
101
+ writeTextFileSync(join(stage, "README.md"), readmeOf(bundle, members));
102
+ const findings: string[] = [];
103
+ if (existsSync(join(root, "LICENSE"))) writeTextFileSync(join(stage, "LICENSE"), readTextFileSync(join(root, "LICENSE")));
104
+ else findings.push(`commons/${l.name} has no LICENSE; run: bun commons/libraries/src/license.ts`);
105
+ if (Object.keys(deps.value).length === 0) findings.push(`${l.name}: the set has no member`);
106
+ findings.push(...inspectPackage(stage));
107
+ if (findings.length > 0) return { stage, tarball: undefined, findings };
108
+ const packed = await run([which("bun") ?? "bun", "pm", "pack", "--destination", dist], { cwd: stage });
109
+ if (packed.code !== 0) return { stage, tarball: undefined, findings: [`bun pm pack stopped: ${packed.stderr || packed.stdout}`.trim()] };
110
+ return { stage, tarball: join(dist, tarballName(`@intellectual-frontiers/${bundle}`, l.version)), findings: [] };
111
+ }
112
+
113
+ /** The command line of a metapackage's `tools/pack.ts`: pack, print what was found (or the tarball) and the exit status. */
114
+ export async function main(root: string, argv: readonly string[]): Promise<number> {
115
+ if (argv.includes("--check") && (env.get("IFC_SKIP_PACK") ?? "") !== "") {
116
+ stdio.stdout.writeSync("pack check skipped: the set did not change in this push (IFC_SKIP_PACK); the release packs it\n");
117
+ return 0;
118
+ }
119
+ const r = await packMetapackage(root);
120
+ for (const f of r.findings) stdio.stderr.writeSync(`${f}\n`);
121
+ if (r.tarball === undefined) return 1;
122
+ stdio.stdout.writeSync(`${argv.includes("--check") ? `packed ${r.tarball}: nothing wrong` : r.tarball}\n`);
123
+ return 0;
124
+ }
125
+
126
+ /** The command line of the generator: 0, or 1 where a file is not what the manifests make (with `--check`) or a manifest is wrong. */
127
+ export function generate(argv: readonly string[]): number {
128
+ const at = argv.indexOf("--repo");
129
+ const repo = at >= 0 ? resolve(argv[at + 1] ?? ".") : resolve(".");
130
+ const check = argv.includes("--check");
131
+ const r = synchronize(repo, !check);
132
+ for (const p of r.problems) stdio.stderr.writeSync(`${p}\n`);
133
+ for (const s of r.stale) stdio.stderr.writeSync(check ? `${s} is not what the manifests make; run: bun commons/libraries/src/pack-metapackage.ts\n` : `wrote ${s}\n`);
134
+ return r.problems.length > 0 || (check && r.stale.length > 0) ? 1 : 0;
135
+ }
136
+
137
+ if (import.meta.main) process.exitCode = generate(process.argv.slice(2));
package/src/pins.ts CHANGED
@@ -1,20 +1,21 @@
1
1
  /**
2
- * Commons' pins as the tools read them (commons 0005-typescript-runtime FR-026): the `KEY=value` lines of `pins.auto.env`, and which of
3
- * Bun's release assets and pin keys belong to a platform.
2
+ * Commons' pins as the tools read them (commons 0005-typescript-practice FR-026, 0002-packaging FR-019): the `KEY=value` lines of `pins.auto.env`, the suffix of a platform's
3
+ * variables, and the download of a pin on a platform (its version, address, SHA-256, format and folder), all taken from the file and written nowhere else.
4
4
  *
5
5
  * Spec: 0001-libraries FR-006
6
6
  *
7
7
  * @module
8
8
  */
9
+ import { err, ok, type Result } from "@intellectual-frontiers/core";
9
10
 
10
- /** The target name of Bun's release asset and the suffix of commons' pin key for a platform (by the names `proc.build` gives); undefined where Bun has no build. */
11
- export function bunTarget(os: string, arch: string): { readonly target: string; readonly key: string } | undefined {
12
- const table: Readonly<Record<string, { readonly target: string; readonly key: string }>> = {
13
- "linux/x86_64": { target: "linux-x64", key: "X86_64" },
14
- "linux/aarch64": { target: "linux-aarch64", key: "AARCH64" },
15
- "darwin/x86_64": { target: "darwin-x64", key: "MACOS_X64" },
16
- "darwin/aarch64": { target: "darwin-aarch64", key: "MACOS_ARM64" },
17
- "windows/x86_64": { target: "windows-x64", key: "WINDOWS_X64" },
11
+ /** The suffix of commons' pin variables for a platform (by the names `proc.build` gives); undefined where commons pins no download for it. */
12
+ export function pinSuffix(os: string, arch: string): string | undefined {
13
+ const table: Readonly<Record<string, string>> = {
14
+ "linux/x86_64": "X86_64",
15
+ "linux/aarch64": "AARCH64",
16
+ "darwin/x86_64": "MACOS_X64",
17
+ "darwin/aarch64": "MACOS_ARM64",
18
+ "windows/x86_64": "WINDOWS_X64",
18
19
  };
19
20
  return Object.hasOwn(table, `${os}/${arch}`) ? table[`${os}/${arch}`] : undefined;
20
21
  }
@@ -28,3 +29,47 @@ export function readEnvFile(text: string): ReadonlyMap<string, string> {
28
29
  }
29
30
  return out;
30
31
  }
32
+
33
+ /** The formats of what a pinned address gives. */
34
+ export type PinFormat = "zip" | "tar" | "file";
35
+
36
+ /** What a download of a pin on a platform needs. */
37
+ export type PinDownload = {
38
+ readonly version: string;
39
+ readonly url: string;
40
+ readonly sha256: string;
41
+ readonly format: PinFormat;
42
+ /** The folder of the programs inside the unpacked artifact; empty for the root. */
43
+ readonly binPath: string;
44
+ };
45
+
46
+ /** Why a pin has no download on a platform. */
47
+ export type PinProblem =
48
+ | { readonly kind: "no-version"; readonly key: string }
49
+ | { readonly kind: "not-pinned"; readonly key: string; readonly suffix: string }
50
+ | { readonly kind: "bad-format"; readonly key: string; readonly suffix: string; readonly format: string };
51
+
52
+ /** The sentence a problem is told in. */
53
+ export function describePinProblem(p: PinProblem): string {
54
+ switch (p.kind) {
55
+ case "no-version":
56
+ return `commons pins no ${p.key}`;
57
+ case "not-pinned":
58
+ return `commons pins no download of ${p.key} for ${p.suffix}`;
59
+ case "bad-format":
60
+ return `commons pins ${p.key} for ${p.suffix} as ${JSON.stringify(p.format)}, which is no format (zip, tar, file)`;
61
+ }
62
+ }
63
+
64
+ /** The download of the pin `key` (`BUN`) for the platform `suffix` (`X86_64`) as `pins.auto.env` states it, or why it has none. */
65
+ export function pinDownload(env: ReadonlyMap<string, string>, key: string, suffix: string): Result<PinDownload, PinProblem> {
66
+ const version = env.get(`${key}_VERSION`);
67
+ if (version === undefined) return err({ kind: "no-version", key });
68
+ const url = env.get(`${key}_URL_${suffix}`);
69
+ const sha256 = env.get(`${key}_SHA256_${suffix}`);
70
+ const format = env.get(`${key}_FORMAT_${suffix}`);
71
+ const binPath = env.get(`${key}_BINPATH_${suffix}`);
72
+ if (url === undefined || sha256 === undefined || format === undefined || binPath === undefined) return err({ kind: "not-pinned", key, suffix });
73
+ if (format !== "zip" && format !== "tar" && format !== "file") return err({ kind: "bad-format", key, suffix, format });
74
+ return ok({ version, url, sha256, format, binPath });
75
+ }