@glutinum/cli 0.14.1 → 1.0.0-beta.10
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/README.md +16 -0
- package/dist/Glutinum.Converter/Abbreviations.js +9 -32
- package/dist/Glutinum.Converter/DelegateExtensions.js +378 -0
- package/dist/Glutinum.Converter/FreeTypeParameters.js +211 -0
- package/dist/Glutinum.Converter/FsharpAST.js +154 -69
- package/dist/Glutinum.Converter/Generate.js +44 -23
- package/dist/Glutinum.Converter/GlueAST.js +122 -74
- package/dist/Glutinum.Converter/Hosting.js +892 -0
- package/dist/Glutinum.Converter/Merge.js +469 -301
- package/dist/Glutinum.Converter/Packages.js +403 -77
- package/dist/Glutinum.Converter/Printer.js +455 -388
- package/dist/Glutinum.Converter/Read.js +366 -104
- package/dist/Glutinum.Converter/Reader/ClassDeclaration.js +9 -9
- package/dist/Glutinum.Converter/Reader/ConditionalTypeNode.js +90 -0
- package/dist/Glutinum.Converter/Reader/Declaration.js +24 -30
- package/dist/Glutinum.Converter/Reader/Deferred.js +706 -0
- package/dist/Glutinum.Converter/Reader/Documentation.js +56 -67
- package/dist/Glutinum.Converter/Reader/ExportAssignment.js +105 -38
- package/dist/Glutinum.Converter/Reader/ExportDeclaration.js +52 -55
- package/dist/Glutinum.Converter/Reader/ExpressionWithTypeArguments.js +59 -0
- package/dist/Glutinum.Converter/Reader/FunctionDeclaration.js +28 -16
- package/dist/Glutinum.Converter/Reader/IndexedAccessType.js +46 -45
- package/dist/Glutinum.Converter/Reader/InterfaceDeclaration.js +48 -5
- package/dist/Glutinum.Converter/Reader/IntersectionTypeNode.js +178 -0
- package/dist/Glutinum.Converter/Reader/MappedTypeNode.js +82 -4
- package/dist/Glutinum.Converter/Reader/MemberTypes.js +126 -0
- package/dist/Glutinum.Converter/Reader/ModuleDeclaration.js +55 -7
- package/dist/Glutinum.Converter/Reader/Node.js +23 -26
- package/dist/Glutinum.Converter/Reader/Parameters.js +39 -30
- package/dist/Glutinum.Converter/Reader/TypeAliasDeclaration.js +5 -5
- package/dist/Glutinum.Converter/Reader/TypeNode.js +410 -1545
- package/dist/Glutinum.Converter/Reader/TypeOperatorNode.js +78 -51
- package/dist/Glutinum.Converter/Reader/TypeQueryNode.js +116 -71
- package/dist/Glutinum.Converter/Reader/TypeScriptReader.js +25 -7
- package/dist/Glutinum.Converter/Reader/Types.js +296 -16
- package/dist/Glutinum.Converter/Reader/UnionTypeNode.js +21 -34
- package/dist/Glutinum.Converter/Reader/UtilityType.js +811 -0
- package/dist/Glutinum.Converter/Reader/Utils.js +584 -148
- package/dist/Glutinum.Converter/Reader/VariableStatement.js +2 -3
- package/dist/Glutinum.Converter/Transform.js +2971 -6875
- package/dist/Glutinum.Converter/Transformer/AnyFunctionOverloads.js +116 -0
- package/dist/Glutinum.Converter/Transformer/CallableProperties.js +454 -0
- package/dist/Glutinum.Converter/Transformer/Comment.js +136 -0
- package/dist/Glutinum.Converter/Transformer/Conditionals.js +1087 -0
- package/dist/Glutinum.Converter/Transformer/Context.js +678 -0
- package/dist/Glutinum.Converter/Transformer/Enum.js +80 -0
- package/dist/Glutinum.Converter/Transformer/Exports.js +704 -0
- package/dist/Glutinum.Converter/Transformer/Heritage.js +493 -0
- package/dist/Glutinum.Converter/Transformer/KeyOfMaps.js +488 -0
- package/dist/Glutinum.Converter/Transformer/ParamObjectCandidate.js +460 -0
- package/dist/Glutinum.Converter/Transformer/TypeParameter.js +219 -0
- package/dist/Glutinum.Converter/Transformer/TypeParameters.js +448 -0
- package/dist/Glutinum.Converter/Transformer/UnionOverloads.js +356 -0
- package/dist/Glutinum.Converter/Transformer/Utils.js +341 -0
- package/dist/Glutinum.Converter/TypeScript.Extensions.js +193 -211
- package/dist/Glutinum.Converter/Utils/Naming.js +7 -0
- package/dist/Glutinum.Converter/Utils/String.js +15 -4
- package/dist/Program.js +107 -232
- package/package.json +4 -3
- 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
|
@@ -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
|
-
}
|
|
@@ -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
|
-
}
|