dt-clean 1.0.1 → 1.1.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/CHANGELOG.md +8 -0
- package/README.md +37 -0
- package/applyChanges.mjs +1 -5
- package/bin.mjs +87 -40
- package/detectIndent.d.ts +3 -0
- package/detectIndent.mjs +5 -0
- package/package.json +3 -2
- package/setup.d.ts +12 -0
- package/setup.mjs +57 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [v1.1.0](https://github.com/ljharb/dt-clean/compare/v1.0.1...v1.1.0) - 2026-06-21
|
|
9
|
+
|
|
10
|
+
### Commits
|
|
11
|
+
|
|
12
|
+
- [New] add `--auto`, meant for the `dependencies` lifecycle script [`bc60a91`](https://github.com/ljharb/dt-clean/commit/bc60a91bf4f3e4d0964f8dcb71d1b0d1182612f9)
|
|
13
|
+
- [Tests] treat taken-down registry packages as skipped, not failures [`d020cc6`](https://github.com/ljharb/dt-clean/commit/d020cc62dd06de08189eebfe45e79a372ec8e5a1)
|
|
14
|
+
- [meta] run `dt-clean --setup` [`459cc0c`](https://github.com/ljharb/dt-clean/commit/459cc0cc3a5a2b64e498946c8930ae49899eb8b3)
|
|
15
|
+
|
|
8
16
|
## [v1.0.1](https://github.com/ljharb/dt-clean/compare/v1.0.0...v1.0.1) - 2026-06-19
|
|
9
17
|
|
|
10
18
|
### Commits
|
package/README.md
CHANGED
|
@@ -47,8 +47,45 @@ With `--update` (`-u`), `dt-clean` edits `package.json` in place - adding, movin
|
|
|
47
47
|
### Options
|
|
48
48
|
|
|
49
49
|
- `-u`, `--update`: apply the changes to `package.json` (default: report only).
|
|
50
|
+
- `--setup`: idempotently wire `dt-clean --auto` into a `dependencies` lifecycle script, without overwriting any existing script (see [Automatic cleanup](#automatic-cleanup)).
|
|
51
|
+
- `--auto`: for use in a `dependencies` lifecycle script - apply the changes like `--update` during `npm install`, but during `npm ci` only print what would change and exit `0` (see [Automatic cleanup](#automatic-cleanup)).
|
|
50
52
|
- `--help`: show usage.
|
|
51
53
|
|
|
54
|
+
### Automatic cleanup
|
|
55
|
+
|
|
56
|
+
To keep your `@types/*` set tidy on every install, run `dt-clean --auto` from a [`dependencies` lifecycle script](https://docs.npmjs.com/cli/using-npm/scripts#npm-install) - the one npm's installer (arborist) runs after any operation that changes `node_modules`.
|
|
57
|
+
|
|
58
|
+
The one-step way to set this up, in any project, is:
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
npx dt-clean --setup
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`--setup` edits `package.json` for you and is safe to run anywhere:
|
|
65
|
+
|
|
66
|
+
- if you have no `dependencies` script, it adds `"dependencies": "dt-clean --auto"`;
|
|
67
|
+
- if you already have one, it adds `dt-clean --auto` to a free `postdependencies` (or `predependencies`) hook instead, so your existing script is never touched - and if every hook is taken, it appends `&& dt-clean --auto` to your `dependencies` script rather than clobbering it;
|
|
68
|
+
- if `dt-clean --auto` is already wired in, it does nothing.
|
|
69
|
+
|
|
70
|
+
It only ever adds this one invocation and leaves the rest of your `package.json` (and its formatting) alone, so it's safe to re-run. The result is simply the equivalent of:
|
|
71
|
+
|
|
72
|
+
```json
|
|
73
|
+
{
|
|
74
|
+
"scripts": {
|
|
75
|
+
"dependencies": "dt-clean --auto"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Once it's wired in, `dt-clean --auto` inspects `npm_command` to decide what to do:
|
|
81
|
+
|
|
82
|
+
- under `npm install`, it applies the changes for you, so a fresh install keeps your `@types/*` set tidy automatically;
|
|
83
|
+
- under `npm ci` (typically used in CI pipelines, where `package.json` must not be mutated), it only prints what would change and exits `0`, so it never edits a checked-in file and never fails the install.
|
|
84
|
+
|
|
85
|
+
When `npm_command` is anything else (or absent), `--auto` applies the changes, exactly as under `npm install`.
|
|
86
|
+
|
|
87
|
+
To avoid surprising edits, `--auto` runs *only* inside the `dependencies` lifecycle (or its `predependencies`/`postdependencies` hooks): it checks `npm_lifecycle_event`, and if it is invoked any other way (for example directly from the shell) it refuses to do anything and exits nonzero. Use `--update` to apply changes manually.
|
|
88
|
+
|
|
52
89
|
### Exit codes
|
|
53
90
|
|
|
54
91
|
In the default report-only mode, the exit code is a bitmask of the kinds of pending changes, so a clean project exits `0` and you can fail CI (or a `git` pre-commit hook) on drift:
|
package/applyChanges.mjs
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { join } from 'path';
|
|
2
2
|
import { readFile, writeFile } from 'fs/promises';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
function detectIndent(raw) {
|
|
6
|
-
const match = (/^[ \t]+/m).exec(raw);
|
|
7
|
-
return match?.[0] ?? '\t';
|
|
8
|
-
}
|
|
4
|
+
import detectIndent from '#/detectIndent';
|
|
9
5
|
|
|
10
6
|
/** @type {<K extends string, V>(obj: Record<K, V>) => Record<K, V>} */
|
|
11
7
|
function sortKeys(obj) {
|
package/bin.mjs
CHANGED
|
@@ -8,20 +8,36 @@ import getDelTa from '#/getDelTa';
|
|
|
8
8
|
import applyChanges from '#/applyChanges';
|
|
9
9
|
import formatReport from '#/report';
|
|
10
10
|
import exitCode from '#/exitCode';
|
|
11
|
+
import setupScripts from '#/setup';
|
|
11
12
|
|
|
12
13
|
const {
|
|
13
|
-
values: {
|
|
14
|
+
values: {
|
|
15
|
+
auto,
|
|
16
|
+
json,
|
|
17
|
+
setup,
|
|
18
|
+
update,
|
|
19
|
+
},
|
|
14
20
|
positionals,
|
|
15
21
|
help,
|
|
16
22
|
} = await pargs(import.meta.filename, {
|
|
17
23
|
allowPositionals: 1,
|
|
18
24
|
description: 'Reports which DefinitelyTyped (`@types/*`) packages a project should add, move, or remove. By default it only reports; with `--update` it edits `package.json`.',
|
|
19
25
|
options: {
|
|
26
|
+
auto: {
|
|
27
|
+
default: false,
|
|
28
|
+
description: 'for a `dependencies` lifecycle script (or its `pre`/`post` hooks): apply the changes like `--update` during `npm install`, but during `npm ci` only print what would change and exit zero',
|
|
29
|
+
type: 'boolean',
|
|
30
|
+
},
|
|
20
31
|
json: {
|
|
21
32
|
default: false,
|
|
22
33
|
description: 'print the result as JSON on stdout (the human-readable report moves to stderr)',
|
|
23
34
|
type: 'boolean',
|
|
24
35
|
},
|
|
36
|
+
setup: {
|
|
37
|
+
default: false,
|
|
38
|
+
description: 'idempotently add `dt-clean --auto` to a `dependencies` lifecycle script in `package.json`, without overwriting any existing script',
|
|
39
|
+
type: 'boolean',
|
|
40
|
+
},
|
|
25
41
|
update: {
|
|
26
42
|
default: false,
|
|
27
43
|
description: 'apply the changes to `package.json`, then run `npm install` (or your package manager\'s equivalent)',
|
|
@@ -36,54 +52,85 @@ await help();
|
|
|
36
52
|
|
|
37
53
|
const cwd = positionals.length > 0 ? resolve(positionals[0]) : process.cwd();
|
|
38
54
|
|
|
39
|
-
const {
|
|
40
|
-
present,
|
|
41
|
-
toAdd,
|
|
42
|
-
toMove,
|
|
43
|
-
toRemain,
|
|
44
|
-
toRemove,
|
|
45
|
-
} = await getDelTa(cwd);
|
|
55
|
+
const { npm_command: npmCommand, npm_lifecycle_event: lifecycleEvent } = process.env;
|
|
46
56
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
// the `dependencies` lifecycle and its `pre`/`post` hooks are the only safe slots for `--auto`,
|
|
58
|
+
// since npm runs them after a reify; `postdependencies`/`predependencies` let you add it without
|
|
59
|
+
// clobbering an existing `dependencies` script.
|
|
60
|
+
const DEPENDENCY_HOOKS = [
|
|
61
|
+
'predependencies',
|
|
62
|
+
'dependencies',
|
|
63
|
+
'postdependencies',
|
|
64
|
+
];
|
|
53
65
|
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
console.error(
|
|
66
|
+
if (setup) {
|
|
67
|
+
const { action, script } = await setupScripts(cwd);
|
|
68
|
+
console.log({
|
|
69
|
+
chained: `Appended \`dt-clean --auto\` to the existing \`${script}\` script in \`package.json\`.`,
|
|
70
|
+
present: `\`${script}\` already runs \`dt-clean --auto\`; nothing to do.`,
|
|
71
|
+
set: `Added \`dt-clean --auto\` to the \`${script}\` script in \`package.json\`.`,
|
|
72
|
+
}[action]);
|
|
73
|
+
} else if (auto && (!lifecycleEvent || !DEPENDENCY_HOOKS.includes(lifecycleEvent))) {
|
|
74
|
+
console.error('`--auto` only runs inside a `dependencies` (or `pre`/`postdependencies`) lifecycle script (see the README); use `--update` to apply changes manually.');
|
|
75
|
+
process.exitCode = 1;
|
|
63
76
|
} else {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (update) {
|
|
68
|
-
// `--update` applies the changes, leaving the project clean, so a successful run exits zero;
|
|
69
|
-
// only an error (e.g. a failed write, which throws) yields a nonzero exit.
|
|
70
|
-
const changed = await applyChanges(cwd, {
|
|
77
|
+
const {
|
|
78
|
+
present,
|
|
71
79
|
toAdd,
|
|
72
80
|
toMove,
|
|
81
|
+
toRemain,
|
|
73
82
|
toRemove,
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
} else {
|
|
79
|
-
// report-only: the exit code is a bitmask of the pending change kinds, so a clean project exits zero.
|
|
80
|
-
const code = exitCode({
|
|
83
|
+
} = await getDelTa(cwd);
|
|
84
|
+
|
|
85
|
+
const report = formatReport({
|
|
86
|
+
present,
|
|
81
87
|
toAdd,
|
|
82
88
|
toMove,
|
|
83
89
|
toRemove,
|
|
84
90
|
});
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
console.
|
|
91
|
+
|
|
92
|
+
if (json) {
|
|
93
|
+
console.log(JSON.stringify({
|
|
94
|
+
present: Object.fromEntries(present),
|
|
95
|
+
toAdd: Object.fromEntries(toAdd),
|
|
96
|
+
toMove: Object.fromEntries(toMove),
|
|
97
|
+
toRemain: Array.from(toRemain),
|
|
98
|
+
toRemove,
|
|
99
|
+
}, null, '\t'));
|
|
100
|
+
console.error(report);
|
|
101
|
+
} else {
|
|
102
|
+
console.log(report);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (update || (auto && npmCommand !== 'ci')) {
|
|
106
|
+
// `--update` applies the changes, leaving the project clean, so a successful run exits zero;
|
|
107
|
+
// only an error (e.g. a failed write, which throws) yields a nonzero exit.
|
|
108
|
+
const changed = await applyChanges(cwd, {
|
|
109
|
+
toAdd,
|
|
110
|
+
toMove,
|
|
111
|
+
toRemove,
|
|
112
|
+
});
|
|
113
|
+
console.error(changed
|
|
114
|
+
? '\nUpdated `package.json`; run `npm install` (or your package manager’s equivalent) to sync.'
|
|
115
|
+
: '\nNo changes needed.');
|
|
116
|
+
} else {
|
|
117
|
+
const code = exitCode({
|
|
118
|
+
toAdd,
|
|
119
|
+
toMove,
|
|
120
|
+
toRemove,
|
|
121
|
+
});
|
|
122
|
+
if (auto) {
|
|
123
|
+
// `--auto` under `npm ci`: leave the exit code at zero so a CI install never fails; the
|
|
124
|
+
// report above already lists what `npm install` would change.
|
|
125
|
+
if (code > 0) {
|
|
126
|
+
console.error('\n`npm ci` detected; `package.json` left unchanged. Run `npm install` or `dt-clean --update` to apply these changes.');
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
// report-only: the exit code is a bitmask of the pending change kinds, so a clean project exits zero.
|
|
130
|
+
process.exitCode = code;
|
|
131
|
+
if (code > 0) {
|
|
132
|
+
console.error('\nRe-run with `--update` (`-u`) to apply these changes to `package.json`.');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
88
135
|
}
|
|
89
136
|
}
|
package/detectIndent.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dt-clean",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Ensures the only DefinitelyTyped (`@types`) packages you have installed are the ones you need",
|
|
5
5
|
"bin": "./bin.mjs",
|
|
6
6
|
"exports": {
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"lint": "eslint .",
|
|
24
24
|
"posttest": "npx npm@'>= 10.2' audit --production",
|
|
25
25
|
"version": "auto-changelog && git add CHANGELOG.md",
|
|
26
|
-
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
|
|
26
|
+
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"",
|
|
27
|
+
"dependencies": "dt-clean --auto"
|
|
27
28
|
},
|
|
28
29
|
"repository": {
|
|
29
30
|
"type": "git",
|
package/setup.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare function setup(cwd: string): Promise<setup.SetupResult>;
|
|
2
|
+
|
|
3
|
+
declare namespace setup {
|
|
4
|
+
type DependencyHook = 'predependencies' | 'dependencies' | 'postdependencies';
|
|
5
|
+
|
|
6
|
+
type SetupResult = {
|
|
7
|
+
action: 'present' | 'set' | 'chained';
|
|
8
|
+
script: DependencyHook;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export = setup;
|
package/setup.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
import { readFile, writeFile } from 'fs/promises';
|
|
3
|
+
|
|
4
|
+
import detectIndent from '#/detectIndent';
|
|
5
|
+
|
|
6
|
+
/** @import { PackageJSON } from './types/types.d.ts' */
|
|
7
|
+
/** @import { SetupResult } from './setup.d.ts' */
|
|
8
|
+
|
|
9
|
+
const AUTO = 'dt-clean --auto';
|
|
10
|
+
|
|
11
|
+
// listed most-preferred first: the `dependencies` event itself, then its `post`/`pre` hooks.
|
|
12
|
+
const HOOKS = /** @type {const} */ ([
|
|
13
|
+
'dependencies',
|
|
14
|
+
'postdependencies',
|
|
15
|
+
'predependencies',
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
/** @param {string | undefined} script */
|
|
19
|
+
function hasAuto(script) {
|
|
20
|
+
// matches a `dt-clean … --auto` invocation confined to one `&&`/`;`/`|`-delimited segment
|
|
21
|
+
return typeof script === 'string' && (/\bdt-clean\b[^&|;]*--auto\b/).test(script);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @type {import('./setup.d.ts')} */
|
|
25
|
+
export default async function setup(cwd) {
|
|
26
|
+
const packageJSONpath = join(cwd, 'package.json');
|
|
27
|
+
const raw = `${await readFile(packageJSONpath)}`;
|
|
28
|
+
|
|
29
|
+
/** @type {PackageJSON} */
|
|
30
|
+
const pkg = JSON.parse(raw);
|
|
31
|
+
|
|
32
|
+
/** @type {NonNullable<PackageJSON['scripts']>} */
|
|
33
|
+
const scripts = { ...pkg.scripts };
|
|
34
|
+
|
|
35
|
+
const present = HOOKS.find((hook) => hasAuto(scripts[hook]));
|
|
36
|
+
if (present) {
|
|
37
|
+
return { action: 'present', script: present };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const free = HOOKS.find((hook) => !scripts[hook]);
|
|
41
|
+
|
|
42
|
+
/** @type {SetupResult} */
|
|
43
|
+
let result;
|
|
44
|
+
if (free) {
|
|
45
|
+
scripts[free] = AUTO;
|
|
46
|
+
result = { action: 'set', script: free };
|
|
47
|
+
} else {
|
|
48
|
+
// every hook is occupied, so chain onto `dependencies` rather than clobber anything.
|
|
49
|
+
scripts.dependencies = `${scripts.dependencies} && ${AUTO}`;
|
|
50
|
+
result = { action: 'chained', script: 'dependencies' };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const next = { ...pkg, scripts };
|
|
54
|
+
await writeFile(packageJSONpath, `${JSON.stringify(next, null, detectIndent(raw))}\n`);
|
|
55
|
+
|
|
56
|
+
return result;
|
|
57
|
+
}
|