@glutinum/cli 0.14.1 → 1.0.0-beta.2

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 +19 -11
  3. package/dist/Glutinum.Converter/GlueAST.js +45 -36
  4. package/dist/Glutinum.Converter/Hosting.js +917 -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 +116 -50
  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 +2137 -735
  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 +99 -232
  34. package/package.json +4 -3
  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
package/dist/Program.js CHANGED
@@ -1,39 +1,49 @@
1
1
 
2
- import { success, info, error, log } from "./Glutinum.Converter/Log.js";
3
- import { generateBindingFile, generatePackagesFromDisk } from "./Glutinum.Converter/Generate.js";
4
- import { length, exists, filter, contains, cons, singleton, append, reverse, ofArray, tail as tail_5, head, isEmpty, empty } from "./fable_modules/fable-library-js.5.17.2/List.js";
2
+ import path from "path";
3
+ import { fileURLToPath } from "url";
4
+ import { writeFileSync, mkdirSync, readFileSync } from "fs";
5
+ import { collect, map, toList } from "./fable_modules/fable-library-js.5.17.2/Seq.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
- import { stringHash, defaultOf } from "./fable_modules/fable-library-js.5.17.2/Util.js";
7
- import { defaultOptions, GenerateOptions } from "./Glutinum.Converter/Packages.js";
8
- import { join, concat } from "./fable_modules/fable-library-js.5.17.2/String.js";
9
- import * as path from "path";
10
- import * as fs from "fs";
8
+ import { defaultOf } from "./fable_modules/fable-library-js.5.17.2/Util.js";
9
+ import { GenerateOptions } from "./Glutinum.Converter/Packages.js";
10
+ import { generateBindingFileWith, generatePackagesFromDisk } from "./Glutinum.Converter/Generate.js";
11
+ import { exists, length, head, tail, isEmpty, empty } from "./fable_modules/fable-library-js.5.17.2/List.js";
12
+ import { defaultArg } from "./fable_modules/fable-library-js.5.17.2/Option.js";
13
+ import { success, error, info } from "./Glutinum.Converter/Log.js";
14
+ import { program as program_1 } from "commander";
11
15
 
12
- export function printHelp() {
13
- const helpText = "\nGenerate Fable bindings from TypeScript definitions - https://github.com/glutinum-org/cl\n\nUSAGE\n\n glutinum <input>... [--out-file <output>]\n glutinum --all [--out-file <output>]\n glue <input>... [--out-file <output>]\n\n <input> can be:\n - an installed package name (e.g. chalk, @types/vscode)\n - a path to a package directory (e.g. ./node_modules/chalk)\n - a path to a .d.ts file (e.g. ./node_modules/chalk/source/index.d.ts)\n\n Several packages are generated in the same file, each one with every\n package it depends on. A .d.ts file is generated alone.\n --all generates every package installed in the nearest node_modules.\n\n The types of @types/node and @types/web (the DOM) are referenced from the\n Glutinum.Node and Glutinum.Web bindings, unless asked for or --no-externals.\n\nOPTIONS\n\n --out-file <output> Destination file to write in\n If not specified, the result will be printed to stdout\n --no-externals Generate @types/node and @types/web with the packages\n using them instead of referencing their bindings\n --external <package> Reference <package> from its own binding instead of\n generating it, `Glutinum.<Module>` is derived from its\n name unless given as <package>=<Module>\n Can be repeated\n --module-name <name> Full name of the generated module, `Glutinum.Types.TypeScript`,\n instead of `Glutinum.<Module>` derived from the package name\n --include <names> Comma-separated declarations to keep, the others are dropped\n Can be repeated\n --no-lib Create the program without the TypeScript library, for a\n package made of its files\n -h, --help Print this help message\n\nEXAMPLES\n\n glutinum chalk --out-file ./Glutinum.Chalk.fs\n glutinum vscode vscode-languageclient --out-file ./Glutinum.Vscode.fs\n glutinum leaflet --external @types/geojson --out-file ./Glutinum.Leaflet.fs\n glutinum --all --out-file ./Glutinum.fs\n glutinum ./node_modules/my-lib/index.d.ts\n ";
14
- log(helpText);
16
+ function getVersion() {
17
+ const moduleDir = path.dirname(fileURLToPath(import.meta.url));
18
+ let packageJson;
19
+ const text = readFileSync(path.join(moduleDir, "..", "package.json"), "utf8");
20
+ packageJson = JSON.parse(text);
21
+ return packageJson.version;
15
22
  }
16
23
 
17
- function getVersion() {
18
- const pkg = JSON.parse(fs.readFileSync(new URL('./../package.json', import.meta.url)));
24
+ const helpText = "\n <input> can be:\n - an installed package name (e.g. chalk, @types/vscode)\n - a path to a package directory (e.g. ./node_modules/chalk)\n - a path to a .d.ts file (e.g. ./node_modules/chalk/source/index.d.ts)\n\n Several packages are generated in the same file, each one with every\n package it depends on. A .d.ts file is generated alone.\n\n The types of @types/node and @types/web (the DOM) are referenced from the\n Glutinum.Node and Glutinum.Web bindings, unless asked for or --no-externals.\n\nExamples:\n glutinum chalk --out-file ./Glutinum.Chalk.fs\n glutinum vscode vscode-languageclient --out-file ./Glutinum.Vscode.fs\n glutinum leaflet --external @types/geojson --out-file ./Glutinum.Leaflet.fs\n glutinum --all --out-file ./Glutinum.fs\n glutinum ./node_modules/my-lib/index.d.ts\n";
19
25
 
20
- return pkg.version;
21
- ;
26
+ function toGenerateOptions(options) {
27
+ const externals = toList(map((value) => {
28
+ const matchValue = split(value, ["="], undefined, 0);
29
+ if (!equalsWith((x, y) => (x === y), matchValue, defaultOf()) && (matchValue.length === 2)) {
30
+ return [item(0, matchValue), item(1, matchValue)];
31
+ }
32
+ else {
33
+ return [value, undefined];
34
+ }
35
+ }, options.external));
36
+ const includes = toList(collect((value_1) => split(value_1, [","], undefined, 0), options.include));
37
+ return new GenerateOptions(options.externals, externals, options.moduleName, includes, !options.lib);
22
38
  }
23
39
 
24
- function generate(options, inputs) {
40
+ function generate(options, isAll, inputs) {
25
41
  let matchResult, input_1, inputs_1;
26
- if (!isEmpty(inputs)) {
27
- if (head(inputs) === "--all") {
28
- if (isEmpty(tail_5(inputs))) {
29
- matchResult = 0;
30
- }
31
- else {
32
- matchResult = 2;
33
- inputs_1 = inputs;
34
- }
35
- }
36
- else if (isEmpty(tail_5(inputs))) {
42
+ if (isAll) {
43
+ matchResult = 0;
44
+ }
45
+ else if (!isEmpty(inputs)) {
46
+ if (isEmpty(tail(inputs))) {
37
47
  if (head(inputs).endsWith(".d.ts")) {
38
48
  matchResult = 1;
39
49
  input_1 = head(inputs);
@@ -56,223 +66,80 @@ function generate(options, inputs) {
56
66
  case 0:
57
67
  return generatePackagesFromDisk(options, empty());
58
68
  case 1:
59
- return generateBindingFile(input_1);
69
+ return generateBindingFileWith(defaultArg(options.ModuleName, "Glutinum"), input_1);
60
70
  default:
61
71
  return generatePackagesFromDisk(options, inputs_1);
62
72
  }
63
73
  }
64
74
 
65
- (function (argv) {
66
- const argv_1 = ofArray(argv);
67
- let matchResult;
68
- if (!isEmpty(argv_1)) {
69
- switch (head(argv_1)) {
70
- case "-h":
71
- case "--help":
72
- case "help": {
73
- matchResult = 0;
74
- break;
75
- }
76
- case "--version": {
77
- if (isEmpty(tail_5(argv_1))) {
78
- matchResult = 1;
79
- }
80
- else {
81
- matchResult = 2;
82
- }
83
- break;
84
- }
85
- default:
86
- matchResult = 2;
87
- }
75
+ function write(outFile, content) {
76
+ if (outFile == null) {
77
+ const stdout = process.stdout;
78
+ stdout.write(content, "utf8");
88
79
  }
89
80
  else {
90
- matchResult = 0;
81
+ const outFile_1 = outFile;
82
+ const mkdirOptions = {};
83
+ mkdirOptions.recursive = true;
84
+ mkdirSync(path.dirname(outFile_1), mkdirOptions);
85
+ writeFileSync(outFile_1, content);
86
+ info(concat("Bindings written to: ", path.resolve(outFile_1)));
91
87
  }
92
- switch (matchResult) {
93
- case 0: {
94
- printHelp();
95
- return 0;
88
+ }
89
+
90
+ function run(argv) {
91
+ const program = program_1;
92
+ program.name("glutinum").description("Generate Fable bindings from TypeScript definitions - https://github.com/glutinum-org/cli").version(getVersion());
93
+ program.argument("[inputs...]", "packages, a package directory, or a .d.ts file to generate").option("--out-file <path>", "destination file to write in, stdout otherwise").option("--all", "generate every package installed in the nearest node_modules").option("--module-name <name>", "full name of the generated module, instead of `Glutinum.<Module>`").option("--no-externals", "generate @types/node and @types/web instead of referencing them").option("--no-lib", "create the program without the TypeScript library");
94
+ program.option("--external <package>", "reference <package> from its own binding, as <package> or <package>=<Module>", (value_3, previous_1) => {
95
+ void (previous_1.push(value_3));
96
+ return previous_1;
97
+ }, []).option("--include <names>", "comma separated declarations to keep, the others are dropped", (value_2, previous) => {
98
+ void (previous.push(value_2));
99
+ return previous;
100
+ }, []).addHelpText("after", helpText).showHelpAfterError();
101
+ const parseOptions = {
102
+ from: "user",
103
+ };
104
+ program.parse(Array.from(argv), parseOptions);
105
+ const options = program.opts();
106
+ const inputs = toList(program.args);
107
+ const isAll = defaultArg(options.all, false);
108
+ const mixesFileAndPackages = (length(inputs) > 1) && exists((input) => input.endsWith(".d.ts"), inputs);
109
+ if (isEmpty(inputs) && !isAll) {
110
+ error("Give at least one input, or --all");
111
+ program.outputHelp({
112
+ error: true,
113
+ });
114
+ return 1;
115
+ }
116
+ else if (mixesFileAndPackages) {
117
+ error("A .d.ts file is generated alone, it can\'t be combined with other inputs");
118
+ return 1;
119
+ }
120
+ else {
121
+ if (isAll) {
122
+ info("Generating binding file for every installed package");
123
+ }
124
+ else {
125
+ info(concat("Generating binding file for ", join(", ", inputs)));
96
126
  }
97
- case 1: {
98
- log(getVersion());
127
+ try {
128
+ const content = generate(toGenerateOptions(options), isAll, inputs);
129
+ write(options.outFile, content);
130
+ success("Success!");
99
131
  return 0;
100
132
  }
101
- default: {
102
- let patternInput;
103
- const matchValue = reverse(argv_1);
104
- let matchResult_1, inputs, outFile;
105
- if (!isEmpty(matchValue)) {
106
- if (!isEmpty(tail_5(matchValue))) {
107
- if (head(tail_5(matchValue)) === "--out-file") {
108
- matchResult_1 = 0;
109
- inputs = tail_5(tail_5(matchValue));
110
- outFile = head(matchValue);
111
- }
112
- else {
113
- matchResult_1 = 1;
114
- }
115
- }
116
- else {
117
- matchResult_1 = 1;
118
- }
119
- }
120
- else {
121
- matchResult_1 = 1;
122
- }
123
- switch (matchResult_1) {
124
- case 0: {
125
- patternInput = [reverse(inputs), outFile];
126
- break;
127
- }
128
- default:
129
- patternInput = [argv_1, undefined];
130
- }
131
- const outFile_1 = patternInput[1];
132
- const takeOptions = (args_mut, options_mut, rest_mut) => {
133
- takeOptions:
134
- while (true) {
135
- const args = args_mut, options = options_mut, rest = rest_mut;
136
- let matchValue_1;
137
- let matchResult_2, tail, value, tail_1, value_1, tail_2, tail_3, value_2, arg, tail_4;
138
- if (isEmpty(args)) {
139
- matchResult_2 = 5;
140
- }
141
- else {
142
- switch (head(args)) {
143
- case "--external": {
144
- if (!isEmpty(tail_5(args))) {
145
- matchResult_2 = 0;
146
- tail = tail_5(tail_5(args));
147
- value = head(tail_5(args));
148
- }
149
- else {
150
- matchResult_2 = 4;
151
- arg = head(args);
152
- tail_4 = tail_5(args);
153
- }
154
- break;
155
- }
156
- case "--module-name": {
157
- if (!isEmpty(tail_5(args))) {
158
- matchResult_2 = 1;
159
- tail_1 = tail_5(tail_5(args));
160
- value_1 = head(tail_5(args));
161
- }
162
- else {
163
- matchResult_2 = 4;
164
- arg = head(args);
165
- tail_4 = tail_5(args);
166
- }
167
- break;
168
- }
169
- case "--no-lib": {
170
- matchResult_2 = 2;
171
- tail_2 = tail_5(args);
172
- break;
173
- }
174
- case "--include": {
175
- if (!isEmpty(tail_5(args))) {
176
- matchResult_2 = 3;
177
- tail_3 = tail_5(tail_5(args));
178
- value_2 = head(tail_5(args));
179
- }
180
- else {
181
- matchResult_2 = 4;
182
- arg = head(args);
183
- tail_4 = tail_5(args);
184
- }
185
- break;
186
- }
187
- default: {
188
- matchResult_2 = 4;
189
- arg = head(args);
190
- tail_4 = tail_5(args);
191
- }
192
- }
193
- }
194
- switch (matchResult_2) {
195
- case 0: {
196
- args_mut = tail;
197
- options_mut = (new GenerateOptions(options.ExternalPackages, append(options.Externals, singleton((matchValue_1 = value.split("="), (!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
- rest_mut = rest;
199
- continue takeOptions;
200
- }
201
- case 1: {
202
- args_mut = tail_1;
203
- options_mut = (new GenerateOptions(options.ExternalPackages, options.Externals, value_1, options.Include, options.NoLib));
204
- rest_mut = rest;
205
- continue takeOptions;
206
- }
207
- case 2: {
208
- args_mut = tail_2;
209
- options_mut = (new GenerateOptions(options.ExternalPackages, options.Externals, options.ModuleName, options.Include, true));
210
- rest_mut = rest;
211
- continue takeOptions;
212
- }
213
- case 3: {
214
- args_mut = tail_3;
215
- options_mut = (new GenerateOptions(options.ExternalPackages, options.Externals, options.ModuleName, append(options.Include, ofArray(value_2.split(","))), options.NoLib));
216
- rest_mut = rest;
217
- continue takeOptions;
218
- }
219
- case 4: {
220
- args_mut = tail_4;
221
- options_mut = options;
222
- rest_mut = cons(arg, rest);
223
- continue takeOptions;
224
- }
225
- default:
226
- return [options, reverse(rest)];
227
- }
228
- break;
229
- }
230
- };
231
- const patternInput_1 = takeOptions(patternInput[0], defaultOptions, empty());
232
- const options_1 = patternInput_1[0];
233
- const inputs_2 = patternInput_1[1];
234
- const options_2 = new GenerateOptions(!contains("--no-externals", inputs_2, {
235
- Equals: (x_1, y_1) => (x_1 === y_1),
236
- GetHashCode: (x_1) => (stringHash(x_1) | 0),
237
- }), options_1.Externals, options_1.ModuleName, options_1.Include, options_1.NoLib);
238
- const inputs_3 = filter((input) => (input !== "--no-externals"), inputs_2);
239
- const hasUnknownOption = exists((input_1) => {
240
- if (input_1.startsWith("-")) {
241
- return input_1 !== "--all";
242
- }
243
- else {
244
- return false;
245
- }
246
- }, inputs_3);
247
- const mixesFileAndPackages = (length(inputs_3) > 1) && exists((input_2) => input_2.endsWith(".d.ts"), inputs_3);
248
- if (isEmpty(inputs_3) ? true : hasUnknownOption) {
249
- error("Invalid arguments");
250
- printHelp();
251
- return 1;
252
- }
253
- else if (mixesFileAndPackages) {
254
- error("A .d.ts file is generated alone, it can\'t be combined with other inputs");
255
- return 1;
256
- }
257
- else {
258
- info(concat("Generating binding file for ", join(", ", inputs_3)));
259
- const res = generate(options_2, inputs_3);
260
- if (outFile_1 == null) {
261
- process.stdout.write(res);
262
- }
263
- else {
264
- const outFile_2 = outFile_1;
265
- const outFileDir = path.dirname(outFile_2);
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)));
271
- }
272
- success("Success!");
273
- return 0;
274
- }
133
+ catch (ex) {
134
+ error(ex.message);
135
+ return 1;
275
136
  }
276
137
  }
138
+ }
139
+
140
+ (function (argv) {
141
+ const exitCode = run(argv) | 0;
142
+ process.exitCode = exitCode;
143
+ return exitCode | 0;
277
144
  })(typeof process === 'object' ? process.argv.slice(2) : []);
278
145
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glutinum/cli",
3
- "version": "0.14.1",
3
+ "version": "1.0.0-beta.2",
4
4
  "description": "TypeScript definition to F# bindings converter",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -51,7 +51,8 @@
51
51
  "vitest": "^1.5.0"
52
52
  },
53
53
  "dependencies": {
54
- "@ts-morph/bootstrap": "^0.22.0",
55
- "chalk": "^5.3.0"
54
+ "@ts-morph/bootstrap": "^0.29.0",
55
+ "chalk": "^5.3.0",
56
+ "commander": "^15.0.0"
56
57
  }
57
58
  }
@@ -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
- }