tsc-after 0.2.0 โ 0.3.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 +69 -40
- package/bin/tsc-after.js +3 -0
- package/dist/tscAfter.js +274 -0
- package/package.json +35 -32
- package/bin/tsc-after +0 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -191
- package/dist/package.js +0 -5
package/README.md
CHANGED
|
@@ -1,50 +1,84 @@
|
|
|
1
1
|
<h1 align="center">tsc-after</h1>
|
|
2
2
|
|
|
3
|
-
<p align="center">
|
|
4
|
-
Your <code>dist</code>, as <code>tsc</code> should've left it.
|
|
5
|
-
<code>tsconfig</code>-aware. Just run <code>tsc && tsc-after</code>.
|
|
6
|
-
๐งน
|
|
7
|
-
</p>
|
|
3
|
+
<p align="center">Your <code>dist</code>, as <code>tsc</code> should've left it. ๐งน</p>
|
|
8
4
|
|
|
9
5
|
<p align="center">
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
<a href="
|
|
13
|
-
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
14
|
-
<!-- prettier-ignore-end -->
|
|
15
|
-
<a href="https://github.com/michal-kocarek/tsc-after/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank"><img alt="๐ค Code of Conduct: Kept" src="https://img.shields.io/badge/%F0%9F%A4%9D_code_of_conduct-kept-21bb42" /></a>
|
|
16
|
-
<a href="https://codecov.io/gh/michal-kocarek/tsc-after" target="_blank"><img alt="๐งช Coverage" src="https://img.shields.io/codecov/c/github/michal-kocarek/tsc-after?label=%F0%9F%A7%AA%20coverage" /></a>
|
|
17
|
-
<a href="https://github.com/michal-kocarek/tsc-after/blob/main/LICENSE.md" target="_blank"><img alt="๐ License: MIT" src="https://img.shields.io/badge/%F0%9F%93%9D_license-MIT-21bb42.svg" /></a>
|
|
18
|
-
<a href="http://npmjs.com/package/tsc-after" target="_blank"><img alt="๐ฆ npm version" src="https://img.shields.io/npm/v/tsc-after?color=21bb42&label=%F0%9F%93%A6%20npm" /></a>
|
|
19
|
-
<img alt="๐ช TypeScript: Strict" src="https://img.shields.io/badge/%F0%9F%92%AA_typescript-strict-21bb42.svg" />
|
|
6
|
+
<a href="https://npmjs.com/package/tsc-after"><img alt="npm version" src="https://img.shields.io/npm/v/tsc-after" /></a>
|
|
7
|
+
<a href="https://codecov.io/gh/michal-kocarek/tsc-after"><img alt="Coverage" src="https://img.shields.io/codecov/c/github/michal-kocarek/tsc-after" /></a>
|
|
8
|
+
<a href="./LICENSE.md"><img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-blue" /></a>
|
|
20
9
|
</p>
|
|
21
10
|
|
|
22
|
-
|
|
11
|
+
`tsc` compiles your files, but it never deletes anything.
|
|
12
|
+
Rename `foo.ts` to `foo/index.ts`, and `dist/foo.js` lives on forever.
|
|
23
13
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
`tsc-after` reads your `tsconfig.json`, works out which files `tsc` would emit today, and removes the stale outputs it wouldn't.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
27
17
|
|
|
28
18
|
```sh
|
|
29
|
-
|
|
19
|
+
# Preview for tsconfig.json; --verbose lists individual paths.
|
|
20
|
+
tsc-after --dry --verbose
|
|
21
|
+
|
|
22
|
+
# Select a tsconfig file instead of the default.
|
|
23
|
+
tsc-after --project tsconfig.build.json
|
|
24
|
+
|
|
25
|
+
# Select a folder with a tsconfig.json.
|
|
26
|
+
tsc-after -p packages/core
|
|
30
27
|
```
|
|
31
28
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
The project path must be inside the current working directory.
|
|
30
|
+
Positional arguments and unknown flags are rejected.
|
|
31
|
+
|
|
32
|
+
For a basic project with a dedicated `outDir`, `tsc-after` uses TypeScript's output-name API to identify expected outputs, removes other regular files in `outDir`, and removes directories left empty by cleanup.
|
|
33
|
+
It skips symbolic links and entries on other devices.
|
|
34
|
+
This behavior and its safety limits are not yet verified for every TypeScript configuration or output type.
|
|
35
|
+
|
|
36
|
+
`--dry` leaves files unchanged, but individual paths are currently printed only with `--verbose`; its summary also reports zero files removed rather than counting planned removals ([issue #8](https://github.com/michal-kocarek/tsc-after/issues/8)).
|
|
37
|
+
|
|
38
|
+
## Limitations
|
|
35
39
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
- `outDir` must be set and dedicated to TypeScript outputs.
|
|
41
|
+
Other regular files there, including copied assets, are treated as stale.
|
|
42
|
+
- By default, `tsc-after` reads `tsconfig.json` from the current directory and does not search parent directories.
|
|
43
|
+
- `incremental`, `tsBuildInfoFile`, `composite`, `declarationDir`, and watch configurations are rejected.
|
|
44
|
+
Project references are not followed.
|
|
45
|
+
- Source files reached only through imports or `paths` aliases are not included when checking source/output overlap or expected outputs.
|
|
46
|
+
Cleanup may delete those sources if they are inside `outDir`, or delete their emitted files as stale ([issue #2](https://github.com/michal-kocarek/tsc-after/issues/2)).
|
|
47
|
+
- Cleanup confinement and source/output overlap checks are incomplete.
|
|
48
|
+
Do not rely on the current checks to protect valuable files.
|
|
49
|
+
- Handling of TypeScript configurations and emitted file types is incomplete and still needs verification.
|
|
40
50
|
|
|
41
|
-
|
|
51
|
+
Related open work: [configuration handling (#2)](https://github.com/michal-kocarek/tsc-after/issues/2), [incremental build state (#4)](https://github.com/michal-kocarek/tsc-after/issues/4), [cleanup safety (#6)](https://github.com/michal-kocarek/tsc-after/issues/6), and [output coverage (#7)](https://github.com/michal-kocarek/tsc-after/issues/7).
|
|
42
52
|
|
|
43
|
-
|
|
53
|
+
Planned: `--copy`, `--exclude`, and `--watch`.
|
|
54
|
+
See [`product.md`](./docs/product.md) for the roadmap.
|
|
44
55
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
56
|
+
## Requirements
|
|
57
|
+
|
|
58
|
+
- the project's tsconfig file must exist inside the current working directory (unlike `tsc`, it does not search parent directories for `tsconfig.json`)
|
|
59
|
+
- source code must be compiled in dedicated `outDir` path
|
|
60
|
+
|
|
61
|
+
## Exit codes
|
|
62
|
+
|
|
63
|
+
- `0` for success, including a dry run or nothing to remove
|
|
64
|
+
- `1` for a configuration or cleanup error
|
|
65
|
+
- `2` for invalid command-line arguments
|
|
66
|
+
|
|
67
|
+
## Why not `rm -rf dist && tsc`?
|
|
68
|
+
|
|
69
|
+
Wiping `outDir` before every build (`rm -rf dist && tsc`, `rimraf dist && tsc`, or a `prebuild` script) is the usual fix for stale outputs.
|
|
70
|
+
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)).
|
|
71
|
+
|
|
72
|
+
`tsc-after` is intended to remove only stale outputs rather than wiping `outDir` and rebuilding everything.
|
|
73
|
+
Incremental builds are not supported.
|
|
74
|
+
|
|
75
|
+
**Using a bundler such as tsup, Vite, or esbuild?**<br>
|
|
76
|
+
They already clean up after themselves, so you probably don't need `tsc-after`.
|
|
77
|
+
|
|
78
|
+
## Contributing
|
|
79
|
+
|
|
80
|
+
See [`CONTRIBUTING.md`](./.github/CONTRIBUTING.md) and [`DEVELOPMENT.md`](./.github/DEVELOPMENT.md).
|
|
81
|
+
For project-specific AI agent guidance, see [`AGENTS.md`](./AGENTS.md).
|
|
48
82
|
|
|
49
83
|
## Contributors
|
|
50
84
|
|
|
@@ -52,14 +86,9 @@ Thanks! ๐งน
|
|
|
52
86
|
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
53
87
|
<!-- prettier-ignore-start -->
|
|
54
88
|
<!-- markdownlint-disable -->
|
|
55
|
-
<
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
<td align="center"><a href="https://github.com/michal-kocarek"><img src="https://avatars.githubusercontent.com/u/762095?v=4?s=100" width="100px;" alt="Michal Koฤรกrek"/><br /><sub><b>Michal Koฤรกrek</b></sub></a><br /><a href="https://github.com/michal-kocarek/tsc-after/commits?author=michal-kocarek" title="Code">๐ป</a> <a href="#content-michal-kocarek" title="Content">๐</a> <a href="https://github.com/michal-kocarek/tsc-after/commits?author=michal-kocarek" title="Documentation">๐</a> <a href="#ideas-michal-kocarek" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#infra-michal-kocarek" title="Infrastructure (Hosting, Build-Tools, etc)">๐</a> <a href="#maintenance-michal-kocarek" title="Maintenance">๐ง</a> <a href="#projectManagement-michal-kocarek" title="Project Management">๐</a> <a href="#tool-michal-kocarek" title="Tools">๐ง</a></td>
|
|
59
|
-
</tr>
|
|
60
|
-
</tbody>
|
|
61
|
-
</table>
|
|
62
|
-
|
|
89
|
+
<ul>
|
|
90
|
+
<li><a href="https://github.com/michal-kocarek">Michal Koฤรกrek</a> <span title="Code">๐ป</span> <span title="Content">๐</span> <span title="Documentation">๐</span> <span title="Ideas, Planning, & Feedback">๐ค</span> <span title="Infrastructure (Hosting, Build-Tools, etc)">๐</span> <span title="Maintenance">๐ง</span> <span title="Project Management">๐</span> <span title="Tools">๐ง</span></li>
|
|
91
|
+
</ul>
|
|
63
92
|
<!-- markdownlint-restore -->
|
|
64
93
|
<!-- prettier-ignore-end -->
|
|
65
94
|
|
package/bin/tsc-after.js
ADDED
package/dist/tscAfter.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import * as util from "node:util";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
// src/common/errors.ts
|
|
6
|
+
/** Error whose message is human-readable and printed to the user without a stack trace. */
|
|
7
|
+
var UserError = class extends Error {};
|
|
8
|
+
function isNodeErrorOf(error, errorClass, code) {
|
|
9
|
+
if (!(error instanceof errorClass)) return false;
|
|
10
|
+
if (code === void 0) return true;
|
|
11
|
+
return "code" in error && error.code === code;
|
|
12
|
+
}
|
|
13
|
+
// src/common/constants.ts
|
|
14
|
+
const binName = "tsc-after";
|
|
15
|
+
const cliArgsOptions = {
|
|
16
|
+
help: {
|
|
17
|
+
default: false,
|
|
18
|
+
helpDescription: "Print this message.",
|
|
19
|
+
short: "h",
|
|
20
|
+
type: "boolean"
|
|
21
|
+
},
|
|
22
|
+
version: {
|
|
23
|
+
default: false,
|
|
24
|
+
helpDescription: "Print the version.",
|
|
25
|
+
type: "boolean"
|
|
26
|
+
},
|
|
27
|
+
project: {
|
|
28
|
+
default: "tsconfig.json",
|
|
29
|
+
helpDescription: "Use the project given the path to its configuration file, or to a folder with a 'tsconfig.json'.",
|
|
30
|
+
short: "p",
|
|
31
|
+
type: "string"
|
|
32
|
+
},
|
|
33
|
+
noClean: {
|
|
34
|
+
default: false,
|
|
35
|
+
helpDescription: "Disable removing stale outputs from 'outDir'.",
|
|
36
|
+
type: "boolean"
|
|
37
|
+
},
|
|
38
|
+
dry: {
|
|
39
|
+
default: false,
|
|
40
|
+
helpDescription: "Show what would be deleted.",
|
|
41
|
+
short: "d",
|
|
42
|
+
type: "boolean"
|
|
43
|
+
},
|
|
44
|
+
verbose: {
|
|
45
|
+
default: false,
|
|
46
|
+
helpDescription: "Enable verbose logging.",
|
|
47
|
+
type: "boolean"
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
// src/common/util.ts
|
|
51
|
+
const EXIT_CODE_ERROR = 1;
|
|
52
|
+
const EXIT_CODE_INVALID_ARGUMENTS = 2;
|
|
53
|
+
/**
|
|
54
|
+
* `util.styleText()` exists only from Node.js 20.12 / 21.7; older versions get plain text.
|
|
55
|
+
*/
|
|
56
|
+
const styleText = "styleText" in util ? util.styleText : (_format, text) => text;
|
|
57
|
+
function print(...messages) {
|
|
58
|
+
console.log(messages.join(" "));
|
|
59
|
+
}
|
|
60
|
+
function printError(...messages) {
|
|
61
|
+
console.error(styleText("red", `error: ${messages.join(" ")}`));
|
|
62
|
+
if ((process.exitCode ?? 0) === 0) process.exitCode = EXIT_CODE_ERROR;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* List of error codes that `util.parseArgs()` can throw
|
|
66
|
+
* @see https://nodejs.org/docs/latest/api/errors.html#nodejs-error-codes
|
|
67
|
+
*/
|
|
68
|
+
const PARSE_ARGS_ERROR_CODES = [
|
|
69
|
+
"ERR_PARSE_ARGS_UNKNOWN_OPTION",
|
|
70
|
+
"ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL",
|
|
71
|
+
"ERR_PARSE_ARGS_INVALID_OPTION_VALUE"
|
|
72
|
+
];
|
|
73
|
+
function parseArgs(args) {
|
|
74
|
+
try {
|
|
75
|
+
return util.parseArgs({
|
|
76
|
+
args,
|
|
77
|
+
options: cliArgsOptions
|
|
78
|
+
});
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (isParseArgsError(error)) {
|
|
81
|
+
process.exitCode = EXIT_CODE_INVALID_ARGUMENTS;
|
|
82
|
+
throw new UserError(error.message.endsWith(".") ? error.message : `${error.message}.`);
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function isParseArgsError(error) {
|
|
88
|
+
return PARSE_ARGS_ERROR_CODES.some((code) => isNodeErrorOf(error, TypeError, code));
|
|
89
|
+
}
|
|
90
|
+
// src/tasks/help.ts
|
|
91
|
+
const FLAG_INDENT = 2;
|
|
92
|
+
const FLAG_GAP = " ";
|
|
93
|
+
function help() {
|
|
94
|
+
const flags = Object.entries(cliArgsOptions).map(([name, opts]) => ({
|
|
95
|
+
description: opts.helpDescription,
|
|
96
|
+
name: "short" in opts ? `--${name}, -${opts.short}` : `--${name}`
|
|
97
|
+
}));
|
|
98
|
+
const flagWidth = FLAG_INDENT + Math.max(...flags.map(({ name }) => name.length));
|
|
99
|
+
const heading = (text) => styleText("bold", text);
|
|
100
|
+
const command = (text, description) => ` ${styleText("blueBright", text)}\n ${description}`;
|
|
101
|
+
const flag = (name, description) => `${styleText("blueBright", name.padStart(flagWidth))}${FLAG_GAP}${description}`;
|
|
102
|
+
print([
|
|
103
|
+
`${styleText("bold", binName)}: Your outDir, as tsc should've left it.`,
|
|
104
|
+
heading("COMMON COMMANDS"),
|
|
105
|
+
command(`tsc && ${binName}`, "Removes stale outputs of the current project (tsconfig.json in the working directory)."),
|
|
106
|
+
command(`${binName} -p ./path/to/tsconfig.json`, "Removes stale outputs of the TypeScript project located at the specified path."),
|
|
107
|
+
command(`${binName} --dry`, "Shows what would be deleted from 'outDir', without deleting anything."),
|
|
108
|
+
heading("COMMAND LINE FLAGS"),
|
|
109
|
+
...flags.map(({ description, name }) => flag(name, description))
|
|
110
|
+
].join("\n\n"));
|
|
111
|
+
}
|
|
112
|
+
// src/common/paths.ts
|
|
113
|
+
function isPathInside(dirPath, filePath) {
|
|
114
|
+
const normalizedDirPath = ts.sys.useCaseSensitiveFileNames ? dirPath : dirPath.toLowerCase();
|
|
115
|
+
const normalizedFilePath = ts.sys.useCaseSensitiveFileNames ? filePath : filePath.toLowerCase();
|
|
116
|
+
const relativePath = path.relative(normalizedDirPath, normalizedFilePath);
|
|
117
|
+
return relativePath === "" || !path.isAbsolute(relativePath) && relativePath !== ".." && !relativePath.startsWith(`..${path.sep}`);
|
|
118
|
+
}
|
|
119
|
+
// src/tasks/mainAction.ts
|
|
120
|
+
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\".";
|
|
121
|
+
function mainAction(args) {
|
|
122
|
+
const cwd = process.cwd();
|
|
123
|
+
const projectAbsolutePath = path.resolve(cwd, args.values.project);
|
|
124
|
+
if (!isPathInside(cwd, projectAbsolutePath)) throw new UserError(`Refusing to traverse outside current working directory to read tsconfig!`);
|
|
125
|
+
const tsconfigAbsolutePath = fs.existsSync(projectAbsolutePath) && fs.statSync(projectAbsolutePath).isDirectory() ? path.join(projectAbsolutePath, "tsconfig.json") : projectAbsolutePath;
|
|
126
|
+
if (!isPathInside(cwd, tsconfigAbsolutePath) || isPathInside(tsconfigAbsolutePath, cwd)) throw new UserError(`Refusing to traverse outside current working directory to read tsconfig!`);
|
|
127
|
+
print(`Using tsconfig: ${path.relative(cwd, tsconfigAbsolutePath)}`);
|
|
128
|
+
const formatHost = {
|
|
129
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
130
|
+
getCurrentDirectory: () => cwd,
|
|
131
|
+
getNewLine: () => ts.sys.newLine
|
|
132
|
+
};
|
|
133
|
+
const tsconfig = ts.getParsedCommandLineOfConfigFile(tsconfigAbsolutePath, void 0, {
|
|
134
|
+
...ts.sys,
|
|
135
|
+
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
136
|
+
throw new UserError(ts.formatDiagnostics([diagnostic], formatHost).trimEnd());
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
if (tsconfig === void 0) throw new UserError("Could not read tsconfig.json.");
|
|
140
|
+
if (tsconfig.errors.length > 0) throw new UserError(ts.formatDiagnostics(tsconfig.errors, formatHost).trimEnd());
|
|
141
|
+
if (tsconfig.options.outDir === void 0) throw new UserError(MISSING_OUT_DIR_ERROR);
|
|
142
|
+
const outDir = tsconfig.options.outDir;
|
|
143
|
+
const outDirAbsolutePath = path.resolve(outDir);
|
|
144
|
+
const outDirRelativePath = path.relative(cwd, outDirAbsolutePath);
|
|
145
|
+
if (!isPathInside(cwd, outDirAbsolutePath) || isPathInside(outDirAbsolutePath, cwd)) throw new UserError(`Refusing to traverse outside safe paths containing tsconfig to process outDir!`);
|
|
146
|
+
if (tsconfig.options.incremental === true) throw new UserError("Incremental mode is not supported");
|
|
147
|
+
if (tsconfig.options.tsBuildInfoFile !== void 0) throw new UserError("tsBuildInfoFile is not supported");
|
|
148
|
+
if (tsconfig.options.composite === true) throw new UserError("Composite mode is not supported");
|
|
149
|
+
if (tsconfig.options.project !== void 0) throw new UserError("Project mode is not supported");
|
|
150
|
+
if (tsconfig.options.declarationDir !== void 0) throw new UserError("declarationDir is not supported");
|
|
151
|
+
if (tsconfig.watchOptions) throw new UserError("Watch mode is not supported");
|
|
152
|
+
const isInsideOutDir = (filePath) => isPathInside(outDirAbsolutePath, filePath) || isPathInside(outDirAbsolutePath, fs.realpathSync(filePath));
|
|
153
|
+
for (const file of tsconfig.fileNames) if (isInsideOutDir(path.resolve(file))) throw new UserError(`Refusing to clean outDir '${outDirRelativePath}': it contains source file '${path.relative(cwd, file)}'.`);
|
|
154
|
+
const configFile = tsconfig.options["configFile"];
|
|
155
|
+
const configFilePaths = [configFile?.fileName ?? tsconfigAbsolutePath, ...configFile?.extendedSourceFiles ?? []];
|
|
156
|
+
for (const file of configFilePaths) if (isInsideOutDir(path.resolve(file))) throw new UserError(`Refusing to clean outDir '${outDirRelativePath}': it contains config file '${path.relative(cwd, file)}'.`);
|
|
157
|
+
if (args.values.noClean) return;
|
|
158
|
+
const expectedOutDirPaths = /* @__PURE__ */ new Set();
|
|
159
|
+
for (const file of tsconfig.fileNames) ts.getOutputFileNames(tsconfig, file, !ts.sys.useCaseSensitiveFileNames).forEach((fiePath) => expectedOutDirPaths.add(fiePath));
|
|
160
|
+
function readDir(dirPath) {
|
|
161
|
+
const verdicts = [];
|
|
162
|
+
const stat = fs.lstatSync(dirPath);
|
|
163
|
+
if (!stat.isDirectory()) throw new UserError(`Not a directory ${dirPath}!`);
|
|
164
|
+
let dirIsEmpty = true;
|
|
165
|
+
for (const child of fs.readdirSync(dirPath)) {
|
|
166
|
+
const fullPath = path.join(dirPath, child);
|
|
167
|
+
const fullPathStat = fs.lstatSync(fullPath);
|
|
168
|
+
if (fullPathStat.dev !== stat.dev) {
|
|
169
|
+
if (args.values.verbose) verdicts.push({
|
|
170
|
+
path: fullPath,
|
|
171
|
+
reason: "different device",
|
|
172
|
+
type: "skipped"
|
|
173
|
+
});
|
|
174
|
+
dirIsEmpty = false;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (fullPathStat.isSymbolicLink()) {
|
|
178
|
+
if (args.values.verbose) verdicts.push({
|
|
179
|
+
path: fullPath,
|
|
180
|
+
reason: "symbolic link",
|
|
181
|
+
type: "skipped"
|
|
182
|
+
});
|
|
183
|
+
dirIsEmpty = false;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (fullPathStat.isFile()) {
|
|
187
|
+
if (expectedOutDirPaths.has(fullPath)) {
|
|
188
|
+
if (args.values.verbose) verdicts.push({
|
|
189
|
+
path: fullPath,
|
|
190
|
+
reason: "expected output file",
|
|
191
|
+
type: "skipped"
|
|
192
|
+
});
|
|
193
|
+
dirIsEmpty = false;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
verdicts.push({
|
|
197
|
+
path: fullPath,
|
|
198
|
+
type: "removeFile"
|
|
199
|
+
});
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (fullPathStat.isDirectory()) {
|
|
203
|
+
const result = readDir(fullPath);
|
|
204
|
+
verdicts.push(...result.verdicts);
|
|
205
|
+
if (!result.dirIsEmpty) dirIsEmpty = false;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
dirIsEmpty = false;
|
|
209
|
+
}
|
|
210
|
+
if (dirIsEmpty) verdicts.push({
|
|
211
|
+
path: dirPath,
|
|
212
|
+
type: "removeDir"
|
|
213
|
+
});
|
|
214
|
+
else verdicts.push({
|
|
215
|
+
path: dirPath,
|
|
216
|
+
reason: "directory not empty",
|
|
217
|
+
type: "skipped"
|
|
218
|
+
});
|
|
219
|
+
return {
|
|
220
|
+
dirIsEmpty,
|
|
221
|
+
verdicts
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
let outDirExists = true;
|
|
225
|
+
try {
|
|
226
|
+
fs.lstatSync(outDir);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (!isNodeErrorOf(error, Error, "ENOENT")) throw error;
|
|
229
|
+
outDirExists = false;
|
|
230
|
+
}
|
|
231
|
+
if (outDirExists && fs.realpathSync(outDir) !== path.resolve(outDir)) throw new UserError(`Refusing to clean outDir through a symbolic link: ${outDirRelativePath}!`);
|
|
232
|
+
const result = outDirExists ? readDir(outDir) : { verdicts: [] };
|
|
233
|
+
if (args.values.verbose) for (const verdict of result.verdicts) switch (verdict.type) {
|
|
234
|
+
case "removeDir":
|
|
235
|
+
print(`Remove empty directory: ${verdict.path}`);
|
|
236
|
+
break;
|
|
237
|
+
case "removeFile":
|
|
238
|
+
print(`Remove file: ${verdict.path}`);
|
|
239
|
+
break;
|
|
240
|
+
case "skipped": print(`Skipped (${verdict.reason}): ${verdict.path}`);
|
|
241
|
+
}
|
|
242
|
+
let removedDirs = 0;
|
|
243
|
+
let removedFiles = 0;
|
|
244
|
+
if (!args.values.dry) for (const verdict of result.verdicts) switch (verdict.type) {
|
|
245
|
+
case "removeDir":
|
|
246
|
+
fs.rmdirSync(verdict.path);
|
|
247
|
+
removedDirs++;
|
|
248
|
+
break;
|
|
249
|
+
case "removeFile":
|
|
250
|
+
fs.rmSync(verdict.path, { recursive: false });
|
|
251
|
+
removedFiles++;
|
|
252
|
+
}
|
|
253
|
+
if (args.values.dry || removedDirs > 0 || removedFiles > 0) print(`${binName}: ${args.values.dry ? "Would remove" : "Removed"} ${removedFiles.toFixed()} files${removedDirs > 0 ? ` and ${removedDirs.toFixed()} empty directories` : ""} from ${styleText("italic", outDirRelativePath)}`);
|
|
254
|
+
}
|
|
255
|
+
// package.json
|
|
256
|
+
var version$1 = "0.3.0";
|
|
257
|
+
// src/tasks/version.ts
|
|
258
|
+
function version() {
|
|
259
|
+
print(`${binName}/${version$1} ${process.platform}-${process.arch} node-${process.versions.node} typescript-${ts.version}`);
|
|
260
|
+
}
|
|
261
|
+
// src/entrypoints/tscAfter.ts
|
|
262
|
+
function main() {
|
|
263
|
+
try {
|
|
264
|
+
const args = parseArgs(process.argv.slice(2));
|
|
265
|
+
if (args.values.version) version();
|
|
266
|
+
else if (args.values.help) help();
|
|
267
|
+
else mainAction(args);
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (!(error instanceof UserError)) throw error;
|
|
270
|
+
printError(error.message);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
main();
|
|
274
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tsc-after",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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",
|
|
@@ -25,13 +25,16 @@
|
|
|
25
25
|
"type": "module",
|
|
26
26
|
"exports": null,
|
|
27
27
|
"bin": {
|
|
28
|
-
"tsc-after": "bin/tsc-after"
|
|
28
|
+
"tsc-after": "bin/tsc-after.js"
|
|
29
29
|
},
|
|
30
30
|
"files": [
|
|
31
|
+
"bin/",
|
|
31
32
|
"dist/"
|
|
32
33
|
],
|
|
33
34
|
"scripts": {
|
|
34
35
|
"build": "tsdown",
|
|
36
|
+
"clean": "rm -rf .tmp dist coverage",
|
|
37
|
+
"dev": "node src/entrypoints/tscAfter.ts",
|
|
35
38
|
"format": "prettier --write .",
|
|
36
39
|
"lint": "eslint . --max-warnings 0",
|
|
37
40
|
"lint:knip": "KNIP_DISABLE_RAW_TRANSFER=1 knip",
|
|
@@ -40,7 +43,8 @@
|
|
|
40
43
|
"release": "release-it",
|
|
41
44
|
"test": "vitest",
|
|
42
45
|
"test:e2e": "vitest --project e2e",
|
|
43
|
-
"test:unit": "vitest --project unit"
|
|
46
|
+
"test:unit": "vitest --project unit",
|
|
47
|
+
"typecheck": "tsc"
|
|
44
48
|
},
|
|
45
49
|
"commitlint": {
|
|
46
50
|
"extends": [
|
|
@@ -51,53 +55,52 @@
|
|
|
51
55
|
"*": "prettier --ignore-unknown --write"
|
|
52
56
|
},
|
|
53
57
|
"devDependencies": {
|
|
54
|
-
"@arethetypeswrong/core": "0.18.5",
|
|
55
58
|
"@commitlint/cli": "21.2.4",
|
|
56
59
|
"@commitlint/config-conventional": "21.2.4",
|
|
57
60
|
"@eslint-community/eslint-plugin-eslint-comments": "4.8.1",
|
|
58
61
|
"@eslint/js": "10.0.1",
|
|
59
|
-
"@eslint/markdown": "
|
|
62
|
+
"@eslint/markdown": "9.0.0",
|
|
60
63
|
"@release-it/conventional-changelog": "12.0.2",
|
|
61
|
-
"@tsconfig/node-lts": "
|
|
62
|
-
"@tsconfig/node-ts": "23.6.
|
|
63
|
-
"@tsconfig/strictest": "2.0.
|
|
64
|
-
"@types/node": "26.6.
|
|
65
|
-
"@vitest/coverage-v8": "5.
|
|
66
|
-
"@vitest/eslint-plugin": "1.6.
|
|
64
|
+
"@tsconfig/node-lts": "26.0.1",
|
|
65
|
+
"@tsconfig/node-ts": "23.6.5",
|
|
66
|
+
"@tsconfig/strictest": "2.0.9",
|
|
67
|
+
"@types/node": "26.6.6",
|
|
68
|
+
"@vitest/coverage-v8": "5.1.1",
|
|
69
|
+
"@vitest/eslint-plugin": "1.6.28",
|
|
67
70
|
"console-fail-test": "0.10.0",
|
|
68
71
|
"conventional-changelog-conventionalcommits": "10.4.1",
|
|
69
|
-
"cspell": "10.
|
|
70
|
-
"eslint": "10.
|
|
72
|
+
"cspell": "10.5.2",
|
|
73
|
+
"eslint": "10.15.0",
|
|
71
74
|
"eslint-import-resolver-typescript": "4.4.5",
|
|
72
|
-
"eslint-plugin-import-x": "4.
|
|
73
|
-
"eslint-plugin-jsdoc": "65.
|
|
75
|
+
"eslint-plugin-import-x": "4.18.1",
|
|
76
|
+
"eslint-plugin-jsdoc": "65.5.0",
|
|
74
77
|
"eslint-plugin-jsonc": "3.4.2",
|
|
75
78
|
"eslint-plugin-n": "18.5.0",
|
|
76
|
-
"eslint-plugin-package-json": "1.
|
|
77
|
-
"eslint-plugin-perfectionist": "5.
|
|
78
|
-
"eslint-plugin-regexp": "3.3.
|
|
79
|
-
"eslint-plugin-yml": "3.
|
|
80
|
-
"execa": "10.2.
|
|
79
|
+
"eslint-plugin-package-json": "1.14.0",
|
|
80
|
+
"eslint-plugin-perfectionist": "5.14.0",
|
|
81
|
+
"eslint-plugin-regexp": "3.3.3",
|
|
82
|
+
"eslint-plugin-yml": "3.10.0",
|
|
83
|
+
"execa": "10.2.1",
|
|
81
84
|
"husky": "9.1.7",
|
|
82
|
-
"knip": "6.
|
|
83
|
-
"lint-staged": "17.6.
|
|
84
|
-
"prettier": "3.
|
|
85
|
-
"prettier-plugin-curly": "0.4.
|
|
86
|
-
"prettier-plugin-packagejson": "3.0
|
|
85
|
+
"knip": "6.47.0",
|
|
86
|
+
"lint-staged": "17.6.1",
|
|
87
|
+
"prettier": "3.10.1",
|
|
88
|
+
"prettier-plugin-curly": "0.4.2",
|
|
89
|
+
"prettier-plugin-packagejson": "3.1.0",
|
|
87
90
|
"prettier-plugin-sentences-per-line": "0.2.5",
|
|
88
91
|
"prettier-plugin-sh": "0.20.2",
|
|
89
92
|
"publint": "0.3.25",
|
|
90
|
-
"release-it": "21.1.
|
|
91
|
-
"tsdown": "0.23.
|
|
93
|
+
"release-it": "21.1.2",
|
|
94
|
+
"tsdown": "0.23.7",
|
|
92
95
|
"typescript": "6.0.3",
|
|
93
|
-
"typescript-eslint": "8.
|
|
94
|
-
"vitest": "5.
|
|
96
|
+
"typescript-eslint": "8.73.0",
|
|
97
|
+
"vitest": "5.1.1"
|
|
95
98
|
},
|
|
96
99
|
"peerDependencies": {
|
|
97
|
-
"typescript": ">=
|
|
100
|
+
"typescript": ">=5.9.3"
|
|
98
101
|
},
|
|
99
|
-
"packageManager": "pnpm@12.
|
|
102
|
+
"packageManager": "pnpm@12.21.0",
|
|
100
103
|
"engines": {
|
|
101
|
-
"node": "
|
|
104
|
+
"node": "^16.19.0 || >=18.11.0"
|
|
102
105
|
}
|
|
103
106
|
}
|
package/bin/tsc-after
DELETED
package/dist/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {}
|
package/dist/index.js
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import { name, version } from "./package.js";
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { parseArgs, styleText } from "node:util";
|
|
5
|
-
import ts from "typescript";
|
|
6
|
-
//#region src/index.ts
|
|
7
|
-
const argsConfig = { options: {
|
|
8
|
-
project: {
|
|
9
|
-
default: "tsconfig.json",
|
|
10
|
-
helpDescription: "Path to configuration file for this project, or to a folder with a 'tsconfig.json'.",
|
|
11
|
-
helpPlaceholder: "<path>",
|
|
12
|
-
short: "p",
|
|
13
|
-
type: "string"
|
|
14
|
-
},
|
|
15
|
-
dryRun: {
|
|
16
|
-
default: false,
|
|
17
|
-
helpDescription: "Do not delete any files.",
|
|
18
|
-
type: "boolean"
|
|
19
|
-
},
|
|
20
|
-
verbose: {
|
|
21
|
-
default: false,
|
|
22
|
-
helpDescription: "Print verbose output.",
|
|
23
|
-
type: "boolean"
|
|
24
|
-
},
|
|
25
|
-
version: {
|
|
26
|
-
default: false,
|
|
27
|
-
helpDescription: "Print version.",
|
|
28
|
-
type: "boolean"
|
|
29
|
-
},
|
|
30
|
-
help: {
|
|
31
|
-
default: false,
|
|
32
|
-
helpDescription: "Print this message.",
|
|
33
|
-
short: "h",
|
|
34
|
-
type: "boolean"
|
|
35
|
-
}
|
|
36
|
-
} };
|
|
37
|
-
const BIN_NAME = name;
|
|
38
|
-
function isNodeErrorOf(error, errorClass, code) {
|
|
39
|
-
if (!(error instanceof errorClass)) return false;
|
|
40
|
-
return !(code !== void 0 && "code" in error && error.code !== code);
|
|
41
|
-
}
|
|
42
|
-
function main() {
|
|
43
|
-
let args;
|
|
44
|
-
try {
|
|
45
|
-
args = parseArgs(argsConfig);
|
|
46
|
-
} catch (error) {
|
|
47
|
-
if (isNodeErrorOf(error, TypeError, "ERR_PARSE_ARGS_UNKNOWN_OPTION")) {
|
|
48
|
-
printError(`${error.message}.`);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
throw error;
|
|
52
|
-
}
|
|
53
|
-
if (args.values.help) {
|
|
54
|
-
const heading = (text) => styleText(["bold"], text), option = (name, description) => ` ${styleText("blueBright", name.padEnd(23))}${description}`;
|
|
55
|
-
print([
|
|
56
|
-
`${styleText("bold", BIN_NAME)} - Your dist, as tsc should've left it.`,
|
|
57
|
-
"",
|
|
58
|
-
"A tsconfig-aware companion to tsc. Removes stale build output by default.",
|
|
59
|
-
"",
|
|
60
|
-
heading("USAGE"),
|
|
61
|
-
` tsc && ${BIN_NAME} [options]`,
|
|
62
|
-
"",
|
|
63
|
-
heading("OPTIONS"),
|
|
64
|
-
...Object.entries(argsConfig.options).map(([name, opts]) => {
|
|
65
|
-
const keyParts = [`--${name}`];
|
|
66
|
-
if ("short" in opts) keyParts.push(`, -${opts.short}`);
|
|
67
|
-
switch (opts.type) {
|
|
68
|
-
case "boolean": break;
|
|
69
|
-
case "string": keyParts.push(` ${opts.helpPlaceholder}`);
|
|
70
|
-
}
|
|
71
|
-
return option(keyParts.join(""), opts.helpDescription);
|
|
72
|
-
})
|
|
73
|
-
].join("\n"));
|
|
74
|
-
process.exit(0);
|
|
75
|
-
}
|
|
76
|
-
if (args.values.version) {
|
|
77
|
-
print(`${BIN_NAME}/${version} ${process.platform}-${process.arch} node-${process.versions.node} typescript-${ts.version}`);
|
|
78
|
-
process.exit(0);
|
|
79
|
-
}
|
|
80
|
-
const cwd = process.cwd(), tsconfigAbsolutePath = path.resolve(cwd, args.values.project), tsconfigRelativePath = path.relative(cwd, tsconfigAbsolutePath);
|
|
81
|
-
if (tsconfigRelativePath === "" || tsconfigRelativePath === ".." || tsconfigRelativePath.startsWith(`..${path.sep}`) || path.isAbsolute(tsconfigRelativePath)) throw new Error(`Refusing to traverse outside current working directory to read tsconfig!`);
|
|
82
|
-
print(`Using tsconfig: ${tsconfigRelativePath}`);
|
|
83
|
-
const tsconfig = ts.getParsedCommandLineOfConfigFile(args.values.project, void 0, {
|
|
84
|
-
...ts.sys,
|
|
85
|
-
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
86
|
-
printError(JSON.stringify(diagnostic));
|
|
87
|
-
throw new Error("Unrecoverable config file diagnostic");
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
if (tsconfig?.options.outDir === void 0) throw new Error("outDir is required");
|
|
91
|
-
const outDirRelativePath = path.relative(path.dirname(tsconfigAbsolutePath), tsconfig.options.outDir);
|
|
92
|
-
if (outDirRelativePath === "" || outDirRelativePath === ".." || outDirRelativePath.startsWith(`..${path.sep}`) || path.isAbsolute(outDirRelativePath)) throw new Error(`Refusing to traverse outside safe paths containing tsconfig to process outDir!`);
|
|
93
|
-
if (tsconfig.options.incremental === true) throw new Error("Incremental mode is not supported");
|
|
94
|
-
if (tsconfig.options.tsBuildInfoFile !== void 0) throw new Error("tsBuildInfoFile is not supported");
|
|
95
|
-
if (tsconfig.options.composite === true) throw new Error("Composite mode is not supported");
|
|
96
|
-
if (tsconfig.options.project !== void 0) throw new Error("Project mode is not supported");
|
|
97
|
-
if (tsconfig.options.declarationDir !== void 0) throw new Error("declarationDir is not supported");
|
|
98
|
-
if (tsconfig.watchOptions) throw new Error("Watch mode is not supported");
|
|
99
|
-
print(JSON.stringify(tsconfig));
|
|
100
|
-
const expectedOutDirPaths = /* @__PURE__ */ new Set();
|
|
101
|
-
for (const file of tsconfig.fileNames) {
|
|
102
|
-
const output = ts.getOutputFileNames(tsconfig, file, !ts.sys.useCaseSensitiveFileNames), stat = fs.statSync(file);
|
|
103
|
-
print(file, " => ", JSON.stringify(output), JSON.stringify(stat));
|
|
104
|
-
output.forEach((fiePath) => expectedOutDirPaths.add(fiePath));
|
|
105
|
-
}
|
|
106
|
-
print(JSON.stringify(expectedOutDirPaths));
|
|
107
|
-
function readDir(dirPath) {
|
|
108
|
-
const verdicts = [], stat = fs.lstatSync(dirPath);
|
|
109
|
-
if (!stat.isDirectory()) throw new Error(`Not a directory ${dirPath}!`);
|
|
110
|
-
let dirIsEmpty = true;
|
|
111
|
-
for (const child of fs.readdirSync(dirPath)) {
|
|
112
|
-
const fullPath = path.join(dirPath, child), fullPathStat = fs.lstatSync(fullPath);
|
|
113
|
-
if (fullPathStat.dev !== stat.dev) {
|
|
114
|
-
if (args.values.verbose) verdicts.push({
|
|
115
|
-
path: fullPath,
|
|
116
|
-
reason: "different device",
|
|
117
|
-
type: "skipped"
|
|
118
|
-
});
|
|
119
|
-
dirIsEmpty = false;
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
if (fullPathStat.isFile()) {
|
|
123
|
-
if (expectedOutDirPaths.has(fullPath)) {
|
|
124
|
-
if (args.values.verbose) verdicts.push({
|
|
125
|
-
path: fullPath,
|
|
126
|
-
reason: "expected output file",
|
|
127
|
-
type: "skipped"
|
|
128
|
-
});
|
|
129
|
-
dirIsEmpty = false;
|
|
130
|
-
continue;
|
|
131
|
-
}
|
|
132
|
-
verdicts.push({
|
|
133
|
-
path: fullPath,
|
|
134
|
-
type: "removeFile"
|
|
135
|
-
});
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
if (fullPathStat.isDirectory()) {
|
|
139
|
-
const result = readDir(fullPath);
|
|
140
|
-
verdicts.push(...result.verdicts);
|
|
141
|
-
if (!result.dirIsEmpty) dirIsEmpty = false;
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
dirIsEmpty = false;
|
|
145
|
-
}
|
|
146
|
-
if (dirIsEmpty) verdicts.push({
|
|
147
|
-
path: dirPath,
|
|
148
|
-
type: "removeDir"
|
|
149
|
-
});
|
|
150
|
-
else verdicts.push({
|
|
151
|
-
path: dirPath,
|
|
152
|
-
reason: "directory not empty",
|
|
153
|
-
type: "skipped"
|
|
154
|
-
});
|
|
155
|
-
return {
|
|
156
|
-
dirIsEmpty,
|
|
157
|
-
verdicts
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
const result = readDir(tsconfig.options.outDir);
|
|
161
|
-
if (args.values.verbose) for (const verdict of result.verdicts) switch (verdict.type) {
|
|
162
|
-
case "removeDir":
|
|
163
|
-
print(`Remove empty directory: ${verdict.path}`);
|
|
164
|
-
break;
|
|
165
|
-
case "removeFile":
|
|
166
|
-
print(`Remove file: ${verdict.path}`);
|
|
167
|
-
break;
|
|
168
|
-
case "skipped": print(`Skipped (${verdict.reason}): ${verdict.path}`);
|
|
169
|
-
}
|
|
170
|
-
let removedDirs = 0, removedFiles = 0;
|
|
171
|
-
if (!args.values.dryRun) for (const verdict of result.verdicts) switch (verdict.type) {
|
|
172
|
-
case "removeDir":
|
|
173
|
-
fs.rmdirSync(verdict.path);
|
|
174
|
-
removedDirs++;
|
|
175
|
-
break;
|
|
176
|
-
case "removeFile":
|
|
177
|
-
fs.rmSync(verdict.path, { recursive: false });
|
|
178
|
-
removedFiles++;
|
|
179
|
-
}
|
|
180
|
-
if (args.values.dryRun || removedDirs > 0 || removedFiles > 0) print(`${BIN_NAME}: ${args.values.dryRun ? "Would remove" : "Removed"} ${removedFiles.toFixed()} files${removedDirs > 0 ? ` and ${removedDirs.toFixed()} empty directories` : ""} from ${styleText("italic", outDirRelativePath)}`);
|
|
181
|
-
}
|
|
182
|
-
function print(...messages) {
|
|
183
|
-
console.log(messages.join(" "));
|
|
184
|
-
}
|
|
185
|
-
function printError(...messages) {
|
|
186
|
-
console.error(styleText("red", `error: ${messages.join(" ")}`));
|
|
187
|
-
process.exitCode = 1;
|
|
188
|
-
}
|
|
189
|
-
main();
|
|
190
|
-
//#endregion
|
|
191
|
-
export {};
|