@intellectual-frontiers/libraries 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/affected.d.ts +28 -0
- package/dist/affected.js +250 -0
- package/dist/changes.d.ts +27 -0
- package/dist/changes.js +470 -0
- package/dist/check.d.ts +15 -0
- package/dist/check.js +198 -0
- package/dist/generate-ontology.d.ts +37 -0
- package/dist/generate-ontology.js +293 -0
- package/dist/generate-workflows.d.ts +12 -0
- package/dist/generate-workflows.js +102 -0
- package/dist/imports/nist-sp-800-53.import.d.ts +33 -0
- package/dist/imports/nist-sp-800-53.import.js +34 -0
- package/dist/imports/schema-org.import.d.ts +33 -0
- package/dist/imports/schema-org.import.js +31 -0
- package/dist/install-bun.d.ts +1 -0
- package/dist/install-bun.js +58 -0
- package/dist/libraries.d.ts +79 -0
- package/dist/libraries.js +386 -0
- package/dist/mod.d.ts +16 -0
- package/dist/mod.js +16 -0
- package/dist/newer.d.ts +27 -0
- package/dist/newer.js +147 -0
- package/dist/oscal-register.d.ts +65 -0
- package/dist/oscal-register.js +344 -0
- package/dist/pack-library.d.ts +47 -0
- package/dist/pack-library.js +498 -0
- package/dist/pins.d.ts +15 -0
- package/dist/pins.js +29 -0
- package/dist/publish-ontology.d.ts +160 -0
- package/dist/publish-ontology.js +227 -0
- package/dist/versions.d.ts +26 -0
- package/dist/versions.js +556 -0
- package/dist/workflows/definitions.d.ts +65 -0
- package/dist/workflows/definitions.js +614 -0
- package/dist/workflows/mod.d.ts +10 -0
- package/dist/workflows/mod.js +10 -0
- package/dist/workflows/model.d.ts +143 -0
- package/dist/workflows/model.js +81 -0
- package/dist/workflows/render.d.ts +9 -0
- package/dist/workflows/render.js +179 -0
- package/package.json +129 -4
- package/src/affected.ts +265 -0
- package/src/changes.ts +546 -0
- package/src/check.ts +223 -0
- package/src/generate-ontology.ts +350 -0
- package/src/generate-workflows.ts +103 -0
- package/src/imports/nist-sp-800-53.import.ts +36 -0
- package/src/imports/schema-org.import.ts +31 -0
- package/src/install-bun.ts +53 -0
- package/src/libraries.ts +385 -0
- package/src/mod.ts +16 -0
- package/src/newer.ts +154 -0
- package/src/oscal-register.ts +443 -0
- package/src/pack-library.ts +522 -0
- package/src/pins.ts +30 -0
- package/src/publish-ontology.ts +354 -0
- package/src/versions.ts +594 -0
- package/src/workflows/definitions.ts +847 -0
- package/src/workflows/mod.ts +30 -0
- package/src/workflows/model.ts +187 -0
- package/src/workflows/render.ts +176 -0
|
@@ -0,0 +1,498 @@
|
|
|
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
|
|
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
|
+
* packs is `inspectPackage`, which a test runs again on the tarball it unpacks.
|
|
5
|
+
*
|
|
6
|
+
* bun tools/pack.ts of a library: pack `dist/<scope>-<name>-<version>.tgz`
|
|
7
|
+
* bun tools/pack.ts --check the same, and print what was found; exit 1 where anything was
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* A library's own `package.json` stays private and names its entries by their sources (`"./src/mod.ts"`); nothing is published from it by hand. A library that depends on another library of commons imports it by package
|
|
11
|
+
* name, and the published package depends on it at the version that library has in this repository. The declarations are written with `.js` specifiers (the compiler leaves `.ts` in a `.d.ts`, which a consumer's
|
|
12
|
+
* compiler cannot read), and only the declarations that the entries reach are published in a bundle. A package is refused where an entry has no declaration, where a declaration reached imports a file that is not in
|
|
13
|
+
* the package or a package that is not a dependency, or where the code imports a package that is not on npm.
|
|
14
|
+
*
|
|
15
|
+
* A package that is also a command, or whose code imports what is not yet on npm, is packed with `bundle`: each entry's code and the command's are one bundle (`bun build`), the declarations are emitted from the
|
|
16
|
+
* sources and only those the entries reach are published, and whatever they import must be a package of npm that the package depends on.
|
|
17
|
+
*
|
|
18
|
+
* @limits
|
|
19
|
+
* It builds a library whose sources are under `src/`, one tarball with one `exports` map.
|
|
20
|
+
*
|
|
21
|
+
* Spec: 0001-libraries FR-007
|
|
22
|
+
*
|
|
23
|
+
* @module
|
|
24
|
+
*/
|
|
25
|
+
import { chmod, readTextFile, writeTextFile } from "@intellectual-frontiers/system/fs";
|
|
26
|
+
import { existsSync, mkdirSync, readDirSync, readTextFileSync, removeSync, writeTextFileSync } from "@intellectual-frontiers/system/fs";
|
|
27
|
+
import { basename, dirname, join, relative, resolve, toPosix } from "@intellectual-frontiers/system/path";
|
|
28
|
+
import { run, which } from "@intellectual-frontiers/system/proc";
|
|
29
|
+
/** The scope of the packages of commons. */
|
|
30
|
+
export const SCOPE = "@intellectual-frontiers";
|
|
31
|
+
const obj = (v) => (isObject(v) ? v : {});
|
|
32
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
33
|
+
const text = (v) => (typeof v === "string" ? v : "");
|
|
34
|
+
/** A text file copied: every file of a package is text. */
|
|
35
|
+
const copyFileSync = (from, to) => writeTextFileSync(to, readTextFileSync(from));
|
|
36
|
+
const noDot = (p) => toPosix(p).replace(/^\.\//, "");
|
|
37
|
+
/** The files under a folder, from the folder, in order (`node_modules` is not looked into). */
|
|
38
|
+
export function filesUnder(root, dir = "") {
|
|
39
|
+
const out = [];
|
|
40
|
+
for (const e of readDirSync(join(root, dir)).sort((a, b) => (a.name < b.name ? -1 : 1))) {
|
|
41
|
+
if (e.name === "node_modules")
|
|
42
|
+
continue;
|
|
43
|
+
const rel = dir === "" ? e.name : `${dir}/${e.name}`;
|
|
44
|
+
if (e.isDirectory)
|
|
45
|
+
out.push(...filesUnder(root, rel));
|
|
46
|
+
else if (e.isFile)
|
|
47
|
+
out.push(rel);
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
const SPECIFIER = /(?:\bfrom\s+|\bimport\s+|\bimport\(\s*)["']([^"']+)["']/g;
|
|
52
|
+
/** The documentation blocks of a source (a block comment that begins a line): an example in one names what a reader may write, not what the module imports. */
|
|
53
|
+
const DOC_BLOCK = /^[ \t]*\/\*[\s\S]*?\*\//gm;
|
|
54
|
+
/** The specifiers a source names, outside its documentation blocks. */
|
|
55
|
+
export const specifiersOf = (source) => [...source.replace(DOC_BLOCK, "").matchAll(SPECIFIER)].map((m) => m[1] ?? "").filter((s) => s !== "");
|
|
56
|
+
/** The package a bare specifier names (`@scope/name/sub` is `@scope/name`), or undefined for a relative path, a `node:` builtin or a `data:` URL. */
|
|
57
|
+
export function packageOf(specifier) {
|
|
58
|
+
if (specifier.startsWith(".") || specifier.startsWith("/") || specifier.startsWith("node:") || specifier.startsWith("data:"))
|
|
59
|
+
return undefined;
|
|
60
|
+
const parts = specifier.split("/");
|
|
61
|
+
return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
62
|
+
}
|
|
63
|
+
/** The target of an export entry for each condition: the string, or the `types`, `bun` or `default` of an object. */
|
|
64
|
+
function targetsOf(entry) {
|
|
65
|
+
if (typeof entry === "string")
|
|
66
|
+
return { default: entry };
|
|
67
|
+
if (!isObject(entry))
|
|
68
|
+
return {};
|
|
69
|
+
return Object.fromEntries(Object.entries(entry).flatMap(([k, v]) => (typeof v === "string" ? [[k, v]] : [])));
|
|
70
|
+
}
|
|
71
|
+
/** The declaration file that a relative specifier of a declaration names, from the package's root: `./x.js` is `x.d.ts`. */
|
|
72
|
+
function declarationFor(from, specifier) {
|
|
73
|
+
const base = toPosix(join(dirname(from), specifier));
|
|
74
|
+
return base.replace(/\.(?:js|ts|mjs)$/, "") + ".d.ts";
|
|
75
|
+
}
|
|
76
|
+
/** The declarations that the entries of a package reach, by relative imports from the `types` of each entry, and what is wrong on the way (a declaration that is not there). */
|
|
77
|
+
export function reachableDeclarations(stage, manifest) {
|
|
78
|
+
const found = [];
|
|
79
|
+
const name = text(manifest["name"]);
|
|
80
|
+
const files = new Set(filesUnder(stage));
|
|
81
|
+
const entries = [];
|
|
82
|
+
for (const [key, entry] of Object.entries(isObject(manifest["exports"]) ? manifest["exports"] : {})) {
|
|
83
|
+
const types = targetsOf(entry)["types"];
|
|
84
|
+
if (types === undefined)
|
|
85
|
+
found.push(`${name}: the export ${key} names no \`types\` file`);
|
|
86
|
+
else if (!files.has(noDot(types)))
|
|
87
|
+
found.push(`${name}: the export ${key} names ${types}, which is not in the package`);
|
|
88
|
+
else
|
|
89
|
+
entries.push(noDot(types));
|
|
90
|
+
}
|
|
91
|
+
if (typeof manifest["types"] === "string" && !files.has(noDot(manifest["types"])))
|
|
92
|
+
found.push(`${name}: types names ${manifest["types"]}, which is not in the package`);
|
|
93
|
+
const reached = new Set();
|
|
94
|
+
const todo = [...entries];
|
|
95
|
+
while (todo.length > 0) {
|
|
96
|
+
const d = todo.pop() ?? "";
|
|
97
|
+
if (reached.has(d))
|
|
98
|
+
continue;
|
|
99
|
+
reached.add(d);
|
|
100
|
+
for (const s of specifiersOf(readTextFileSync(join(stage, d)))) {
|
|
101
|
+
if (!s.startsWith("."))
|
|
102
|
+
continue;
|
|
103
|
+
const next = declarationFor(d, s);
|
|
104
|
+
if (!files.has(next))
|
|
105
|
+
found.push(`${d} imports ${s}, whose declaration ${next} is not in the package`);
|
|
106
|
+
else
|
|
107
|
+
todo.push(next);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return { reached, findings: found };
|
|
111
|
+
}
|
|
112
|
+
/** What is wrong with the package staged in a folder (the files of the tarball, `package/` unpacked): see the remarks of this module. */
|
|
113
|
+
export function inspectPackage(stage) {
|
|
114
|
+
const found = [];
|
|
115
|
+
const manifestFile = join(stage, "package.json");
|
|
116
|
+
if (!existsSync(manifestFile))
|
|
117
|
+
return ["package.json is not in the package"];
|
|
118
|
+
const manifest = JSON.parse(readTextFileSync(manifestFile));
|
|
119
|
+
const name = text(manifest["name"]);
|
|
120
|
+
const files = new Set(filesUnder(stage));
|
|
121
|
+
const declared = new Set([
|
|
122
|
+
...Object.keys(isObject(manifest["dependencies"]) ? manifest["dependencies"] : {}),
|
|
123
|
+
...Object.keys(isObject(manifest["peerDependencies"]) ? manifest["peerDependencies"] : {}),
|
|
124
|
+
...Object.keys(isObject(manifest["optionalDependencies"]) ? manifest["optionalDependencies"] : {}),
|
|
125
|
+
]);
|
|
126
|
+
const exportsMap = isObject(manifest["exports"]) ? manifest["exports"] : {};
|
|
127
|
+
const bin = isObject(manifest["bin"]) ? manifest["bin"] : {};
|
|
128
|
+
if (Object.keys(exportsMap).length === 0 && Object.keys(bin).length === 0)
|
|
129
|
+
found.push(`${name}: the package has no exports and no bin`);
|
|
130
|
+
for (const [cmd, target] of Object.entries(bin))
|
|
131
|
+
if (typeof target !== "string" || !files.has(noDot(target)))
|
|
132
|
+
found.push(`${name}: the command ${cmd} names ${String(target)}, which is not in the package`);
|
|
133
|
+
// every entry has declarations and the files its conditions name
|
|
134
|
+
for (const [key, entry] of Object.entries(exportsMap)) {
|
|
135
|
+
for (const [cond, target] of Object.entries(targetsOf(entry))) {
|
|
136
|
+
if (cond !== "types" && !files.has(noDot(target)))
|
|
137
|
+
found.push(`${name}: the export ${key} (${cond}) names ${target}, which is not in the package`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// the declarations that the entries reach: each import is a file of the package or a package the manifest depends on
|
|
141
|
+
const { reached, findings } = reachableDeclarations(stage, manifest);
|
|
142
|
+
found.push(...findings);
|
|
143
|
+
for (const d of reached) {
|
|
144
|
+
for (const s of specifiersOf(readTextFileSync(join(stage, d)))) {
|
|
145
|
+
const pkg = packageOf(s);
|
|
146
|
+
if (pkg === undefined)
|
|
147
|
+
continue;
|
|
148
|
+
if (pkg === `${SCOPE}/commons`)
|
|
149
|
+
found.push(`${d} imports ${s}: the package ${pkg} is not on npm`);
|
|
150
|
+
else if (pkg !== name && !declared.has(pkg))
|
|
151
|
+
found.push(`${d} imports ${pkg}, which is not a dependency of ${name}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// the compiled code of a library (files under dist/ other than bundles) imports no package that is not a dependency, and none that is not on npm; a declaration never imports a `.ts` path
|
|
155
|
+
for (const f of files) {
|
|
156
|
+
if (!/\.(?:js|mjs|d\.ts)$/.test(f) || !f.startsWith("dist/"))
|
|
157
|
+
continue;
|
|
158
|
+
const source = readTextFileSync(join(stage, f));
|
|
159
|
+
if (/\.d\.ts$/.test(f)) {
|
|
160
|
+
if (/(?:from\s+|import\(\s*)["']\.[^"']*\.ts["']/.test(source))
|
|
161
|
+
found.push(`${f} imports a \`.ts\` path, which a consumer's compiler cannot read`);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (isBundle(source))
|
|
165
|
+
continue;
|
|
166
|
+
for (const s of specifiersOf(source)) {
|
|
167
|
+
const pkg = packageOf(s);
|
|
168
|
+
if (pkg === undefined)
|
|
169
|
+
continue;
|
|
170
|
+
if (pkg === `${SCOPE}/commons`)
|
|
171
|
+
found.push(`${f} imports ${s}: the package ${pkg} is not on npm`);
|
|
172
|
+
else if (pkg !== name && !declared.has(pkg))
|
|
173
|
+
found.push(`${f} imports ${pkg}, which is not a dependency of ${name}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return found;
|
|
177
|
+
}
|
|
178
|
+
/** Whether a compiled file is a bundle (`bun build` marks its output): its imports are what it holds, so it is not read for them. */
|
|
179
|
+
const isBundle = (source) => /^\/\/ @bun\b|^\/\/ @bun\s*$/m.test(source.slice(0, 400)) || source.startsWith("#!") || /\/\/ \.\.\/\.\.\//.test(source.slice(0, 2000));
|
|
180
|
+
/** Writes the declarations with `.js` in the place of `.ts` in their relative specifiers. */
|
|
181
|
+
export function rewriteDeclarationSpecifiers(source) {
|
|
182
|
+
return source.replace(/((?:from\s+|import\(\s*|import\s+)["'])(\.[^"']*?)\.ts(["'])/g, "$1$2.js$3");
|
|
183
|
+
}
|
|
184
|
+
/** The tarball's name for a package: `@scope/name` at a version is `scope-name-version.tgz`. */
|
|
185
|
+
export const tarballName = (name, version) => `${name.replace(/^@/, "").replace("/", "-")}-${version}.tgz`;
|
|
186
|
+
/** The version of a library of commons, from its `package.json`, and whether it is published to npm (its manifest names that). */
|
|
187
|
+
function libraryOf(commons, name) {
|
|
188
|
+
const pkg = join(commons, name, "package.json");
|
|
189
|
+
if (!existsSync(pkg))
|
|
190
|
+
return undefined;
|
|
191
|
+
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 };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
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
|
+
*/
|
|
200
|
+
function vendorDeclarations(outRoot, lib, entries, commonsPkg, stageDist) {
|
|
201
|
+
const found = [];
|
|
202
|
+
const placed = new Map(); // a file of the compiler's output, and where it goes under the stage's dist/
|
|
203
|
+
const place = (file) => {
|
|
204
|
+
const rel = toPosix(relative(outRoot, file));
|
|
205
|
+
const own = `${lib}/src/`;
|
|
206
|
+
return rel.startsWith(own) ? rel.slice(own.length) : `vendor/${rel}`;
|
|
207
|
+
};
|
|
208
|
+
const todo = [...entries];
|
|
209
|
+
while (todo.length > 0) {
|
|
210
|
+
const file = todo.pop() ?? "";
|
|
211
|
+
if (placed.has(file))
|
|
212
|
+
continue;
|
|
213
|
+
if (!existsSync(file)) {
|
|
214
|
+
found.push(`the compiler wrote no declaration ${toPosix(relative(outRoot, file))}`);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
const dest = place(file);
|
|
218
|
+
placed.set(file, dest);
|
|
219
|
+
for (const s of specifiersOf(readTextFileSync(file))) {
|
|
220
|
+
if (s.startsWith("."))
|
|
221
|
+
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
|
+
}
|
|
230
|
+
}
|
|
231
|
+
for (const [file, dest] of placed) {
|
|
232
|
+
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
|
+
// 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
|
+
source = source.replace(/((?:from\s+|import\(\s*|import\s+)["'])(\.[^"']+)(["'])/g, (whole, pre, spec, post) => {
|
|
245
|
+
const target = join(dirname(file), spec.replace(/\.(?:js|ts|mjs)$/, "") + ".d.ts");
|
|
246
|
+
const there = placed.get(target);
|
|
247
|
+
if (there === undefined)
|
|
248
|
+
return whole;
|
|
249
|
+
let rel = toPosix(relative(dirname(dest), there)).replace(/\.d\.ts$/, ".js");
|
|
250
|
+
if (!rel.startsWith("."))
|
|
251
|
+
rel = `./${rel}`;
|
|
252
|
+
return `${pre}${rel}${post}`;
|
|
253
|
+
});
|
|
254
|
+
const out = join(stageDist, dest);
|
|
255
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
256
|
+
writeTextFileSync(out, source);
|
|
257
|
+
}
|
|
258
|
+
return found;
|
|
259
|
+
}
|
|
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
|
+
/** 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). */
|
|
274
|
+
function namesNodeTypes(folder) {
|
|
275
|
+
if (!existsSync(folder))
|
|
276
|
+
return false;
|
|
277
|
+
for (const e of readDirSync(folder)) {
|
|
278
|
+
const p = join(folder, e.name);
|
|
279
|
+
if (e.isDirectory) {
|
|
280
|
+
if (namesNodeTypes(p))
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
else if (e.name.endsWith(".d.ts") && /(?:from\s+|import\()"node:/.test(readTextFileSync(p)))
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
/** 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
|
+
export async function packLibrary(root, options = {}) {
|
|
290
|
+
const own = JSON.parse(readTextFileSync(join(root, "package.json")));
|
|
291
|
+
const name = text(own["name"]);
|
|
292
|
+
const short = name.replace(`${SCOPE}/`, "");
|
|
293
|
+
const commons = dirname(resolve(root));
|
|
294
|
+
const dist = join(root, "dist");
|
|
295
|
+
const stage = join(dist, "package");
|
|
296
|
+
const bundle = options.bundle;
|
|
297
|
+
if (existsSync(dist))
|
|
298
|
+
removeSync(dist, { recursive: true });
|
|
299
|
+
mkdirSync(stage, { recursive: true });
|
|
300
|
+
// 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"));
|
|
302
|
+
writeTextFileSync(join(dist, "tsconfig.build.json"), JSON.stringify({
|
|
303
|
+
extends: toPosix(base),
|
|
304
|
+
compilerOptions: {
|
|
305
|
+
noEmit: false,
|
|
306
|
+
declaration: true,
|
|
307
|
+
rewriteRelativeImportExtensions: true,
|
|
308
|
+
rootDir: toPosix(relative(dist, commons)),
|
|
309
|
+
outDir: "out",
|
|
310
|
+
types: options.types ?? [],
|
|
311
|
+
skipLibCheck: true,
|
|
312
|
+
// 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
|
+
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
|
+
},
|
|
317
|
+
include: [toPosix(relative(dist, join(root, "src"))) + "/**/*.ts"],
|
|
318
|
+
}, null, 2));
|
|
319
|
+
const bun = which("bun") ?? "bun";
|
|
320
|
+
const built = await run([bun, "x", "tsc", "-p", join(dist, "tsconfig.build.json")], { cwd: root });
|
|
321
|
+
if (built.code !== 0)
|
|
322
|
+
return { stage, tarball: undefined, findings: [`the compiler stopped: ${built.stdout || built.stderr}`.trim()], manifest: {} };
|
|
323
|
+
// what src/ became is in out/<name>/src/: the declarations go to dist/, and the compiled files too unless the package is a bundle
|
|
324
|
+
const out = join(dist, "out", basename(resolve(root)), "src");
|
|
325
|
+
const findings = [];
|
|
326
|
+
const ownExports = isObject(own["exports"]) ? own["exports"] : {};
|
|
327
|
+
if (bundle === undefined) {
|
|
328
|
+
for (const f of filesUnder(out)) {
|
|
329
|
+
const target = join(stage, "dist", f);
|
|
330
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
331
|
+
if (f.endsWith(".d.ts"))
|
|
332
|
+
writeTextFileSync(target, rewriteDeclarationSpecifiers(readTextFileSync(join(out, f))));
|
|
333
|
+
else if (f.endsWith(".js"))
|
|
334
|
+
copyFileSync(join(out, f), target);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
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
|
+
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")));
|
|
348
|
+
}
|
|
349
|
+
// the sources, for the `bun` condition (a bundle has no source to offer: what it imports is not on npm)
|
|
350
|
+
if (bundle === undefined) {
|
|
351
|
+
for (const f of filesUnder(join(root, "src"))) {
|
|
352
|
+
if (!f.endsWith(".ts"))
|
|
353
|
+
continue;
|
|
354
|
+
const target = join(stage, "src", f);
|
|
355
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
356
|
+
copyFileSync(join(root, "src", f), target);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
// the entries: each names its declarations, its source (Bun) and its compiled file
|
|
360
|
+
const exportsOut = {};
|
|
361
|
+
for (const [key, entry] of Object.entries(ownExports)) {
|
|
362
|
+
const source = targetsOf(entry)["default"] ?? text(entry);
|
|
363
|
+
const rel = toPosix(source)
|
|
364
|
+
.replace(/^\.\/src\//, "")
|
|
365
|
+
.replace(/\.ts$/, "");
|
|
366
|
+
exportsOut[key] =
|
|
367
|
+
bundle === undefined
|
|
368
|
+
? { types: `./dist/${rel}.d.ts`, bun: `./src/${rel}.ts`, default: `./dist/${rel}.js` }
|
|
369
|
+
: { types: `./dist/${rel}.d.ts`, default: `./dist/${rel}.js` };
|
|
370
|
+
if (bundle !== undefined) {
|
|
371
|
+
const bundled = await run([bun, "build", join(root, source), "--target=node", "--format=esm", "--outfile", join(stage, "dist", `${rel}.js`)], { cwd: root });
|
|
372
|
+
if (bundled.code !== 0)
|
|
373
|
+
findings.push(`bun build stopped on ${source}: ${bundled.stderr || bundled.stdout}`.trim());
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
// the commands of a bundle: one file each, with the line that runs it under Node
|
|
377
|
+
const binOut = {};
|
|
378
|
+
for (const [cmd, source] of Object.entries(bundle?.bin ?? {})) {
|
|
379
|
+
const file = join(stage, `${cmd}.mjs`);
|
|
380
|
+
const bundled = await run([bun, "build", join(root, source), "--target=node", "--format=esm", "--outfile", file], { cwd: root });
|
|
381
|
+
if (bundled.code !== 0) {
|
|
382
|
+
findings.push(`bun build stopped on ${source}: ${bundled.stderr || bundled.stdout}`.trim());
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const code = await readTextFile(file);
|
|
386
|
+
await writeTextFile(file, code.startsWith("#!") ? code : `#!/usr/bin/env node\n${code}`);
|
|
387
|
+
await chmod(file, 0o755);
|
|
388
|
+
binOut[cmd] = `./${cmd}.mjs`;
|
|
389
|
+
}
|
|
390
|
+
// the dependencies: the packages that the compiled code names (of a bundle, the declarations the entries reach), the libraries of commons at the version they have here, the others as the library declares them
|
|
391
|
+
const declaredDeps = isObject(own["dependencies"]) ? own["dependencies"] : {};
|
|
392
|
+
const dependencies = {};
|
|
393
|
+
const scanned = [];
|
|
394
|
+
if (bundle === undefined)
|
|
395
|
+
for (const f of filesUnder(join(stage, "dist")))
|
|
396
|
+
scanned.push({ file: `dist/${f}`, source: readTextFileSync(join(stage, "dist", f)) });
|
|
397
|
+
else {
|
|
398
|
+
const draft = {
|
|
399
|
+
name,
|
|
400
|
+
exports: exportsOut,
|
|
401
|
+
...(typeof exportsOut["."] === "object" ? { types: exportsOut["."]["types"] } : {}),
|
|
402
|
+
};
|
|
403
|
+
const { reached } = reachableDeclarations(stage, draft);
|
|
404
|
+
for (const d of reached)
|
|
405
|
+
scanned.push({ file: d, source: readTextFileSync(join(stage, d)) });
|
|
406
|
+
// only the declarations the entries reach are published
|
|
407
|
+
for (const f of filesUnder(join(stage, "dist")))
|
|
408
|
+
if (f.endsWith(".d.ts") && !reached.has(`dist/${f}`))
|
|
409
|
+
removeSync(join(stage, "dist", f));
|
|
410
|
+
}
|
|
411
|
+
for (const { file, source } of scanned) {
|
|
412
|
+
for (const s of specifiersOf(source)) {
|
|
413
|
+
const pkg = packageOf(s);
|
|
414
|
+
if (pkg === undefined || pkg === name)
|
|
415
|
+
continue;
|
|
416
|
+
if (pkg.startsWith(`${SCOPE}/`) && pkg !== `${SCOPE}/commons`) {
|
|
417
|
+
const lib = libraryOf(commons, pkg.slice(SCOPE.length + 1));
|
|
418
|
+
if (lib === undefined)
|
|
419
|
+
findings.push(`${file} imports ${pkg}, which is no library of commons`);
|
|
420
|
+
else if (!lib.npm)
|
|
421
|
+
findings.push(`${file} imports ${pkg}, which is not published to npm`);
|
|
422
|
+
else
|
|
423
|
+
dependencies[pkg] = lib.version;
|
|
424
|
+
}
|
|
425
|
+
else if (pkg !== `${SCOPE}/commons`) {
|
|
426
|
+
const v = declaredDeps[pkg];
|
|
427
|
+
if (typeof v === "string")
|
|
428
|
+
dependencies[pkg] = v;
|
|
429
|
+
else
|
|
430
|
+
findings.push(`${file} imports ${pkg}, which the package.json of ${short} does not list under dependencies`);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
const extraFiles = bundle?.extraFiles ?? [];
|
|
435
|
+
for (const f of extraFiles)
|
|
436
|
+
copyFileSync(join(root, f), join(stage, f));
|
|
437
|
+
const manifest = {
|
|
438
|
+
name,
|
|
439
|
+
version: text(own["version"]),
|
|
440
|
+
description: text(own["description"]),
|
|
441
|
+
license: "MIT",
|
|
442
|
+
type: "module",
|
|
443
|
+
engines: { node: ">=26" },
|
|
444
|
+
sideEffects: false,
|
|
445
|
+
...(Object.keys(binOut).length === 0 ? {} : { bin: binOut }),
|
|
446
|
+
...(exportsOut["."] === undefined ? {} : { types: exportsOut["."]["types"] }),
|
|
447
|
+
...(Object.keys(exportsOut).length === 0 ? {} : { exports: exportsOut }),
|
|
448
|
+
...(Object.keys(dependencies).length === 0
|
|
449
|
+
? {}
|
|
450
|
+
: { dependencies: Object.fromEntries(Object.entries(dependencies).sort(([a], [b]) => (a < b ? -1 : 1))) }),
|
|
451
|
+
...(namesNodeTypes(join(stage, "dist")) ? { peerDependencies: { "@types/node": ">=26" } } : {}),
|
|
452
|
+
files: [
|
|
453
|
+
...(Object.keys(exportsOut).length === 0 ? [] : ["dist"]),
|
|
454
|
+
...(bundle === undefined ? ["src"] : []),
|
|
455
|
+
...Object.values(binOut).map(noDot),
|
|
456
|
+
"README.md",
|
|
457
|
+
"LICENSE",
|
|
458
|
+
...extraFiles,
|
|
459
|
+
],
|
|
460
|
+
repository: { type: "git", url: "git+https://github.com/intellectual-frontiers/.github.git", directory: `commons/${short}` },
|
|
461
|
+
homepage: `https://github.com/intellectual-frontiers/.github/tree/main/commons/${short}`,
|
|
462
|
+
bugs: { url: "https://github.com/intellectual-frontiers/.github/issues" },
|
|
463
|
+
...(bundle?.keywords === undefined ? {} : { keywords: bundle.keywords }),
|
|
464
|
+
publishConfig: { access: "public" },
|
|
465
|
+
};
|
|
466
|
+
writeTextFileSync(join(stage, "package.json"), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
467
|
+
writeTextFileSync(join(stage, "README.md"), bundle?.readme ??
|
|
468
|
+
[
|
|
469
|
+
`# ${name}`,
|
|
470
|
+
"",
|
|
471
|
+
text(own["description"]),
|
|
472
|
+
"",
|
|
473
|
+
"```sh",
|
|
474
|
+
`bun add ${name} # or: npm install ${name}`,
|
|
475
|
+
"```",
|
|
476
|
+
"",
|
|
477
|
+
`Sources and specifications: https://github.com/intellectual-frontiers/.github/tree/main/commons/${short}`,
|
|
478
|
+
"",
|
|
479
|
+
].join("\n"));
|
|
480
|
+
copyFileSync(existsSync(join(root, "LICENSE")) ? join(root, "LICENSE") : join(commons, "ergonex", "LICENSE"), join(stage, "LICENSE"));
|
|
481
|
+
findings.push(...inspectPackage(stage));
|
|
482
|
+
if (findings.length > 0)
|
|
483
|
+
return { stage, tarball: undefined, findings, manifest };
|
|
484
|
+
const packed = await run([bun, "pm", "pack", "--destination", dist], { cwd: stage });
|
|
485
|
+
if (packed.code !== 0)
|
|
486
|
+
return { stage, tarball: undefined, findings: [`bun pm pack stopped: ${packed.stderr || packed.stdout}`.trim()], manifest };
|
|
487
|
+
return { stage, tarball: join(dist, tarballName(name, text(own["version"]))), findings: [], manifest };
|
|
488
|
+
}
|
|
489
|
+
/** The command line of a library's `tools/pack.ts`: pack, print what was found (or the tarball) and the exit status. */
|
|
490
|
+
export async function main(root, argv, options = {}) {
|
|
491
|
+
const r = await packLibrary(root, options);
|
|
492
|
+
for (const f of r.findings)
|
|
493
|
+
console.error(f);
|
|
494
|
+
if (r.tarball === undefined)
|
|
495
|
+
return 1;
|
|
496
|
+
console.log(argv.includes("--check") ? `packed ${r.tarball}: nothing wrong` : r.tarball);
|
|
497
|
+
return 0;
|
|
498
|
+
}
|
package/dist/pins.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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.
|
|
4
|
+
*
|
|
5
|
+
* Spec: 0001-libraries FR-006
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/** 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. */
|
|
10
|
+
export declare function bunTarget(os: string, arch: string): {
|
|
11
|
+
readonly target: string;
|
|
12
|
+
readonly key: string;
|
|
13
|
+
} | undefined;
|
|
14
|
+
/** The `KEY=value` lines of an env file; comments and blank lines left out. */
|
|
15
|
+
export declare function readEnvFile(text: string): ReadonlyMap<string, string>;
|
package/dist/pins.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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.
|
|
4
|
+
*
|
|
5
|
+
* Spec: 0001-libraries FR-006
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/** 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. */
|
|
10
|
+
export function bunTarget(os, arch) {
|
|
11
|
+
const table = {
|
|
12
|
+
"linux/x86_64": { target: "linux-x64", key: "X86_64" },
|
|
13
|
+
"linux/aarch64": { target: "linux-aarch64", key: "AARCH64" },
|
|
14
|
+
"darwin/x86_64": { target: "darwin-x64", key: "MACOS_X64" },
|
|
15
|
+
"darwin/aarch64": { target: "darwin-aarch64", key: "MACOS_ARM64" },
|
|
16
|
+
"windows/x86_64": { target: "windows-x64", key: "WINDOWS_X64" },
|
|
17
|
+
};
|
|
18
|
+
return Object.hasOwn(table, `${os}/${arch}`) ? table[`${os}/${arch}`] : undefined;
|
|
19
|
+
}
|
|
20
|
+
/** The `KEY=value` lines of an env file; comments and blank lines left out. */
|
|
21
|
+
export function readEnvFile(text) {
|
|
22
|
+
const out = new Map();
|
|
23
|
+
for (const line of text.split(/\r?\n/)) {
|
|
24
|
+
const m = /^([A-Z0-9_]+)=(.*)$/.exec(line);
|
|
25
|
+
if (m?.[1] !== undefined && m[2] !== undefined)
|
|
26
|
+
out.set(m[1], m[2]);
|
|
27
|
+
}
|
|
28
|
+
return out;
|
|
29
|
+
}
|