tsc-after 0.4.1 → 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
CHANGED
|
@@ -48,10 +48,9 @@ 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` and `declarationDir` must hold only TypeScript outputs: anything else, including copied assets
|
|
52
|
-
-
|
|
53
|
-
-
|
|
54
|
-
- Support for TypeScript versions 3.9 - 4.1 is limited:
|
|
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:
|
|
55
54
|
- `rootDir` is required ([#15](https://github.com/michal-kocarek/tsc-after/issues/15)).
|
|
56
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)).
|
|
57
56
|
- TypeScript 7.1 beta support is experimental.
|
|
@@ -66,24 +65,25 @@ See [`product.md`](./docs/product.md) for the roadmap.
|
|
|
66
65
|
- Cleanup is refused when `outDir` or `declarationDir` contains a source file of the project.
|
|
67
66
|
- Cleanup is refused when `outDir` or `declarationDir` is reached through a symbolic link.
|
|
68
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.
|
|
69
69
|
|
|
70
70
|
## Requirements
|
|
71
71
|
|
|
72
72
|
- Node.js 16.19.0 or later, including the latest LTS.
|
|
73
73
|
- TypeScript 3.9.10 through 7.x, including 7.1 beta (except 7.0, which has no API support).
|
|
74
74
|
|
|
75
|
-
## Exit codes
|
|
76
|
-
|
|
77
|
-
- `0` for success, including a dry run or nothing to remove
|
|
78
|
-
- `1` for a configuration or cleanup error
|
|
79
|
-
- `2` for invalid command-line arguments
|
|
80
|
-
|
|
81
75
|
## Why not `rm -rf dist && tsc`?
|
|
82
76
|
|
|
83
77
|
Wiping `outDir` before every build (`rm -rf dist && tsc`, `rimraf dist && tsc`, or a `prebuild` script) is the usual fix for stale outputs.
|
|
84
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)).
|
|
85
79
|
|
|
86
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.
|
|
87
87
|
|
|
88
88
|
**Using a bundler such as tsup, Vite, or esbuild?**<br>
|
|
89
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. */
|
|
@@ -122,18 +122,86 @@ function normalizePathForComparison(filePath, useCaseSensitiveFileNames) {
|
|
|
122
122
|
const normalizedPath = path.resolve(filePath);
|
|
123
123
|
return useCaseSensitiveFileNames ? normalizedPath : normalizedPath.toLowerCase();
|
|
124
124
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return plan;
|
|
157
|
+
}
|
|
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);
|
|
185
|
+
}
|
|
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) {
|
|
129
198
|
try {
|
|
130
|
-
fs.lstatSync(
|
|
199
|
+
fs.lstatSync(rootPath);
|
|
131
200
|
} catch (error) {
|
|
132
201
|
if (isNodeErrorOf(error, Error, "ENOENT")) return [];
|
|
133
202
|
throw error;
|
|
134
203
|
}
|
|
135
|
-
|
|
136
|
-
return readDir(root.path);
|
|
204
|
+
return readDir(rootPath);
|
|
137
205
|
function readDir(dirPath) {
|
|
138
206
|
const verdicts = [];
|
|
139
207
|
const stat = fs.lstatSync(dirPath);
|
|
@@ -161,7 +229,7 @@ function readCleanupRoot(root, expectedOutputPaths, useCaseSensitiveFileNames) {
|
|
|
161
229
|
continue;
|
|
162
230
|
}
|
|
163
231
|
if (fullPathStat.isFile()) {
|
|
164
|
-
if (
|
|
232
|
+
if (isOutputPath(fullPath)) {
|
|
165
233
|
verdicts.push({
|
|
166
234
|
path: fullPath,
|
|
167
235
|
reason: "expected output file",
|
|
@@ -177,6 +245,11 @@ function readCleanupRoot(root, expectedOutputPaths, useCaseSensitiveFileNames) {
|
|
|
177
245
|
continue;
|
|
178
246
|
}
|
|
179
247
|
if (fullPathStat.isDirectory()) {
|
|
248
|
+
const isRemovedElsewhere = isDirOwnedElsewhere(fullPath);
|
|
249
|
+
if (isRemovedElsewhere !== void 0) {
|
|
250
|
+
if (!isRemovedElsewhere) dirIsEmpty = false;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
180
253
|
const childVerdicts = readDir(fullPath);
|
|
181
254
|
verdicts.push(...childVerdicts);
|
|
182
255
|
if (childVerdicts.some((verdict) => verdict.type === "skipped")) dirIsEmpty = false;
|
|
@@ -201,23 +274,18 @@ function readCleanupRoot(root, expectedOutputPaths, useCaseSensitiveFileNames) {
|
|
|
201
274
|
return verdicts;
|
|
202
275
|
}
|
|
203
276
|
}
|
|
204
|
-
function resolveCleanupRoots(project, cwd
|
|
205
|
-
|
|
206
|
-
const outDirAbsolutePath = path.resolve(cwd, project.options.outDir);
|
|
277
|
+
function resolveCleanupRoots(project, cwd) {
|
|
278
|
+
const { declarationDir, outDir, useCaseSensitiveFileNames } = project;
|
|
207
279
|
const roots = [{
|
|
208
280
|
name: "outDir",
|
|
209
|
-
path:
|
|
210
|
-
relativePath: path.relative(cwd,
|
|
281
|
+
path: outDir,
|
|
282
|
+
relativePath: path.relative(cwd, outDir)
|
|
211
283
|
}];
|
|
212
|
-
if (
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
path: declarationDir,
|
|
218
|
-
relativePath: path.relative(cwd, declarationDir)
|
|
219
|
-
});
|
|
220
|
-
}
|
|
284
|
+
if (declarationDir !== void 0) roots.push({
|
|
285
|
+
name: "declarationDir",
|
|
286
|
+
path: declarationDir,
|
|
287
|
+
relativePath: path.relative(cwd, declarationDir)
|
|
288
|
+
});
|
|
221
289
|
for (const root of roots) {
|
|
222
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}!`);
|
|
223
291
|
let componentPath = cwd;
|
|
@@ -233,21 +301,18 @@ function resolveCleanupRoots(project, cwd, useCaseSensitiveFileNames) {
|
|
|
233
301
|
if (stat.isSymbolicLink()) throw new UserError(`Refusing to clean ${root.name} through a symbolic link: ${root.relativePath}!`);
|
|
234
302
|
if (!stat.isDirectory()) throw new UserError(notADirectoryError(root, path.relative(cwd, componentPath)));
|
|
235
303
|
}
|
|
236
|
-
for (const file of project.
|
|
304
|
+
for (const file of project.sourceFileNames) {
|
|
237
305
|
const filePath = path.resolve(cwd, file);
|
|
238
306
|
let realFilePath = filePath;
|
|
239
307
|
try {
|
|
240
|
-
realFilePath =
|
|
308
|
+
realFilePath = realpath(filePath);
|
|
241
309
|
} catch (error) {
|
|
242
310
|
if (!isNodeErrorOf(error, Error, "ENOENT")) throw error;
|
|
243
311
|
}
|
|
244
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)}'.`);
|
|
245
313
|
}
|
|
246
314
|
}
|
|
247
|
-
return roots.filter((root) => root.name === "outDir" || !isPathInside(
|
|
248
|
-
}
|
|
249
|
-
function notADirectoryError(root, componentPath) {
|
|
250
|
-
return `Refusing to clean ${root.name} '${root.relativePath}': '${componentPath}' is not a directory.`;
|
|
315
|
+
return roots.filter((root) => root.name === "outDir" || !isPathInside(outDir, root.path, useCaseSensitiveFileNames));
|
|
251
316
|
}
|
|
252
317
|
const TYPESCRIPT_4_0 = 4e6;
|
|
253
318
|
const TYPESCRIPT_4_2 = 4002e3;
|
|
@@ -260,73 +325,83 @@ function parseTypeScriptVersion(version) {
|
|
|
260
325
|
const typescriptVersion = parseTypeScriptVersion(ts.version);
|
|
261
326
|
// src/common/typescript/loadCompiler.ts
|
|
262
327
|
async function loadCompiler() {
|
|
328
|
+
if (Number.isNaN(typescriptVersion)) throw new UserError(`Cannot parse TypeScript version '${ts.version}'.`);
|
|
263
329
|
if (typescriptVersion < 3009e3) throw new UserError("TypeScript 3.8 and earlier are not supported. Use TypeScript 3.9 or later.");
|
|
264
330
|
if (typescriptVersion < 7e6) {
|
|
265
|
-
const { TypeScriptPre7Compiler } = await import("./typescriptPre7Compiler-
|
|
331
|
+
const { TypeScriptPre7Compiler } = await import("./typescriptPre7Compiler-CZ1GIAWf.js");
|
|
266
332
|
return new TypeScriptPre7Compiler();
|
|
267
333
|
}
|
|
268
334
|
if (typescriptVersion < 7001e3) throw new UserError("TypeScript 7.0 is not supported. Use TypeScript 7.1 or later.");
|
|
269
|
-
const { TypeScript7Compiler } = await import("./typescript7Compiler-
|
|
335
|
+
const { TypeScript7Compiler } = await import("./typescript7Compiler-D6FwNrdQ.js");
|
|
270
336
|
return TypeScript7Compiler.create();
|
|
271
337
|
}
|
|
272
|
-
// src/
|
|
338
|
+
// src/common/project.ts
|
|
339
|
+
const OUTSIDE_CWD_ERROR = "Refusing to traverse outside current working directory to read tsconfig!";
|
|
273
340
|
const NO_EMIT_ERROR = "Refusing to clean outDir because noEmit is enabled.";
|
|
274
|
-
|
|
275
|
-
|
|
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) {
|
|
276
348
|
const compiler = await loadCompiler();
|
|
277
|
-
let cleanupRoots;
|
|
278
|
-
let expectedOutDirPaths;
|
|
279
|
-
let outDirAbsolutePath;
|
|
280
349
|
try {
|
|
281
|
-
const
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
if (project.options.composite === true) throw new UserError("Composite mode is not supported");
|
|
293
|
-
if (project.options.project !== void 0) throw new UserError("Project mode is not supported");
|
|
294
|
-
cleanupRoots = resolveCleanupRoots(project, cwd, compiler.useCaseSensitiveFileNames);
|
|
295
|
-
const outDirRoot = cleanupRoots.find((root) => root.name === "outDir");
|
|
296
|
-
if (outDirRoot === void 0) throw new Error("Missing outDir cleanup root.");
|
|
297
|
-
outDirAbsolutePath = outDirRoot.path;
|
|
298
|
-
if (args.values.noClean) return;
|
|
299
|
-
expectedOutDirPaths = compiler.getOutputPaths(project);
|
|
350
|
+
const configFilePath = resolveConfigFilePath(compiler, projectPath, cwd);
|
|
351
|
+
const parsedProject = compiler.parseProject(configFilePath);
|
|
352
|
+
const { declarationDir, outDir } = resolveOutputDirs(parsedProject, cwd);
|
|
353
|
+
return {
|
|
354
|
+
configFilePath,
|
|
355
|
+
declarationDir,
|
|
356
|
+
outDir,
|
|
357
|
+
outputPaths: parsedProject.getOutputPaths(),
|
|
358
|
+
sourceFileNames: parsedProject.sourceFileNames,
|
|
359
|
+
useCaseSensitiveFileNames: compiler.useCaseSensitiveFileNames
|
|
360
|
+
};
|
|
300
361
|
} finally {
|
|
301
362
|
compiler.close();
|
|
302
363
|
}
|
|
303
|
-
|
|
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;
|
|
304
391
|
const { dry, verbose } = args.values;
|
|
392
|
+
const results = applyCleanup(plan, { dry });
|
|
305
393
|
const removeVerb = dry ? "Would remove" : "Removed";
|
|
306
|
-
for (const
|
|
307
|
-
const result = results[index];
|
|
308
|
-
if (result === void 0) throw new Error("Missing cleanup result.");
|
|
394
|
+
for (const { root, verdicts } of results) {
|
|
309
395
|
let fileCount = 0;
|
|
310
396
|
let dirCount = 0;
|
|
311
|
-
for (const verdict of
|
|
312
|
-
if (root.name === "declarationDir" && isPathInside(outDirAbsolutePath, verdict.path, compiler.useCaseSensitiveFileNames)) continue;
|
|
397
|
+
for (const verdict of verdicts) {
|
|
313
398
|
const displayPath = path.relative(cwd, verdict.path);
|
|
314
399
|
switch (verdict.type) {
|
|
315
400
|
case "removeDir":
|
|
316
|
-
if (!dry) try {
|
|
317
|
-
fs.rmdirSync(verdict.path);
|
|
318
|
-
} catch (error) {
|
|
319
|
-
if (verdict.path === root.path && isNodeErrorOf(error, Error, "EBUSY")) {
|
|
320
|
-
if (verbose) print(`Skipped (busy directory): ${displayPath}`);
|
|
321
|
-
break;
|
|
322
|
-
}
|
|
323
|
-
throw error;
|
|
324
|
-
}
|
|
325
401
|
dirCount++;
|
|
326
402
|
if (dry || verbose) print(`${removeVerb} empty directory: ${displayPath}`);
|
|
327
403
|
break;
|
|
328
404
|
case "removeFile":
|
|
329
|
-
if (!dry) fs.rmSync(verdict.path, { recursive: false });
|
|
330
405
|
fileCount++;
|
|
331
406
|
if (dry || verbose) print(`${removeVerb} file: ${displayPath}`);
|
|
332
407
|
break;
|
|
@@ -340,14 +415,14 @@ async function mainAction(args) {
|
|
|
340
415
|
if (fileCount > 0) parts.push(formatCount(fileCount, "file", "files"));
|
|
341
416
|
if (dirCount > 0) parts.push(formatCount(dirCount, "empty directory", "empty directories"));
|
|
342
417
|
if (parts.length > 0) print(`${binName}: ${removeVerb} ${parts.join(" and ")} from ${styleText("italic", root.relativePath)}`);
|
|
343
|
-
else if (dry ||
|
|
418
|
+
else if (dry || results.length > 1) print(`${binName}: Nothing to remove from ${styleText("italic", root.relativePath)}`);
|
|
344
419
|
}
|
|
345
420
|
}
|
|
346
421
|
function formatCount(count, singular, plural) {
|
|
347
422
|
return `${count.toFixed()} ${count === 1 ? singular : plural}`;
|
|
348
423
|
}
|
|
349
424
|
// package.json
|
|
350
|
-
var version$1 = "0.
|
|
425
|
+
var version$1 = "0.5.0";
|
|
351
426
|
// src/tasks/version.ts
|
|
352
427
|
function version() {
|
|
353
428
|
print(`${binName}/${version$1} ${process.platform}-${process.arch} node-${process.versions.node} typescript-${ts.version}`);
|
|
@@ -7,8 +7,19 @@ const declarationFileExtensions = [
|
|
|
7
7
|
".d.mts",
|
|
8
8
|
".d.cts"
|
|
9
9
|
];
|
|
10
|
+
const tsBuildInfoExtension = ".tsbuildinfo";
|
|
10
11
|
const esmFileExtensions = [".mts", ".mjs"];
|
|
11
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
|
+
];
|
|
12
23
|
function getOutputFileNames(options, fileName) {
|
|
13
24
|
const sourcePath = path.resolve(fileName);
|
|
14
25
|
const sourceExtension = path.extname(sourcePath);
|
|
@@ -35,6 +46,17 @@ function getOutputFileNames(options, fileName) {
|
|
|
35
46
|
}
|
|
36
47
|
return outputFileNames;
|
|
37
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
|
+
}
|
|
38
60
|
function getDeclarationExtension(sourceExtension) {
|
|
39
61
|
if (esmFileExtensions.includes(sourceExtension)) return ".d.mts";
|
|
40
62
|
if (cjsFileExtensions.includes(sourceExtension)) return ".d.cts";
|
|
@@ -55,6 +77,10 @@ function getOutputPath(sourcePath, outputDirectory, rootDir, configFilePath) {
|
|
|
55
77
|
function isDeclarationFile(fileName) {
|
|
56
78
|
return declarationFileExtensions.some((extension) => fileName.endsWith(extension)) || /\.d\..+\.ts$/u.test(path.basename(fileName));
|
|
57
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
|
+
}
|
|
58
84
|
function replaceExtension(fileName, extension) {
|
|
59
85
|
return `${fileName.slice(0, -path.extname(fileName).length)}${extension}`;
|
|
60
86
|
}
|
|
@@ -70,10 +96,6 @@ const syncModuleSpecifier = "typescript/sync";
|
|
|
70
96
|
var TypeScript7Compiler = class TypeScript7Compiler {
|
|
71
97
|
useCaseSensitiveFileNames;
|
|
72
98
|
api;
|
|
73
|
-
cachedProject;
|
|
74
|
-
isClosed = false;
|
|
75
|
-
program;
|
|
76
|
-
projects = /* @__PURE__ */ new WeakMap();
|
|
77
99
|
typescriptSync;
|
|
78
100
|
constructor(typescriptSync, api) {
|
|
79
101
|
this.typescriptSync = typescriptSync;
|
|
@@ -92,45 +114,9 @@ var TypeScript7Compiler = class TypeScript7Compiler {
|
|
|
92
114
|
}
|
|
93
115
|
}
|
|
94
116
|
close() {
|
|
95
|
-
|
|
96
|
-
this.isClosed = true;
|
|
97
|
-
const program = this.program;
|
|
98
|
-
this.program = void 0;
|
|
99
|
-
this.cachedProject = void 0;
|
|
100
|
-
try {
|
|
101
|
-
program?.dispose();
|
|
102
|
-
} finally {
|
|
103
|
-
this.api.close();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
getOutputPaths(project) {
|
|
107
|
-
this.assertOpen();
|
|
108
|
-
const config = this.projects.get(project);
|
|
109
|
-
if (config === void 0) throw new Error("Project was not parsed by this compiler.");
|
|
110
|
-
if (this.cachedProject !== project) {
|
|
111
|
-
const previousProgram = this.program;
|
|
112
|
-
this.program = void 0;
|
|
113
|
-
this.cachedProject = void 0;
|
|
114
|
-
previousProgram?.dispose();
|
|
115
|
-
this.program = this.api.createProgram(config.fileNames, config.options);
|
|
116
|
-
this.cachedProject = project;
|
|
117
|
-
}
|
|
118
|
-
const program = this.program;
|
|
119
|
-
if (program === void 0) throw new Error("Could not create TypeScript program.");
|
|
120
|
-
const configFilePath = project.configFilePath;
|
|
121
|
-
const outputOptions = {
|
|
122
|
-
...config.options,
|
|
123
|
-
configFilePath,
|
|
124
|
-
jsx: config.options.jsx === this.typescriptSync.JsxEmit.Preserve ? "preserve" : void 0
|
|
125
|
-
};
|
|
126
|
-
const outputPaths = program.getSourceFileNames().filter((fileName) => {
|
|
127
|
-
const metadata = program.getSourceFileMetadata(fileName);
|
|
128
|
-
return metadata !== void 0 && !metadata.isDefaultLibrary && !metadata.isFromExternalLibrary;
|
|
129
|
-
}).flatMap((fileName) => getOutputFileNames(outputOptions, fileName));
|
|
130
|
-
return new Set(outputPaths);
|
|
117
|
+
this.api.close();
|
|
131
118
|
}
|
|
132
119
|
parseProject(configFilePath) {
|
|
133
|
-
this.assertOpen();
|
|
134
120
|
try {
|
|
135
121
|
fs.readFileSync(configFilePath);
|
|
136
122
|
} catch {
|
|
@@ -152,23 +138,30 @@ var TypeScript7Compiler = class TypeScript7Compiler {
|
|
|
152
138
|
const config = this.api.parseConfigFile(configFilePath);
|
|
153
139
|
checkDiagnostics(config.errors);
|
|
154
140
|
const program = this.api.createProgram(config.fileNames, config.options);
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
options: config.options,
|
|
159
|
-
programFileNames: program.getSourceFileNames().filter((fileName) => {
|
|
141
|
+
let sourceFileNames;
|
|
142
|
+
try {
|
|
143
|
+
sourceFileNames = program.getSourceFileNames().filter((fileName) => {
|
|
160
144
|
const metadata = program.getSourceFileMetadata(fileName);
|
|
161
145
|
return metadata !== void 0 && !metadata.isDefaultLibrary && !metadata.isFromExternalLibrary;
|
|
162
|
-
})
|
|
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
|
|
163
164
|
};
|
|
164
|
-
this.program?.dispose();
|
|
165
|
-
this.program = program;
|
|
166
|
-
this.cachedProject = project;
|
|
167
|
-
this.projects.set(project, config);
|
|
168
|
-
return project;
|
|
169
|
-
}
|
|
170
|
-
assertOpen() {
|
|
171
|
-
if (this.isClosed) throw new Error("Compiler is closed.");
|
|
172
165
|
}
|
|
173
166
|
};
|
|
174
167
|
async function loadTypeScript7SyncModule() {
|
|
@@ -4,16 +4,23 @@ import { i as UserError, r as typescriptVersion } from "./tscAfter.js";
|
|
|
4
4
|
/** Uses the `typescript` API for TypeScript 3.9 through 6.x. */
|
|
5
5
|
var TypeScriptPre7Compiler = class {
|
|
6
6
|
useCaseSensitiveFileNames;
|
|
7
|
-
parsedCommandLines = /* @__PURE__ */ new WeakMap();
|
|
8
7
|
constructor() {
|
|
9
8
|
this.useCaseSensitiveFileNames = ts.sys.useCaseSensitiveFileNames;
|
|
10
9
|
}
|
|
11
10
|
close() {}
|
|
12
|
-
|
|
13
|
-
const parsedCommandLine =
|
|
14
|
-
|
|
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) {
|
|
15
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.");
|
|
16
|
-
const emittableFileNames =
|
|
23
|
+
const emittableFileNames = sourceFileNames.filter((fileName) => !isDeclarationFileName(fileName));
|
|
17
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.");
|
|
18
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.");
|
|
19
26
|
const outputCommandLine = {
|
|
@@ -22,40 +29,10 @@ var TypeScriptPre7Compiler = class {
|
|
|
22
29
|
};
|
|
23
30
|
const outputPaths = /* @__PURE__ */ new Set();
|
|
24
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);
|
|
25
34
|
return outputPaths;
|
|
26
35
|
}
|
|
27
|
-
parseProject(configFilePath) {
|
|
28
|
-
const formatHost = {
|
|
29
|
-
getCanonicalFileName: (fileName) => fileName,
|
|
30
|
-
getCurrentDirectory: () => process.cwd(),
|
|
31
|
-
getNewLine: () => ts.sys.newLine
|
|
32
|
-
};
|
|
33
|
-
const checkConfigFile = (filePath) => {
|
|
34
|
-
const config = ts.readConfigFile(filePath.replaceAll("\\", "/"), (name) => ts.sys.readFile(name));
|
|
35
|
-
if (config.error !== void 0) throw new UserError(ts.formatDiagnostics([config.error], formatHost).trimEnd());
|
|
36
|
-
};
|
|
37
|
-
checkConfigFile(configFilePath);
|
|
38
|
-
const parsedCommandLine = ts.getParsedCommandLineOfConfigFile(configFilePath, void 0, {
|
|
39
|
-
...ts.sys,
|
|
40
|
-
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
41
|
-
throw new UserError(ts.formatDiagnostics([diagnostic], formatHost).trimEnd());
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
if (parsedCommandLine === void 0) throw new UserError("Could not read tsconfig.json.");
|
|
45
|
-
if (parsedCommandLine.errors.length > 0) throw new UserError(ts.formatDiagnostics(parsedCommandLine.errors, formatHost).trimEnd());
|
|
46
|
-
const configFileOption = parsedCommandLine.options["configFile"];
|
|
47
|
-
const configFile = isConfigFileMetadata(configFileOption) ? configFileOption : void 0;
|
|
48
|
-
for (const filePath of configFile?.extendedSourceFiles ?? []) checkConfigFile(filePath);
|
|
49
|
-
const program = ts.createProgram(parsedCommandLine.fileNames, parsedCommandLine.options);
|
|
50
|
-
const project = {
|
|
51
|
-
configFilePath: configFile?.fileName ?? configFilePath,
|
|
52
|
-
fileNames: parsedCommandLine.fileNames,
|
|
53
|
-
options: parsedCommandLine.options,
|
|
54
|
-
programFileNames: program.getSourceFiles().filter((sourceFile) => !program.isSourceFileDefaultLibrary(sourceFile) && !program.isSourceFileFromExternalLibrary(sourceFile)).map((sourceFile) => sourceFile.fileName)
|
|
55
|
-
};
|
|
56
|
-
this.parsedCommandLines.set(project, parsedCommandLine);
|
|
57
|
-
return project;
|
|
58
|
-
}
|
|
59
36
|
};
|
|
60
37
|
function isConfigFileMetadata(value) {
|
|
61
38
|
if (typeof value !== "object" || value === null || !("fileName" in value) || typeof value.fileName !== "string") return false;
|
|
@@ -65,4 +42,28 @@ function isConfigFileMetadata(value) {
|
|
|
65
42
|
function isDeclarationFileName(fileName) {
|
|
66
43
|
return fileName.endsWith(".d.ts") || fileName.endsWith(".d.mts") || fileName.endsWith(".d.cts");
|
|
67
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
|
+
}
|
|
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",
|