dt-clean 1.0.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/.github/FUNDING.yml +12 -0
- package/.nycrc +15 -0
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +62 -0
- package/applyChanges.d.ts +9 -0
- package/applyChanges.mjs +65 -0
- package/bin.mjs +77 -0
- package/getDelTa.d.ts +18 -0
- package/getDelTa.mjs +146 -0
- package/package.json +83 -0
- package/report.d.ts +12 -0
- package/report.mjs +82 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# These are supported funding model platforms
|
|
2
|
+
|
|
3
|
+
github: [ljharb]
|
|
4
|
+
patreon: # Replace with a single Patreon username
|
|
5
|
+
open_collective: # Replace with a single Open Collective username
|
|
6
|
+
ko_fi: # Replace with a single Ko-fi username
|
|
7
|
+
tidelift: npm/dt-clean
|
|
8
|
+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
9
|
+
liberapay: # Replace with a single Liberapay username
|
|
10
|
+
issuehunt: # Replace with a single IssueHunt username
|
|
11
|
+
otechie: # Replace with a single Otechie username
|
|
12
|
+
custom: # Replace with a single custom sponsorship URL
|
package/.nycrc
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## v1.0.0 - 2026-06-19
|
|
9
|
+
|
|
10
|
+
### Commits
|
|
11
|
+
|
|
12
|
+
- Initial implementation, readme, tests, types, etc [`202952a`](https://github.com/ljharb/dt-clean/commit/202952a1ed579c6864988bb421eda7e27e52c532)
|
|
13
|
+
- Initial commit [`e817527`](https://github.com/ljharb/dt-clean/commit/e8175279344f1ce2c490779a6cb5900ed94ec6d2)
|
|
14
|
+
- npm init [`9dd6e5c`](https://github.com/ljharb/dt-clean/commit/9dd6e5cbaa1000a0e029c1531c13e359769db844)
|
|
15
|
+
- Only apps should have lockfiles [`dd98916`](https://github.com/ljharb/dt-clean/commit/dd9891647c9b54e793ddbb68cdd569fa0aad5967)
|
|
16
|
+
- [meta] add version settings [`63d1636`](https://github.com/ljharb/dt-clean/commit/63d16368dfa19750fd19c6264e304f6a0491620c)
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jordan Harband
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# dt-clean <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
|
2
|
+
|
|
3
|
+
[![github actions][actions-image]][actions-url]
|
|
4
|
+
[![coverage][codecov-image]][codecov-url]
|
|
5
|
+
[![License][license-image]][license-url]
|
|
6
|
+
[![Downloads][downloads-image]][downloads-url]
|
|
7
|
+
|
|
8
|
+
[![npm badge][npm-badge-png]][package-url]
|
|
9
|
+
|
|
10
|
+
Ensures the only DefinitelyTyped (`@types/*`) packages you have installed are the ones you need, and points out the ones you're missing.
|
|
11
|
+
|
|
12
|
+
`dt-clean` inspects your `package.json` and, for each dependency, decides what should happen to its DefinitelyTyped types package:
|
|
13
|
+
|
|
14
|
+
- **add**: the runtime package ships no types of its own, but a matching `@types/*` package exists on the registry.
|
|
15
|
+
- **move**: an `@types/*` package is in `dependencies`, but belongs in `devDependencies`.
|
|
16
|
+
- **remove**: an `@types/*` package is installed but no longer needed: the runtime package now bundles its own types, or it no longer corresponds to any dependency.
|
|
17
|
+
- **keep**: the `@types/*` package is present and still needed. `@types/node` is always kept.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
# report only (the default)
|
|
23
|
+
npx dt-clean [dir]
|
|
24
|
+
|
|
25
|
+
# apply the changes to package.json
|
|
26
|
+
npx dt-clean --update [dir]
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`dir` is the directory containing the `package.json` to inspect, and defaults to the current directory.
|
|
30
|
+
|
|
31
|
+
By default, `dt-clean` only prints a report and changes nothing:
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
Package State Action Version
|
|
35
|
+
-------------- ------- ------ -------
|
|
36
|
+
@types/express present remove ^4.0.0
|
|
37
|
+
@types/lodash missing add ^4.17
|
|
38
|
+
@types/node present keep ^25.0.0
|
|
39
|
+
|
|
40
|
+
3 `@types/*` packages: 1 keep, 0 move, 1 remove, 1 add.
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The `State` column tells you whether each DefinitelyTyped package is currently `present` or `missing`; the `Action` column tells you what `--update` would do about it.
|
|
44
|
+
|
|
45
|
+
With `--update` (`-u`), `dt-clean` edits `package.json` in place - adding, moving, and removing the relevant `@types/*` entries - and then reminds you to run `npm install` (or your package manager's equivalent) to sync `node_modules`.
|
|
46
|
+
|
|
47
|
+
### Options
|
|
48
|
+
|
|
49
|
+
- `-u`, `--update`: apply the changes to `package.json` (default: report only).
|
|
50
|
+
- `--help`: show usage.
|
|
51
|
+
|
|
52
|
+
[package-url]: https://npmjs.org/package/dt-clean
|
|
53
|
+
[npm-version-svg]: https://versionbadg.es/ljharb/dt-clean.svg
|
|
54
|
+
[npm-badge-png]: https://nodei.co/npm/dt-clean.png?downloads=true&stars=true
|
|
55
|
+
[license-image]: https://img.shields.io/npm/l/dt-clean.svg
|
|
56
|
+
[license-url]: LICENSE
|
|
57
|
+
[downloads-image]: https://img.shields.io/npm/dm/dt-clean.svg
|
|
58
|
+
[downloads-url]: https://npm-stat.com/charts.html?package=dt-clean
|
|
59
|
+
[codecov-image]: https://codecov.io/gh/ljharb/dt-clean/branch/main/graphs/badge.svg
|
|
60
|
+
[codecov-url]: https://app.codecov.io/gh/ljharb/dt-clean/
|
|
61
|
+
[actions-image]: https://img.shields.io/github/check-runs/ljharb/dt-clean/main
|
|
62
|
+
[actions-url]: https://github.com/ljharb/dt-clean/actions
|
package/applyChanges.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
import { readFile, writeFile } from 'fs/promises';
|
|
3
|
+
|
|
4
|
+
/** @param {string} raw */
|
|
5
|
+
function detectIndent(raw) {
|
|
6
|
+
const match = (/^[ \t]+/m).exec(raw);
|
|
7
|
+
return match?.[0] ?? '\t';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** @type {<K extends string, V>(obj: Record<K, V>) => Record<K, V>} */
|
|
11
|
+
function sortKeys(obj) {
|
|
12
|
+
return Object.fromEntries(Object.entries(obj).sort(([a], [b]) => a.localeCompare(b)));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** @import { PackageJSON } from './types/types.d.ts' */
|
|
16
|
+
|
|
17
|
+
/** @type {import('./applyChanges.d.ts')} */
|
|
18
|
+
export default async function applyChanges(cwd, {
|
|
19
|
+
toAdd,
|
|
20
|
+
toMove,
|
|
21
|
+
toRemove,
|
|
22
|
+
}) {
|
|
23
|
+
const packageJSONpath = join(cwd, 'package.json');
|
|
24
|
+
const raw = `${await readFile(packageJSONpath)}`;
|
|
25
|
+
|
|
26
|
+
/** @type {PackageJSON} */
|
|
27
|
+
const pkg = JSON.parse(raw);
|
|
28
|
+
|
|
29
|
+
/** @type {NonNullable<PackageJSON['dependencies']>} */
|
|
30
|
+
const deps = { ...pkg.dependencies };
|
|
31
|
+
/** @type {NonNullable<PackageJSON['devDependencies']>} */
|
|
32
|
+
const devDeps = { ...pkg.devDependencies };
|
|
33
|
+
|
|
34
|
+
toMove.forEach((version, name) => {
|
|
35
|
+
delete deps[name];
|
|
36
|
+
devDeps[name] = version;
|
|
37
|
+
});
|
|
38
|
+
toAdd.forEach((version, name) => {
|
|
39
|
+
devDeps[name] = version;
|
|
40
|
+
});
|
|
41
|
+
toRemove.forEach((name) => {
|
|
42
|
+
delete deps[name];
|
|
43
|
+
delete devDeps[name];
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const next = { ...pkg };
|
|
47
|
+
if (Object.keys(deps).length > 0) {
|
|
48
|
+
next.dependencies = sortKeys(deps);
|
|
49
|
+
} else {
|
|
50
|
+
delete next.dependencies;
|
|
51
|
+
}
|
|
52
|
+
if (Object.keys(devDeps).length > 0) {
|
|
53
|
+
next.devDependencies = sortKeys(devDeps);
|
|
54
|
+
} else {
|
|
55
|
+
delete next.devDependencies;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const serialized = `${JSON.stringify(next, null, detectIndent(raw))}\n`;
|
|
59
|
+
if (serialized === raw) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
await writeFile(packageJSONpath, serialized);
|
|
64
|
+
return true;
|
|
65
|
+
}
|
package/bin.mjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { resolve } from 'path';
|
|
4
|
+
|
|
5
|
+
import pargs from 'pargs';
|
|
6
|
+
|
|
7
|
+
import getDelTa from '#/getDelTa';
|
|
8
|
+
import applyChanges from '#/applyChanges';
|
|
9
|
+
import formatReport from '#/report';
|
|
10
|
+
|
|
11
|
+
const {
|
|
12
|
+
values: { json, update },
|
|
13
|
+
positionals,
|
|
14
|
+
help,
|
|
15
|
+
} = await pargs(import.meta.filename, {
|
|
16
|
+
allowPositionals: 1,
|
|
17
|
+
description: 'Reports which DefinitelyTyped (`@types/*`) packages a project should add, move, or remove. By default it only reports; with `--update` it edits `package.json`.',
|
|
18
|
+
options: {
|
|
19
|
+
json: {
|
|
20
|
+
default: false,
|
|
21
|
+
description: 'print the result as JSON on stdout (the human-readable report moves to stderr)',
|
|
22
|
+
type: 'boolean',
|
|
23
|
+
},
|
|
24
|
+
update: {
|
|
25
|
+
default: false,
|
|
26
|
+
description: 'apply the changes to `package.json`, then run `npm install` (or your package manager\'s equivalent)',
|
|
27
|
+
short: 'u',
|
|
28
|
+
type: 'boolean',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
positionals: [{ description: 'directory containing the `package.json` to inspect (default: the current directory)', name: 'dir' }],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
await help();
|
|
35
|
+
|
|
36
|
+
const cwd = positionals.length > 0 ? resolve(positionals[0]) : process.cwd();
|
|
37
|
+
|
|
38
|
+
const {
|
|
39
|
+
present,
|
|
40
|
+
toAdd,
|
|
41
|
+
toMove,
|
|
42
|
+
toRemain,
|
|
43
|
+
toRemove,
|
|
44
|
+
} = await getDelTa(cwd);
|
|
45
|
+
|
|
46
|
+
const report = formatReport({
|
|
47
|
+
present,
|
|
48
|
+
toAdd,
|
|
49
|
+
toMove,
|
|
50
|
+
toRemove,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (json) {
|
|
54
|
+
console.log(JSON.stringify({
|
|
55
|
+
present: Object.fromEntries(present),
|
|
56
|
+
toAdd: Object.fromEntries(toAdd),
|
|
57
|
+
toMove: Object.fromEntries(toMove),
|
|
58
|
+
toRemain: Array.from(toRemain),
|
|
59
|
+
toRemove,
|
|
60
|
+
}, null, '\t'));
|
|
61
|
+
console.error(report);
|
|
62
|
+
} else {
|
|
63
|
+
console.log(report);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (update) {
|
|
67
|
+
const changed = await applyChanges(cwd, {
|
|
68
|
+
toAdd,
|
|
69
|
+
toMove,
|
|
70
|
+
toRemove,
|
|
71
|
+
});
|
|
72
|
+
console.error(changed
|
|
73
|
+
? '\nUpdated `package.json`; run `npm install` (or your package manager’s equivalent) to sync.'
|
|
74
|
+
: '\nNo changes needed.');
|
|
75
|
+
} else if (toAdd.size + toMove.size + toRemove.length > 0) {
|
|
76
|
+
console.error('\nRe-run with `--update` (`-u`) to apply these changes to `package.json`.');
|
|
77
|
+
}
|
package/getDelTa.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
declare function getDelTa(
|
|
2
|
+
cwd?: string,
|
|
3
|
+
): Promise<getDelTa.DTDelta>;
|
|
4
|
+
|
|
5
|
+
declare namespace getDelTa {
|
|
6
|
+
type Version = string & {};
|
|
7
|
+
type DTPackage<T extends string = string> = `@types/${T}`;
|
|
8
|
+
|
|
9
|
+
type DTDelta = {
|
|
10
|
+
present: Map<DTPackage, Version>;
|
|
11
|
+
toAdd: Map<DTPackage, Version>;
|
|
12
|
+
toMove: Map<DTPackage, Version>;
|
|
13
|
+
toRemain: Set<DTPackage>;
|
|
14
|
+
toRemove: DTPackage[];
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export = getDelTa;
|
package/getDelTa.mjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
import { pathToFileURL } from 'url';
|
|
3
|
+
import { existsSync } from 'fs';
|
|
4
|
+
import { readFile } from 'fs/promises';
|
|
5
|
+
import { findPackageJSON } from 'module';
|
|
6
|
+
import { mangleScopedPackage, typesPackageNameToRealName } from '@definitelytyped/utils';
|
|
7
|
+
|
|
8
|
+
import hasTypes from 'hastypes';
|
|
9
|
+
import semver from 'semver';
|
|
10
|
+
|
|
11
|
+
/** @import getDelTA, { DTPackage, Version } from './getDelTa.d.ts' */
|
|
12
|
+
/** @import { PackageJSON } from './types/types.d.ts'*/
|
|
13
|
+
|
|
14
|
+
/** @type {<T>(entry: [string, T]) => entry is [DTPackage, T]} */
|
|
15
|
+
function isDTPackageEntry([name]) {
|
|
16
|
+
return name.startsWith('@types/');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @type {<T extends string>(name: T) => DTPackage<T>} */
|
|
20
|
+
function toDTName(name) {
|
|
21
|
+
return `@types/${mangleScopedPackage(name)}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @type {<T extends string>(name: DTPackage<T>) => T} */
|
|
25
|
+
function fromDTName(name) {
|
|
26
|
+
return typesPackageNameToRealName(name);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @type {getDelTA} */
|
|
30
|
+
export default async function getDelTa(cwd = process.cwd()) {
|
|
31
|
+
const packageJSONpath = join(cwd, 'package.json');
|
|
32
|
+
const anchor = pathToFileURL(packageJSONpath);
|
|
33
|
+
|
|
34
|
+
/** @type {PackageJSON} */
|
|
35
|
+
const {
|
|
36
|
+
dependencies,
|
|
37
|
+
devDependencies,
|
|
38
|
+
} = JSON.parse(`${await readFile(packageJSONpath)}`);
|
|
39
|
+
|
|
40
|
+
const deps = new Map(dependencies ? Object.entries(dependencies) : []);
|
|
41
|
+
const devDeps = new Map(devDependencies ? Object.entries(devDependencies) : []);
|
|
42
|
+
|
|
43
|
+
const allDeps = /** @type {const} */ ([...deps, ...devDeps]);
|
|
44
|
+
|
|
45
|
+
// `@types/*` packages declared as runtime `dependencies` belong in `devDependencies`.
|
|
46
|
+
const dtRuntimeDepsPresent = new Map(deps.entries().filter(isDTPackageEntry));
|
|
47
|
+
|
|
48
|
+
// every `@types/*` package present, whether a runtime or a dev dependency.
|
|
49
|
+
const dtPackagesPresent = new Map(allDeps.filter(isDTPackageEntry));
|
|
50
|
+
|
|
51
|
+
if (!existsSync(join(cwd, 'tsconfig.json'))) {
|
|
52
|
+
return {
|
|
53
|
+
present: dtPackagesPresent,
|
|
54
|
+
toAdd: new Map(),
|
|
55
|
+
toMove: new Map(),
|
|
56
|
+
toRemain: new Set(),
|
|
57
|
+
toRemove: dtPackagesPresent.keys().toArray(),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @type {(name: string) => Promise<Version | null>} */
|
|
62
|
+
async function installedVersion(name) {
|
|
63
|
+
try {
|
|
64
|
+
const pkgPath = /** @type {string} */ (findPackageJSON(name, anchor));
|
|
65
|
+
const { version } = JSON.parse(`${await readFile(pkgPath)}`);
|
|
66
|
+
return typeof version === 'string' ? version : null;
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @type {(name: string, declaredRange: string) => Promise<Version | null>} */
|
|
73
|
+
async function resolveVersion(name, declaredRange) {
|
|
74
|
+
const installed = await installedVersion(name);
|
|
75
|
+
if (installed !== null) {
|
|
76
|
+
return installed;
|
|
77
|
+
}
|
|
78
|
+
// keep any prerelease tag: a prerelease-only package (e.g. `1.0.0-beta.2`) has no coerced stable
|
|
79
|
+
// version on the registry, so dropping it would make the `@types` lookup miss
|
|
80
|
+
return semver.coerce(declaredRange, { includePrerelease: true })?.version ?? null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// `node` is never a dependency, yet `@types/node` is always wanted, so it is exempt.
|
|
84
|
+
const nodeTypes = toDTName('node');
|
|
85
|
+
|
|
86
|
+
const pToRemove = Promise.all(dtPackagesPresent.keys()
|
|
87
|
+
.filter((name) => name !== nodeTypes)
|
|
88
|
+
.map(async (name) => {
|
|
89
|
+
const realName = fromDTName(name);
|
|
90
|
+
const range = deps.get(realName) ?? devDeps.get(realName);
|
|
91
|
+
// if the real package isn't a declared dependency it may still be installed (transitively, or
|
|
92
|
+
// as a type-only dependency whose types matter), so fall back to the installed version
|
|
93
|
+
const version = typeof range === 'undefined'
|
|
94
|
+
? await installedVersion(realName)
|
|
95
|
+
: await resolveVersion(realName, range);
|
|
96
|
+
if (version === null) {
|
|
97
|
+
// neither declared nor installed => a genuine orphan to remove; declared but unresolvable => keep
|
|
98
|
+
return /** @type {const} */ ([name, typeof range === 'undefined']);
|
|
99
|
+
}
|
|
100
|
+
// query the exact resolved version (a `major.minor` range can miss prerelease-only minors); a
|
|
101
|
+
// dependency whose lookup can't resolve is left alone rather than crashing the whole run
|
|
102
|
+
const shipsOwnTypes = await hasTypes(`${realName}@${version}`).catch(() => false) === true;
|
|
103
|
+
return /** @type {const} */ ([name, shipsOwnTypes]);
|
|
104
|
+
})
|
|
105
|
+
.toArray()).then((entries) => entries.filter(([, remove]) => remove).map(([name]) => name));
|
|
106
|
+
|
|
107
|
+
// `hastypes` returns `true` if `X` ships its own types, `false` if no `@types/X`
|
|
108
|
+
// exists, or the `@types/X` specifier string when one exists and is needed.
|
|
109
|
+
const pToAdd = Promise.all(allDeps
|
|
110
|
+
.filter(([name]) => !isDTPackageEntry([name, '']) && !dtPackagesPresent.has(toDTName(name)))
|
|
111
|
+
.map(async ([name, range]) => {
|
|
112
|
+
const version = await resolveVersion(name, range);
|
|
113
|
+
if (version === null) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
const result = await hasTypes(`${name}@${version}`).catch(() => null);
|
|
117
|
+
if (typeof result !== 'string') {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
// `hastypes` resolves the `@types` specifier to a full caret range (`@types/x@^a.b.c`); use it as-is
|
|
121
|
+
const dtRange = result.slice(result.lastIndexOf('@') + 1);
|
|
122
|
+
return /** @type {const} */ ([toDTName(name), dtRange]);
|
|
123
|
+
}));
|
|
124
|
+
|
|
125
|
+
const [
|
|
126
|
+
toRemove,
|
|
127
|
+
toAdd,
|
|
128
|
+
] = await Promise.all([
|
|
129
|
+
pToRemove,
|
|
130
|
+
pToAdd,
|
|
131
|
+
]);
|
|
132
|
+
|
|
133
|
+
const removed = new Set(toRemove);
|
|
134
|
+
|
|
135
|
+
const toRemain = new Set(dtPackagesPresent.keys().filter((name) => !removed.has(name)));
|
|
136
|
+
|
|
137
|
+
const toMove = new Map(dtRuntimeDepsPresent.entries().filter(([name]) => !removed.has(name)));
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
present: dtPackagesPresent,
|
|
141
|
+
toAdd: new Map(toAdd.filter((x) => x !== null)),
|
|
142
|
+
toMove,
|
|
143
|
+
toRemain,
|
|
144
|
+
toRemove,
|
|
145
|
+
};
|
|
146
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dt-clean",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Ensures the only DefinitelyTyped (`@types`) packages you have installed are the ones you need",
|
|
5
|
+
"bin": "./bin.mjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./package.json": "./package.json"
|
|
8
|
+
},
|
|
9
|
+
"imports": {
|
|
10
|
+
"#/*": "./*.mjs"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"prepack": "npmignore --auto --commentLines=autogenerated",
|
|
14
|
+
"prepublish": "not-in-publish || npm run prepublishOnly",
|
|
15
|
+
"prepublishOnly": "safe-publish-latest",
|
|
16
|
+
"postlint": "tsc && attw -P",
|
|
17
|
+
"pretest": "npm run lint",
|
|
18
|
+
"test": "npm run tests-only",
|
|
19
|
+
"tests-only": "c8 tape 'test/*.mjs'",
|
|
20
|
+
"tests:live": "LIVE=1 tape 'test/fixtures.mjs'",
|
|
21
|
+
"tests:registry": "tape 'test/live/registry.mjs'",
|
|
22
|
+
"tests:projects": "tape 'test/live/projects.mjs'",
|
|
23
|
+
"lint": "eslint .",
|
|
24
|
+
"posttest": "npx npm@'>= 10.2' audit --production",
|
|
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)\")\""
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/ljharb/dt-clean.git"
|
|
31
|
+
},
|
|
32
|
+
"author": "Jordan Harband <ljharb@gmail.com>",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/ljharb/dt-clean/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/ljharb/dt-clean#readme",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@definitelytyped/utils": "^0.1.14",
|
|
40
|
+
"es-errors": "^1.3.0",
|
|
41
|
+
"hastypes": "^4.0.4",
|
|
42
|
+
"pargs": "^1.4.2",
|
|
43
|
+
"semver": "^7.8.4"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@arethetypeswrong/cli": "^0.18.3",
|
|
47
|
+
"@ljharb/eslint-config": "^22.2.3",
|
|
48
|
+
"@ljharb/tsconfig": "^0.3.2",
|
|
49
|
+
"@types/node": "^25.9.3",
|
|
50
|
+
"@types/semver": "^7.7.1",
|
|
51
|
+
"auto-changelog": "^2.6.0",
|
|
52
|
+
"c8": "^11.0.0",
|
|
53
|
+
"eslint": "^10.5.0",
|
|
54
|
+
"esmock": "^2.7.6",
|
|
55
|
+
"globals": "^17.6.0",
|
|
56
|
+
"in-publish": "^2.0.1",
|
|
57
|
+
"npm-high-impact": "^1.13.0",
|
|
58
|
+
"npmignore": "^0.3.5",
|
|
59
|
+
"safe-publish-latest": "^2.0.0",
|
|
60
|
+
"tape": "^5.10.2",
|
|
61
|
+
"typescript": "next"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": "^24.16 || >= 26.3"
|
|
65
|
+
},
|
|
66
|
+
"auto-changelog": {
|
|
67
|
+
"output": "CHANGELOG.md",
|
|
68
|
+
"template": "keepachangelog",
|
|
69
|
+
"unreleased": false,
|
|
70
|
+
"commitLimit": false,
|
|
71
|
+
"backfillLimit": false,
|
|
72
|
+
"hideCredit": true
|
|
73
|
+
},
|
|
74
|
+
"publishConfig": {
|
|
75
|
+
"ignore": [
|
|
76
|
+
".github/workflows",
|
|
77
|
+
".c8rc.json",
|
|
78
|
+
"eslint.config.mjs",
|
|
79
|
+
"test",
|
|
80
|
+
"types"
|
|
81
|
+
]
|
|
82
|
+
}
|
|
83
|
+
}
|
package/report.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DTDelta } from './getDelTa.d.ts';
|
|
2
|
+
|
|
3
|
+
declare function formatReport(delta: formatReport.ReportDelta): string;
|
|
4
|
+
|
|
5
|
+
declare namespace formatReport {
|
|
6
|
+
type ReportDelta = Pick<
|
|
7
|
+
DTDelta,
|
|
8
|
+
'present' | 'toAdd' | 'toMove' | 'toRemove'
|
|
9
|
+
>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export = formatReport;
|
package/report.mjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const STATES = /** @type {const} */ ({
|
|
2
|
+
add: ['missing', 'add'],
|
|
3
|
+
keep: ['present', 'keep'],
|
|
4
|
+
move: ['present', 'move'],
|
|
5
|
+
remove: ['present', 'remove'],
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
/** @import { ReportDelta } from './report.d.ts' */
|
|
9
|
+
|
|
10
|
+
/** @param {ReportDelta} delta */
|
|
11
|
+
function toRows({
|
|
12
|
+
present,
|
|
13
|
+
toAdd,
|
|
14
|
+
toMove,
|
|
15
|
+
toRemove,
|
|
16
|
+
}) {
|
|
17
|
+
const removed = new Set(toRemove);
|
|
18
|
+
|
|
19
|
+
const rows = present.entries().map(([name, version]) => {
|
|
20
|
+
const action = removed.has(name)
|
|
21
|
+
? 'remove'
|
|
22
|
+
: toMove.has(name)
|
|
23
|
+
? 'move'
|
|
24
|
+
: 'keep';
|
|
25
|
+
|
|
26
|
+
/** @typedef {typeof STATES[keyof typeof STATES][number]} State */
|
|
27
|
+
|
|
28
|
+
return /** @type {[typeof name, State, State, typeof version]} */ ([
|
|
29
|
+
name,
|
|
30
|
+
STATES[action][0],
|
|
31
|
+
STATES[action][1],
|
|
32
|
+
version,
|
|
33
|
+
]);
|
|
34
|
+
}).toArray();
|
|
35
|
+
|
|
36
|
+
toAdd.forEach((version, name) => {
|
|
37
|
+
rows.push([
|
|
38
|
+
name,
|
|
39
|
+
STATES.add[0],
|
|
40
|
+
STATES.add[1],
|
|
41
|
+
version,
|
|
42
|
+
]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return rows.sort((a, b) => a[0].localeCompare(b[0]));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** @param {string[]} headers @param {string[][]} rows */
|
|
49
|
+
function table(headers, rows) {
|
|
50
|
+
const widths = headers.map((header, i) => Math.max(header.length, ...rows.map((row) => row[i].length)));
|
|
51
|
+
|
|
52
|
+
/** @param {string[]} cells */
|
|
53
|
+
function render(cells) {
|
|
54
|
+
return cells.map((cell, i) => cell.padEnd(widths[i])).join(' ').replace(/ +$/, '');
|
|
55
|
+
}
|
|
56
|
+
return /** @type {string[]} */ ([]).concat(
|
|
57
|
+
render(headers),
|
|
58
|
+
render(widths.map((width) => '-'.repeat(width))),
|
|
59
|
+
rows.map(render),
|
|
60
|
+
).join('\n');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** @type {import('./report.d.ts')} */
|
|
64
|
+
export default function formatReport(delta) {
|
|
65
|
+
const rows = toRows(delta);
|
|
66
|
+
if (rows.length === 0) {
|
|
67
|
+
return 'No `@types/*` packages are present or needed.';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @param {string} action */
|
|
71
|
+
function count(action) {
|
|
72
|
+
return rows.filter((row) => row[2] === action).length;
|
|
73
|
+
}
|
|
74
|
+
const summary = `${rows.length} \`@types/*\` package${rows.length === 1 ? '' : 's'}: ${count('keep')} keep, ${count('move')} move, ${count('remove')} remove, ${count('add')} add.`;
|
|
75
|
+
|
|
76
|
+
return `${table([
|
|
77
|
+
'Package',
|
|
78
|
+
'State',
|
|
79
|
+
'Action',
|
|
80
|
+
'Version',
|
|
81
|
+
], rows)}\n\n${summary}`;
|
|
82
|
+
}
|