tsc-after 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -16
- package/dist/tscAfter.js +220 -105
- package/dist/typescript7Compiler-D6FwNrdQ.js +170 -0
- package/dist/typescriptPre7Compiler-CZ1GIAWf.js +69 -0
- package/package.json +3 -2
- package/dist/typescript7Compiler-D2MC51P1.js +0 -220
- package/dist/typescriptPre7Compiler-DdKME3sA.js +0 -67
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ tsc-after -p packages/core
|
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
`tsc-after` asks TypeScript which files the project emits, then removes every other regular file in `outDir` and `declarationDir`, along with directories left empty.
|
|
42
|
-
It skips symbolic links and entries on other devices
|
|
42
|
+
It skips symbolic links and entries on other devices inside `outDir` or `declarationDir`.
|
|
43
43
|
|
|
44
44
|
`--dry` lists planned removals without changing files.
|
|
45
45
|
Use `--verbose` to add individual removed and skipped entries.
|
|
@@ -48,28 +48,29 @@ Use `--verbose` to add individual removed and skipped entries.
|
|
|
48
48
|
|
|
49
49
|
`tsc-after` reads your tsconfig the way `tsc` does; this list covers only where it differs.
|
|
50
50
|
|
|
51
|
-
- `outDir`
|
|
52
|
-
|
|
53
|
-
-
|
|
54
|
-
-
|
|
55
|
-
-
|
|
56
|
-
- TypeScript
|
|
57
|
-
- TypeScript 7.1 beta support is experimental, and `extends` there must be a file path, not a package name.
|
|
58
|
-
- Safety checks ([#6](https://github.com/michal-kocarek/tsc-after/issues/6)) and output coverage ([#7](https://github.com/michal-kocarek/tsc-after/issues/7)) are incomplete, so keep valuable files out of `outDir`.
|
|
51
|
+
- `outDir` (and `declarationDir`) must hold only TypeScript outputs: anything else, including copied assets, is removed as stale.
|
|
52
|
+
- `outFile` is rejected, and project references are not followed.
|
|
53
|
+
- Support for TypeScript 3.9 - 4.1 is limited, because they compute output paths differently:
|
|
54
|
+
- `rootDir` is required ([#15](https://github.com/michal-kocarek/tsc-after/issues/15)).
|
|
55
|
+
- 3.9 only: `emitDeclarationOnly` with `resolveJsonModule` and JSON source files, and `allowJs` with `jsx: preserve`, `declaration`, and `.jsx` source files are refused ([#15](https://github.com/michal-kocarek/tsc-after/issues/15)).
|
|
56
|
+
- TypeScript 7.1 beta support is experimental.
|
|
59
57
|
|
|
60
58
|
Planned: `--copy`, `--exclude`, and `--watch`.
|
|
61
59
|
See [`product.md`](./docs/product.md) for the roadmap.
|
|
62
60
|
|
|
63
|
-
##
|
|
61
|
+
## Security measures
|
|
64
62
|
|
|
65
|
-
-
|
|
66
|
-
-
|
|
63
|
+
- Cleanup is refused unless `outDir` is set and `noEmit` is disabled.
|
|
64
|
+
- `outDir` and `declarationDir` must be inside, and not equal to, the current working directory.
|
|
65
|
+
- Cleanup is refused when `outDir` or `declarationDir` contains a source file of the project.
|
|
66
|
+
- Cleanup is refused when `outDir` or `declarationDir` is reached through a symbolic link.
|
|
67
|
+
- A `--project` path outside the current working directory is refused.
|
|
68
|
+
- Without `--project`, only `tsconfig.json` in the current working directory is read; unlike `tsc`, parent directories are not searched.
|
|
67
69
|
|
|
68
|
-
##
|
|
70
|
+
## Requirements
|
|
69
71
|
|
|
70
|
-
-
|
|
71
|
-
-
|
|
72
|
-
- `2` for invalid command-line arguments
|
|
72
|
+
- Node.js 16.19.0 or later, including the latest LTS.
|
|
73
|
+
- TypeScript 3.9.10 through 7.x, including 7.1 beta (except 7.0, which has no API support).
|
|
73
74
|
|
|
74
75
|
## Why not `rm -rf dist && tsc`?
|
|
75
76
|
|
|
@@ -77,6 +78,12 @@ Wiping `outDir` before every build (`rm -rf dist && tsc`, `rimraf dist && tsc`,
|
|
|
77
78
|
On GitHub, about 274k `package.json` files run `rm -rf dist` and another 199k run `rimraf dist` (see [`market-research.md`](./docs/market-research.md)).
|
|
78
79
|
|
|
79
80
|
`tsc-after` is intended to remove only stale outputs rather than wiping `outDir` and rebuilding everything.
|
|
81
|
+
That brings:
|
|
82
|
+
|
|
83
|
+
- Incremental builds without extra tooling: with `incremental` or `composite`, the `.tsbuildinfo` file and unchanged outputs survive, so `tsc` re-emits only what changed.
|
|
84
|
+
- Less work for file watchers: IDEs, `tsc --watch` in dependent packages, `node --watch`, nodemon, and test runners see only the changed files instead of every output being deleted and recreated.
|
|
85
|
+
- No missing outputs mid-build: dependent packages, running dev servers, and IDE type checking never see an empty `outDir`.
|
|
86
|
+
- A failed build never leaves `outDir` empty: with `tsc && tsc-after`, cleanup runs only after `tsc` succeeds.
|
|
80
87
|
|
|
81
88
|
**Using a bundler such as tsup, Vite, or esbuild?**<br>
|
|
82
89
|
They already clean up after themselves, so you probably don't need `tsc-after`.
|
package/dist/tscAfter.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as util from "node:util";
|
|
2
|
-
import fs from "node:fs";
|
|
3
2
|
import path from "node:path";
|
|
3
|
+
import fs from "node:fs";
|
|
4
4
|
import ts from "typescript";
|
|
5
5
|
// src/common/errors.ts
|
|
6
6
|
/** Error whose message is human-readable and printed to the user without a stack trace. */
|
|
@@ -114,88 +114,94 @@ function help() {
|
|
|
114
114
|
}
|
|
115
115
|
// src/common/paths.ts
|
|
116
116
|
function isPathInside(dirPath, filePath, useCaseSensitiveFileNames) {
|
|
117
|
-
const normalizedDirPath =
|
|
118
|
-
const normalizedFilePath =
|
|
119
|
-
|
|
120
|
-
return relativePath === "" || !path.isAbsolute(relativePath) && relativePath !== ".." && !relativePath.startsWith(`..${path.sep}`);
|
|
117
|
+
const normalizedDirPath = normalizePathForComparison(dirPath, useCaseSensitiveFileNames);
|
|
118
|
+
const normalizedFilePath = normalizePathForComparison(filePath, useCaseSensitiveFileNames);
|
|
119
|
+
return normalizedDirPath === normalizedFilePath || normalizedFilePath.startsWith(normalizedDirPath.endsWith(path.sep) ? normalizedDirPath : `${normalizedDirPath}${path.sep}`);
|
|
121
120
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (match?.groups === void 0) return NaN;
|
|
126
|
-
const { major, minor, patch } = match.groups;
|
|
127
|
-
return Number(major) * 1e6 + Number(minor) * 1e3 + Number(patch);
|
|
121
|
+
function normalizePathForComparison(filePath, useCaseSensitiveFileNames) {
|
|
122
|
+
const normalizedPath = path.resolve(filePath);
|
|
123
|
+
return useCaseSensitiveFileNames ? normalizedPath : normalizedPath.toLowerCase();
|
|
128
124
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Uses the native realpath implementation to match TypeScript's canonical
|
|
127
|
+
* paths, including Windows 8.3 names and junctions.
|
|
128
|
+
*/
|
|
129
|
+
function realpath(filePath) {
|
|
130
|
+
return fs.realpathSync.native(filePath);
|
|
131
|
+
}
|
|
132
|
+
// src/common/cleanup.ts
|
|
133
|
+
/**
|
|
134
|
+
* Plans the removal of stale outputs from each cleanup root without changing
|
|
135
|
+
* anything, refusing roots that are unsafe to clean.
|
|
136
|
+
* A path inside outDir always belongs to outDir, even when outDir is nested in
|
|
137
|
+
* declarationDir.
|
|
138
|
+
*/
|
|
139
|
+
function planCleanup(project, cwd) {
|
|
140
|
+
const { outDir, useCaseSensitiveFileNames } = project;
|
|
141
|
+
const normalizedOutputPaths = new Set([...project.outputPaths].map((filePath) => normalizePathForComparison(filePath, useCaseSensitiveFileNames)));
|
|
142
|
+
const isOutputPath = (filePath) => normalizedOutputPaths.has(normalizePathForComparison(filePath, useCaseSensitiveFileNames));
|
|
143
|
+
let outDirIsRemoved = false;
|
|
144
|
+
const plan = [];
|
|
145
|
+
for (const root of resolveCleanupRoots(project, cwd)) {
|
|
146
|
+
const verdicts = readCleanupRoot(root.path, isOutputPath, (dirPath) => {
|
|
147
|
+
if (root.name === "outDir" || !isPathInside(outDir, dirPath, useCaseSensitiveFileNames)) return;
|
|
148
|
+
return outDirIsRemoved;
|
|
149
|
+
});
|
|
150
|
+
if (root.name === "outDir") outDirIsRemoved = verdicts.some((verdict) => verdict.type === "removeDir" && verdict.path === root.path);
|
|
151
|
+
plan.push({
|
|
152
|
+
root,
|
|
153
|
+
verdicts
|
|
154
|
+
});
|
|
136
155
|
}
|
|
137
|
-
|
|
138
|
-
return (await import("./typescript7Compiler-D2MC51P1.js")).TypeScript7Compiler.create();
|
|
156
|
+
return plan;
|
|
139
157
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (project.options.declarationDir !== void 0) {
|
|
168
|
-
if (project.options.declaration !== true) throw new UserError(DECLARATION_DISABLED_ERROR);
|
|
169
|
-
const declarationDir = path.resolve(project.options.declarationDir);
|
|
170
|
-
roots.push({
|
|
171
|
-
name: "declarationDir",
|
|
172
|
-
path: declarationDir,
|
|
173
|
-
relativePath: path.relative(cwd, declarationDir)
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
if (project.options.incremental === true) throw new UserError("Incremental mode is not supported");
|
|
177
|
-
if (project.options.tsBuildInfoFile !== void 0) throw new UserError("tsBuildInfoFile is not supported");
|
|
178
|
-
if (project.options.composite === true) throw new UserError("Composite mode is not supported");
|
|
179
|
-
if (project.options.project !== void 0) throw new UserError("Project mode is not supported");
|
|
180
|
-
if (project.hasWatchOptions) throw new UserError("Watch mode is not supported");
|
|
181
|
-
for (const root of roots) {
|
|
182
|
-
if (!isPathInsideWithCompiler(cwd, root.path) || isPathInsideWithCompiler(root.path, cwd)) throw new UserError(`Refusing to traverse outside safe paths containing tsconfig to process ${root.name}!`);
|
|
183
|
-
const isInsideRoot = (filePath) => isPathInsideWithCompiler(root.path, filePath) || isPathInsideWithCompiler(root.path, fs.realpathSync(filePath));
|
|
184
|
-
for (const file of project.programFileNames) if (isInsideRoot(path.resolve(file))) throw new UserError(`Refusing to clean ${root.name} '${root.relativePath}': it contains source file '${path.relative(cwd, file)}'.`);
|
|
185
|
-
for (const file of project.configFilePaths) if (isInsideRoot(path.resolve(file))) throw new UserError(`Refusing to clean ${root.name} '${root.relativePath}': it contains config file '${path.relative(cwd, file)}'.`);
|
|
186
|
-
try {
|
|
187
|
-
fs.lstatSync(root.path);
|
|
188
|
-
if (fs.realpathSync(root.path) !== root.path) throw new UserError(`Refusing to clean ${root.name} through a symbolic link: ${root.relativePath}!`);
|
|
189
|
-
} catch (error) {
|
|
190
|
-
if (!isNodeErrorOf(error, Error, "ENOENT")) throw error;
|
|
158
|
+
/**
|
|
159
|
+
* Removes what the plan marks for removal, contents before their directories.
|
|
160
|
+
* Returns the plan's verdicts, with a busy cleanup root reported as skipped.
|
|
161
|
+
*/
|
|
162
|
+
function applyCleanup(plan, { dry }) {
|
|
163
|
+
return plan.map(({ root, verdicts }) => ({
|
|
164
|
+
root,
|
|
165
|
+
verdicts: verdicts.map((verdict) => {
|
|
166
|
+
if (dry) return verdict;
|
|
167
|
+
switch (verdict.type) {
|
|
168
|
+
case "removeDir":
|
|
169
|
+
try {
|
|
170
|
+
fs.rmdirSync(verdict.path);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (verdict.path === root.path && isNodeErrorOf(error, Error, "EBUSY")) return {
|
|
173
|
+
path: verdict.path,
|
|
174
|
+
reason: "busy directory",
|
|
175
|
+
type: "skipped"
|
|
176
|
+
};
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
return verdict;
|
|
180
|
+
case "removeFile":
|
|
181
|
+
fs.rmSync(verdict.path, { recursive: false });
|
|
182
|
+
return verdict;
|
|
183
|
+
case "skipped": return verdict;
|
|
184
|
+
default: return assertUnreachable(verdict);
|
|
191
185
|
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
|
|
186
|
+
})
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
function notADirectoryError(root, componentPath) {
|
|
190
|
+
return `Refusing to clean ${root.name} '${root.relativePath}': '${componentPath}' is not a directory.`;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Walks a cleanup root, listing contents before their directory.
|
|
194
|
+
* `isDirOwnedElsewhere` returns `undefined` for a directory this root owns, or
|
|
195
|
+
* whether another root's plan removes it.
|
|
196
|
+
*/
|
|
197
|
+
function readCleanupRoot(rootPath, isOutputPath, isDirOwnedElsewhere) {
|
|
198
|
+
try {
|
|
199
|
+
fs.lstatSync(rootPath);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (isNodeErrorOf(error, Error, "ENOENT")) return [];
|
|
202
|
+
throw error;
|
|
198
203
|
}
|
|
204
|
+
return readDir(rootPath);
|
|
199
205
|
function readDir(dirPath) {
|
|
200
206
|
const verdicts = [];
|
|
201
207
|
const stat = fs.lstatSync(dirPath);
|
|
@@ -205,7 +211,7 @@ async function mainAction(args) {
|
|
|
205
211
|
const fullPath = path.join(dirPath, child);
|
|
206
212
|
const fullPathStat = fs.lstatSync(fullPath);
|
|
207
213
|
if (fullPathStat.dev !== stat.dev) {
|
|
208
|
-
|
|
214
|
+
verdicts.push({
|
|
209
215
|
path: fullPath,
|
|
210
216
|
reason: "different device",
|
|
211
217
|
type: "skipped"
|
|
@@ -214,7 +220,7 @@ async function mainAction(args) {
|
|
|
214
220
|
continue;
|
|
215
221
|
}
|
|
216
222
|
if (fullPathStat.isSymbolicLink()) {
|
|
217
|
-
|
|
223
|
+
verdicts.push({
|
|
218
224
|
path: fullPath,
|
|
219
225
|
reason: "symbolic link",
|
|
220
226
|
type: "skipped"
|
|
@@ -223,8 +229,8 @@ async function mainAction(args) {
|
|
|
223
229
|
continue;
|
|
224
230
|
}
|
|
225
231
|
if (fullPathStat.isFile()) {
|
|
226
|
-
if (
|
|
227
|
-
|
|
232
|
+
if (isOutputPath(fullPath)) {
|
|
233
|
+
verdicts.push({
|
|
228
234
|
path: fullPath,
|
|
229
235
|
reason: "expected output file",
|
|
230
236
|
type: "skipped"
|
|
@@ -239,11 +245,21 @@ async function mainAction(args) {
|
|
|
239
245
|
continue;
|
|
240
246
|
}
|
|
241
247
|
if (fullPathStat.isDirectory()) {
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
248
|
+
const isRemovedElsewhere = isDirOwnedElsewhere(fullPath);
|
|
249
|
+
if (isRemovedElsewhere !== void 0) {
|
|
250
|
+
if (!isRemovedElsewhere) dirIsEmpty = false;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const childVerdicts = readDir(fullPath);
|
|
254
|
+
verdicts.push(...childVerdicts);
|
|
255
|
+
if (childVerdicts.some((verdict) => verdict.type === "skipped")) dirIsEmpty = false;
|
|
245
256
|
continue;
|
|
246
257
|
}
|
|
258
|
+
verdicts.push({
|
|
259
|
+
path: fullPath,
|
|
260
|
+
reason: "unknown file type",
|
|
261
|
+
type: "skipped"
|
|
262
|
+
});
|
|
247
263
|
dirIsEmpty = false;
|
|
248
264
|
}
|
|
249
265
|
if (dirIsEmpty) verdicts.push({
|
|
@@ -255,38 +271,137 @@ async function mainAction(args) {
|
|
|
255
271
|
reason: "directory not empty",
|
|
256
272
|
type: "skipped"
|
|
257
273
|
});
|
|
274
|
+
return verdicts;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function resolveCleanupRoots(project, cwd) {
|
|
278
|
+
const { declarationDir, outDir, useCaseSensitiveFileNames } = project;
|
|
279
|
+
const roots = [{
|
|
280
|
+
name: "outDir",
|
|
281
|
+
path: outDir,
|
|
282
|
+
relativePath: path.relative(cwd, outDir)
|
|
283
|
+
}];
|
|
284
|
+
if (declarationDir !== void 0) roots.push({
|
|
285
|
+
name: "declarationDir",
|
|
286
|
+
path: declarationDir,
|
|
287
|
+
relativePath: path.relative(cwd, declarationDir)
|
|
288
|
+
});
|
|
289
|
+
for (const root of roots) {
|
|
290
|
+
if (!isPathInside(cwd, root.path, useCaseSensitiveFileNames) || isPathInside(root.path, cwd, useCaseSensitiveFileNames)) throw new UserError(`Refusing to traverse outside safe paths containing tsconfig to process ${root.name}!`);
|
|
291
|
+
let componentPath = cwd;
|
|
292
|
+
for (const component of root.relativePath.split(path.sep)) {
|
|
293
|
+
componentPath = path.join(componentPath, component);
|
|
294
|
+
let stat;
|
|
295
|
+
try {
|
|
296
|
+
stat = fs.lstatSync(componentPath);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
if (isNodeErrorOf(error, Error, "ENOENT")) break;
|
|
299
|
+
throw error;
|
|
300
|
+
}
|
|
301
|
+
if (stat.isSymbolicLink()) throw new UserError(`Refusing to clean ${root.name} through a symbolic link: ${root.relativePath}!`);
|
|
302
|
+
if (!stat.isDirectory()) throw new UserError(notADirectoryError(root, path.relative(cwd, componentPath)));
|
|
303
|
+
}
|
|
304
|
+
for (const file of project.sourceFileNames) {
|
|
305
|
+
const filePath = path.resolve(cwd, file);
|
|
306
|
+
let realFilePath = filePath;
|
|
307
|
+
try {
|
|
308
|
+
realFilePath = realpath(filePath);
|
|
309
|
+
} catch (error) {
|
|
310
|
+
if (!isNodeErrorOf(error, Error, "ENOENT")) throw error;
|
|
311
|
+
}
|
|
312
|
+
if (isPathInside(root.path, filePath, useCaseSensitiveFileNames) || isPathInside(root.path, realFilePath, useCaseSensitiveFileNames)) throw new UserError(`Refusing to clean ${root.name} '${root.relativePath}': it contains source file '${path.relative(cwd, file)}'.`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return roots.filter((root) => root.name === "outDir" || !isPathInside(outDir, root.path, useCaseSensitiveFileNames));
|
|
316
|
+
}
|
|
317
|
+
const TYPESCRIPT_4_0 = 4e6;
|
|
318
|
+
const TYPESCRIPT_4_2 = 4002e3;
|
|
319
|
+
function parseTypeScriptVersion(version) {
|
|
320
|
+
const match = /^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:-[-.0-9A-Za-z]+)?(?:\+[-.0-9A-Za-z]+)?$/u.exec(version);
|
|
321
|
+
if (match?.groups === void 0) return NaN;
|
|
322
|
+
const { major, minor, patch } = match.groups;
|
|
323
|
+
return Number(major) * 1e6 + Number(minor) * 1e3 + Number(patch);
|
|
324
|
+
}
|
|
325
|
+
const typescriptVersion = parseTypeScriptVersion(ts.version);
|
|
326
|
+
// src/common/typescript/loadCompiler.ts
|
|
327
|
+
async function loadCompiler() {
|
|
328
|
+
if (Number.isNaN(typescriptVersion)) throw new UserError(`Cannot parse TypeScript version '${ts.version}'.`);
|
|
329
|
+
if (typescriptVersion < 3009e3) throw new UserError("TypeScript 3.8 and earlier are not supported. Use TypeScript 3.9 or later.");
|
|
330
|
+
if (typescriptVersion < 7e6) {
|
|
331
|
+
const { TypeScriptPre7Compiler } = await import("./typescriptPre7Compiler-CZ1GIAWf.js");
|
|
332
|
+
return new TypeScriptPre7Compiler();
|
|
333
|
+
}
|
|
334
|
+
if (typescriptVersion < 7001e3) throw new UserError("TypeScript 7.0 is not supported. Use TypeScript 7.1 or later.");
|
|
335
|
+
const { TypeScript7Compiler } = await import("./typescript7Compiler-D6FwNrdQ.js");
|
|
336
|
+
return TypeScript7Compiler.create();
|
|
337
|
+
}
|
|
338
|
+
// src/common/project.ts
|
|
339
|
+
const OUTSIDE_CWD_ERROR = "Refusing to traverse outside current working directory to read tsconfig!";
|
|
340
|
+
const NO_EMIT_ERROR = "Refusing to clean outDir because noEmit is enabled.";
|
|
341
|
+
const MISSING_OUT_DIR_ERROR = "outDir is not set in tsconfig.json; tsc-after only cleans a dedicated outDir. Set compilerOptions.outDir, e.g. \"dist\".";
|
|
342
|
+
const DECLARATION_DISABLED_ERROR = "Refusing to clean declarationDir because declaration is not enabled.";
|
|
343
|
+
/**
|
|
344
|
+
* Loads the project selected by `--project`, refusing configurations whose
|
|
345
|
+
* outputs cannot be derived reliably.
|
|
346
|
+
*/
|
|
347
|
+
async function loadProject(projectPath, cwd) {
|
|
348
|
+
const compiler = await loadCompiler();
|
|
349
|
+
try {
|
|
350
|
+
const configFilePath = resolveConfigFilePath(compiler, projectPath, cwd);
|
|
351
|
+
const parsedProject = compiler.parseProject(configFilePath);
|
|
352
|
+
const { declarationDir, outDir } = resolveOutputDirs(parsedProject, cwd);
|
|
258
353
|
return {
|
|
259
|
-
|
|
260
|
-
|
|
354
|
+
configFilePath,
|
|
355
|
+
declarationDir,
|
|
356
|
+
outDir,
|
|
357
|
+
outputPaths: parsedProject.getOutputPaths(),
|
|
358
|
+
sourceFileNames: parsedProject.sourceFileNames,
|
|
359
|
+
useCaseSensitiveFileNames: compiler.useCaseSensitiveFileNames
|
|
261
360
|
};
|
|
361
|
+
} finally {
|
|
362
|
+
compiler.close();
|
|
262
363
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
364
|
+
}
|
|
365
|
+
function resolveConfigFilePath(compiler, projectPath, cwd) {
|
|
366
|
+
const isInsideCwd = (filePath) => isPathInside(cwd, filePath, compiler.useCaseSensitiveFileNames);
|
|
367
|
+
const projectAbsolutePath = path.resolve(cwd, projectPath);
|
|
368
|
+
if (!isInsideCwd(projectAbsolutePath)) throw new UserError(OUTSIDE_CWD_ERROR);
|
|
369
|
+
const configFilePath = fs.existsSync(projectAbsolutePath) && fs.statSync(projectAbsolutePath).isDirectory() ? path.join(projectAbsolutePath, "tsconfig.json") : projectAbsolutePath;
|
|
370
|
+
if (!isInsideCwd(configFilePath) || isPathInside(configFilePath, cwd, compiler.useCaseSensitiveFileNames)) throw new UserError(OUTSIDE_CWD_ERROR);
|
|
371
|
+
return configFilePath;
|
|
372
|
+
}
|
|
373
|
+
function resolveOutputDirs({ options }, cwd) {
|
|
374
|
+
if (options.noEmit === true) throw new UserError(NO_EMIT_ERROR);
|
|
375
|
+
if (options.outFile !== void 0) throw new UserError("outFile is not supported");
|
|
376
|
+
if (options.project !== void 0) throw new UserError("Project mode is not supported");
|
|
377
|
+
if (options.outDir === void 0) throw new UserError(MISSING_OUT_DIR_ERROR);
|
|
378
|
+
if (options.declarationDir !== void 0 && options.declaration !== true && options.composite !== true) throw new UserError(DECLARATION_DISABLED_ERROR);
|
|
379
|
+
return {
|
|
380
|
+
declarationDir: options.declarationDir === void 0 ? void 0 : path.resolve(cwd, options.declarationDir),
|
|
381
|
+
outDir: path.resolve(cwd, options.outDir)
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
// src/tasks/mainAction.ts
|
|
385
|
+
async function mainAction(args) {
|
|
386
|
+
const cwd = realpath(process.cwd());
|
|
387
|
+
const project = await loadProject(args.values.project, cwd);
|
|
388
|
+
print(`Using tsconfig: ${path.relative(cwd, project.configFilePath)}`);
|
|
389
|
+
const plan = planCleanup(project, cwd);
|
|
390
|
+
if (args.values.noClean) return;
|
|
272
391
|
const { dry, verbose } = args.values;
|
|
392
|
+
const results = applyCleanup(plan, { dry });
|
|
273
393
|
const removeVerb = dry ? "Would remove" : "Removed";
|
|
274
|
-
for (const
|
|
275
|
-
const result = results[index];
|
|
276
|
-
if (result === void 0) throw new Error("Missing cleanup result.");
|
|
394
|
+
for (const { root, verdicts } of results) {
|
|
277
395
|
let fileCount = 0;
|
|
278
396
|
let dirCount = 0;
|
|
279
|
-
for (const verdict of
|
|
280
|
-
if (root.name === "declarationDir" && isPathInside(outDirAbsolutePath, verdict.path, compiler.useCaseSensitiveFileNames)) continue;
|
|
397
|
+
for (const verdict of verdicts) {
|
|
281
398
|
const displayPath = path.relative(cwd, verdict.path);
|
|
282
399
|
switch (verdict.type) {
|
|
283
400
|
case "removeDir":
|
|
284
|
-
if (!dry) fs.rmdirSync(verdict.path);
|
|
285
401
|
dirCount++;
|
|
286
402
|
if (dry || verbose) print(`${removeVerb} empty directory: ${displayPath}`);
|
|
287
403
|
break;
|
|
288
404
|
case "removeFile":
|
|
289
|
-
if (!dry) fs.rmSync(verdict.path, { recursive: false });
|
|
290
405
|
fileCount++;
|
|
291
406
|
if (dry || verbose) print(`${removeVerb} file: ${displayPath}`);
|
|
292
407
|
break;
|
|
@@ -300,14 +415,14 @@ async function mainAction(args) {
|
|
|
300
415
|
if (fileCount > 0) parts.push(formatCount(fileCount, "file", "files"));
|
|
301
416
|
if (dirCount > 0) parts.push(formatCount(dirCount, "empty directory", "empty directories"));
|
|
302
417
|
if (parts.length > 0) print(`${binName}: ${removeVerb} ${parts.join(" and ")} from ${styleText("italic", root.relativePath)}`);
|
|
303
|
-
else if (dry ||
|
|
418
|
+
else if (dry || results.length > 1) print(`${binName}: Nothing to remove from ${styleText("italic", root.relativePath)}`);
|
|
304
419
|
}
|
|
305
420
|
}
|
|
306
421
|
function formatCount(count, singular, plural) {
|
|
307
422
|
return `${count.toFixed()} ${count === 1 ? singular : plural}`;
|
|
308
423
|
}
|
|
309
424
|
// package.json
|
|
310
|
-
var version$1 = "0.
|
|
425
|
+
var version$1 = "0.5.0";
|
|
311
426
|
// src/tasks/version.ts
|
|
312
427
|
function version() {
|
|
313
428
|
print(`${binName}/${version$1} ${process.platform}-${process.arch} node-${process.versions.node} typescript-${ts.version}`);
|
|
@@ -325,4 +440,4 @@ async function main() {
|
|
|
325
440
|
}
|
|
326
441
|
}
|
|
327
442
|
main();
|
|
328
|
-
export {
|
|
443
|
+
export { UserError as i, TYPESCRIPT_4_2 as n, typescriptVersion as r, TYPESCRIPT_4_0 as t };
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { i as UserError } from "./tscAfter.js";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
// src/common/typescript/outputPaths.ts
|
|
5
|
+
const declarationFileExtensions = [
|
|
6
|
+
".d.ts",
|
|
7
|
+
".d.mts",
|
|
8
|
+
".d.cts"
|
|
9
|
+
];
|
|
10
|
+
const tsBuildInfoExtension = ".tsbuildinfo";
|
|
11
|
+
const esmFileExtensions = [".mts", ".mjs"];
|
|
12
|
+
const cjsFileExtensions = [".cts", ".cjs"];
|
|
13
|
+
const knownFileExtensions = [
|
|
14
|
+
...declarationFileExtensions,
|
|
15
|
+
...esmFileExtensions,
|
|
16
|
+
...cjsFileExtensions,
|
|
17
|
+
".ts",
|
|
18
|
+
".js",
|
|
19
|
+
".tsx",
|
|
20
|
+
".jsx",
|
|
21
|
+
".json"
|
|
22
|
+
];
|
|
23
|
+
function getOutputFileNames(options, fileName) {
|
|
24
|
+
const sourcePath = path.resolve(fileName);
|
|
25
|
+
const sourceExtension = path.extname(sourcePath);
|
|
26
|
+
if (isDeclarationFile(sourcePath)) return [];
|
|
27
|
+
const isJsonFile = sourceExtension === ".json";
|
|
28
|
+
if ((sourceExtension === ".js" || sourceExtension === ".jsx" || sourceExtension === ".mjs" || sourceExtension === ".cjs") && options.allowJs !== true) return [];
|
|
29
|
+
const outputFileNames = [];
|
|
30
|
+
const outputPath = getOutputPath(sourcePath, resolveOptionPath(options.outDir, options.configFilePath), options.rootDir, options.configFilePath);
|
|
31
|
+
if (isJsonFile) {
|
|
32
|
+
if (options.emitDeclarationOnly !== true) outputFileNames.push(outputPath);
|
|
33
|
+
return outputFileNames;
|
|
34
|
+
}
|
|
35
|
+
const javaScriptExtension = getJavaScriptExtension(sourceExtension, options.jsx);
|
|
36
|
+
if (javaScriptExtension === void 0) return [];
|
|
37
|
+
if (options.emitDeclarationOnly !== true) {
|
|
38
|
+
const javaScriptPath = replaceExtension(outputPath, javaScriptExtension);
|
|
39
|
+
outputFileNames.push(javaScriptPath);
|
|
40
|
+
if (options.sourceMap === true) outputFileNames.push(`${javaScriptPath}.map`);
|
|
41
|
+
}
|
|
42
|
+
if (options.declaration === true || options.composite === true) {
|
|
43
|
+
const declarationPath = replaceExtension(getOutputPath(sourcePath, resolveOptionPath(options.declarationDir ?? options.outDir, options.configFilePath), options.rootDir, options.configFilePath), getDeclarationExtension(sourceExtension));
|
|
44
|
+
outputFileNames.push(declarationPath);
|
|
45
|
+
if (options.declarationMap === true) outputFileNames.push(`${declarationPath}.map`);
|
|
46
|
+
}
|
|
47
|
+
return outputFileNames;
|
|
48
|
+
}
|
|
49
|
+
/** Returns the build-info file `tsc` writes, or `undefined` when it writes none. */
|
|
50
|
+
function getTsBuildInfoEmitOutputFilePath(options) {
|
|
51
|
+
if (options.incremental !== true && options.composite !== true) return;
|
|
52
|
+
const { configFilePath } = options;
|
|
53
|
+
const tsBuildInfoFile = resolveOptionPath(options.tsBuildInfoFile, configFilePath);
|
|
54
|
+
if (tsBuildInfoFile !== void 0) return tsBuildInfoFile;
|
|
55
|
+
const outDir = resolveOptionPath(options.outDir, configFilePath);
|
|
56
|
+
let buildInfoPath = configFilePath;
|
|
57
|
+
if (outDir !== void 0) buildInfoPath = options.rootDir === void 0 ? path.join(outDir, path.basename(configFilePath)) : getOutputPath(configFilePath, outDir, options.rootDir, configFilePath);
|
|
58
|
+
return `${removeFileExtension(buildInfoPath)}${tsBuildInfoExtension}`;
|
|
59
|
+
}
|
|
60
|
+
function getDeclarationExtension(sourceExtension) {
|
|
61
|
+
if (esmFileExtensions.includes(sourceExtension)) return ".d.mts";
|
|
62
|
+
if (cjsFileExtensions.includes(sourceExtension)) return ".d.cts";
|
|
63
|
+
return ".d.ts";
|
|
64
|
+
}
|
|
65
|
+
function getJavaScriptExtension(sourceExtension, jsx) {
|
|
66
|
+
if (sourceExtension === ".tsx" || sourceExtension === ".jsx") return jsx === "preserve" ? ".jsx" : ".js";
|
|
67
|
+
if (sourceExtension === ".ts" || sourceExtension === ".js") return ".js";
|
|
68
|
+
if (sourceExtension === ".mts") return ".mjs";
|
|
69
|
+
if (sourceExtension === ".cts") return ".cjs";
|
|
70
|
+
if (sourceExtension === ".mjs" || sourceExtension === ".cjs") return sourceExtension;
|
|
71
|
+
}
|
|
72
|
+
function getOutputPath(sourcePath, outputDirectory, rootDir, configFilePath) {
|
|
73
|
+
if (outputDirectory === void 0) return sourcePath;
|
|
74
|
+
const resolvedRootDir = path.resolve(path.dirname(configFilePath), rootDir ?? ".");
|
|
75
|
+
return path.resolve(outputDirectory, path.relative(resolvedRootDir, sourcePath));
|
|
76
|
+
}
|
|
77
|
+
function isDeclarationFile(fileName) {
|
|
78
|
+
return declarationFileExtensions.some((extension) => fileName.endsWith(extension)) || /\.d\..+\.ts$/u.test(path.basename(fileName));
|
|
79
|
+
}
|
|
80
|
+
function removeFileExtension(fileName) {
|
|
81
|
+
const extension = knownFileExtensions.find((knownExtension) => fileName.endsWith(knownExtension));
|
|
82
|
+
return extension === void 0 ? fileName : fileName.slice(0, -extension.length);
|
|
83
|
+
}
|
|
84
|
+
function replaceExtension(fileName, extension) {
|
|
85
|
+
return `${fileName.slice(0, -path.extname(fileName).length)}${extension}`;
|
|
86
|
+
}
|
|
87
|
+
function resolveOptionPath(optionPath, configFilePath) {
|
|
88
|
+
return optionPath === void 0 ? void 0 : path.resolve(path.dirname(configFilePath), optionPath);
|
|
89
|
+
}
|
|
90
|
+
// src/common/typescript/typescript7Compiler.ts
|
|
91
|
+
const syncModuleSpecifier = "typescript/sync";
|
|
92
|
+
/**
|
|
93
|
+
* Uses the `typescript/sync` API for TypeScript 7.1 and later.
|
|
94
|
+
* The documented synchronous client fits this synchronous compiler contract.
|
|
95
|
+
*/
|
|
96
|
+
var TypeScript7Compiler = class TypeScript7Compiler {
|
|
97
|
+
useCaseSensitiveFileNames;
|
|
98
|
+
api;
|
|
99
|
+
typescriptSync;
|
|
100
|
+
constructor(typescriptSync, api) {
|
|
101
|
+
this.typescriptSync = typescriptSync;
|
|
102
|
+
this.api = api;
|
|
103
|
+
this.useCaseSensitiveFileNames = api.getCanonicalFileName("a") !== api.getCanonicalFileName("A");
|
|
104
|
+
}
|
|
105
|
+
static async create() {
|
|
106
|
+
const typescriptSync = await loadTypeScript7SyncModule();
|
|
107
|
+
const api = new typescriptSync.API();
|
|
108
|
+
try {
|
|
109
|
+
api.parseCommandLine([]);
|
|
110
|
+
return new TypeScript7Compiler(typescriptSync, api);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
api.close();
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
close() {
|
|
117
|
+
this.api.close();
|
|
118
|
+
}
|
|
119
|
+
parseProject(configFilePath) {
|
|
120
|
+
try {
|
|
121
|
+
fs.readFileSync(configFilePath);
|
|
122
|
+
} catch {
|
|
123
|
+
throw new UserError(`error TS5083: Cannot read file '${configFilePath}'.`);
|
|
124
|
+
}
|
|
125
|
+
const checkDiagnostics = (diagnostics) => {
|
|
126
|
+
if (diagnostics.length === 0) return;
|
|
127
|
+
throw new UserError(this.typescriptSync.formatDiagnosticsWithColorAndContext(diagnostics, {
|
|
128
|
+
getCanonicalFileName: (fileName) => this.api.getCanonicalFileName(fileName),
|
|
129
|
+
getCurrentDirectory: () => this.api.getCurrentDirectory(),
|
|
130
|
+
getNewLine: () => this.api.getNewLine()
|
|
131
|
+
}).trimEnd());
|
|
132
|
+
};
|
|
133
|
+
const checkConfigFile = (filePath) => {
|
|
134
|
+
const result = this.api.readConfigFile(filePath);
|
|
135
|
+
if (result.error !== void 0) checkDiagnostics([result.error]);
|
|
136
|
+
};
|
|
137
|
+
checkConfigFile(configFilePath);
|
|
138
|
+
const config = this.api.parseConfigFile(configFilePath);
|
|
139
|
+
checkDiagnostics(config.errors);
|
|
140
|
+
const program = this.api.createProgram(config.fileNames, config.options);
|
|
141
|
+
let sourceFileNames;
|
|
142
|
+
try {
|
|
143
|
+
sourceFileNames = program.getSourceFileNames().filter((fileName) => {
|
|
144
|
+
const metadata = program.getSourceFileMetadata(fileName);
|
|
145
|
+
return metadata !== void 0 && !metadata.isDefaultLibrary && !metadata.isFromExternalLibrary;
|
|
146
|
+
});
|
|
147
|
+
} finally {
|
|
148
|
+
program.dispose();
|
|
149
|
+
}
|
|
150
|
+
const outputOptions = {
|
|
151
|
+
...config.options,
|
|
152
|
+
configFilePath,
|
|
153
|
+
jsx: config.options.jsx === this.typescriptSync.JsxEmit.Preserve ? "preserve" : void 0
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
getOutputPaths: () => {
|
|
157
|
+
const outputPaths = new Set(sourceFileNames.flatMap((fileName) => getOutputFileNames(outputOptions, fileName)));
|
|
158
|
+
const tsBuildInfoFile = getTsBuildInfoEmitOutputFilePath(outputOptions);
|
|
159
|
+
if (tsBuildInfoFile !== void 0) outputPaths.add(tsBuildInfoFile);
|
|
160
|
+
return outputPaths;
|
|
161
|
+
},
|
|
162
|
+
options: config.options,
|
|
163
|
+
sourceFileNames
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
async function loadTypeScript7SyncModule() {
|
|
168
|
+
return await import(syncModuleSpecifier);
|
|
169
|
+
}
|
|
170
|
+
export { TypeScript7Compiler };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { i as UserError, r as typescriptVersion } from "./tscAfter.js";
|
|
3
|
+
// src/common/typescript/typescriptPre7Compiler.ts
|
|
4
|
+
/** Uses the `typescript` API for TypeScript 3.9 through 6.x. */
|
|
5
|
+
var TypeScriptPre7Compiler = class {
|
|
6
|
+
useCaseSensitiveFileNames;
|
|
7
|
+
constructor() {
|
|
8
|
+
this.useCaseSensitiveFileNames = ts.sys.useCaseSensitiveFileNames;
|
|
9
|
+
}
|
|
10
|
+
close() {}
|
|
11
|
+
parseProject(configFilePath) {
|
|
12
|
+
const parsedCommandLine = parseCommandLine(configFilePath);
|
|
13
|
+
const program = ts.createProgram(parsedCommandLine.fileNames, parsedCommandLine.options);
|
|
14
|
+
const sourceFileNames = program.getSourceFiles().filter((sourceFile) => !program.isSourceFileDefaultLibrary(sourceFile) && !program.isSourceFileFromExternalLibrary(sourceFile)).map((sourceFile) => sourceFile.fileName);
|
|
15
|
+
return {
|
|
16
|
+
getOutputPaths: () => this.getOutputPaths(parsedCommandLine, sourceFileNames),
|
|
17
|
+
options: parsedCommandLine.options,
|
|
18
|
+
sourceFileNames
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
getOutputPaths(parsedCommandLine, sourceFileNames) {
|
|
22
|
+
if (typescriptVersion < 4002e3 && parsedCommandLine.options.rootDir === void 0) throw new UserError("Refusing to clean outDir: TypeScript 3.9 through 4.1 require rootDir to be set.");
|
|
23
|
+
const emittableFileNames = sourceFileNames.filter((fileName) => !isDeclarationFileName(fileName));
|
|
24
|
+
if (typescriptVersion < 4e6 && parsedCommandLine.options.emitDeclarationOnly === true && parsedCommandLine.options.resolveJsonModule === true && emittableFileNames.some((fileName) => fileName.endsWith(".json"))) throw new UserError("Refusing to clean outDir: TypeScript 3.9 cannot safely determine outputs for emitDeclarationOnly with resolveJsonModule.");
|
|
25
|
+
if (typescriptVersion < 4e6 && parsedCommandLine.options.allowJs === true && parsedCommandLine.options.jsx === ts.JsxEmit.Preserve && parsedCommandLine.options.declaration === true && emittableFileNames.some((fileName) => fileName.endsWith(".jsx"))) throw new UserError("Refusing to clean outDir: TypeScript 3.9 cannot safely determine outputs for allowJs JSX preserve with declaration.");
|
|
26
|
+
const outputCommandLine = {
|
|
27
|
+
...parsedCommandLine,
|
|
28
|
+
fileNames: emittableFileNames
|
|
29
|
+
};
|
|
30
|
+
const outputPaths = /* @__PURE__ */ new Set();
|
|
31
|
+
for (const fileName of emittableFileNames) for (const outputPath of ts.getOutputFileNames(outputCommandLine, fileName, !this.useCaseSensitiveFileNames)) outputPaths.add(outputPath);
|
|
32
|
+
const tsBuildInfoFile = ts.getTsBuildInfoEmitOutputFilePath(parsedCommandLine.options);
|
|
33
|
+
if (tsBuildInfoFile !== void 0) outputPaths.add(tsBuildInfoFile);
|
|
34
|
+
return outputPaths;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
function isConfigFileMetadata(value) {
|
|
38
|
+
if (typeof value !== "object" || value === null || !("fileName" in value) || typeof value.fileName !== "string") return false;
|
|
39
|
+
if ("extendedSourceFiles" in value && value.extendedSourceFiles !== void 0) return Array.isArray(value.extendedSourceFiles) && value.extendedSourceFiles.every((fileName) => typeof fileName === "string");
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
function isDeclarationFileName(fileName) {
|
|
43
|
+
return fileName.endsWith(".d.ts") || fileName.endsWith(".d.mts") || fileName.endsWith(".d.cts");
|
|
44
|
+
}
|
|
45
|
+
function parseCommandLine(configFilePath) {
|
|
46
|
+
const formatHost = {
|
|
47
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
48
|
+
getCurrentDirectory: () => process.cwd(),
|
|
49
|
+
getNewLine: () => ts.sys.newLine
|
|
50
|
+
};
|
|
51
|
+
const checkConfigFile = (filePath) => {
|
|
52
|
+
const config = ts.readConfigFile(filePath.replaceAll("\\", "/"), (name) => ts.sys.readFile(name));
|
|
53
|
+
if (config.error !== void 0) throw new UserError(ts.formatDiagnostics([config.error], formatHost).trimEnd());
|
|
54
|
+
};
|
|
55
|
+
checkConfigFile(configFilePath);
|
|
56
|
+
const parsedCommandLine = ts.getParsedCommandLineOfConfigFile(configFilePath, void 0, {
|
|
57
|
+
...ts.sys,
|
|
58
|
+
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
59
|
+
throw new UserError(ts.formatDiagnostics([diagnostic], formatHost).trimEnd());
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
if (parsedCommandLine === void 0) throw new UserError("Could not read tsconfig.json.");
|
|
63
|
+
if (parsedCommandLine.errors.length > 0) throw new UserError(ts.formatDiagnostics(parsedCommandLine.errors, formatHost).trimEnd());
|
|
64
|
+
const configFileOption = parsedCommandLine.options["configFile"];
|
|
65
|
+
const configFile = isConfigFileMetadata(configFileOption) ? configFileOption : void 0;
|
|
66
|
+
for (const filePath of configFile?.extendedSourceFiles ?? []) checkConfigFile(filePath);
|
|
67
|
+
return parsedCommandLine;
|
|
68
|
+
}
|
|
69
|
+
export { TypeScriptPre7Compiler };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tsc-after",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Remove stale build output that tsc leaves behind when you delete or rename source files. Tsconfig-aware: just run tsc && tsc-after. 🧹",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"build",
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"*": "prettier --ignore-unknown --write"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
+
"@codecov/bundle-analyzer": "2.0.1",
|
|
58
59
|
"@commitlint/cli": "21.2.4",
|
|
59
60
|
"@commitlint/config-conventional": "21.2.4",
|
|
60
61
|
"@eslint-community/eslint-plugin-eslint-comments": "4.8.1",
|
|
@@ -74,7 +75,7 @@
|
|
|
74
75
|
"eslint": "10.15.0",
|
|
75
76
|
"eslint-import-resolver-typescript": "4.4.5",
|
|
76
77
|
"eslint-plugin-import-x": "4.18.1",
|
|
77
|
-
"eslint-plugin-jsdoc": "65.5.
|
|
78
|
+
"eslint-plugin-jsdoc": "65.5.1",
|
|
78
79
|
"eslint-plugin-jsonc": "3.4.2",
|
|
79
80
|
"eslint-plugin-n": "18.5.0",
|
|
80
81
|
"eslint-plugin-package-json": "1.14.0",
|
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { r as UserError } from "./tscAfter.js";
|
|
4
|
-
// src/common/typescript/outputPaths.ts
|
|
5
|
-
function getOutputFileNames(options, fileName) {
|
|
6
|
-
const sourcePath = path.resolve(fileName);
|
|
7
|
-
const sourceExtension = path.extname(sourcePath);
|
|
8
|
-
if (isDeclarationFile(sourcePath)) return [];
|
|
9
|
-
const isJsonFile = sourceExtension === ".json";
|
|
10
|
-
if ((sourceExtension === ".js" || sourceExtension === ".jsx" || sourceExtension === ".mjs" || sourceExtension === ".cjs") && options.allowJs !== true) return [];
|
|
11
|
-
const outputFileNames = [];
|
|
12
|
-
const outputPath = getOutputPath(sourcePath, resolveOptionPath(options.outDir, options.configFilePath), options.rootDir, options.configFilePath);
|
|
13
|
-
if (isJsonFile) {
|
|
14
|
-
outputFileNames.push(outputPath);
|
|
15
|
-
return outputFileNames;
|
|
16
|
-
}
|
|
17
|
-
const javaScriptExtension = getJavaScriptExtension(sourceExtension, options.jsx);
|
|
18
|
-
if (javaScriptExtension === void 0) return [];
|
|
19
|
-
if (options.emitDeclarationOnly !== true) {
|
|
20
|
-
const javaScriptPath = replaceExtension(outputPath, javaScriptExtension);
|
|
21
|
-
outputFileNames.push(javaScriptPath);
|
|
22
|
-
if (options.sourceMap === true) outputFileNames.push(`${javaScriptPath}.map`);
|
|
23
|
-
}
|
|
24
|
-
if (options.declaration === true || options.composite === true) {
|
|
25
|
-
const declarationPath = replaceExtension(getOutputPath(sourcePath, resolveOptionPath(options.declarationDir ?? options.outDir, options.configFilePath), options.rootDir, options.configFilePath), getDeclarationExtension(sourceExtension));
|
|
26
|
-
outputFileNames.push(declarationPath);
|
|
27
|
-
if (options.declarationMap === true) outputFileNames.push(`${declarationPath}.map`);
|
|
28
|
-
}
|
|
29
|
-
return outputFileNames;
|
|
30
|
-
}
|
|
31
|
-
function getDeclarationExtension(sourceExtension) {
|
|
32
|
-
if (sourceExtension === ".mts" || sourceExtension === ".mjs") return ".d.mts";
|
|
33
|
-
if (sourceExtension === ".cts" || sourceExtension === ".cjs") return ".d.cts";
|
|
34
|
-
return ".d.ts";
|
|
35
|
-
}
|
|
36
|
-
function getJavaScriptExtension(sourceExtension, jsx) {
|
|
37
|
-
if (sourceExtension === ".tsx" || sourceExtension === ".jsx") return jsx === "preserve" ? ".jsx" : ".js";
|
|
38
|
-
if (sourceExtension === ".ts" || sourceExtension === ".js") return ".js";
|
|
39
|
-
if (sourceExtension === ".mts") return ".mjs";
|
|
40
|
-
if (sourceExtension === ".cts") return ".cjs";
|
|
41
|
-
if (sourceExtension === ".mjs" || sourceExtension === ".cjs") return sourceExtension;
|
|
42
|
-
}
|
|
43
|
-
function getOutputPath(sourcePath, outputDirectory, rootDir, configFilePath) {
|
|
44
|
-
if (outputDirectory === void 0) return sourcePath;
|
|
45
|
-
const resolvedRootDir = path.resolve(path.dirname(configFilePath), rootDir ?? ".");
|
|
46
|
-
return path.resolve(outputDirectory, path.relative(resolvedRootDir, sourcePath));
|
|
47
|
-
}
|
|
48
|
-
function isDeclarationFile(fileName) {
|
|
49
|
-
return fileName.endsWith(".d.ts") || fileName.endsWith(".d.mts") || fileName.endsWith(".d.cts");
|
|
50
|
-
}
|
|
51
|
-
function replaceExtension(fileName, extension) {
|
|
52
|
-
return `${fileName.slice(0, -path.extname(fileName).length)}${extension}`;
|
|
53
|
-
}
|
|
54
|
-
function resolveOptionPath(optionPath, configFilePath) {
|
|
55
|
-
return optionPath === void 0 ? void 0 : path.resolve(path.dirname(configFilePath), optionPath);
|
|
56
|
-
}
|
|
57
|
-
// src/common/typescript/typescript7Compiler.ts
|
|
58
|
-
const syncModuleSpecifier = "typescript/sync";
|
|
59
|
-
/**
|
|
60
|
-
* Uses the `typescript/sync` API for TypeScript 7.1 and later.
|
|
61
|
-
* The documented synchronous client fits this synchronous compiler contract.
|
|
62
|
-
*/
|
|
63
|
-
var TypeScript7Compiler = class TypeScript7Compiler {
|
|
64
|
-
useCaseSensitiveFileNames;
|
|
65
|
-
api;
|
|
66
|
-
cachedProject;
|
|
67
|
-
isClosed = false;
|
|
68
|
-
program;
|
|
69
|
-
projects = /* @__PURE__ */ new WeakMap();
|
|
70
|
-
typescriptSync;
|
|
71
|
-
constructor(typescriptSync, api) {
|
|
72
|
-
this.typescriptSync = typescriptSync;
|
|
73
|
-
this.api = api;
|
|
74
|
-
this.useCaseSensitiveFileNames = api.getCanonicalFileName("a") !== api.getCanonicalFileName("A");
|
|
75
|
-
}
|
|
76
|
-
static async create() {
|
|
77
|
-
const typescriptSyncModule = await import(syncModuleSpecifier);
|
|
78
|
-
if (!isTypeScript7SyncModule(typescriptSyncModule)) throw new UserError("Could not load the TypeScript 7.1 synchronous API.");
|
|
79
|
-
const apiValue = new typescriptSyncModule.API();
|
|
80
|
-
if (!isTypeScript7Api(apiValue)) throw new UserError("Could not initialize the TypeScript 7.1 synchronous API.");
|
|
81
|
-
const typescriptSync = typescriptSyncModule;
|
|
82
|
-
const api = apiValue;
|
|
83
|
-
try {
|
|
84
|
-
api.parseCommandLine([]);
|
|
85
|
-
return new TypeScript7Compiler(typescriptSync, api);
|
|
86
|
-
} catch (error) {
|
|
87
|
-
api.close();
|
|
88
|
-
throw error;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
close() {
|
|
92
|
-
if (this.isClosed) return;
|
|
93
|
-
this.isClosed = true;
|
|
94
|
-
const program = this.program;
|
|
95
|
-
this.program = void 0;
|
|
96
|
-
this.cachedProject = void 0;
|
|
97
|
-
try {
|
|
98
|
-
program?.dispose();
|
|
99
|
-
} finally {
|
|
100
|
-
this.api.close();
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
getOutputPaths(project) {
|
|
104
|
-
this.assertOpen();
|
|
105
|
-
const config = this.projects.get(project);
|
|
106
|
-
if (config === void 0) throw new Error("Project was not parsed by this compiler.");
|
|
107
|
-
if (this.cachedProject !== project) {
|
|
108
|
-
const previousProgram = this.program;
|
|
109
|
-
this.program = void 0;
|
|
110
|
-
this.cachedProject = void 0;
|
|
111
|
-
previousProgram?.dispose();
|
|
112
|
-
this.program = this.api.createProgram(config.fileNames, config.options);
|
|
113
|
-
this.cachedProject = project;
|
|
114
|
-
}
|
|
115
|
-
const program = this.program;
|
|
116
|
-
if (program === void 0) throw new Error("Could not create TypeScript program.");
|
|
117
|
-
const configFilePath = project.configFilePaths[0];
|
|
118
|
-
if (configFilePath === void 0) throw new Error("Project has no config file path.");
|
|
119
|
-
const outputOptions = {
|
|
120
|
-
...config.options,
|
|
121
|
-
configFilePath,
|
|
122
|
-
jsx: config.options.jsx === this.typescriptSync.JsxEmit.Preserve ? "preserve" : void 0
|
|
123
|
-
};
|
|
124
|
-
const outputPaths = program.getSourceFileNames().filter((fileName) => {
|
|
125
|
-
const metadata = program.getSourceFileMetadata(fileName);
|
|
126
|
-
return metadata !== void 0 && !metadata.isDefaultLibrary && !metadata.isFromExternalLibrary;
|
|
127
|
-
}).flatMap((fileName) => getOutputFileNames(outputOptions, fileName));
|
|
128
|
-
return new Set(outputPaths);
|
|
129
|
-
}
|
|
130
|
-
parseProject(configFilePath) {
|
|
131
|
-
this.assertOpen();
|
|
132
|
-
try {
|
|
133
|
-
fs.readFileSync(configFilePath);
|
|
134
|
-
} catch {
|
|
135
|
-
throw new UserError(`error TS5083: Cannot read file '${configFilePath}'.`);
|
|
136
|
-
}
|
|
137
|
-
const checkDiagnostics = (diagnostics) => {
|
|
138
|
-
if (diagnostics.length === 0) return;
|
|
139
|
-
throw new UserError(this.typescriptSync.formatDiagnosticsWithColorAndContext(diagnostics, {
|
|
140
|
-
getCanonicalFileName: (fileName) => this.api.getCanonicalFileName(fileName),
|
|
141
|
-
getCurrentDirectory: () => this.api.getCurrentDirectory(),
|
|
142
|
-
getNewLine: () => this.api.getNewLine()
|
|
143
|
-
}).trimEnd());
|
|
144
|
-
};
|
|
145
|
-
const readConfig = (filePath) => {
|
|
146
|
-
const result = this.api.readConfigFile(filePath);
|
|
147
|
-
if (result.error !== void 0) checkDiagnostics([result.error]);
|
|
148
|
-
return result.config;
|
|
149
|
-
};
|
|
150
|
-
const rootConfig = readConfig(configFilePath);
|
|
151
|
-
const config = this.api.parseConfigFile(configFilePath);
|
|
152
|
-
checkDiagnostics(config.errors);
|
|
153
|
-
const extendedConfigInfo = getExtendedConfigInfo(readConfig, configFilePath, rootConfig);
|
|
154
|
-
const program = this.api.createProgram(config.fileNames, config.options);
|
|
155
|
-
const project = {
|
|
156
|
-
configFilePaths: extendedConfigInfo.configFilePaths,
|
|
157
|
-
fileNames: config.fileNames,
|
|
158
|
-
hasWatchOptions: extendedConfigInfo.hasWatchOptions,
|
|
159
|
-
options: config.options,
|
|
160
|
-
programFileNames: program.getSourceFileNames().filter((fileName) => {
|
|
161
|
-
const metadata = program.getSourceFileMetadata(fileName);
|
|
162
|
-
return metadata !== void 0 && !metadata.isDefaultLibrary && !metadata.isFromExternalLibrary;
|
|
163
|
-
})
|
|
164
|
-
};
|
|
165
|
-
this.program?.dispose();
|
|
166
|
-
this.program = program;
|
|
167
|
-
this.cachedProject = project;
|
|
168
|
-
this.projects.set(project, config);
|
|
169
|
-
return project;
|
|
170
|
-
}
|
|
171
|
-
assertOpen() {
|
|
172
|
-
if (this.isClosed) throw new Error("Compiler is closed.");
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
function getExtendedConfigInfo(readConfig, configFilePath, rootConfig) {
|
|
176
|
-
const configFilePaths = /* @__PURE__ */ new Set();
|
|
177
|
-
let hasWatchOptions = false;
|
|
178
|
-
function visit(currentConfigFilePath, rawConfig) {
|
|
179
|
-
const normalizedConfigFilePath = path.resolve(currentConfigFilePath);
|
|
180
|
-
if (configFilePaths.has(normalizedConfigFilePath)) return;
|
|
181
|
-
configFilePaths.add(normalizedConfigFilePath);
|
|
182
|
-
if (!isRecord(rawConfig)) throw new UserError(`Could not safely read TypeScript config '${normalizedConfigFilePath}'.`);
|
|
183
|
-
hasWatchOptions ||= rawConfig["watchOptions"] !== void 0;
|
|
184
|
-
const extendsValue = rawConfig["extends"];
|
|
185
|
-
if (extendsValue === void 0) return;
|
|
186
|
-
const extendedConfigPaths = Array.isArray(extendsValue) ? extendsValue : [extendsValue];
|
|
187
|
-
for (const extendedConfigPath of extendedConfigPaths) {
|
|
188
|
-
if (typeof extendedConfigPath !== "string") throw new UserError(`Could not safely determine extended config paths for '${normalizedConfigFilePath}'.`);
|
|
189
|
-
const resolvedConfigPath = resolveExtendedConfigPath(normalizedConfigFilePath, extendedConfigPath);
|
|
190
|
-
visit(resolvedConfigPath, readConfig(resolvedConfigPath));
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
visit(configFilePath, rootConfig);
|
|
194
|
-
return {
|
|
195
|
-
configFilePaths: [...configFilePaths],
|
|
196
|
-
hasWatchOptions
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
function isRecord(value) {
|
|
200
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
201
|
-
}
|
|
202
|
-
function isTypeScript7Api(value) {
|
|
203
|
-
return isRecord(value) && typeof value["close"] === "function" && typeof value["createProgram"] === "function" && typeof value["getCanonicalFileName"] === "function" && typeof value["getCurrentDirectory"] === "function" && typeof value["getNewLine"] === "function" && typeof value["parseCommandLine"] === "function" && typeof value["parseConfigFile"] === "function" && typeof value["readConfigFile"] === "function";
|
|
204
|
-
}
|
|
205
|
-
function isTypeScript7SyncModule(value) {
|
|
206
|
-
if (!isRecord(value)) return false;
|
|
207
|
-
const jsxEmit = value["JsxEmit"];
|
|
208
|
-
return typeof value["API"] === "function" && isRecord(jsxEmit) && typeof jsxEmit["Preserve"] === "number" && typeof value["formatDiagnosticsWithColorAndContext"] === "function";
|
|
209
|
-
}
|
|
210
|
-
function resolveExtendedConfigPath(configFilePath, extendsSpecifier) {
|
|
211
|
-
const specifier = extendsSpecifier.replaceAll("\\", "/");
|
|
212
|
-
if (path.isAbsolute(specifier) || specifier.startsWith("./") || specifier.startsWith("../")) {
|
|
213
|
-
const basePath = path.resolve(path.dirname(configFilePath), specifier);
|
|
214
|
-
for (const candidatePath of [basePath, `${basePath}.json`]) try {
|
|
215
|
-
if (fs.statSync(candidatePath).isFile()) return candidatePath;
|
|
216
|
-
} catch {}
|
|
217
|
-
}
|
|
218
|
-
throw new UserError(`Could not safely determine extended config '${extendsSpecifier}' from '${configFilePath}'.`);
|
|
219
|
-
}
|
|
220
|
-
export { TypeScript7Compiler };
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
import { n as typescriptVersion, r as UserError } from "./tscAfter.js";
|
|
3
|
-
// src/common/typescript/typescriptPre7Compiler.ts
|
|
4
|
-
/** Uses the `typescript` API for TypeScript 3.9 through 6.x. */
|
|
5
|
-
var TypeScriptPre7Compiler = class {
|
|
6
|
-
useCaseSensitiveFileNames;
|
|
7
|
-
parsedCommandLines = /* @__PURE__ */ new WeakMap();
|
|
8
|
-
constructor() {
|
|
9
|
-
this.useCaseSensitiveFileNames = ts.sys.useCaseSensitiveFileNames;
|
|
10
|
-
}
|
|
11
|
-
close() {}
|
|
12
|
-
getOutputPaths(project) {
|
|
13
|
-
const parsedCommandLine = this.parsedCommandLines.get(project);
|
|
14
|
-
if (parsedCommandLine === void 0) throw new Error("Project was not parsed by this compiler.");
|
|
15
|
-
if (typescriptVersion < 4002e3 && parsedCommandLine.options.rootDir === void 0) throw new UserError("Refusing to clean outDir: TypeScript 3.9 through 4.1 require rootDir to be set.");
|
|
16
|
-
const emittableFileNames = project.programFileNames.filter((fileName) => !isDeclarationFileName(fileName));
|
|
17
|
-
const outputCommandLine = {
|
|
18
|
-
...parsedCommandLine,
|
|
19
|
-
fileNames: emittableFileNames
|
|
20
|
-
};
|
|
21
|
-
const outputPaths = /* @__PURE__ */ new Set();
|
|
22
|
-
for (const fileName of emittableFileNames) for (const outputPath of ts.getOutputFileNames(outputCommandLine, fileName, !this.useCaseSensitiveFileNames)) outputPaths.add(outputPath);
|
|
23
|
-
return outputPaths;
|
|
24
|
-
}
|
|
25
|
-
parseProject(configFilePath) {
|
|
26
|
-
const formatHost = {
|
|
27
|
-
getCanonicalFileName: (fileName) => fileName,
|
|
28
|
-
getCurrentDirectory: () => process.cwd(),
|
|
29
|
-
getNewLine: () => ts.sys.newLine
|
|
30
|
-
};
|
|
31
|
-
const checkConfigFile = (filePath) => {
|
|
32
|
-
const config = ts.readConfigFile(filePath, (name) => ts.sys.readFile(name));
|
|
33
|
-
if (config.error !== void 0) throw new UserError(ts.formatDiagnostics([config.error], formatHost).trimEnd());
|
|
34
|
-
};
|
|
35
|
-
checkConfigFile(configFilePath);
|
|
36
|
-
const parsedCommandLine = ts.getParsedCommandLineOfConfigFile(configFilePath, void 0, {
|
|
37
|
-
...ts.sys,
|
|
38
|
-
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
39
|
-
throw new UserError(ts.formatDiagnostics([diagnostic], formatHost).trimEnd());
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
if (parsedCommandLine === void 0) throw new UserError("Could not read tsconfig.json.");
|
|
43
|
-
if (parsedCommandLine.errors.length > 0) throw new UserError(ts.formatDiagnostics(parsedCommandLine.errors, formatHost).trimEnd());
|
|
44
|
-
const configFileOption = parsedCommandLine.options["configFile"];
|
|
45
|
-
const configFile = isConfigFileMetadata(configFileOption) ? configFileOption : void 0;
|
|
46
|
-
for (const filePath of configFile?.extendedSourceFiles ?? []) checkConfigFile(filePath);
|
|
47
|
-
const program = ts.createProgram(parsedCommandLine.fileNames, parsedCommandLine.options);
|
|
48
|
-
const project = {
|
|
49
|
-
configFilePaths: [configFile?.fileName ?? configFilePath, ...configFile?.extendedSourceFiles ?? []],
|
|
50
|
-
fileNames: parsedCommandLine.fileNames,
|
|
51
|
-
hasWatchOptions: parsedCommandLine.watchOptions !== void 0,
|
|
52
|
-
options: parsedCommandLine.options,
|
|
53
|
-
programFileNames: program.getSourceFiles().filter((sourceFile) => !program.isSourceFileDefaultLibrary(sourceFile) && !program.isSourceFileFromExternalLibrary(sourceFile)).map((sourceFile) => sourceFile.fileName)
|
|
54
|
-
};
|
|
55
|
-
this.parsedCommandLines.set(project, parsedCommandLine);
|
|
56
|
-
return project;
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
function isConfigFileMetadata(value) {
|
|
60
|
-
if (typeof value !== "object" || value === null || !("fileName" in value) || typeof value.fileName !== "string") return false;
|
|
61
|
-
if ("extendedSourceFiles" in value && value.extendedSourceFiles !== void 0) return Array.isArray(value.extendedSourceFiles) && value.extendedSourceFiles.every((fileName) => typeof fileName === "string");
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
function isDeclarationFileName(fileName) {
|
|
65
|
-
return fileName.endsWith(".d.ts") || fileName.endsWith(".d.mts") || fileName.endsWith(".d.cts");
|
|
66
|
-
}
|
|
67
|
-
export { TypeScriptPre7Compiler };
|