@ljharb/unused-files 0.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/.github/FUNDING.yml +12 -0
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +59 -0
- package/bin.mjs +33 -0
- package/help.txt +11 -0
- package/index.mjs +41 -0
- package/package.json +83 -0
- package/pargs.d.ts +5 -0
- package/pargs.mjs +100 -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/@ljharb/unused-files
|
|
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 up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
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
|
+
## v0.1.0 - 2024-03-13
|
|
9
|
+
|
|
10
|
+
### Commits
|
|
11
|
+
|
|
12
|
+
- Initial implementation, tests, readme, types [`bb8f77e`](https://github.com/ljharb/unused-files/commit/bb8f77e0de26f496a1b31f93fc5d6cedf6b81c32)
|
|
13
|
+
- Initial commit [`891f569`](https://github.com/ljharb/unused-files/commit/891f56957344e97a98628b4b3c51bd9a4c617a78)
|
|
14
|
+
- npm init [`82926ab`](https://github.com/ljharb/unused-files/commit/82926ab583b8cdd4603ac92bd06461a32c5138be)
|
|
15
|
+
- Only apps should have lockfiles [`04a0888`](https://github.com/ljharb/unused-files/commit/04a088837dbc0f441c57b2244591454fd8b9e563)
|
|
16
|
+
- [types] use the DT PR instead of waiting on the package [`32687bf`](https://github.com/ljharb/unused-files/commit/32687bfa844a97e64ef0cf24abd20e806991f0d1)
|
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,59 @@
|
|
|
1
|
+
# @ljharb/unused-files <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
|
+
List unused files in your package.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npx @ljharb/unused-files # if not installed
|
|
16
|
+
|
|
17
|
+
unused-files # if installed and in the PATH
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
$ unused-files --help
|
|
22
|
+
Usage: unused-files [options]
|
|
23
|
+
|
|
24
|
+
Options:
|
|
25
|
+
--ignorePattern <pattern> Ignore files matching the given pattern
|
|
26
|
+
[string]
|
|
27
|
+
[multiple]
|
|
28
|
+
[default: .github/**, coverage/**, test/**, tests/**, example/**]
|
|
29
|
+
|
|
30
|
+
--json Output the result as a JSON array
|
|
31
|
+
[boolean]
|
|
32
|
+
[default: false]
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
npm install --save-dev @ljharb/unused-files
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT
|
|
44
|
+
|
|
45
|
+
[package-url]: https://npmjs.org/package/@ljharb/unused-files
|
|
46
|
+
[npm-version-svg]: https://versionbadg.es/ljharb/unused-files.svg
|
|
47
|
+
[deps-svg]: https://david-dm.org/ljharb/unused-files.svg
|
|
48
|
+
[deps-url]: https://david-dm.org/ljharb/unused-files
|
|
49
|
+
[dev-deps-svg]: https://david-dm.org/ljharb/unused-files/dev-status.svg
|
|
50
|
+
[dev-deps-url]: https://david-dm.org/ljharb/unused-files#info=devDependencies
|
|
51
|
+
[npm-badge-png]: https://nodei.co/npm/@ljharb/unused-files.png?downloads=true&stars=true
|
|
52
|
+
[license-image]: https://img.shields.io/npm/l/@ljharb/unused-files.svg
|
|
53
|
+
[license-url]: LICENSE
|
|
54
|
+
[downloads-image]: https://img.shields.io/npm/dm/@ljharb/unused-files.svg
|
|
55
|
+
[downloads-url]: https://npm-stat.com/charts.html?package=@ljharb/unused-files
|
|
56
|
+
[codecov-image]: https://codecov.io/gh/ljharb/unused-files/branch/main/graphs/badge.svg
|
|
57
|
+
[codecov-url]: https://app.codecov.io/gh/ljharb/unused-files/
|
|
58
|
+
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/unused-files
|
|
59
|
+
[actions-url]: https://github.com/ljharb/unused-files/actions
|
package/bin.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { readFile } from 'fs/promises';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
|
|
6
|
+
import unusedFiles from './index.mjs';
|
|
7
|
+
|
|
8
|
+
import pargs from './pargs.mjs';
|
|
9
|
+
|
|
10
|
+
const help = await readFile(join(import.meta.dirname, './help.txt'), 'utf8');
|
|
11
|
+
|
|
12
|
+
const { values: { json, ignorePattern } } = pargs(help, import.meta.filename, {
|
|
13
|
+
options: {
|
|
14
|
+
ignorePattern: {
|
|
15
|
+
default: undefined,
|
|
16
|
+
multiple: true,
|
|
17
|
+
type: 'string',
|
|
18
|
+
},
|
|
19
|
+
json: {
|
|
20
|
+
default: false,
|
|
21
|
+
type: 'boolean',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// eslint-disable-next-line no-extra-parens
|
|
27
|
+
const files = await unusedFiles(process.cwd(), /** @type {string[] | undefined} */ (ignorePattern));
|
|
28
|
+
|
|
29
|
+
if (json) {
|
|
30
|
+
console.log(JSON.stringify(files, null, '\t'));
|
|
31
|
+
} else {
|
|
32
|
+
files.forEach((x) => console.log(x));
|
|
33
|
+
}
|
package/help.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Usage: unused-files [options]
|
|
2
|
+
|
|
3
|
+
Options:
|
|
4
|
+
--ignorePattern <pattern> Ignore files matching the given pattern
|
|
5
|
+
[string]
|
|
6
|
+
[multiple]
|
|
7
|
+
[default: .github/**, coverage/**, test/**, tests/**, example/**]
|
|
8
|
+
|
|
9
|
+
--json Output the result as a JSON array
|
|
10
|
+
[boolean]
|
|
11
|
+
[default: false]
|
package/index.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { relative } from 'path';
|
|
2
|
+
import micromatch from 'micromatch';
|
|
3
|
+
|
|
4
|
+
export default async function unusedFiles(
|
|
5
|
+
cwd = process.cwd(),
|
|
6
|
+
ignorePattern = [
|
|
7
|
+
'.github/**',
|
|
8
|
+
'coverage/**',
|
|
9
|
+
'test/**',
|
|
10
|
+
'tests/**',
|
|
11
|
+
'example/**',
|
|
12
|
+
],
|
|
13
|
+
) {
|
|
14
|
+
const origArgv = process.argv;
|
|
15
|
+
let files;
|
|
16
|
+
try {
|
|
17
|
+
process.argv = origArgv.slice(0, 2);
|
|
18
|
+
const { main: knip } = await import('knip');
|
|
19
|
+
|
|
20
|
+
({ issues: { files } } = await knip({
|
|
21
|
+
cwd,
|
|
22
|
+
fixTypes: [],
|
|
23
|
+
gitignore: true,
|
|
24
|
+
isFix: false,
|
|
25
|
+
isIncludeEntryExports: true,
|
|
26
|
+
isIsolateWorkspaces: false,
|
|
27
|
+
isProduction: true,
|
|
28
|
+
isShowProgress: false,
|
|
29
|
+
isStrict: true,
|
|
30
|
+
tags: [[], []],
|
|
31
|
+
}));
|
|
32
|
+
} finally {
|
|
33
|
+
process.argv = origArgv;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return Array.from(files).flatMap((x) => (
|
|
37
|
+
micromatch.isMatch(relative(cwd, x), ignorePattern)
|
|
38
|
+
? []
|
|
39
|
+
: `./${relative(cwd, x)}`
|
|
40
|
+
));
|
|
41
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ljharb/unused-files",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "List unused files in your package.",
|
|
5
|
+
"bin": "./bin.mjs",
|
|
6
|
+
"main": false,
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package.json": "./package.json"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"prepublishOnly": "safe-publish-latest",
|
|
13
|
+
"prepublish": "not-in-publish || npm run prepublishOnly",
|
|
14
|
+
"prepack": "npmignore --auto",
|
|
15
|
+
"lint": "eslint --ext=js,mjs .",
|
|
16
|
+
"postlint": "tsc -p . && attw -P",
|
|
17
|
+
"pretest": "npm run lint",
|
|
18
|
+
"tests-only": "c8 tape 'test/**/*.mjs'",
|
|
19
|
+
"test": "npm run tests-only",
|
|
20
|
+
"posttest": "aud --production",
|
|
21
|
+
"version": "auto-changelog && git add CHANGELOG.md",
|
|
22
|
+
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/ljharb/unused-files.git"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"unused",
|
|
30
|
+
"files",
|
|
31
|
+
"package"
|
|
32
|
+
],
|
|
33
|
+
"author": "Jordan Harband <ljharb@gmail.com>",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/ljharb/unused-files/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/ljharb/unused-files#readme",
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"knip": "^5.0.4",
|
|
41
|
+
"micromatch": "^4.0.5",
|
|
42
|
+
"object.groupby": "^1.0.2"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@arethetypeswrong/cli": "^0.15.1",
|
|
46
|
+
"@ljharb/eslint-config": "^21.1.0",
|
|
47
|
+
"@ljharb/tsconfig": "^0.2.0",
|
|
48
|
+
"@types/micromatch": "^4.0.6",
|
|
49
|
+
"@types/object.groupby": "https://gitpkg.now.sh/DefinitelyTyped/DefinitelyTyped/types/object.groupby?2489af71f28f861b4bf3884ecf35a3d204e3bb48",
|
|
50
|
+
"@types/tape": "^5.6.4",
|
|
51
|
+
"@types/webpack": "^5.28.5",
|
|
52
|
+
"aud": "^2.0.4",
|
|
53
|
+
"auto-changelog": "^2.4.0",
|
|
54
|
+
"c8": "^9.1.0",
|
|
55
|
+
"eslint": "^8.8.0",
|
|
56
|
+
"in-publish": "^2.0.1",
|
|
57
|
+
"npmignore": "^0.3.1",
|
|
58
|
+
"playwright": "^1.42.1",
|
|
59
|
+
"safe-publish-latest": "^2.0.0",
|
|
60
|
+
"tape": "^5.7.5",
|
|
61
|
+
"typescript": "next"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": "^20.11.1 || >= 21.7.1"
|
|
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
|
+
"access": "public",
|
|
76
|
+
"ignore": [
|
|
77
|
+
".github/workflows",
|
|
78
|
+
".eslintrc",
|
|
79
|
+
".nycrc",
|
|
80
|
+
"test"
|
|
81
|
+
]
|
|
82
|
+
}
|
|
83
|
+
}
|
package/pargs.d.ts
ADDED
package/pargs.mjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { parseArgs } from 'util';
|
|
2
|
+
import { realpathSync } from 'fs';
|
|
3
|
+
|
|
4
|
+
import groupByPolyfill from 'object.groupby/polyfill';
|
|
5
|
+
|
|
6
|
+
const groupBy = groupByPolyfill();
|
|
7
|
+
|
|
8
|
+
/** @typedef {import('util').ParseArgsConfig} ParseArgsConfig */
|
|
9
|
+
|
|
10
|
+
/** @typedef {(Error | TypeError) & { code: 'ERR_PARSE_ARGS_UNKNOWN_OPTION' | 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE' | 'ERR_INVALID_ARG_TYPE' | 'ERR_INVALID_ARG_VALUE' | 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL'}} ParseArgsError */
|
|
11
|
+
|
|
12
|
+
/** @type {(e: unknown) => e is ParseArgsError} */
|
|
13
|
+
function isParseArgsError(e) {
|
|
14
|
+
return !!e
|
|
15
|
+
&& typeof e === 'object'
|
|
16
|
+
&& 'code' in e
|
|
17
|
+
&& (
|
|
18
|
+
e.code === 'ERR_PARSE_ARGS_UNKNOWN_OPTION'
|
|
19
|
+
|| e.code === 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
|
|
20
|
+
|| e.code === 'ERR_INVALID_ARG_TYPE'
|
|
21
|
+
|| e.code === 'ERR_INVALID_ARG_VALUE'
|
|
22
|
+
|| e.code === 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL'
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @type {(helpText: string, entrypointPath: string, obj: Exclude<ParseArgsConfig, 'args'>) => ReturnType<typeof parseArgs>} */
|
|
27
|
+
export default function pargs(helpText, entrypointPath, obj) {
|
|
28
|
+
const argv = process.argv.flatMap((arg) => {
|
|
29
|
+
try {
|
|
30
|
+
const realpathedArg = realpathSync(arg);
|
|
31
|
+
if (
|
|
32
|
+
realpathedArg === process.execPath
|
|
33
|
+
|| realpathedArg === entrypointPath
|
|
34
|
+
) {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
} catch (e) { /**/ }
|
|
38
|
+
return arg;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if ('help' in obj) {
|
|
42
|
+
throw new TypeError('The "help" option is reserved');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const bools = obj.options ? Object.entries(obj.options).filter(([key, { type }]) => type === 'boolean' && key !== 'help') : [];
|
|
46
|
+
const inverseBools = Object.fromEntries(bools.map(([key, value]) => [
|
|
47
|
+
`no-${key}`,
|
|
48
|
+
{ default: !value.default, type: 'boolean' },
|
|
49
|
+
]));
|
|
50
|
+
/** @type {ParseArgsConfig & { tokens: true }} */
|
|
51
|
+
const newObj = {
|
|
52
|
+
args: argv,
|
|
53
|
+
...obj,
|
|
54
|
+
options: {
|
|
55
|
+
...obj.options,
|
|
56
|
+
...inverseBools,
|
|
57
|
+
help: {
|
|
58
|
+
default: false,
|
|
59
|
+
type: 'boolean',
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
tokens: true,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const { tokens, ...results } = parseArgs(newObj);
|
|
67
|
+
|
|
68
|
+
if ('help' in results.values && results.values.help) {
|
|
69
|
+
console.log(helpText);
|
|
70
|
+
process.exit(0);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** @typedef {Extract<typeof tokens[number], { kind: 'option' }>} OptionToken */
|
|
74
|
+
const optionTokens = tokens.filter(/** @type {(token: typeof tokens[number]) => token is OptionToken} */ (token) => token.kind === 'option');
|
|
75
|
+
const passedArgs = new Set(optionTokens.map(({ name }) => name));
|
|
76
|
+
const groups = groupBy(passedArgs, (x) => x.replace(/^no-/, ''));
|
|
77
|
+
bools.forEach(([key]) => {
|
|
78
|
+
if ((groups[key]?.length ?? 0) > 1) {
|
|
79
|
+
console.log(helpText);
|
|
80
|
+
console.error(`Error: Arguments --${key} and --no-${key} are mutually exclusive`);
|
|
81
|
+
process.exit(2);
|
|
82
|
+
}
|
|
83
|
+
if (passedArgs.has(`no-${key}`)) {
|
|
84
|
+
// @ts-expect-error
|
|
85
|
+
results.values[key] = !results.values[`no-${key}`];
|
|
86
|
+
}
|
|
87
|
+
// @ts-expect-error
|
|
88
|
+
delete results.values[`no-${key}`];
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return obj.tokens ? { ...results, tokens } : results;
|
|
92
|
+
} catch (e) {
|
|
93
|
+
if (isParseArgsError(e)) {
|
|
94
|
+
console.log(helpText);
|
|
95
|
+
console.error(`Error: ${e.message}`);
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
throw e;
|
|
99
|
+
}
|
|
100
|
+
}
|
package/tsconfig.json
ADDED