knip 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -66,6 +66,19 @@ with a configuration file (or a `knip` property in `package.json`). Let's name t
66
66
  The `entry` files target the starting point(s) to resolve the rest of the imported code. The `project` files should
67
67
  contain all files to match against the files resolved from the entry files, including potentially unused files.
68
68
 
69
+ Use `knip.ts` with TypeScript if you prefer:
70
+
71
+ ```ts
72
+ import type { KnipConfig } from 'knip';
73
+
74
+ const config: KnipConfig = {
75
+ entry: ['src/index.ts'],
76
+ project: ['src/**/*.ts'],
77
+ };
78
+
79
+ export default config;
80
+ ```
81
+
69
82
  If you have, please see [workspaces & monorepos][1].
70
83
 
71
84
  Then run the checks with `npx knip`. Or first add this script to `package.json`:
@@ -84,11 +97,13 @@ Use `npm run knip` to analyze the project and output unused files, dependencies
84
97
  ## Command-line options
85
98
 
86
99
  $ npx knip --help
87
- knip [options]
100
+ ✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects
101
+
102
+ Usage: knip [options]
88
103
 
89
104
  Options:
90
- -c/--config [file] Configuration file path (default: [.]knip.json[c], knip.js, knip.ts or package.json#knip)
91
- -t/--tsConfig [file] TypeScript configuration path (default: tsconfig.json)
105
+ -c, --config [file] Configuration file path (default: [.]knip.json[c], knip.js, knip.ts or package.json#knip)
106
+ -t, --tsConfig [file] TypeScript configuration path (default: tsconfig.json)
92
107
  --production Analyze only production source files (e.g. no tests, devDependencies, exported types)
93
108
  --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)
94
109
  --workspace Analyze a single workspace (default: analyze all configured workspaces)
@@ -107,6 +122,8 @@ Use `npm run knip` to analyze the project and output unused files, dependencies
107
122
  --debug Show debug output
108
123
  --debug-file-filter Filter for files in debug output (regex as string)
109
124
  --performance Measure running time of expensive functions and display stats table
125
+ --h, --help Print this help text
126
+ --V, version Print version
110
127
 
111
128
  (1) Issue types: files, dependencies, unlisted, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates
112
129
 
@@ -119,7 +136,7 @@ Use `npm run knip` to analyze the project and output unused files, dependencies
119
136
  $ knip --reporter codeowners --reporter-options '{"path":".github/CODEOWNERS"}'
120
137
  $ knip --debug --debug-file-filter '(specific|particular)-module'
121
138
 
122
- More info: https://github.com/webpro/knip
139
+ More documentation and bug reports: https://github.com/webpro/knip
123
140
 
124
141
  ## Screenshots
125
142
 
@@ -363,7 +380,7 @@ aliases. They can be configured manually:
363
380
 
364
381
  ```json
365
382
  {
366
- "$schema": "/Users/lars/p/knip/schema.json",
383
+ "$schema": "https://unpkg.com/knip@1/schema.json",
367
384
  "paths": {
368
385
  "@lib": ["./lib/index.ts"],
369
386
  "@lib/*": ["./lib/*"]
package/dist/cli.js CHANGED
@@ -6,13 +6,18 @@ import parsedArgs, { helpText } from './util/cli-arguments.js';
6
6
  import { ConfigurationError } from './util/errors.js';
7
7
  import { _load } from './util/loader.js';
8
8
  import { measure } from './util/performance.js';
9
+ import { version } from './version.js';
9
10
  import { main } from './index.js';
10
11
  register();
11
- const { values: { debug: isDebug = false, help, 'include-entry-exports': isIncludeEntryExports = false, 'max-issues': maxIssues = '0', 'no-exit-code': noExitCode = false, 'no-gitignore': isNoGitIgnore = false, 'no-progress': isNoProgress = false, production: isProduction = false, reporter = 'symbols', 'reporter-options': reporterOptions = '', strict: isStrict = false, tsConfig, }, } = parsedArgs;
12
- if (help) {
12
+ const { values: { debug: isDebug = false, help: isHelp, 'include-entry-exports': isIncludeEntryExports = false, 'max-issues': maxIssues = '0', 'no-exit-code': noExitCode = false, 'no-gitignore': isNoGitIgnore = false, 'no-progress': isNoProgress = false, production: isProduction = false, reporter = 'symbols', 'reporter-options': reporterOptions = '', strict: isStrict = false, tsConfig, version: isVersion, }, } = parsedArgs;
13
+ if (isHelp) {
13
14
  console.log(helpText);
14
15
  process.exit(0);
15
16
  }
17
+ if (isVersion) {
18
+ console.log(version);
19
+ process.exit(0);
20
+ }
16
21
  const cwd = process.cwd();
17
22
  const isShowProgress = !isDebug && isNoProgress === false && process.stdout.isTTY && typeof process.stdout.cursorTo === 'function';
18
23
  const printReport = reporter in reporters ? reporters[reporter] : await _load(path.join(cwd, reporter));
@@ -12,6 +12,7 @@ export default class ConfigurationChief {
12
12
  manifestPath: undefined | string;
13
13
  manifest: undefined | PackageJson;
14
14
  manifestWorkspaces: undefined | string[];
15
+ resolvedConfigFilePath: undefined | string;
15
16
  constructor({ cwd, isProduction }: ConfigurationManagerOptions);
16
17
  loadLocalConfig(): Promise<void>;
17
18
  normalize(rawLocalConfig: RawConfiguration): {
@@ -34,6 +34,7 @@ export default class ConfigurationChief {
34
34
  manifestPath;
35
35
  manifest;
36
36
  manifestWorkspaces;
37
+ resolvedConfigFilePath;
37
38
  constructor({ cwd, isProduction }) {
38
39
  this.cwd = cwd;
39
40
  this.isProduction = isProduction;
@@ -52,16 +53,15 @@ export default class ConfigurationChief {
52
53
  if (this.manifest && !this.manifest.workspaces && pnpmWorkspaces) {
53
54
  this.manifest.workspaces = pnpmWorkspaces;
54
55
  }
55
- let resolvedConfigFilePath;
56
56
  for (const configPath of rawConfigArg ? [rawConfigArg] : KNIP_CONFIG_LOCATIONS) {
57
- resolvedConfigFilePath = findFile(this.cwd, configPath);
58
- if (resolvedConfigFilePath)
57
+ this.resolvedConfigFilePath = findFile(this.cwd, configPath);
58
+ if (this.resolvedConfigFilePath)
59
59
  break;
60
60
  }
61
- if (rawConfigArg && !resolvedConfigFilePath && !manifest.knip) {
61
+ if (rawConfigArg && !this.resolvedConfigFilePath && !manifest.knip) {
62
62
  throw new ConfigurationError(`Unable to find ${rawConfigArg} or package.json#knip`);
63
63
  }
64
- const rawLocalConfig = resolvedConfigFilePath ? await _load(resolvedConfigFilePath) : manifest.knip;
64
+ const rawLocalConfig = this.resolvedConfigFilePath ? await _load(this.resolvedConfigFilePath) : manifest.knip;
65
65
  if (rawLocalConfig) {
66
66
  this.config = this.normalize(ConfigurationValidator.parse(rawLocalConfig));
67
67
  }
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ import WorkspaceWorker from './workspace-worker.js';
17
17
  export const main = async (unresolvedConfiguration) => {
18
18
  const { cwd, tsConfigFile, gitignore, isStrict, isProduction, isShowProgress, isIncludeEntryExports } = unresolvedConfiguration;
19
19
  const chief = new ConfigurationChief({ cwd, isProduction });
20
- debugLogObject('Unresolved configuration', unresolvedConfiguration);
20
+ debugLogObject('Unresolved configuration (from CLI arguments)', unresolvedConfiguration);
21
21
  const collector = new IssueCollector({ cwd, isShowProgress });
22
22
  collector.updateMessage('Reading configuration and manifest files...');
23
23
  await chief.loadLocalConfig();
@@ -195,6 +195,9 @@ export const main = async (unresolvedConfiguration) => {
195
195
  }
196
196
  collector.setReport(report);
197
197
  collector.updateMessage('Connecting the dots...');
198
+ if (chief.resolvedConfigFilePath) {
199
+ principal.removeProjectPath(chief.resolvedConfigFilePath);
200
+ }
198
201
  principal.createProjects();
199
202
  const moduleSpecifierCache = new WeakMap();
200
203
  const resolvedFiles = principal.getResolvedFiles();
@@ -12,6 +12,7 @@ export default class ProjectPrincipal {
12
12
  addEntryPath(filePath: string): void;
13
13
  addSourceFile(filePath: string): void;
14
14
  addProjectPath(filePath: string): void;
15
+ removeProjectPath(filePath: string): void;
15
16
  addTypeScriptPaths(workspaceDir: string, compilerOptions: TsConfigJson['compilerOptions']): void;
16
17
  createProjects(): void;
17
18
  getResolvedFiles(): SourceFile[];
@@ -31,6 +31,9 @@ export default class ProjectPrincipal {
31
31
  addProjectPath(filePath) {
32
32
  this.projectPaths.add(filePath);
33
33
  }
34
+ removeProjectPath(filePath) {
35
+ this.projectPaths.delete(filePath);
36
+ }
34
37
  addTypeScriptPaths(workspaceDir, compilerOptions) {
35
38
  if (!compilerOptions || !compilerOptions.paths)
36
39
  return;
@@ -1,4 +1,4 @@
1
- export declare const helpText = "knip [options]\n\nOptions:\n -c/--config [file] Configuration file path (default: [.]knip.json[c], knip.js, knip.ts or package.json#knip)\n -t/--tsConfig [file] TypeScript configuration path (default: tsconfig.json)\n --production Analyze only production source files (e.g. no tests, devDependencies, exported types)\n --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)\n --workspace Analyze a single workspace (default: analyze all configured workspaces)\n --include-entry-exports Include unused exports in entry files (without `@public`)\n --ignore Ignore files matching this glob pattern, can be repeated\n --no-gitignore Don't use .gitignore\n --include Report only provided issue type(s), can be comma-separated or repeated (1)\n --exclude Exclude provided issue type(s) from report, can be comma-separated or repeated (1)\n --dependencies Shortcut for --include dependencies,unlisted\n --exports Shortcut for --include exports,nsExports,classMembers,types,nsTypes,enumMembers,duplicates\n --no-progress Don't show dynamic progress updates\n --reporter Select reporter: symbols, compact, codeowners, json (default: symbols)\n --reporter-options Pass extra options to the reporter (as JSON string, see example)\n --no-exit-code Always exit with code zero (0)\n --max-issues Maximum number of issues before non-zero exit code (default: 0)\n --debug Show debug output\n --debug-file-filter Filter for files in debug output (regex as string)\n --performance Measure running time of expensive functions and display stats table\n\n(1) Issue types: files, dependencies, unlisted, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates\n\nExamples:\n\n$ knip\n$ knip --production\n$ knip --workspace packages/client --include files,dependencies\n$ knip -c ./config/knip.json --reporter compact\n$ knip --reporter codeowners --reporter-options '{\"path\":\".github/CODEOWNERS\"}'\n$ knip --debug --debug-file-filter '(specific|particular)-module'\n\nMore info: https://github.com/webpro/knip";
1
+ export declare const helpText = "\u2702\uFE0F Find unused files, dependencies and exports in your JavaScript and TypeScript projects\n\nUsage: knip [options]\n\nOptions:\n -c, --config [file] Configuration file path (default: [.]knip.json[c], knip.js, knip.ts or package.json#knip)\n -t, --tsConfig [file] TypeScript configuration path (default: tsconfig.json)\n --production Analyze only production source files (e.g. no tests, devDependencies, exported types)\n --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)\n --workspace Analyze a single workspace (default: analyze all configured workspaces)\n --include-entry-exports Include unused exports in entry files (without `@public`)\n --ignore Ignore files matching this glob pattern, can be repeated\n --no-gitignore Don't use .gitignore\n --include Report only provided issue type(s), can be comma-separated or repeated (1)\n --exclude Exclude provided issue type(s) from report, can be comma-separated or repeated (1)\n --dependencies Shortcut for --include dependencies,unlisted\n --exports Shortcut for --include exports,nsExports,classMembers,types,nsTypes,enumMembers,duplicates\n --no-progress Don't show dynamic progress updates\n --reporter Select reporter: symbols, compact, codeowners, json (default: symbols)\n --reporter-options Pass extra options to the reporter (as JSON string, see example)\n --no-exit-code Always exit with code zero (0)\n --max-issues Maximum number of issues before non-zero exit code (default: 0)\n --debug Show debug output\n --debug-file-filter Filter for files in debug output (regex as string)\n --performance Measure running time of expensive functions and display stats table\n --h, --help Print this help text\n --V, version Print version\n\n(1) Issue types: files, dependencies, unlisted, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates\n\nExamples:\n\n$ knip\n$ knip --production\n$ knip --workspace packages/client --include files,dependencies\n$ knip -c ./config/knip.json --reporter compact\n$ knip --reporter codeowners --reporter-options '{\"path\":\".github/CODEOWNERS\"}'\n$ knip --debug --debug-file-filter '(specific|particular)-module'\n\nMore documentation and bug reports: https://github.com/webpro/knip";
2
2
  declare const _default: {
3
3
  values: {
4
4
  config: string | undefined;
@@ -21,6 +21,7 @@ declare const _default: {
21
21
  'reporter-options': string | undefined;
22
22
  strict: boolean | undefined;
23
23
  tsConfig: string | undefined;
24
+ version: boolean | undefined;
24
25
  workspace: string | undefined;
25
26
  };
26
27
  positionals: [];
@@ -1,9 +1,11 @@
1
1
  import { parseArgs } from 'node:util';
2
- export const helpText = `knip [options]
2
+ export const helpText = `✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects
3
+
4
+ Usage: knip [options]
3
5
 
4
6
  Options:
5
- -c/--config [file] Configuration file path (default: [.]knip.json[c], knip.js, knip.ts or package.json#knip)
6
- -t/--tsConfig [file] TypeScript configuration path (default: tsconfig.json)
7
+ -c, --config [file] Configuration file path (default: [.]knip.json[c], knip.js, knip.ts or package.json#knip)
8
+ -t, --tsConfig [file] TypeScript configuration path (default: tsconfig.json)
7
9
  --production Analyze only production source files (e.g. no tests, devDependencies, exported types)
8
10
  --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)
9
11
  --workspace Analyze a single workspace (default: analyze all configured workspaces)
@@ -22,6 +24,8 @@ Options:
22
24
  --debug Show debug output
23
25
  --debug-file-filter Filter for files in debug output (regex as string)
24
26
  --performance Measure running time of expensive functions and display stats table
27
+ --h, --help Print this help text
28
+ --V, version Print version
25
29
 
26
30
  (1) Issue types: files, dependencies, unlisted, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates
27
31
 
@@ -34,7 +38,7 @@ $ knip -c ./config/knip.json --reporter compact
34
38
  $ knip --reporter codeowners --reporter-options '{"path":".github/CODEOWNERS"}'
35
39
  $ knip --debug --debug-file-filter '(specific|particular)-module'
36
40
 
37
- More info: https://github.com/webpro/knip`;
41
+ More documentation and bug reports: https://github.com/webpro/knip`;
38
42
  export default parseArgs({
39
43
  options: {
40
44
  config: { type: 'string', short: 'c' },
@@ -57,6 +61,7 @@ export default parseArgs({
57
61
  'reporter-options': { type: 'string' },
58
62
  strict: { type: 'boolean' },
59
63
  tsConfig: { type: 'string', short: 't' },
64
+ version: { type: 'boolean', short: 'V' },
60
65
  workspace: { type: 'string' },
61
66
  },
62
67
  });
@@ -0,0 +1 @@
1
+ export declare const version = "1.9.0";
@@ -0,0 +1 @@
1
+ export const version = '1.9.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects",
5
5
  "homepage": "https://github.com/webpro/knip",
6
6
  "repository": "github:webpro/knip",
@@ -63,6 +63,7 @@
63
63
  "devDependencies": {
64
64
  "@jest/types": "29.3.1",
65
65
  "@npmcli/package-json": "3.0.0",
66
+ "@release-it/bumper": "^4.0.2",
66
67
  "@types/eslint": "8.4.10",
67
68
  "@types/js-yaml": "4.0.5",
68
69
  "@types/micromatch": "4.0.2",
@@ -98,6 +99,11 @@
98
99
  },
99
100
  "github": {
100
101
  "release": true
102
+ },
103
+ "plugins": {
104
+ "@release-it/bumper": {
105
+ "out": "{dist,src}/version.*"
106
+ }
101
107
  }
102
108
  },
103
109
  "remarkConfig": {