@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.
- package/dist/Glutinum.Converter/FsharpAST.js +38 -4
- package/dist/Glutinum.Converter/Generate.js +15 -7
- package/dist/Glutinum.Converter/GlueAST.js +45 -36
- package/dist/Glutinum.Converter/Hosting.js +918 -0
- package/dist/Glutinum.Converter/Merge.js +218 -82
- package/dist/Glutinum.Converter/Packages.js +44 -8
- package/dist/Glutinum.Converter/Printer.js +77 -49
- package/dist/Glutinum.Converter/Read.js +8 -8
- package/dist/Glutinum.Converter/Reader/ClassDeclaration.js +7 -7
- package/dist/Glutinum.Converter/Reader/Declaration.js +26 -26
- package/dist/Glutinum.Converter/Reader/Documentation.js +37 -36
- package/dist/Glutinum.Converter/Reader/ExportAssignment.js +5 -5
- package/dist/Glutinum.Converter/Reader/ExportDeclaration.js +31 -19
- package/dist/Glutinum.Converter/Reader/FunctionDeclaration.js +6 -7
- package/dist/Glutinum.Converter/Reader/IndexedAccessType.js +36 -29
- package/dist/Glutinum.Converter/Reader/InterfaceDeclaration.js +3 -3
- package/dist/Glutinum.Converter/Reader/MappedTypeNode.js +82 -4
- package/dist/Glutinum.Converter/Reader/ModuleDeclaration.js +3 -3
- package/dist/Glutinum.Converter/Reader/Node.js +20 -20
- package/dist/Glutinum.Converter/Reader/Parameters.js +7 -7
- package/dist/Glutinum.Converter/Reader/TypeAliasDeclaration.js +5 -5
- package/dist/Glutinum.Converter/Reader/TypeNode.js +291 -232
- package/dist/Glutinum.Converter/Reader/TypeOperatorNode.js +8 -8
- package/dist/Glutinum.Converter/Reader/TypeQueryNode.js +108 -40
- package/dist/Glutinum.Converter/Reader/TypeScriptReader.js +2 -2
- package/dist/Glutinum.Converter/Reader/Types.js +6 -6
- package/dist/Glutinum.Converter/Reader/UnionTypeNode.js +7 -6
- package/dist/Glutinum.Converter/Reader/Utils.js +106 -56
- package/dist/Glutinum.Converter/Transform.js +2057 -700
- package/dist/Glutinum.Converter/TypeScript.Extensions.js +194 -202
- package/dist/Glutinum.Converter/Utils/Naming.js +7 -0
- package/dist/Glutinum.Converter/Utils/String.js +2 -2
- package/dist/Program.js +38 -26
- package/package.json +2 -2
- package/dist/Glutinum.Converter/TypeScript.js +0 -12
- package/src/Glutinum.Converter/js/bootstrap.js +0 -123
- package/src/Glutinum.Converter/js/host.js +0 -144
- package/src/Glutinum.Converter/js/npm.js +0 -137
- package/src/Glutinum.Converter/js/resolve.js +0 -402
package/dist/Program.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
import { success, info, error, log } from "./Glutinum.Converter/Log.js";
|
|
3
|
-
import {
|
|
4
|
-
import { length, exists, filter, contains, cons, singleton, append, reverse,
|
|
3
|
+
import { generateBindingFileWith, generatePackagesFromDisk } from "./Glutinum.Converter/Generate.js";
|
|
4
|
+
import { length, exists, filter, contains, cons, ofArray, singleton, append, reverse, tail as tail_5, head, isEmpty, empty } from "./fable_modules/fable-library-js.5.17.2/List.js";
|
|
5
|
+
import { defaultArg } from "./fable_modules/fable-library-js.5.17.2/Option.js";
|
|
6
|
+
import { join, concat, split } from "./fable_modules/fable-library-js.5.17.2/String.js";
|
|
5
7
|
import { item, equalsWith } from "./fable_modules/fable-library-js.5.17.2/Array.js";
|
|
6
8
|
import { stringHash, defaultOf } from "./fable_modules/fable-library-js.5.17.2/Util.js";
|
|
7
9
|
import { defaultOptions, GenerateOptions } from "./Glutinum.Converter/Packages.js";
|
|
8
|
-
import { join, concat } from "./fable_modules/fable-library-js.5.17.2/String.js";
|
|
9
10
|
import * as path from "path";
|
|
10
11
|
import * as fs from "fs";
|
|
11
12
|
|
|
@@ -56,17 +57,16 @@ function generate(options, inputs) {
|
|
|
56
57
|
case 0:
|
|
57
58
|
return generatePackagesFromDisk(options, empty());
|
|
58
59
|
case 1:
|
|
59
|
-
return
|
|
60
|
+
return generateBindingFileWith(defaultArg(options.ModuleName, "Glutinum"), input_1);
|
|
60
61
|
default:
|
|
61
62
|
return generatePackagesFromDisk(options, inputs_1);
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
|
|
65
|
-
|
|
66
|
-
const argv_1 = ofArray(argv);
|
|
66
|
+
function run(argv) {
|
|
67
67
|
let matchResult;
|
|
68
|
-
if (!isEmpty(
|
|
69
|
-
switch (head(
|
|
68
|
+
if (!isEmpty(argv)) {
|
|
69
|
+
switch (head(argv)) {
|
|
70
70
|
case "-h":
|
|
71
71
|
case "--help":
|
|
72
72
|
case "help": {
|
|
@@ -74,7 +74,7 @@ function generate(options, inputs) {
|
|
|
74
74
|
break;
|
|
75
75
|
}
|
|
76
76
|
case "--version": {
|
|
77
|
-
if (isEmpty(tail_5(
|
|
77
|
+
if (isEmpty(tail_5(argv))) {
|
|
78
78
|
matchResult = 1;
|
|
79
79
|
}
|
|
80
80
|
else {
|
|
@@ -100,7 +100,7 @@ function generate(options, inputs) {
|
|
|
100
100
|
}
|
|
101
101
|
default: {
|
|
102
102
|
let patternInput;
|
|
103
|
-
const matchValue = reverse(
|
|
103
|
+
const matchValue = reverse(argv);
|
|
104
104
|
let matchResult_1, inputs, outFile;
|
|
105
105
|
if (!isEmpty(matchValue)) {
|
|
106
106
|
if (!isEmpty(tail_5(matchValue))) {
|
|
@@ -126,7 +126,7 @@ function generate(options, inputs) {
|
|
|
126
126
|
break;
|
|
127
127
|
}
|
|
128
128
|
default:
|
|
129
|
-
patternInput = [
|
|
129
|
+
patternInput = [argv, undefined];
|
|
130
130
|
}
|
|
131
131
|
const outFile_1 = patternInput[1];
|
|
132
132
|
const takeOptions = (args_mut, options_mut, rest_mut) => {
|
|
@@ -194,7 +194,7 @@ function generate(options, inputs) {
|
|
|
194
194
|
switch (matchResult_2) {
|
|
195
195
|
case 0: {
|
|
196
196
|
args_mut = tail;
|
|
197
|
-
options_mut = (new GenerateOptions(options.ExternalPackages, append(options.Externals, singleton((matchValue_1 =
|
|
197
|
+
options_mut = (new GenerateOptions(options.ExternalPackages, append(options.Externals, singleton((matchValue_1 = split(value, ["="], undefined, 0), (!equalsWith((x, y) => (x === y), matchValue_1, defaultOf()) && (matchValue_1.length === 2)) ? [item(0, matchValue_1), item(1, matchValue_1)] : [value, undefined]))), options.ModuleName, options.Include, options.NoLib));
|
|
198
198
|
rest_mut = rest;
|
|
199
199
|
continue takeOptions;
|
|
200
200
|
}
|
|
@@ -212,7 +212,7 @@ function generate(options, inputs) {
|
|
|
212
212
|
}
|
|
213
213
|
case 3: {
|
|
214
214
|
args_mut = tail_3;
|
|
215
|
-
options_mut = (new GenerateOptions(options.ExternalPackages, options.Externals, options.ModuleName, append(options.Include, ofArray(
|
|
215
|
+
options_mut = (new GenerateOptions(options.ExternalPackages, options.Externals, options.ModuleName, append(options.Include, ofArray(split(value_2, [","], undefined, 0))), options.NoLib));
|
|
216
216
|
rest_mut = rest;
|
|
217
217
|
continue takeOptions;
|
|
218
218
|
}
|
|
@@ -256,23 +256,35 @@ function generate(options, inputs) {
|
|
|
256
256
|
}
|
|
257
257
|
else {
|
|
258
258
|
info(concat("Generating binding file for ", join(", ", inputs_3)));
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
259
|
+
try {
|
|
260
|
+
const res = generate(options_2, inputs_3);
|
|
261
|
+
if (outFile_1 == null) {
|
|
262
|
+
process.stdout.write(res);
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
const outFile_2 = outFile_1;
|
|
266
|
+
const outFileDir = path.dirname(outFile_2);
|
|
267
|
+
fs.mkdirSync(outFileDir, {
|
|
268
|
+
recursive: true,
|
|
269
|
+
});
|
|
270
|
+
fs.writeFileSync(outFile_2, res);
|
|
271
|
+
info(concat("Bindings written to: ", path.join(process.cwd(), outFile_2)));
|
|
272
|
+
}
|
|
273
|
+
success("Success!");
|
|
274
|
+
return 0;
|
|
262
275
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
fs.mkdirSync(outFileDir, {
|
|
267
|
-
recursive: true,
|
|
268
|
-
});
|
|
269
|
-
fs.writeFileSync(outFile_2, res);
|
|
270
|
-
info(concat("Bindings written to: ", path.join(process.cwd(), outFile_2)));
|
|
276
|
+
catch (ex) {
|
|
277
|
+
error(ex.message);
|
|
278
|
+
return 1;
|
|
271
279
|
}
|
|
272
|
-
success("Success!");
|
|
273
|
-
return 0;
|
|
274
280
|
}
|
|
275
281
|
}
|
|
276
282
|
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
(function (argv) {
|
|
286
|
+
const exitCode = run(ofArray(argv)) | 0;
|
|
287
|
+
process.exitCode = exitCode;
|
|
288
|
+
return exitCode | 0;
|
|
277
289
|
})(typeof process === 'object' ? process.argv.slice(2) : []);
|
|
278
290
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glutinum/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
4
|
"description": "TypeScript definition to F# bindings converter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"vitest": "^1.5.0"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@ts-morph/bootstrap": "^0.
|
|
54
|
+
"@ts-morph/bootstrap": "^0.29.0",
|
|
55
55
|
"chalk": "^5.3.0"
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { class_type } from "../fable_modules/fable-library-js.5.17.2/Reflection.js";
|
|
3
|
-
|
|
4
|
-
export class Ts_NoisTypeNodede {
|
|
5
|
-
constructor() {
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function Ts_NoisTypeNodede_$reflection() {
|
|
10
|
-
return class_type("TypeScript.Ts.NoisTypeNodede", undefined, Ts_NoisTypeNodede);
|
|
11
|
-
}
|
|
12
|
-
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { createProjectSync, ts } from "@ts-morph/bootstrap";
|
|
2
|
-
import { describePackage, findPackageDir } from "./resolve.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
* @param {string} filePath
|
|
7
|
-
* @param {string} source
|
|
8
|
-
* @returns
|
|
9
|
-
*/
|
|
10
|
-
export default function createProgramForCLI(filePath, source) {
|
|
11
|
-
|
|
12
|
-
// `strict`, so that `T | undefined` is kept by the checker
|
|
13
|
-
const project = createProjectSync({ compilerOptions: { strict: true } })
|
|
14
|
-
|
|
15
|
-
project.createSourceFile(filePath, source)
|
|
16
|
-
|
|
17
|
-
return project.createProgram()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Create a program from declaration files of the host, following their imports.
|
|
22
|
-
*
|
|
23
|
-
* @param {import("./host.js").Host} host
|
|
24
|
-
* @param {string[]} entryFiles
|
|
25
|
-
* @param {{ withoutDomLib?: boolean, noLib?: boolean }} options
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
28
|
-
export function createProgramFromFiles(host, entryFiles, options = {}) {
|
|
29
|
-
// ESNext, so that lib types such as `AsyncIterable` resolve instead of being left undefined.
|
|
30
|
-
// `types: []` stops TypeScript from loading every package under `node_modules/@types`.
|
|
31
|
-
// ESNext module + Bundler resolution: follows `exports` maps and node_modules
|
|
32
|
-
const compilerOptions = { target: 99, module: 99, moduleResolution: 100, types: [], strict: true }
|
|
33
|
-
|
|
34
|
-
// A package replacing the DOM lib (`@types/web`) redeclares its globals
|
|
35
|
-
if (options.withoutDomLib) {
|
|
36
|
-
compilerOptions.lib = ["lib.esnext.d.ts", "lib.decorators.d.ts", "lib.decorators.legacy.d.ts"]
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// A package made of the ES library files declares the library itself
|
|
40
|
-
if (options.noLib) {
|
|
41
|
-
compilerOptions.noLib = true
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const project = host.createProject(compilerOptions)
|
|
45
|
-
|
|
46
|
-
for (const entryFile of entryFiles) {
|
|
47
|
-
project.addSourceFileAtPathSync(entryFile)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
project.resolveSourceFileDependencies()
|
|
51
|
-
|
|
52
|
-
return project.createProgram()
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The files reachable from the entry files through imports and references, without
|
|
57
|
-
* going through a package of `excludedRuntimeNames` (`node` for `@types/node`).
|
|
58
|
-
*
|
|
59
|
-
* @param {import("./host.js").Host} host
|
|
60
|
-
* @param {import("typescript").Program} program
|
|
61
|
-
* @param {string[]} entryFiles
|
|
62
|
-
* @param {string[]} excludedRuntimeNames
|
|
63
|
-
* @returns {string[]}
|
|
64
|
-
*/
|
|
65
|
-
export function reachableFiles(host, program, entryFiles, excludedRuntimeNames) {
|
|
66
|
-
const runtimeNames = new Map();
|
|
67
|
-
|
|
68
|
-
const isExcluded = (fileName) => {
|
|
69
|
-
const dir = findPackageDir(host, fileName);
|
|
70
|
-
|
|
71
|
-
if (dir === null) {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (!runtimeNames.has(dir)) {
|
|
76
|
-
const description = describePackage(host, dir);
|
|
77
|
-
runtimeNames.set(dir, description === null ? null : description.runtimeName);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return excludedRuntimeNames.includes(runtimeNames.get(dir));
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const seen = new Set();
|
|
84
|
-
const queue = [...entryFiles];
|
|
85
|
-
|
|
86
|
-
const visit = (fileName) => {
|
|
87
|
-
if (fileName !== undefined && !seen.has(fileName) && !isExcluded(fileName)) {
|
|
88
|
-
queue.push(fileName);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
while (queue.length > 0) {
|
|
93
|
-
const fileName = queue.pop();
|
|
94
|
-
|
|
95
|
-
if (seen.has(fileName)) {
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const sourceFile = program.getSourceFile(fileName);
|
|
100
|
-
|
|
101
|
-
if (sourceFile === undefined) {
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
seen.add(fileName);
|
|
106
|
-
|
|
107
|
-
for (const usage of sourceFile.imports ?? []) {
|
|
108
|
-
const mode = ts.getModeForUsageLocation(sourceFile, usage);
|
|
109
|
-
visit(program.getResolvedModule(sourceFile, usage.text, mode)?.resolvedModule?.resolvedFileName);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
for (const reference of sourceFile.referencedFiles ?? []) {
|
|
113
|
-
visit(host.path.resolve(host.path.dirname(sourceFile.fileName), reference.fileName));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
for (const reference of sourceFile.typeReferenceDirectives ?? []) {
|
|
117
|
-
const resolved = program.getResolvedTypeReferenceDirective(sourceFile, reference.fileName, reference.resolutionMode);
|
|
118
|
-
visit(resolved?.resolvedTypeReferenceDirective?.resolvedFileName);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return [...seen];
|
|
123
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { createProjectSync, InMemoryFileSystemHost } from "@ts-morph/bootstrap";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @typedef {object} Host
|
|
5
|
-
* @property {string} cwd
|
|
6
|
-
* @property {{ join: (...parts: string[]) => string, dirname: (p: string) => string, basename: (p: string) => string, resolve: (...parts: string[]) => string }} path
|
|
7
|
-
* @property {{ fileExists: (p: string) => boolean, directoryExists: (p: string) => boolean, readFile: (p: string) => string, readDirectory: (p: string) => string[], realPath: (p: string) => string }} fs
|
|
8
|
-
* @property {(compilerOptions: object) => import("@ts-morph/bootstrap").Project} createProject
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* `/` separated paths, the only ones of the in-memory file system.
|
|
13
|
-
*/
|
|
14
|
-
export const posixPath = {
|
|
15
|
-
normalize(p) {
|
|
16
|
-
const absolute = p.startsWith("/");
|
|
17
|
-
const segments = [];
|
|
18
|
-
|
|
19
|
-
for (const segment of p.split("/")) {
|
|
20
|
-
if (segment === "" || segment === ".") {
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (segment === "..") {
|
|
25
|
-
if (segments.length > 0 && segments[segments.length - 1] !== "..") {
|
|
26
|
-
segments.pop();
|
|
27
|
-
} else if (!absolute) {
|
|
28
|
-
segments.push("..");
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
segments.push(segment);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const joined = segments.join("/");
|
|
38
|
-
|
|
39
|
-
return absolute ? "/" + joined : joined === "" ? "." : joined;
|
|
40
|
-
},
|
|
41
|
-
|
|
42
|
-
join(...parts) {
|
|
43
|
-
return posixPath.normalize(parts.filter((part) => part !== "").join("/"));
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
dirname(p) {
|
|
47
|
-
const normalized = posixPath.normalize(p);
|
|
48
|
-
const index = normalized.lastIndexOf("/");
|
|
49
|
-
|
|
50
|
-
if (index === -1) {
|
|
51
|
-
return ".";
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return index === 0 ? "/" : normalized.slice(0, index);
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
basename(p) {
|
|
58
|
-
const normalized = posixPath.normalize(p);
|
|
59
|
-
|
|
60
|
-
return normalized.slice(normalized.lastIndexOf("/") + 1);
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
resolve(...parts) {
|
|
64
|
-
let result = "/";
|
|
65
|
-
|
|
66
|
-
for (const part of parts) {
|
|
67
|
-
result = part.startsWith("/") ? part : result + "/" + part;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return posixPath.normalize(result);
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* A host over a ts-morph file system, the in-memory one in the browser.
|
|
76
|
-
*
|
|
77
|
-
* @param {import("@ts-morph/bootstrap").FileSystemHost} fileSystem
|
|
78
|
-
* @param {string} cwd
|
|
79
|
-
* @returns {Host}
|
|
80
|
-
*/
|
|
81
|
-
export function createFileSystemHost(fileSystem, cwd = "/") {
|
|
82
|
-
return {
|
|
83
|
-
cwd,
|
|
84
|
-
fileSystem,
|
|
85
|
-
path: posixPath,
|
|
86
|
-
fs: {
|
|
87
|
-
fileExists: (p) => fileSystem.fileExistsSync(p),
|
|
88
|
-
directoryExists: (p) => fileSystem.directoryExistsSync(p),
|
|
89
|
-
readFile: (p) => fileSystem.readFileSync(p, "utf8"),
|
|
90
|
-
readDirectory: (p) => fileSystem.readDirSync(p).map((entry) => entry.name),
|
|
91
|
-
realPath: (p) => {
|
|
92
|
-
try {
|
|
93
|
-
return fileSystem.realpathSync(p);
|
|
94
|
-
} catch {
|
|
95
|
-
return p;
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
createProject: (compilerOptions) =>
|
|
100
|
-
createProjectSync({ fileSystem, compilerOptions, skipAddingFilesFromTsConfig: true }),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* @param {string} cwd
|
|
106
|
-
* @returns {Host}
|
|
107
|
-
*/
|
|
108
|
-
export function createInMemoryHost(cwd = "/") {
|
|
109
|
-
return createFileSystemHost(new InMemoryFileSystemHost(), cwd);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* The host of the CLI, over the disk.
|
|
114
|
-
*
|
|
115
|
-
* @param {typeof import("node:fs")} fs
|
|
116
|
-
* @param {typeof import("node:path")} path
|
|
117
|
-
* @param {string} cwd
|
|
118
|
-
* @returns {Host}
|
|
119
|
-
*/
|
|
120
|
-
export function createNodeHost(fs, path, cwd) {
|
|
121
|
-
return {
|
|
122
|
-
cwd,
|
|
123
|
-
path: {
|
|
124
|
-
join: (...parts) => path.join(...parts),
|
|
125
|
-
dirname: (p) => path.dirname(p),
|
|
126
|
-
basename: (p) => path.basename(p),
|
|
127
|
-
resolve: (...parts) => path.resolve(...parts),
|
|
128
|
-
},
|
|
129
|
-
fs: {
|
|
130
|
-
fileExists: (p) => fs.existsSync(p) && fs.statSync(p).isFile(),
|
|
131
|
-
directoryExists: (p) => fs.existsSync(p) && fs.statSync(p).isDirectory(),
|
|
132
|
-
readFile: (p) => fs.readFileSync(p, "utf8"),
|
|
133
|
-
readDirectory: (p) => fs.readdirSync(p),
|
|
134
|
-
realPath: (p) => {
|
|
135
|
-
try {
|
|
136
|
-
return fs.realpathSync(p);
|
|
137
|
-
} catch {
|
|
138
|
-
return p;
|
|
139
|
-
}
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
createProject: (compilerOptions) => createProjectSync({ compilerOptions, skipAddingFilesFromTsConfig: true }),
|
|
143
|
-
};
|
|
144
|
-
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
const DECLARATION_FILE = /\.d\.[cm]?ts$/;
|
|
2
|
-
const REGISTRY = "https://data.jsdelivr.com/v1/package";
|
|
3
|
-
const CDN = "https://cdn.jsdelivr.net/npm";
|
|
4
|
-
const CONCURRENCY = 8;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* `@types/foo` for `foo`, `@types/scope__foo` for `@scope/foo`
|
|
8
|
-
*
|
|
9
|
-
* @param {string} name
|
|
10
|
-
*/
|
|
11
|
-
export function typesPackageName(name) {
|
|
12
|
-
return "@types/" + name.replace(/^@/, "").replace("/", "__");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* `chalk`, `chalk@5`, `@scope/name@^1.2`
|
|
17
|
-
*
|
|
18
|
-
* @param {string} spec
|
|
19
|
-
* @returns {{ name: string, range: string }}
|
|
20
|
-
*/
|
|
21
|
-
export function parsePackageSpec(spec) {
|
|
22
|
-
const at = spec.indexOf("@", 1);
|
|
23
|
-
|
|
24
|
-
if (at === -1) {
|
|
25
|
-
return { name: spec, range: "latest" };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return { name: spec.slice(0, at), range: spec.slice(at + 1) || "latest" };
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Download the declaration files of a package and of the packages it depends on into
|
|
33
|
-
* `/node_modules` of the file system, like `npm install` would, from jsDelivr.
|
|
34
|
-
*
|
|
35
|
-
* @param {import("@ts-morph/bootstrap").FileSystemHost} fileSystem
|
|
36
|
-
* @param {string} spec package name, with an optional version range
|
|
37
|
-
* @param {{ fetch?: typeof fetch, onProgress?: (message: string) => void, skip?: string[] }} options
|
|
38
|
-
* @returns {Promise<{ name: string, version: string }>} the package installed for `spec`
|
|
39
|
-
*/
|
|
40
|
-
export async function installPackage(fileSystem, spec, options = {}) {
|
|
41
|
-
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
42
|
-
const onProgress = options.onProgress ?? (() => {});
|
|
43
|
-
// `@types/node` describes the runtime, it is not needed to generate a package
|
|
44
|
-
const skip = new Set(options.skip ?? ["@types/node"]);
|
|
45
|
-
const installed = new Map();
|
|
46
|
-
|
|
47
|
-
const getJson = async (url) => {
|
|
48
|
-
const response = await fetchImpl(url);
|
|
49
|
-
|
|
50
|
-
if (!response.ok) {
|
|
51
|
-
throw new Error(`${url}: ${response.status}`);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return await response.json();
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const getText = async (url) => {
|
|
58
|
-
const response = await fetchImpl(url);
|
|
59
|
-
|
|
60
|
-
if (!response.ok) {
|
|
61
|
-
throw new Error(`${url}: ${response.status}`);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return await response.text();
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const install = async (name, range) => {
|
|
68
|
-
if (installed.has(name) || skip.has(name)) {
|
|
69
|
-
return installed.get(name) ?? null;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const resolveResponse = await fetchImpl(`${REGISTRY}/resolve/npm/${name}@${encodeURIComponent(range)}`);
|
|
73
|
-
|
|
74
|
-
if (resolveResponse.status === 404) {
|
|
75
|
-
throw new Error(`No package named '${name}' on npm`);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (!resolveResponse.ok) {
|
|
79
|
-
throw new Error(`${resolveResponse.url}: ${resolveResponse.status}`);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const { version } = await resolveResponse.json();
|
|
83
|
-
|
|
84
|
-
if (version === null || version === undefined) {
|
|
85
|
-
throw new Error(`No version of '${name}' matches '${range}'`);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
installed.set(name, { name, version });
|
|
89
|
-
onProgress(`${name}@${version}: listing files`);
|
|
90
|
-
|
|
91
|
-
const { files } = await getJson(`${REGISTRY}/npm/${name}@${version}/flat`);
|
|
92
|
-
|
|
93
|
-
const wanted = files
|
|
94
|
-
.map((file) => file.name)
|
|
95
|
-
.filter((file) => DECLARATION_FILE.test(file) || file.endsWith("/package.json"));
|
|
96
|
-
|
|
97
|
-
let done = 0;
|
|
98
|
-
|
|
99
|
-
const worker = async () => {
|
|
100
|
-
while (wanted.length > 0) {
|
|
101
|
-
const file = wanted.shift();
|
|
102
|
-
const content = await getText(`${CDN}/${name}@${version}${file}`);
|
|
103
|
-
|
|
104
|
-
fileSystem.writeFileSync(`/node_modules/${name}${file}`, content);
|
|
105
|
-
done++;
|
|
106
|
-
onProgress(`${name}@${version}: ${done} files`);
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
await Promise.all(Array.from({ length: CONCURRENCY }, worker));
|
|
111
|
-
|
|
112
|
-
const packageJson = JSON.parse(fileSystem.readFileSync(`/node_modules/${name}/package.json`, "utf8"));
|
|
113
|
-
|
|
114
|
-
const hasDeclarations = files.some((file) => DECLARATION_FILE.test(file.name));
|
|
115
|
-
|
|
116
|
-
// A package without declaration files (`ws`) is described by its `@types` package
|
|
117
|
-
if (!hasDeclarations && !name.startsWith("@types/")) {
|
|
118
|
-
try {
|
|
119
|
-
await install(typesPackageName(name), "latest");
|
|
120
|
-
} catch {
|
|
121
|
-
// No `@types` package either, the package stays untyped
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
for (const [dependency, dependencyRange] of Object.entries(packageJson.dependencies ?? {})) {
|
|
126
|
-
await install(dependency, dependencyRange);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return installed.get(name);
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
const { name, range } = parsePackageSpec(spec);
|
|
133
|
-
const result = await install(name, range);
|
|
134
|
-
|
|
135
|
-
// `ws` itself has no declaration file: the package to generate is its `@types` package
|
|
136
|
-
return result;
|
|
137
|
-
}
|