@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
@@ -0,0 +1,190 @@
1
+ /**
2
+ * The metapackages of commons (commons 0036-distribution FR-005 to FR-008): five packages with no code of their own, each a set of libraries at the exact versions of one release. `@intellectual-frontiers/commons` is every
3
+ * consumer-facing library; `foundation`, `patterns`, `programs` and `standards` are the four roles of the libraries. A repository other than `.github` installs commons by installing one of them, or a library, from a registry.
4
+ *
5
+ * @remarks
6
+ * Pure: the libraries as data give the members of a set, the exact dependencies of its package, the file that records them, the `package.json` and the README that are published. The role of a library is read from `ROLE_OF` of
7
+ * `readme.ts`, the one place that says it, and the set of a role is the role's: `foundation`, `pattern` (`patterns`), `program` (`programs`) and `standard` (`standards`). A library is consumer-facing where its distribution
8
+ * names npm and it is not a metapackage and not held back ({@link HELD_BACK}); the package of a member is the package it publishes ({@link publishedPackage}). A member whose role is not known is a problem and never a member
9
+ * of no set. The effects (writing the files, packing) are `pack-metapackage.ts`.
10
+ *
11
+ * @limits
12
+ * It reads no file and knows no registry. It does not know what is published: a release that has not published a member makes the set refer to a version npm does not hold yet, which the release's order prevents.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { dependenciesOf } from "./metapackages.ts";
17
+ * dependenciesOf([{ library: "core", package: "@intellectual-frontiers/core", version: "0.1.5" }]); // { "@intellectual-frontiers/core": "0.1.5" }
18
+ * ```
19
+ *
20
+ * Spec: 0001-libraries FR-023
21
+ *
22
+ * @module
23
+ */
24
+ import { absurd, err, ok } from "@intellectual-frontiers/core";
25
+ import { BUNDLES, publishedPackage } from "./libraries.js";
26
+ import { DEFAULT_LICENSE } from "./license/standard.js";
27
+ import { ROLE_OF } from "./readme.js";
28
+ /** The libraries that are not in any set yet, each with why: they declare npm and are not ready to be installed (commons 0036-distribution FR-006). A library leaves this list when its owner releases it. */
29
+ export const HELD_BACK = new Map([
30
+ ["archon", "the Archon library is not released yet"],
31
+ ["archon-dialect-apt", "a dialect of Archon, which is not released yet"],
32
+ ["archon-dialect-homebrew", "a dialect of Archon, which is not released yet"],
33
+ ["archon-dialect-mise", "a dialect of Archon, which is not released yet"],
34
+ ["archon-dialect-native-pinned", "a dialect of Archon, which is not released yet"],
35
+ ["archon-dialect-winget", "a dialect of Archon, which is not released yet"],
36
+ ["package-managers", "the kit of the dialects of Archon and Toolchain, which are not released yet"],
37
+ ["toolchain", "the Toolchain library is not released yet"],
38
+ ]);
39
+ /** The roles' sets: a role is in exactly one. */
40
+ export const BUNDLE_OF_ROLE = {
41
+ foundation: "foundation",
42
+ pattern: "patterns",
43
+ program: "programs",
44
+ standard: "standards",
45
+ };
46
+ /** Whether a library is installed by a set: it names npm, it is no metapackage and it is not held back. */
47
+ export function isConsumerFacing(l) {
48
+ return l.held === undefined && l.bundle === undefined && l.distribution.includes("npm") && !HELD_BACK.has(l.name);
49
+ }
50
+ /** Whether a set holds a library of this role. */
51
+ function holds(bundle, role) {
52
+ switch (bundle) {
53
+ case "commons":
54
+ return true;
55
+ case "foundation":
56
+ case "patterns":
57
+ case "programs":
58
+ case "standards":
59
+ return BUNDLE_OF_ROLE[role] === bundle;
60
+ default:
61
+ return absurd(bundle);
62
+ }
63
+ }
64
+ /** The members of a set, in the order of their packages' names; the problem where a consumer-facing library has no role in `ROLE_OF`. */
65
+ export function membersOf(bundle, libraries) {
66
+ const out = [];
67
+ for (const l of libraries) {
68
+ if (!isConsumerFacing(l))
69
+ continue;
70
+ const role = ROLE_OF.get(l.name);
71
+ if (role === undefined)
72
+ return err(`commons/${l.name} has no role in ROLE_OF (commons/libraries/src/readme.ts), so no set can hold it`);
73
+ if (holds(bundle, role))
74
+ out.push({ library: l.name, package: publishedPackage(l), version: l.version });
75
+ }
76
+ return ok(out.sort((a, b) => (a.package < b.package ? -1 : a.package > b.package ? 1 : 0)));
77
+ }
78
+ /** The exact dependencies of the package of a set: each member's package at its version, no range. */
79
+ export function dependenciesOf(members) {
80
+ return Object.fromEntries(members.map((m) => [m.package, m.version]));
81
+ }
82
+ /** What the generated file of a set holds: the set and its exact dependencies. */
83
+ export function setFileText(bundle, members) {
84
+ return `${JSON.stringify({ generated: "bun commons/libraries/src/pack-metapackage.ts; do not edit (commons 0036-distribution FR-007)", set: bundle, dependencies: dependenciesOf(members) }, null, 2)}\n`;
85
+ }
86
+ /** The generated file of a set, from its folder. */
87
+ export const SET_FILE = "dependencies.auto.json";
88
+ /** The dependencies a generated file holds, or why its text is not one. */
89
+ export function readSetFile(text) {
90
+ let json;
91
+ try {
92
+ json = JSON.parse(text);
93
+ }
94
+ catch {
95
+ return err(`${SET_FILE} is not JSON`);
96
+ }
97
+ const deps = typeof json === "object" && json !== null && "dependencies" in json ? json.dependencies : undefined;
98
+ if (typeof deps !== "object" || deps === null || Array.isArray(deps))
99
+ return err(`${SET_FILE} has no dependencies`);
100
+ const out = {};
101
+ for (const [k, v] of Object.entries(deps)) {
102
+ if (typeof v !== "string")
103
+ return err(`${SET_FILE}: ${k} has no version`);
104
+ out[k] = v;
105
+ }
106
+ return ok(out);
107
+ }
108
+ /** What a set is for, in words, for its README. */
109
+ const PURPOSE = {
110
+ commons: "Every consumer-facing library of Intellectual Frontiers Commons, at the versions of one release.",
111
+ foundation: "The foundation libraries: errors as values and decoders, formats, cryptography, the system, the terminal, telemetry, the network, the test runtime, RDF, the Git host and the lint rules.",
112
+ patterns: "The pattern libraries: the headless program pattern and the layers of Kanon.",
113
+ programs: "The program libraries: the operations program of a repository, workspaces, data, integrations and the rest of the programs.",
114
+ standards: "The standards libraries: Mizan, the ontologies, the quality standards and the standards of integrations.",
115
+ };
116
+ /** The text of a set's own `description`, which the package carries. */
117
+ export function descriptionOf(bundle) {
118
+ return `${PURPOSE[bundle]} No code of its own: it depends on each library at one exact version.`;
119
+ }
120
+ /** The scope of the packages. */
121
+ const SCOPE = "@intellectual-frontiers";
122
+ /** The README of a set's package: what it holds, how to install it from npm and from the GitHub package registry, and how to keep one copy of each library. */
123
+ export function readmeOf(bundle, members) {
124
+ const name = `${SCOPE}/${bundle}`;
125
+ const rows = members.map((m) => `| \`${m.package}\` | \`${m.version}\` |`);
126
+ return [
127
+ `# ${name}`,
128
+ "",
129
+ descriptionOf(bundle),
130
+ "",
131
+ "```sh",
132
+ `bun add ${name}@<version> # or: npm install --save-exact ${name}@<version>`,
133
+ "```",
134
+ "",
135
+ `The package has no code of its own. Its dependencies are the libraries below, each at one exact version, so that one version of \`${name}\` is one coherent set. Pin it exactly, and import the libraries by their own names.`,
136
+ "",
137
+ "| Library | Version |",
138
+ "| --- | --- |",
139
+ ...rows,
140
+ "",
141
+ "## One copy of each library",
142
+ "",
143
+ "Where another package of your tree depends on one of these libraries at a range, make the tree hold one copy: add",
144
+ "",
145
+ "```json",
146
+ `{ "overrides": { "${SCOPE}/core": "<the version above>" } }`,
147
+ "```",
148
+ "",
149
+ "to your `package.json` for each library you find twice (`bun pm ls --all`, `npm ls @intellectual-frontiers/core`). `repoctl hygiene` names the library that is installed twice.",
150
+ "",
151
+ "## From the GitHub package registry",
152
+ "",
153
+ "The same package is on the GitHub package registry of the organization. Put the scoped line in `.npmrc` and a token that may read packages in the environment:",
154
+ "",
155
+ "```sh",
156
+ "@intellectual-frontiers:registry=https://npm.pkg.github.com",
157
+ "//npm.pkg.github.com/:_authToken=${ERGONEX_GITHUB_TOKEN}",
158
+ "```",
159
+ "",
160
+ `License: ${DEFAULT_LICENSE}. Sources and specifications: https://github.com/intellectual-frontiers/.github/tree/main/commons/${bundle}`,
161
+ "",
162
+ ].join("\n");
163
+ }
164
+ /** The `package.json` that is published for a set: no code, one empty entry, the exact dependencies. */
165
+ export function publishedManifest(bundle, version, dependencies, license) {
166
+ return {
167
+ name: `${SCOPE}/${bundle}`,
168
+ version,
169
+ description: descriptionOf(bundle),
170
+ license,
171
+ type: "module",
172
+ engines: { node: ">=26" },
173
+ sideEffects: false,
174
+ types: "./index.d.ts",
175
+ exports: { ".": { types: "./index.d.ts", default: "./index.js" } },
176
+ dependencies,
177
+ files: ["index.js", "index.d.ts", "README.md", "LICENSE"],
178
+ repository: { type: "git", url: "git+https://github.com/intellectual-frontiers/.github.git", directory: `commons/${bundle}` },
179
+ homepage: `https://github.com/intellectual-frontiers/.github/tree/main/commons/${bundle}`,
180
+ bugs: { url: "https://github.com/intellectual-frontiers/.github/issues" },
181
+ keywords: ["intellectual-frontiers", "commons", bundle],
182
+ publishConfig: { access: "public" },
183
+ };
184
+ }
185
+ /** The one file of code of a set's package, and its declaration: an empty module, so that the entry imports. */
186
+ export const ENTRY_JS = "export {};\n";
187
+ /** The declaration of {@link ENTRY_JS}. */
188
+ export const ENTRY_DTS = "export {};\n";
189
+ /** Every set, in the order the README lists them. */
190
+ export const ALL_BUNDLES = BUNDLES;
package/dist/mod.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * What of Mizan is still in this package (commons 0032-lifting-libraries): the commands that need the whole repository. `check.ts` holds a repository's spec kit and ontologies, `versions.ts` and
2
+ * What of Mizan is still in this package (commons 0032-libraries-by-concern): the commands that need the whole repository. `check.ts` holds a repository's spec kit and ontologies, `versions.ts` and
3
3
  * `changes.ts` are the commands of the release check and the change-control check, `libraries.ts` reads the manifests of the libraries, `newer.ts` reports a standard newer than its pin, `generate-ontology.ts` writes the Turtle of the ontologies and
4
4
  * `oscal-register.ts` the register of proofs as OSCAL. The harness, the rules, the levels, the integrations, the change-control and release rules as plugins, and the trace of requirements are the
5
5
  * library `mizan` (`@intellectual-frontiers/mizan`), and the ontologies are the library `ontology` (`@intellectual-frontiers/ontology`).
package/dist/mod.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * What of Mizan is still in this package (commons 0032-lifting-libraries): the commands that need the whole repository. `check.ts` holds a repository's spec kit and ontologies, `versions.ts` and
2
+ * What of Mizan is still in this package (commons 0032-libraries-by-concern): the commands that need the whole repository. `check.ts` holds a repository's spec kit and ontologies, `versions.ts` and
3
3
  * `changes.ts` are the commands of the release check and the change-control check, `libraries.ts` reads the manifests of the libraries, `newer.ts` reports a standard newer than its pin, `generate-ontology.ts` writes the Turtle of the ontologies and
4
4
  * `oscal-register.ts` the register of proofs as OSCAL. The harness, the rules, the levels, the integrations, the change-control and release rules as plugins, and the trace of requirements are the
5
5
  * library `mizan` (`@intellectual-frontiers/mizan`), and the ontologies are the library `ontology` (`@intellectual-frontiers/ontology`).
@@ -20,15 +20,24 @@ export declare function inspectPackage(stage: string): Findings;
20
20
  export declare function rewriteDeclarationSpecifiers(source: string): string;
21
21
  /** The tarball's name for a package: `@scope/name` at a version is `scope-name-version.tgz`. */
22
22
  export declare const tarballName: (name: string, version: string) => string;
23
+ /** What packing a library made: the staging folder, the tarball, the findings and the manifest. */
23
24
  export type Packed = {
24
25
  readonly stage: string;
25
26
  readonly tarball: string | undefined;
26
27
  readonly findings: Findings;
27
28
  readonly manifest: Json;
28
29
  };
30
+ /** What packing a library is told: its folder, where to stage it and whether to make the tarball. */
29
31
  export type PackOptions = {
30
32
  /** Types the compiler is given (`["node"]` for a library that names Node's). */
31
33
  readonly types?: readonly string[];
34
+ /**
35
+ * 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
36
+ * 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.
37
+ */
38
+ readonly launchers?: Readonly<Record<string, string>>;
39
+ /** 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. */
40
+ readonly optionalPeers?: Readonly<Record<string, string>>;
32
41
  /** 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. */
33
42
  readonly bundle?: {
34
43
  /** The commands: their name, and the source that is the command's entry from the library's folder. */
@@ -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,11 +24,12 @@
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, LIBRARY_FILE } from "./libraries.js";
28
+ import { COMMAND as LICENSE_COMMAND, licenseOf } from "./license.js";
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
  /** The scope of the packages of commons. */
30
32
  export const SCOPE = "@intellectual-frontiers";
31
- const obj = (v) => (isObject(v) ? v : {});
32
33
  const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
33
34
  const text = (v) => (typeof v === "string" ? v : "");
34
35
  /** A text file copied: every file of a package is text. */
@@ -146,7 +147,7 @@ export function inspectPackage(stage) {
146
147
  if (pkg === undefined)
147
148
  continue;
148
149
  if (pkg === `${SCOPE}/commons`)
149
- found.push(`${d} imports ${s}: the package ${pkg} is not on npm`);
150
+ found.push(`${d} imports ${s}: the package ${pkg} is a metapackage with no code: import the library that holds what is used`);
150
151
  else if (pkg !== name && !declared.has(pkg))
151
152
  found.push(`${d} imports ${pkg}, which is not a dependency of ${name}`);
152
153
  }
@@ -168,7 +169,7 @@ export function inspectPackage(stage) {
168
169
  if (pkg === undefined)
169
170
  continue;
170
171
  if (pkg === `${SCOPE}/commons`)
171
- found.push(`${f} imports ${s}: the package ${pkg} is not on npm`);
172
+ found.push(`${f} imports ${s}: the package ${pkg} is a metapackage with no code: import the library that holds what is used`);
172
173
  else if (pkg !== name && !declared.has(pkg))
173
174
  found.push(`${f} imports ${pkg}, which is not a dependency of ${name}`);
174
175
  }
@@ -189,15 +190,16 @@ function libraryOf(commons, name) {
189
190
  if (!existsSync(pkg))
190
191
  return undefined;
191
192
  const lib = join(commons, name, "library.ts");
192
- const npm = existsSync(lib) && /distribution:\s*\[[^\]]*"npm"/.test(readTextFileSync(lib));
193
- return { version: text(JSON.parse(readTextFileSync(pkg))["version"]), npm };
193
+ const source = existsSync(lib) ? readTextFileSync(lib) : "";
194
+ const npm = /distribution:\s*\[[^\]]*"npm"/.test(source);
195
+ const held = /\bheld:\s*"/.test(source);
196
+ return { version: text(JSON.parse(readTextFileSync(pkg))["version"]), npm, held };
194
197
  }
195
198
  /**
196
- * 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
197
- * 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
198
- * 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.
199
+ * 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/`
200
+ * 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.
199
201
  */
200
- function vendorDeclarations(outRoot, lib, entries, commonsPkg, stageDist) {
202
+ function vendorDeclarations(outRoot, lib, entries, stageDist) {
201
203
  const found = [];
202
204
  const placed = new Map(); // a file of the compiler's output, and where it goes under the stage's dist/
203
205
  const place = (file) => {
@@ -219,27 +221,10 @@ function vendorDeclarations(outRoot, lib, entries, commonsPkg, stageDist) {
219
221
  for (const s of specifiersOf(readTextFileSync(file))) {
220
222
  if (s.startsWith("."))
221
223
  todo.push(join(dirname(file), s.replace(/\.(?:js|ts|mjs)$/, "") + ".d.ts"));
222
- else if (s.startsWith(`${SCOPE}/commons/`)) {
223
- const target = commonsPkg.exports.get(`./${s.slice(`${SCOPE}/commons/`.length)}`);
224
- if (target === undefined)
225
- found.push(`${dest} imports ${s}, which the exports of commons do not name`);
226
- else
227
- todo.push(join(outRoot, commonsPkg.folder, target.replace(/^\.\//, "").replace(/\.ts$/, "") + ".d.ts"));
228
- }
229
224
  }
230
225
  }
231
226
  for (const [file, dest] of placed) {
232
227
  let source = rewriteDeclarationSpecifiers(readTextFileSync(file));
233
- source = source.replace(new RegExp(`((?:from\\s+|import\\(\\s*|import\\s+)["'])${SCOPE}/commons/([^"']+)(["'])`, "g"), (whole, pre, sub, post) => {
234
- const target = commonsPkg.exports.get(`./${sub}`);
235
- if (target === undefined)
236
- return whole;
237
- const there = place(join(outRoot, commonsPkg.folder, target.replace(/^\.\//, "").replace(/\.ts$/, "") + ".d.ts"));
238
- let rel = toPosix(relative(dirname(dest), there)).replace(/\.d\.ts$/, ".js");
239
- if (!rel.startsWith("."))
240
- rel = `./${rel}`;
241
- return `${pre}${rel}${post}`;
242
- });
243
228
  // 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
244
229
  source = source.replace(/((?:from\s+|import\(\s*|import\s+)["'])(\.[^"']+)(["'])/g, (whole, pre, spec, post) => {
245
230
  const target = join(dirname(file), spec.replace(/\.(?:js|ts|mjs)$/, "") + ".d.ts");
@@ -257,20 +242,8 @@ function vendorDeclarations(outRoot, lib, entries, commonsPkg, stageDist) {
257
242
  }
258
243
  return found;
259
244
  }
260
- /** 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. */
261
- function commonsPaths(commons, dist) {
262
- const file = join(commons, "ts", "package.json");
263
- const out = {};
264
- if (!existsSync(file))
265
- return out;
266
- for (const [k, v] of Object.entries(obj(obj(JSON.parse(readTextFileSync(file)))["exports"]))) {
267
- if (typeof v === "string" && v.endsWith(".ts"))
268
- out[`${SCOPE}/commons/${k.replace(/^\.\//, "")}`] = [toPosix(relative(dist, join(commons, "ts", v)))];
269
- }
270
- return out;
271
- }
272
245
  /** What a package adds to the common form. */
273
- /** 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). */
246
+ /** 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). */
274
247
  function namesNodeTypes(folder) {
275
248
  if (!existsSync(folder))
276
249
  return false;
@@ -285,6 +258,14 @@ function namesNodeTypes(folder) {
285
258
  }
286
259
  return false;
287
260
  }
261
+ /** The manifest of the library at `root`, where it has one that can be read. */
262
+ function manifestOf(root) {
263
+ const file = join(root, LIBRARY_FILE);
264
+ if (!existsSync(file))
265
+ return undefined;
266
+ const r = decodeLibrarySource(readTextFileSync(file));
267
+ return r.kind === "ok" ? r.value : undefined;
268
+ }
288
269
  /** 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. */
289
270
  export async function packLibrary(root, options = {}) {
290
271
  const own = JSON.parse(readTextFileSync(join(root, "package.json")));
@@ -298,7 +279,7 @@ export async function packLibrary(root, options = {}) {
298
279
  removeSync(dist, { recursive: true });
299
280
  mkdirSync(stage, { recursive: true });
300
281
  // the compiler's configuration for this build: the base of commons, emitting modules and declarations of src/ and nothing else
301
- const base = relative(dist, join(commons, "ts", "tsconfig.json"));
282
+ const base = relative(dist, join(commons, "tsconfig.base.json"));
302
283
  writeTextFileSync(join(dist, "tsconfig.build.json"), JSON.stringify({
303
284
  extends: toPosix(base),
304
285
  compilerOptions: {
@@ -311,8 +292,6 @@ export async function packLibrary(root, options = {}) {
311
292
  skipLibCheck: true,
312
293
  // 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
313
294
  preserveSymlinks: true,
314
- // 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
315
- ...(bundle === undefined ? {} : { paths: commonsPaths(commons, dist) }),
316
295
  },
317
296
  include: [toPosix(relative(dist, join(root, "src"))) + "/**/*.ts"],
318
297
  }, null, 2));
@@ -335,25 +314,21 @@ export async function packLibrary(root, options = {}) {
335
314
  }
336
315
  }
337
316
  else {
338
- const commonsPkgFile = join(commons, "ts", "package.json");
339
- const commonsExports = new Map();
340
- if (existsSync(commonsPkgFile)) {
341
- const declared = obj(obj(JSON.parse(readTextFileSync(commonsPkgFile)))["exports"]);
342
- for (const [k, v] of Object.entries(declared))
343
- if (typeof v === "string")
344
- commonsExports.set(k, v);
345
- }
346
317
  const entryFiles = Object.values(ownExports).map((e) => join(out, (targetsOf(e)["default"] ?? text(e)).replace(/^\.\/src\//, "").replace(/\.ts$/, "") + ".d.ts"));
347
- findings.push(...vendorDeclarations(join(dist, "out"), basename(resolve(root)), entryFiles, { folder: "ts", exports: commonsExports }, join(stage, "dist")));
318
+ findings.push(...vendorDeclarations(join(dist, "out"), basename(resolve(root)), entryFiles, join(stage, "dist")));
348
319
  }
349
320
  // the sources, for the `bun` condition (a bundle has no source to offer: what it imports is not on npm)
350
321
  if (bundle === undefined) {
351
322
  for (const f of filesUnder(join(root, "src"))) {
352
- if (!f.endsWith(".ts"))
353
- continue;
354
323
  const target = join(stage, "src", f);
355
324
  mkdirSync(dirname(target), { recursive: true });
356
325
  copyFileSync(join(root, "src", f), target);
326
+ if (!f.endsWith(".ts")) {
327
+ // a file that is not TypeScript is read beside the module that reads it, from the sources and from the compiled modules alike
328
+ const compiled = join(stage, "dist", f);
329
+ mkdirSync(dirname(compiled), { recursive: true });
330
+ copyFileSync(join(root, "src", f), compiled);
331
+ }
357
332
  }
358
333
  }
359
334
  // the entries: each names its declarations, its source (Bun) and its compiled file
@@ -375,6 +350,13 @@ export async function packLibrary(root, options = {}) {
375
350
  }
376
351
  // the commands of a bundle: one file each, with the line that runs it under Node
377
352
  const binOut = {};
353
+ for (const [cmd, source] of Object.entries(options.launchers ?? {})) {
354
+ const rel = toPosix(source).replace(/^\.?\/?(src\/)?/, "").replace(/\.ts$/, "");
355
+ const file = join(stage, `${cmd}.mjs`);
356
+ await writeTextFile(file, `#!/usr/bin/env node\nimport { main } from "./dist/${rel}.js";\n\nprocess.exit(await main(process.argv.slice(2)));\n`);
357
+ await chmod(file, 0o755);
358
+ binOut[cmd] = `./${cmd}.mjs`;
359
+ }
378
360
  for (const [cmd, source] of Object.entries(bundle?.bin ?? {})) {
379
361
  const file = join(stage, `${cmd}.mjs`);
380
362
  const bundled = await run([bun, "build", join(root, source), "--target=node", "--format=esm", "--outfile", file], { cwd: root });
@@ -417,7 +399,7 @@ export async function packLibrary(root, options = {}) {
417
399
  const lib = libraryOf(commons, pkg.slice(SCOPE.length + 1));
418
400
  if (lib === undefined)
419
401
  findings.push(`${file} imports ${pkg}, which is no library of commons`);
420
- else if (!lib.npm)
402
+ else if (!lib.npm && !(libraryOf(commons, short)?.held === true && lib.held))
421
403
  findings.push(`${file} imports ${pkg}, which is not published to npm`);
422
404
  else
423
405
  dependencies[pkg] = lib.version;
@@ -438,7 +420,7 @@ export async function packLibrary(root, options = {}) {
438
420
  name,
439
421
  version: text(own["version"]),
440
422
  description: text(own["description"]),
441
- license: "MIT",
423
+ license: licenseOf(manifestOf(root)),
442
424
  type: "module",
443
425
  engines: { node: ">=26" },
444
426
  sideEffects: false,
@@ -448,7 +430,10 @@ export async function packLibrary(root, options = {}) {
448
430
  ...(Object.keys(dependencies).length === 0
449
431
  ? {}
450
432
  : { dependencies: Object.fromEntries(Object.entries(dependencies).sort(([a], [b]) => (a < b ? -1 : 1))) }),
451
- ...(namesNodeTypes(join(stage, "dist")) ? { peerDependencies: { "@types/node": ">=26" } } : {}),
433
+ ...(namesNodeTypes(join(stage, "dist")) || options.optionalPeers !== undefined
434
+ ? { peerDependencies: { ...(namesNodeTypes(join(stage, "dist")) ? { "@types/node": ">=26" } : {}), ...(options.optionalPeers ?? {}) } }
435
+ : {}),
436
+ ...(options.optionalPeers === undefined ? {} : { peerDependenciesMeta: Object.fromEntries(Object.keys(options.optionalPeers).map((n) => [n, { optional: true }])) }),
452
437
  files: [
453
438
  ...(Object.keys(exportsOut).length === 0 ? [] : ["dist"]),
454
439
  ...(bundle === undefined ? ["src"] : []),
@@ -477,7 +462,10 @@ export async function packLibrary(root, options = {}) {
477
462
  `Sources and specifications: https://github.com/intellectual-frontiers/.github/tree/main/commons/${short}`,
478
463
  "",
479
464
  ].join("\n"));
480
- copyFileSync(existsSync(join(root, "LICENSE")) ? join(root, "LICENSE") : join(commons, "ergonex", "LICENSE"), join(stage, "LICENSE"));
465
+ if (existsSync(join(root, "LICENSE")))
466
+ copyFileSync(join(root, "LICENSE"), join(stage, "LICENSE"));
467
+ else
468
+ findings.push(`commons/${short} has no LICENSE; run: ${LICENSE_COMMAND}`);
481
469
  findings.push(...inspectPackage(stage));
482
470
  if (findings.length > 0)
483
471
  return { stage, tarball: undefined, findings, manifest };
@@ -0,0 +1,22 @@
1
+ import { type Library } from "./libraries.js";
2
+ /** The metapackages of a repository: the libraries whose manifest names a set. */
3
+ export declare function metapackagesOf(repo: string): readonly (Library & {
4
+ readonly bundle: NonNullable<Library["bundle"]>;
5
+ })[];
6
+ /** 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. */
7
+ export declare function synchronize(repo: string, write: boolean): {
8
+ readonly stale: readonly string[];
9
+ readonly problems: readonly string[];
10
+ };
11
+ /** What packing a metapackage gave: the staged folder, the tarball where nothing was wrong, and what was wrong. */
12
+ export type PackedSet = {
13
+ readonly stage: string;
14
+ readonly tarball: string | undefined;
15
+ readonly findings: readonly string[];
16
+ };
17
+ /** Stages, inspects and packs the metapackage in the folder `root` (`commons/<set>/`). */
18
+ export declare function packMetapackage(root: string): Promise<PackedSet>;
19
+ /** The command line of a metapackage's `tools/pack.ts`: pack, print what was found (or the tarball) and the exit status. */
20
+ export declare function main(root: string, argv: readonly string[]): Promise<number>;
21
+ /** 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. */
22
+ export declare function generate(argv: readonly string[]): number;
@@ -0,0 +1,147 @@
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 } 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, LIBRARY_FILE, readLibraries } from "./libraries.js";
34
+ import { licenseOf } from "./license.js";
35
+ import { ENTRY_DTS, ENTRY_JS, membersOf, publishedManifest, readmeOf, readSetFile, SET_FILE, setFileText } from "./metapackages.js";
36
+ import { inspectPackage, tarballName } from "./pack-library.js";
37
+ /** The metapackages of a repository: the libraries whose manifest names a set. */
38
+ export function metapackagesOf(repo) {
39
+ return readLibraries(repo).flatMap((f) => {
40
+ if (!("library" in f))
41
+ return [];
42
+ const l = f.library;
43
+ return l.bundle === undefined ? [] : [{ ...l, bundle: l.bundle }];
44
+ });
45
+ }
46
+ /** 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. */
47
+ export function synchronize(repo, write) {
48
+ const root = repo.replace(/[\\/]$/, "");
49
+ const libraries = readLibraries(root).flatMap((f) => ("library" in f ? [f.library] : []));
50
+ const stale = [];
51
+ const problems = [];
52
+ for (const m of metapackagesOf(root)) {
53
+ const members = membersOf(m.bundle, libraries);
54
+ if (members.kind === "err") {
55
+ problems.push(members.error);
56
+ continue;
57
+ }
58
+ const path = `commons/${m.name}/${SET_FILE}`;
59
+ const file = join(root, path);
60
+ const want = setFileText(m.bundle, members.value);
61
+ if (existsSync(file) && readTextFileSync(file) === want)
62
+ continue;
63
+ stale.push(path);
64
+ if (write)
65
+ writeTextFileSync(file, want);
66
+ }
67
+ return { stale, problems };
68
+ }
69
+ /** The manifest of the metapackage at `root`, or why it is none. */
70
+ function manifestAt(root) {
71
+ const file = join(root, LIBRARY_FILE);
72
+ if (!existsSync(file))
73
+ return err(`${file} is missing`);
74
+ const r = decodeLibrarySource(readTextFileSync(file));
75
+ if (r.kind === "err")
76
+ return err(r.error);
77
+ return r.value.bundle === undefined ? err(`${file} names no bundle: it is not a metapackage`) : ok(r.value);
78
+ }
79
+ /** Stages, inspects and packs the metapackage in the folder `root` (`commons/<set>/`). */
80
+ export async function packMetapackage(root) {
81
+ const own = manifestAt(root);
82
+ const dist = join(root, "dist");
83
+ const stage = join(dist, "package");
84
+ if (own.kind === "err")
85
+ return { stage, tarball: undefined, findings: [own.error] };
86
+ const l = own.value;
87
+ const bundle = l.bundle;
88
+ if (bundle === undefined)
89
+ return { stage, tarball: undefined, findings: [`${l.name} names no bundle`] };
90
+ const setFile = join(root, SET_FILE);
91
+ if (!existsSync(setFile))
92
+ return { stage, tarball: undefined, findings: [`${SET_FILE} is missing: run bun commons/libraries/src/pack-metapackage.ts`] };
93
+ const deps = readSetFile(readTextFileSync(setFile));
94
+ if (deps.kind === "err")
95
+ return { stage, tarball: undefined, findings: [deps.error] };
96
+ if (existsSync(dist))
97
+ removeSync(dist, { recursive: true });
98
+ mkdirSync(stage, { recursive: true });
99
+ const members = Object.entries(deps.value).map(([pkg, version]) => ({ library: pkg.slice(pkg.indexOf("/") + 1), package: pkg, version }));
100
+ const manifest = publishedManifest(bundle, l.version, deps.value, licenseOf(l));
101
+ writeTextFileSync(join(stage, "package.json"), `${JSON.stringify(manifest, null, 2)}\n`);
102
+ writeTextFileSync(join(stage, "index.js"), ENTRY_JS);
103
+ writeTextFileSync(join(stage, "index.d.ts"), ENTRY_DTS);
104
+ writeTextFileSync(join(stage, "README.md"), readmeOf(bundle, members));
105
+ const findings = [];
106
+ if (existsSync(join(root, "LICENSE")))
107
+ writeTextFileSync(join(stage, "LICENSE"), readTextFileSync(join(root, "LICENSE")));
108
+ else
109
+ findings.push(`commons/${l.name} has no LICENSE; run: bun commons/libraries/src/license.ts`);
110
+ if (Object.keys(deps.value).length === 0)
111
+ findings.push(`${l.name}: the set has no member`);
112
+ findings.push(...inspectPackage(stage));
113
+ if (findings.length > 0)
114
+ return { stage, tarball: undefined, findings };
115
+ const packed = await run([which("bun") ?? "bun", "pm", "pack", "--destination", dist], { cwd: stage });
116
+ if (packed.code !== 0)
117
+ return { stage, tarball: undefined, findings: [`bun pm pack stopped: ${packed.stderr || packed.stdout}`.trim()] };
118
+ return { stage, tarball: join(dist, tarballName(`@intellectual-frontiers/${bundle}`, l.version)), findings: [] };
119
+ }
120
+ /** The command line of a metapackage's `tools/pack.ts`: pack, print what was found (or the tarball) and the exit status. */
121
+ export async function main(root, argv) {
122
+ if (argv.includes("--check") && (env.get("IFC_SKIP_PACK") ?? "") !== "") {
123
+ stdio.stdout.writeSync("pack check skipped: the set did not change in this push (IFC_SKIP_PACK); the release packs it\n");
124
+ return 0;
125
+ }
126
+ const r = await packMetapackage(root);
127
+ for (const f of r.findings)
128
+ stdio.stderr.writeSync(`${f}\n`);
129
+ if (r.tarball === undefined)
130
+ return 1;
131
+ stdio.stdout.writeSync(`${argv.includes("--check") ? `packed ${r.tarball}: nothing wrong` : r.tarball}\n`);
132
+ return 0;
133
+ }
134
+ /** 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. */
135
+ export function generate(argv) {
136
+ const at = argv.indexOf("--repo");
137
+ const repo = at >= 0 ? resolve(argv[at + 1] ?? ".") : resolve(".");
138
+ const check = argv.includes("--check");
139
+ const r = synchronize(repo, !check);
140
+ for (const p of r.problems)
141
+ stdio.stderr.writeSync(`${p}\n`);
142
+ for (const s of r.stale)
143
+ stdio.stderr.writeSync(check ? `${s} is not what the manifests make; run: bun commons/libraries/src/pack-metapackage.ts\n` : `wrote ${s}\n`);
144
+ return r.problems.length > 0 || (check && r.stale.length > 0) ? 1 : 0;
145
+ }
146
+ if (import.meta.main)
147
+ process.exitCode = generate(process.argv.slice(2));