@glutinum/cli 0.14.1 → 1.0.0-beta.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 (39) hide show
  1. package/dist/Glutinum.Converter/FsharpAST.js +38 -4
  2. package/dist/Glutinum.Converter/Generate.js +15 -7
  3. package/dist/Glutinum.Converter/GlueAST.js +45 -36
  4. package/dist/Glutinum.Converter/Hosting.js +918 -0
  5. package/dist/Glutinum.Converter/Merge.js +218 -82
  6. package/dist/Glutinum.Converter/Packages.js +44 -8
  7. package/dist/Glutinum.Converter/Printer.js +77 -49
  8. package/dist/Glutinum.Converter/Read.js +8 -8
  9. package/dist/Glutinum.Converter/Reader/ClassDeclaration.js +7 -7
  10. package/dist/Glutinum.Converter/Reader/Declaration.js +26 -26
  11. package/dist/Glutinum.Converter/Reader/Documentation.js +37 -36
  12. package/dist/Glutinum.Converter/Reader/ExportAssignment.js +5 -5
  13. package/dist/Glutinum.Converter/Reader/ExportDeclaration.js +31 -19
  14. package/dist/Glutinum.Converter/Reader/FunctionDeclaration.js +6 -7
  15. package/dist/Glutinum.Converter/Reader/IndexedAccessType.js +36 -29
  16. package/dist/Glutinum.Converter/Reader/InterfaceDeclaration.js +3 -3
  17. package/dist/Glutinum.Converter/Reader/MappedTypeNode.js +82 -4
  18. package/dist/Glutinum.Converter/Reader/ModuleDeclaration.js +3 -3
  19. package/dist/Glutinum.Converter/Reader/Node.js +20 -20
  20. package/dist/Glutinum.Converter/Reader/Parameters.js +7 -7
  21. package/dist/Glutinum.Converter/Reader/TypeAliasDeclaration.js +5 -5
  22. package/dist/Glutinum.Converter/Reader/TypeNode.js +291 -232
  23. package/dist/Glutinum.Converter/Reader/TypeOperatorNode.js +8 -8
  24. package/dist/Glutinum.Converter/Reader/TypeQueryNode.js +108 -40
  25. package/dist/Glutinum.Converter/Reader/TypeScriptReader.js +2 -2
  26. package/dist/Glutinum.Converter/Reader/Types.js +6 -6
  27. package/dist/Glutinum.Converter/Reader/UnionTypeNode.js +7 -6
  28. package/dist/Glutinum.Converter/Reader/Utils.js +106 -56
  29. package/dist/Glutinum.Converter/Transform.js +2057 -700
  30. package/dist/Glutinum.Converter/TypeScript.Extensions.js +194 -202
  31. package/dist/Glutinum.Converter/Utils/Naming.js +7 -0
  32. package/dist/Glutinum.Converter/Utils/String.js +2 -2
  33. package/dist/Program.js +38 -26
  34. package/package.json +2 -2
  35. package/dist/Glutinum.Converter/TypeScript.js +0 -12
  36. package/src/Glutinum.Converter/js/bootstrap.js +0 -123
  37. package/src/Glutinum.Converter/js/host.js +0 -144
  38. package/src/Glutinum.Converter/js/npm.js +0 -137
  39. package/src/Glutinum.Converter/js/resolve.js +0 -402
@@ -1,402 +0,0 @@
1
- import { ts } from "@ts-morph/bootstrap";
2
-
3
- const DECLARATION_FILE = /\.d\.[cm]?ts$/;
4
-
5
- /**
6
- * Directory of the nearest `node_modules` folder, walking up from `cwd`.
7
- *
8
- * @param {import("./host.js").Host} host
9
- * @param {string} cwd
10
- * @returns {string | null}
11
- */
12
- export function findNodeModules(host, cwd) {
13
- const { path, fs } = host;
14
- let dir = path.resolve(cwd);
15
-
16
- while (true) {
17
- const candidate = path.join(dir, "node_modules");
18
-
19
- if (fs.directoryExists(candidate)) {
20
- return candidate;
21
- }
22
-
23
- const parent = path.dirname(dir);
24
-
25
- if (parent === dir) {
26
- return null;
27
- }
28
-
29
- dir = parent;
30
- }
31
- }
32
-
33
- /**
34
- * Real path of a directory: pnpm links `node_modules/<pkg>` into its store, the
35
- * dependencies of a package are only reachable from the real location.
36
- *
37
- * @param {string} dir
38
- * @returns {string}
39
- */
40
- function realDir(host, dir) {
41
- return host.fs.realPath(dir);
42
- }
43
-
44
- /**
45
- * Directory of the package that owns `file`: the closest parent directory containing a `package.json`.
46
- *
47
- * @param {import("./host.js").Host} host
48
- * @param {string} file
49
- * @returns {string | null}
50
- */
51
- export function findPackageDir(host, file) {
52
- const { path, fs } = host;
53
- let dir = path.dirname(path.resolve(file));
54
-
55
- while (true) {
56
- const packageJsonPath = path.join(dir, "package.json");
57
-
58
- // `dist/esm/package.json` files only holding `{ "type": "module" }` do not delimit a package
59
- if (fs.fileExists(packageJsonPath) && JSON.parse(fs.readFile(packageJsonPath)).name !== undefined) {
60
- return realDir(host, dir);
61
- }
62
-
63
- const parent = path.dirname(dir);
64
-
65
- if (parent === dir) {
66
- return null;
67
- }
68
-
69
- dir = parent;
70
- }
71
- }
72
-
73
- /**
74
- * The `types` entries found in a package.json `exports` map, keyed by subpath.
75
- *
76
- * @param {unknown} exportsField
77
- * @returns {{ subpath: string, file: string }[]}
78
- */
79
- function collectExportsTypes(exportsField) {
80
- const result = [];
81
-
82
- const visit = (subpath, node) => {
83
- if (node === null || node === undefined) {
84
- return;
85
- }
86
-
87
- if (typeof node === "string") {
88
- if (DECLARATION_FILE.test(node)) {
89
- result.push({ subpath, file: node });
90
- }
91
-
92
- return;
93
- }
94
-
95
- if (typeof node === "object") {
96
- for (const [key, value] of Object.entries(node)) {
97
- if (key.startsWith(".")) {
98
- visit(key, value);
99
- } else if (key === "types" || key === "typings") {
100
- visit(subpath, value);
101
- } else {
102
- // Conditions such as `import`, `default`, `node`, ...
103
- visit(subpath, value);
104
- }
105
- }
106
- }
107
- };
108
-
109
- if (typeof exportsField === "string" || (exportsField && !Object.keys(exportsField).some((key) => key.startsWith(".")))) {
110
- visit(".", exportsField);
111
- } else {
112
- visit(".", exportsField);
113
- }
114
-
115
- // Keep the first declaration file found for a subpath
116
- const bySubpath = new Map();
117
-
118
- for (const entry of result) {
119
- if (!bySubpath.has(entry.subpath)) {
120
- bySubpath.set(entry.subpath, entry.file);
121
- }
122
- }
123
-
124
- return [...bySubpath.entries()].map(([subpath, file]) => ({ subpath, file }));
125
- }
126
-
127
-
128
- /**
129
- * `[major, minor, patch]` of a version, missing parts are 0
130
- *
131
- * @param {string} version
132
- */
133
- function parseVersion(version) {
134
- const parts = version.split(".").map((part) => parseInt(part, 10));
135
-
136
- return [parts[0] || 0, parts[1] || 0, parts[2] || 0];
137
- }
138
-
139
- /**
140
- * @param {number[]} a
141
- * @param {number[]} b
142
- */
143
- function compareVersions(a, b) {
144
- for (let i = 0; i < 3; i++) {
145
- if (a[i] !== b[i]) {
146
- return a[i] - b[i];
147
- }
148
- }
149
-
150
- return 0;
151
- }
152
-
153
- /**
154
- * Whether `version` satisfies one comparator of a `typesVersions` range (`<=5.5`, `>=4.2`, `*`)
155
- *
156
- * @param {number[]} version
157
- * @param {string} comparator
158
- */
159
- function satisfiesComparator(version, comparator) {
160
- const match = comparator.match(/^(>=|<=|>|<|=|\^|~)?\s*(.+)$/);
161
-
162
- if (match === null) {
163
- return false;
164
- }
165
-
166
- const operator = match[1] ?? "=";
167
- const text = match[2];
168
-
169
- if (text === "*" || text === "x") {
170
- return true;
171
- }
172
-
173
- const parts = text.split(".").map((part) => parseInt(part, 10)).filter((part) => !isNaN(part));
174
- const lower = [parts[0] ?? 0, parts[1] ?? 0, parts[2] ?? 0];
175
-
176
- // `5.5` stands for every `5.5.x`, the bound after it is `5.6.0`
177
- const upper =
178
- parts.length === 1 ? [lower[0] + 1, 0, 0]
179
- : parts.length === 2 ? [lower[0], lower[1] + 1, 0]
180
- : [lower[0], lower[1], lower[2] + 1];
181
-
182
- switch (operator) {
183
- case ">=":
184
- return compareVersions(version, lower) >= 0;
185
- case ">":
186
- return compareVersions(version, upper) >= 0;
187
- case "<":
188
- return compareVersions(version, lower) < 0;
189
- case "<=":
190
- return compareVersions(version, upper) < 0;
191
- case "^":
192
- return compareVersions(version, lower) >= 0 && compareVersions(version, [lower[0] + 1, 0, 0]) < 0;
193
- case "~":
194
- return compareVersions(version, lower) >= 0 && compareVersions(version, [lower[0], lower[1] + 1, 0]) < 0;
195
- default:
196
- return compareVersions(version, lower) >= 0 && compareVersions(version, upper) < 0;
197
- }
198
- }
199
-
200
- /**
201
- * @param {string} version
202
- * @param {string} range
203
- */
204
- function satisfiesRange(version, range) {
205
- const parsed = parseVersion(version);
206
-
207
- return range
208
- .split("||")
209
- .some((alternative) => alternative.trim().split(/\s+/).every((comparator) => satisfiesComparator(parsed, comparator)));
210
- }
211
-
212
- /**
213
- * The declaration file to use for the bundled TypeScript, following the `typesVersions` of the
214
- * package (`{ "<=5.5": { "*": ["ts5.5/*"] } }`).
215
- *
216
- * @param {Record<string, Record<string, string[]>> | undefined} typesVersions
217
- * @param {string} file
218
- */
219
- function applyTypesVersions(typesVersions, file) {
220
- if (typesVersions === undefined || typesVersions === null) {
221
- return file;
222
- }
223
-
224
- const paths = Object.entries(typesVersions).find(([range]) => satisfiesRange(ts.version, range))?.[1];
225
-
226
- if (paths === undefined) {
227
- return file;
228
- }
229
-
230
- const normalized = file.replace(/^\.\//, "");
231
-
232
- for (const [pattern, targets] of Object.entries(paths)) {
233
- const [prefix, suffix = ""] = pattern.split("*");
234
-
235
- if (normalized.startsWith(prefix) && normalized.endsWith(suffix) && targets.length > 0) {
236
- const captured = normalized.slice(prefix.length, normalized.length - suffix.length);
237
-
238
- return targets[0].replace("*", captured);
239
- }
240
- }
241
-
242
- return file;
243
- }
244
-
245
- /**
246
- * Describe an installed package: its runtime name and declaration entry points.
247
- *
248
- * @param {import("./host.js").Host} host
249
- * @param {string} packageDir
250
- * @returns {{ name: string, runtimeName: string, dir: string, typesRoot: string, entryFile: string, subpathEntries: { subpath: string, file: string }[] } | null}
251
- */
252
- export function describePackage(host, packageDir) {
253
- const { path, fs } = host;
254
- const packageJsonPath = path.join(packageDir, "package.json");
255
-
256
- if (!fs.fileExists(packageJsonPath)) {
257
- return null;
258
- }
259
-
260
- const pkg = JSON.parse(fs.readFile(packageJsonPath));
261
- const name = pkg.name ?? path.basename(packageDir);
262
-
263
- // `@types/foo` describes the `foo` package, `@types/scope__foo` the `@scope/foo` one
264
- let runtimeName = name;
265
-
266
- if (name.startsWith("@types/")) {
267
- const typed = name.slice("@types/".length);
268
- runtimeName = typed.includes("__") ? "@" + typed.replace("__", "/") : typed;
269
- }
270
-
271
- const candidates = [];
272
- const typesField = pkg.types ?? pkg.typings;
273
-
274
- if (typeof typesField === "string") {
275
- candidates.push({ subpath: ".", file: typesField });
276
- }
277
-
278
- candidates.push(...collectExportsTypes(pkg.exports));
279
-
280
- if (typeof pkg.main === "string") {
281
- candidates.push({ subpath: ".", file: pkg.main.replace(/\.[cm]?js$/, ".d.ts") });
282
- }
283
-
284
- candidates.push({ subpath: ".", file: "index.d.ts" });
285
-
286
- const entries = [];
287
-
288
- for (const candidate of candidates) {
289
- const mappedFile = applyTypesVersions(pkg.typesVersions, candidate.file);
290
- let file = path.resolve(packageDir, mappedFile);
291
-
292
- // `"types": "./lib/umd/main"` is allowed without an extension
293
- if (!DECLARATION_FILE.test(file) && fs.fileExists(file + ".d.ts")) {
294
- file = file + ".d.ts";
295
- }
296
-
297
- if (!fs.fileExists(file) || !DECLARATION_FILE.test(file)) {
298
- continue;
299
- }
300
-
301
- if (!entries.some((entry) => entry.subpath === candidate.subpath || entry.file === file)) {
302
- entries.push({ subpath: candidate.subpath, file, isMapped: mappedFile !== candidate.file });
303
- }
304
- }
305
-
306
- const main = entries.find((entry) => entry.subpath === ".");
307
-
308
- if (main === undefined) {
309
- return null;
310
- }
311
-
312
- return {
313
- name,
314
- runtimeName,
315
- dir: packageDir,
316
- // The files of a `typesVersions` folder are named as if they were at the root
317
- typesRoot: main.isMapped ? path.dirname(main.file) : packageDir,
318
- entryFile: main.file,
319
- subpathEntries: entries
320
- .filter((entry) => entry !== main)
321
- .map((entry) => ({ subpath: entry.subpath.replace(/^\.\//, ""), file: entry.file })),
322
- };
323
- }
324
-
325
- /**
326
- * Resolve the CLI input to a package directory or a declaration file.
327
- *
328
- * @param {import("./host.js").Host} host
329
- * @param {string} input package name, package directory or `.d.ts` path
330
- * @returns {{ kind: "file", file: string } | { kind: "package", packageDir: string }}
331
- */
332
- export function resolveInput(host, input) {
333
- const { path, fs } = host;
334
- const asPath = path.resolve(host.cwd, input);
335
-
336
- if (DECLARATION_FILE.test(input) && fs.fileExists(asPath)) {
337
- return { kind: "file", file: asPath };
338
- }
339
-
340
- if (fs.directoryExists(asPath)) {
341
- return { kind: "package", packageDir: realDir(host, asPath) };
342
- }
343
-
344
- const nodeModules = findNodeModules(host, host.cwd);
345
-
346
- if (nodeModules !== null) {
347
- // `date-fns/locale` is a subpath of `date-fns`, generated with the package
348
- const segments = input.split("/");
349
- const packageName = input.startsWith("@") ? segments.slice(0, 2).join("/") : segments[0];
350
-
351
- // A package without declaration files (`ws`) is described by its `@types` package
352
- for (const candidate of [packageName, path.join("@types", packageName.replace(/^@/, "").replace("/", "__"))]) {
353
- const packageDir = realDir(host, path.join(nodeModules, candidate));
354
-
355
- if (fs.fileExists(path.join(packageDir, "package.json")) && describePackage(host, packageDir) !== null) {
356
- return { kind: "package", packageDir };
357
- }
358
- }
359
- }
360
-
361
- throw new Error(
362
- `Could not find '${input}': it is neither a declaration file, a package directory, nor a package installed in node_modules.`
363
- );
364
- }
365
-
366
- /**
367
- * Every package installed in the nearest `node_modules` that ships declaration files.
368
- *
369
- * @param {import("./host.js").Host} host
370
- * @returns {string[]} package directories
371
- */
372
- export function listInstalledPackages(host) {
373
- const { path, fs } = host;
374
- const nodeModules = findNodeModules(host, host.cwd);
375
-
376
- if (nodeModules === null) {
377
- return [];
378
- }
379
-
380
- const result = [];
381
-
382
- for (const entry of fs.readDirectory(nodeModules)) {
383
- if (entry.startsWith(".")) {
384
- continue;
385
- }
386
-
387
- const entryPath = path.join(nodeModules, entry);
388
-
389
- if (entry.startsWith("@")) {
390
- for (const scoped of fs.readDirectory(entryPath)) {
391
- result.push(path.join(entryPath, scoped));
392
- }
393
- } else {
394
- result.push(entryPath);
395
- }
396
- }
397
-
398
- return result
399
- .filter((dir) => fs.fileExists(path.join(dir, "package.json")))
400
- .map((dir) => realDir(host, dir))
401
- .filter((dir) => describePackage(host, dir) !== null);
402
- }