@intellectual-frontiers/libraries 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/affected.d.ts +1 -1
  2. package/dist/changes.js +2 -1
  3. package/dist/check.js +21 -2
  4. package/dist/dev.js +1 -1
  5. package/dist/generate-ontology.d.ts +2 -0
  6. package/dist/generate-ontology.js +41 -1
  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 +1 -1
  10. package/dist/libraries.d.ts +34 -2
  11. package/dist/libraries.js +70 -9
  12. package/dist/license/PolyForm-Noncommercial-1.0.0.md +73 -0
  13. package/dist/link.d.ts +1 -1
  14. package/dist/link.js +4 -4
  15. package/dist/metapackages.d.ts +62 -0
  16. package/dist/metapackages.js +187 -0
  17. package/dist/mod.d.ts +1 -1
  18. package/dist/mod.js +1 -1
  19. package/dist/pack-library.d.ts +7 -0
  20. package/dist/pack-library.js +31 -56
  21. package/dist/pack-metapackage.d.ts +22 -0
  22. package/dist/pack-metapackage.js +147 -0
  23. package/dist/pins.d.ts +1 -1
  24. package/dist/pins.js +1 -1
  25. package/dist/readme.d.ts +13 -1
  26. package/dist/readme.js +45 -4
  27. package/dist/releasing.d.ts +8 -1
  28. package/dist/releasing.js +44 -14
  29. package/dist/workflows/definitions.d.ts +2 -2
  30. package/dist/workflows/definitions.js +31 -113
  31. package/dist/workflows/model.d.ts +2 -0
  32. package/dist/workflows/render.js +2 -0
  33. package/dist/workflows/schema/github-workflow.schema.json +2290 -0
  34. package/package.json +28 -13
  35. package/src/affected.ts +1 -1
  36. package/src/changes.ts +2 -1
  37. package/src/check.ts +21 -2
  38. package/src/dev.ts +1 -1
  39. package/src/generate-ontology.ts +42 -1
  40. package/src/imports/nist-sp-800-53.auto.prov.ttl +22 -0
  41. package/src/imports/schema-org.auto.prov.ttl +22 -0
  42. package/src/install-bun.ts +1 -1
  43. package/src/libraries.ts +88 -8
  44. package/src/license/PolyForm-Noncommercial-1.0.0.md +73 -0
  45. package/src/link.ts +4 -4
  46. package/src/metapackages.ts +203 -0
  47. package/src/mod.ts +1 -1
  48. package/src/pack-library.ts +37 -53
  49. package/src/pack-metapackage.ts +137 -0
  50. package/src/pins.ts +1 -1
  51. package/src/readme.ts +49 -4
  52. package/src/releasing.ts +47 -16
  53. package/src/workflows/definitions.ts +34 -131
  54. package/src/workflows/model.ts +2 -0
  55. package/src/workflows/render.ts +1 -0
  56. package/src/workflows/schema/github-workflow.schema.json +2290 -0
@@ -0,0 +1,203 @@
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, type Result } from "@intellectual-frontiers/core";
25
+ import { type BundleName, BUNDLES, type Library, publishedPackage } from "./libraries.ts";
26
+ import { DEFAULT_LICENSE } from "./license/standard.ts";
27
+ import { ROLE_OF, type Role } from "./readme.ts";
28
+
29
+ /** 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. */
30
+ export const HELD_BACK: ReadonlyMap<string, string> = new Map([
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
+ ]);
37
+
38
+ /** The roles' sets: a role is in exactly one. */
39
+ export const BUNDLE_OF_ROLE: Readonly<Record<Role, Exclude<BundleName, "commons">>> = {
40
+ foundation: "foundation",
41
+ pattern: "patterns",
42
+ program: "programs",
43
+ standard: "standards",
44
+ };
45
+
46
+ /** A member of a set: the library, the package it publishes and the version of this release. */
47
+ export type Member = { readonly library: string; readonly package: string; readonly version: string };
48
+
49
+ /** The exact dependencies of a package: the name of each member and its version, nothing else. */
50
+ export type Dependencies = Readonly<Record<string, string>>;
51
+
52
+ /** Whether a library is installed by a set: it names npm, it is no metapackage and it is not held back. */
53
+ export function isConsumerFacing(l: Pick<Library, "name" | "bundle" | "distribution" | "held">): boolean {
54
+ return l.held === undefined && l.bundle === undefined && l.distribution.includes("npm") && !HELD_BACK.has(l.name);
55
+ }
56
+
57
+ /** Whether a set holds a library of this role. */
58
+ function holds(bundle: BundleName, role: Role): boolean {
59
+ switch (bundle) {
60
+ case "commons":
61
+ return true;
62
+ case "foundation":
63
+ case "patterns":
64
+ case "programs":
65
+ case "standards":
66
+ return BUNDLE_OF_ROLE[role] === bundle;
67
+ default:
68
+ return absurd(bundle);
69
+ }
70
+ }
71
+
72
+ /** 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`. */
73
+ export function membersOf(bundle: BundleName, libraries: readonly Library[]): Result<readonly Member[], string> {
74
+ const out: Member[] = [];
75
+ for (const l of libraries) {
76
+ if (!isConsumerFacing(l)) continue;
77
+ const role = ROLE_OF.get(l.name);
78
+ if (role === undefined) return err(`commons/${l.name} has no role in ROLE_OF (commons/libraries/src/readme.ts), so no set can hold it`);
79
+ if (holds(bundle, role)) out.push({ library: l.name, package: publishedPackage(l), version: l.version });
80
+ }
81
+ return ok(out.sort((a, b) => (a.package < b.package ? -1 : a.package > b.package ? 1 : 0)));
82
+ }
83
+
84
+ /** The exact dependencies of the package of a set: each member's package at its version, no range. */
85
+ export function dependenciesOf(members: readonly Member[]): Dependencies {
86
+ return Object.fromEntries(members.map((m) => [m.package, m.version]));
87
+ }
88
+
89
+ /** What the generated file of a set holds: the set and its exact dependencies. */
90
+ export function setFileText(bundle: BundleName, members: readonly Member[]): string {
91
+ 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`;
92
+ }
93
+
94
+ /** The generated file of a set, from its folder. */
95
+ export const SET_FILE = "dependencies.auto.json";
96
+
97
+ /** The dependencies a generated file holds, or why its text is not one. */
98
+ export function readSetFile(text: string): Result<Dependencies, string> {
99
+ let json: unknown;
100
+ try {
101
+ json = JSON.parse(text);
102
+ } catch {
103
+ return err(`${SET_FILE} is not JSON`);
104
+ }
105
+ const deps = typeof json === "object" && json !== null && "dependencies" in json ? json.dependencies : undefined;
106
+ if (typeof deps !== "object" || deps === null || Array.isArray(deps)) return err(`${SET_FILE} has no dependencies`);
107
+ const out: Record<string, string> = {};
108
+ for (const [k, v] of Object.entries(deps)) {
109
+ if (typeof v !== "string") return err(`${SET_FILE}: ${k} has no version`);
110
+ out[k] = v;
111
+ }
112
+ return ok(out);
113
+ }
114
+
115
+ /** What a set is for, in words, for its README. */
116
+ const PURPOSE: Readonly<Record<BundleName, string>> = {
117
+ commons: "Every consumer-facing library of Intellectual Frontiers Commons, at the versions of one release.",
118
+ 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.",
119
+ patterns: "The pattern libraries: the headless program pattern and the layers of Kanon.",
120
+ programs: "The program libraries: the operations program of a repository, workspaces, data, integrations and the rest of the programs.",
121
+ standards: "The standards libraries: Mizan, the ontologies, the quality standards and the standards of integrations.",
122
+ };
123
+
124
+ /** The text of a set's own `description`, which the package carries. */
125
+ export function descriptionOf(bundle: BundleName): string {
126
+ return `${PURPOSE[bundle]} No code of its own: it depends on each library at one exact version.`;
127
+ }
128
+
129
+ /** The scope of the packages. */
130
+ const SCOPE = "@intellectual-frontiers";
131
+
132
+ /** 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. */
133
+ export function readmeOf(bundle: BundleName, members: readonly Member[]): string {
134
+ const name = `${SCOPE}/${bundle}`;
135
+ const rows = members.map((m) => `| \`${m.package}\` | \`${m.version}\` |`);
136
+ return [
137
+ `# ${name}`,
138
+ "",
139
+ descriptionOf(bundle),
140
+ "",
141
+ "```sh",
142
+ `bun add ${name}@<version> # or: npm install --save-exact ${name}@<version>`,
143
+ "```",
144
+ "",
145
+ `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.`,
146
+ "",
147
+ "| Library | Version |",
148
+ "| --- | --- |",
149
+ ...rows,
150
+ "",
151
+ "## One copy of each library",
152
+ "",
153
+ "Where another package of your tree depends on one of these libraries at a range, make the tree hold one copy: add",
154
+ "",
155
+ "```json",
156
+ `{ "overrides": { "${SCOPE}/core": "<the version above>" } }`,
157
+ "```",
158
+ "",
159
+ "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.",
160
+ "",
161
+ "## From the GitHub package registry",
162
+ "",
163
+ "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:",
164
+ "",
165
+ "```sh",
166
+ "@intellectual-frontiers:registry=https://npm.pkg.github.com",
167
+ "//npm.pkg.github.com/:_authToken=${ERGONEX_GITHUB_TOKEN}",
168
+ "```",
169
+ "",
170
+ `License: ${DEFAULT_LICENSE}. Sources and specifications: https://github.com/intellectual-frontiers/.github/tree/main/commons/${bundle}`,
171
+ "",
172
+ ].join("\n");
173
+ }
174
+
175
+ /** The `package.json` that is published for a set: no code, one empty entry, the exact dependencies. */
176
+ export function publishedManifest(bundle: BundleName, version: string, dependencies: Dependencies, license: string): Readonly<Record<string, unknown>> {
177
+ return {
178
+ name: `${SCOPE}/${bundle}`,
179
+ version,
180
+ description: descriptionOf(bundle),
181
+ license,
182
+ type: "module",
183
+ engines: { node: ">=26" },
184
+ sideEffects: false,
185
+ types: "./index.d.ts",
186
+ exports: { ".": { types: "./index.d.ts", default: "./index.js" } },
187
+ dependencies,
188
+ files: ["index.js", "index.d.ts", "README.md", "LICENSE"],
189
+ repository: { type: "git", url: "git+https://github.com/intellectual-frontiers/.github.git", directory: `commons/${bundle}` },
190
+ homepage: `https://github.com/intellectual-frontiers/.github/tree/main/commons/${bundle}`,
191
+ bugs: { url: "https://github.com/intellectual-frontiers/.github/issues" },
192
+ keywords: ["intellectual-frontiers", "commons", bundle],
193
+ publishConfig: { access: "public" },
194
+ };
195
+ }
196
+
197
+ /** The one file of code of a set's package, and its declaration: an empty module, so that the entry imports. */
198
+ export const ENTRY_JS = "export {};\n";
199
+ /** The declaration of {@link ENTRY_JS}. */
200
+ export const ENTRY_DTS = "export {};\n";
201
+
202
+ /** Every set, in the order the README lists them. */
203
+ export const ALL_BUNDLES: readonly BundleName[] = BUNDLES;
package/src/mod.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`).
@@ -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
  *
@@ -37,7 +37,6 @@ export type Findings = readonly string[];
37
37
 
38
38
  type Json = Readonly<Record<string, unknown>>;
39
39
 
40
- const obj = (v: unknown): Json => (isObject(v) ? v : {});
41
40
  const isObject = (v: unknown): v is Json => typeof v === "object" && v !== null && !Array.isArray(v);
42
41
  const text = (v: unknown): string => (typeof v === "string" ? v : "");
43
42
  /** A text file copied: every file of a package is text. */
@@ -152,7 +151,7 @@ export function inspectPackage(stage: string): Findings {
152
151
  for (const s of specifiersOf(readTextFileSync(join(stage, d)))) {
153
152
  const pkg = packageOf(s);
154
153
  if (pkg === undefined) continue;
155
- 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`);
156
155
  else if (pkg !== name && !declared.has(pkg)) found.push(`${d} imports ${pkg}, which is not a dependency of ${name}`);
157
156
  }
158
157
  }
@@ -169,7 +168,7 @@ export function inspectPackage(stage: string): Findings {
169
168
  for (const s of specifiersOf(source)) {
170
169
  const pkg = packageOf(s);
171
170
  if (pkg === undefined) continue;
172
- 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`);
173
172
  else if (pkg !== name && !declared.has(pkg)) found.push(`${f} imports ${pkg}, which is not a dependency of ${name}`);
174
173
  }
175
174
  }
@@ -189,24 +188,24 @@ export function rewriteDeclarationSpecifiers(source: string): string {
189
188
  export const tarballName = (name: string, version: string): string => `${name.replace(/^@/, "").replace("/", "-")}-${version}.tgz`;
190
189
 
191
190
  /** The version of a library of commons, from its `package.json`, and whether it is published to npm (its manifest names that). */
192
- 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 {
193
192
  const pkg = join(commons, name, "package.json");
194
193
  if (!existsSync(pkg)) return undefined;
195
194
  const lib = join(commons, name, "library.ts");
196
- const npm = existsSync(lib) && /distribution:\s*\[[^\]]*"npm"/.test(readTextFileSync(lib));
197
- 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 };
198
199
  }
199
200
 
200
201
  /**
201
- * 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
202
- * 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
203
- * 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.
204
204
  */
205
205
  function vendorDeclarations(
206
206
  outRoot: string,
207
207
  lib: string,
208
208
  entries: readonly string[],
209
- commonsPkg: { readonly folder: string; readonly exports: ReadonlyMap<string, string> },
210
209
  stageDist: string,
211
210
  ): Findings {
212
211
  const found: string[] = [];
@@ -228,26 +227,10 @@ function vendorDeclarations(
228
227
  placed.set(file, dest);
229
228
  for (const s of specifiersOf(readTextFileSync(file))) {
230
229
  if (s.startsWith(".")) todo.push(join(dirname(file), s.replace(/\.(?:js|ts|mjs)$/, "") + ".d.ts"));
231
- else if (s.startsWith(`${SCOPE}/commons/`)) {
232
- const target = commonsPkg.exports.get(`./${s.slice(`${SCOPE}/commons/`.length)}`);
233
- if (target === undefined) found.push(`${dest} imports ${s}, which the exports of commons do not name`);
234
- else todo.push(join(outRoot, commonsPkg.folder, target.replace(/^\.\//, "").replace(/\.ts$/, "") + ".d.ts"));
235
- }
236
230
  }
237
231
  }
238
232
  for (const [file, dest] of placed) {
239
233
  let source = rewriteDeclarationSpecifiers(readTextFileSync(file));
240
- source = source.replace(
241
- new RegExp(`((?:from\\s+|import\\(\\s*|import\\s+)["'])${SCOPE}/commons/([^"']+)(["'])`, "g"),
242
- (whole, pre: string, sub: string, post: string) => {
243
- const target = commonsPkg.exports.get(`./${sub}`);
244
- if (target === undefined) return whole;
245
- const there = place(join(outRoot, commonsPkg.folder, target.replace(/^\.\//, "").replace(/\.ts$/, "") + ".d.ts"));
246
- let rel = toPosix(relative(dirname(dest), there)).replace(/\.d\.ts$/, ".js");
247
- if (!rel.startsWith(".")) rel = `./${rel}`;
248
- return `${pre}${rel}${post}`;
249
- },
250
- );
251
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
252
235
  source = source.replace(
253
236
  /((?:from\s+|import\(\s*|import\s+)["'])(\.[^"']+)(["'])/g,
@@ -267,23 +250,11 @@ function vendorDeclarations(
267
250
  return found;
268
251
  }
269
252
 
270
- /** 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. */
271
- function commonsPaths(commons: string, dist: string): Record<string, readonly string[]> {
272
- const file = join(commons, "ts", "package.json");
273
- const out: Record<string, readonly string[]> = {};
274
- if (!existsSync(file)) return out;
275
- for (const [k, v] of Object.entries(obj(obj(JSON.parse(readTextFileSync(file)) as unknown)["exports"]))) {
276
- if (typeof v === "string" && v.endsWith(".ts"))
277
- out[`${SCOPE}/commons/${k.replace(/^\.\//, "")}`] = [toPosix(relative(dist, join(commons, "ts", v)))];
278
- }
279
- return out;
280
- }
281
-
282
253
  /** What packing a library made: the staging folder, the tarball, the findings and the manifest. */
283
254
  export type Packed = { readonly stage: string; readonly tarball: string | undefined; readonly findings: Findings; readonly manifest: Json };
284
255
 
285
256
  /** What a package adds to the common form. */
286
- /** 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). */
287
258
  function namesNodeTypes(folder: string): boolean {
288
259
  if (!existsSync(folder)) return false;
289
260
  for (const e of readDirSync(folder)) {
@@ -299,6 +270,13 @@ function namesNodeTypes(folder: string): boolean {
299
270
  export type PackOptions = {
300
271
  /** Types the compiler is given (`["node"]` for a library that names Node's). */
301
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>>;
302
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. */
303
281
  readonly bundle?: {
304
282
  /** The commands: their name, and the source that is the command's entry from the library's folder. */
@@ -331,7 +309,7 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
331
309
  if (existsSync(dist)) removeSync(dist, { recursive: true });
332
310
  mkdirSync(stage, { recursive: true });
333
311
  // the compiler's configuration for this build: the base of commons, emitting modules and declarations of src/ and nothing else
334
- const base = relative(dist, join(commons, "ts", "tsconfig.json"));
312
+ const base = relative(dist, join(commons, "tsconfig.base.json"));
335
313
  writeTextFileSync(
336
314
  join(dist, "tsconfig.build.json"),
337
315
  JSON.stringify(
@@ -347,8 +325,6 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
347
325
  skipLibCheck: true,
348
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
349
327
  preserveSymlinks: true,
350
- // 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
351
- ...(bundle === undefined ? {} : { paths: commonsPaths(commons, dist) }),
352
328
  },
353
329
  include: [toPosix(relative(dist, join(root, "src"))) + "/**/*.ts"],
354
330
  },
@@ -372,12 +348,6 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
372
348
  else if (f.endsWith(".js")) copyFileSync(join(out, f), target);
373
349
  }
374
350
  } else {
375
- const commonsPkgFile = join(commons, "ts", "package.json");
376
- const commonsExports = new Map<string, string>();
377
- if (existsSync(commonsPkgFile)) {
378
- const declared = obj(obj(JSON.parse(readTextFileSync(commonsPkgFile)) as unknown)["exports"]);
379
- for (const [k, v] of Object.entries(declared)) if (typeof v === "string") commonsExports.set(k, v);
380
- }
381
351
  const entryFiles = Object.values(ownExports).map((e) =>
382
352
  join(out, (targetsOf(e)["default"] ?? text(e)).replace(/^\.\/src\//, "").replace(/\.ts$/, "") + ".d.ts"),
383
353
  );
@@ -386,7 +356,6 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
386
356
  join(dist, "out"),
387
357
  basename(resolve(root)),
388
358
  entryFiles,
389
- { folder: "ts", exports: commonsExports },
390
359
  join(stage, "dist"),
391
360
  ),
392
361
  );
@@ -394,10 +363,15 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
394
363
  // the sources, for the `bun` condition (a bundle has no source to offer: what it imports is not on npm)
395
364
  if (bundle === undefined) {
396
365
  for (const f of filesUnder(join(root, "src"))) {
397
- if (!f.endsWith(".ts")) continue;
398
366
  const target = join(stage, "src", f);
399
367
  mkdirSync(dirname(target), { recursive: true });
400
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
+ }
401
375
  }
402
376
  }
403
377
  // the entries: each names its declarations, its source (Bun) and its compiled file
@@ -421,6 +395,13 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
421
395
  }
422
396
  // the commands of a bundle: one file each, with the line that runs it under Node
423
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
+ }
424
405
  for (const [cmd, source] of Object.entries(bundle?.bin ?? {})) {
425
406
  const file = join(stage, `${cmd}.mjs`);
426
407
  const bundled = await run([bun, "build", join(root, source), "--target=node", "--format=esm", "--outfile", file], { cwd: root });
@@ -458,7 +439,7 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
458
439
  if (pkg.startsWith(`${SCOPE}/`) && pkg !== `${SCOPE}/commons`) {
459
440
  const lib = libraryOf(commons, pkg.slice(SCOPE.length + 1));
460
441
  if (lib === undefined) findings.push(`${file} imports ${pkg}, which is no library of commons`);
461
- 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`);
462
443
  else dependencies[pkg] = lib.version;
463
444
  } else if (pkg !== `${SCOPE}/commons`) {
464
445
  const v = declaredDeps[pkg];
@@ -483,7 +464,10 @@ export async function packLibrary(root: string, options: PackOptions = {}): Prom
483
464
  ...(Object.keys(dependencies).length === 0
484
465
  ? {}
485
466
  : { dependencies: Object.fromEntries(Object.entries(dependencies).sort(([a], [b]) => (a < b ? -1 : 1))) }),
486
- ...(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 }])) }),
487
471
  files: [
488
472
  ...(Object.keys(exportsOut).length === 0 ? [] : ["dist"]),
489
473
  ...(bundle === undefined ? ["src"] : []),
@@ -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,5 +1,5 @@
1
1
  /**
2
- * Commons' pins as the tools read them (commons 0005-typescript-runtime FR-026, 0002-packaging FR-019): the `KEY=value` lines of `pins.auto.env`, the suffix of a platform's
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
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