Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dependency-cruiser plugin #911

Merged
merged 7 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions packages/knip/fixtures/plugins/dependency-cruiser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@fixtures/dependency-cruiser",
"version": "*",
"devDependencies": {
"dependency-cruiser": "*"
},
"scripts": {
"test": "dependency-cruiser",
"test:custom": "depcruise --config custom-depcruise-config.js",
"test:baseline": "depcruise-baseline -c baseline.config.js",
"test:dependency-cruise": "dependency-cruise -c dependency-cruise.config.js"
}
}
4 changes: 4 additions & 0 deletions packages/knip/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@
"title": "Cypress plugin configuration (https://knip.dev/reference/plugins/cypress)",
"$ref": "#/definitions/plugin"
},
"dependency-cruiser": {
"title": "dependency-cruiser plugin configuration (https://knip.dev/reference/plugins/dependency-cruiser)",
"$ref": "#/definitions/plugin"
},
"dotenv": {
"title": "dotenv plugin configuration (https://knip.dev/reference/plugins/dotenv)",
"$ref": "#/definitions/plugin"
Expand Down
25 changes: 25 additions & 0 deletions packages/knip/src/plugins/dependency-cruiser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { IsPluginEnabled, Plugin } from '../../types/config.js';
import { hasDependency } from '../../util/plugin.js';

// https://github.com/sverweij/dependency-cruiser

const title = 'dependency-cruiser';

const enablers = ['dependency-cruiser'];

const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);

const config = ['.dependency-cruiser.{js,cjs,mjs,json}'];

const args = {
binaries: ['depcruise', 'dependency-cruise', 'dependency-cruiser', 'depcruise-baseline'],
config: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting config: true is a shorthand that sets the alias and string options as well:

/**
* Define arguments that contain config file path.
* Usually you'll want to set aliases too. Use `true` for shorthand to set `alias` + `string` + `config`
*
* @example `config: ["p"]` for e.g. `tsc -p tsconfig.lib.json`
*
* @example `config: true` for e.g. `tsup --config tsup.client.json`
*
* @default undefined
*/
config?: ConfigArg;

just a heads-up, no need to change if you prefer explicit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I didn't know that

};

export default {
title,
enablers,
isEnabled,
config,
args,
} satisfies Plugin;
2 changes: 2 additions & 0 deletions packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { default as commitlint } from './commitlint/index.js';
import { default as cspell } from './cspell/index.js';
import { default as cucumber } from './cucumber/index.js';
import { default as cypress } from './cypress/index.js';
import { default as dependencyCruiser } from './dependency-cruiser/index.js';
import { default as dotenv } from './dotenv/index.js';
import { default as drizzle } from './drizzle/index.js';
import { default as eleventy } from './eleventy/index.js';
Expand Down Expand Up @@ -103,6 +104,7 @@ export const Plugins = {
cspell,
cucumber,
cypress,
'dependency-cruiser': dependencyCruiser,
dotenv,
drizzle,
eleventy,
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/schema/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const pluginsSchema = z.object({
cspell: pluginSchema,
cucumber: pluginSchema,
cypress: pluginSchema,
'dependency-cruiser': pluginSchema,
dotenv: pluginSchema,
drizzle: pluginSchema,
eleventy: pluginSchema,
Expand Down
2 changes: 2 additions & 0 deletions packages/knip/src/types/PluginNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type PluginName =
| 'cspell'
| 'cucumber'
| 'cypress'
| 'dependency-cruiser'
| 'dotenv'
| 'drizzle'
| 'eleventy'
Expand Down Expand Up @@ -104,6 +105,7 @@ export const pluginNames = [
'cspell',
'cucumber',
'cypress',
'dependency-cruiser',
'dotenv',
'drizzle',
'eleventy',
Expand Down
21 changes: 21 additions & 0 deletions packages/knip/test/plugins/dependency-cruiser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { main } from '../../src/index.js';
import { resolve } from '../../src/util/path.js';
import baseArguments from '../helpers/baseArguments.js';
import baseCounters from '../helpers/baseCounters.js';

const cwd = resolve('fixtures/plugins/dependency-cruiser');

test('Find dependencies with the dependency-cruiser plugin', async () => {
const { counters } = await main({
...baseArguments,
cwd,
});

assert.deepEqual(counters, {
...baseCounters,
processed: 4,
total: 4,
});
});