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

yarn.config.cjs support #864

Merged
merged 7 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 7 additions & 0 deletions packages/knip/fixtures/plugins/yarn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@fixtures/yarn",
"version": "*",
"devDependencies": {
"@yarnpkg/types": "latest"
}
}
10 changes: 10 additions & 0 deletions packages/knip/fixtures/plugins/yarn/yarn.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://yarnpkg.com/features/constraints

/** @type {import('@yarnpkg/types')} */
const { defineConfig } = require('@yarnpkg/types');

module.exports = defineConfig({
async constraints({Yarn}) {
// `Yarn` is now well-typed ✨
},
});
6 changes: 6 additions & 0 deletions packages/knip/fixtures/plugins/yarn/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 8
cacheKey: 10
4 changes: 4 additions & 0 deletions packages/knip/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@
"title": "xo plugin configuration (https://knip.dev/reference/plugins/xo)",
"$ref": "#/definitions/plugin"
},
"yarn": {
"title": "yarn plugin configuration (https://knip.dev/reference/plugins/yarn)",
"$ref": "#/definitions/plugin"
},
"yorkie": {
"title": "yorkie plugin configuration (https://knip.dev/reference/plugins/yorkie)",
"$ref": "#/definitions/plugin"
Expand Down
2 changes: 2 additions & 0 deletions packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import { default as webpack } from './webpack/index.js';
import { default as wireit } from './wireit/index.js';
import { default as wrangler } from './wrangler/index.js';
import { default as xo } from './xo/index.js';
import { default as yarn } from './yarn/index.js';
import { default as yorkie } from './yorkie/index.js';

export const Plugins = {
Expand Down Expand Up @@ -172,5 +173,6 @@ export const Plugins = {
wireit,
wrangler,
xo,
yarn,
yorkie,
};
19 changes: 19 additions & 0 deletions packages/knip/src/plugins/yarn/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { IsPluginEnabled, Plugin } from '../../types/config.js';
import { _glob } from '../../util/glob.js';

// https://yarnpkg.com/features/constraints

const title = 'Yarn';

const enablers = 'This plugin is enabled when a `yarn.lock` file is found in the root folder.';

const isEnabled: IsPluginEnabled = async ({ cwd }) => (await _glob({ cwd, patterns: ['yarn.lock'] })).length > 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's _firstGlob for this, any chance we could use that? Example: https://github.com/webpro-nl/knip/blob/main/packages/knip/src/plugins/github-actions/index.ts#L13-L14 (probably should move into a nice helper but we could consider this in another PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great! I've changed it 0b8a898


const entry: string[] = ['yarn.config.cjs'];

export default {
title,
enablers,
isEnabled,
entry,
} satisfies Plugin;
1 change: 1 addition & 0 deletions packages/knip/src/schema/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,6 @@ export const pluginsSchema = z.object({
wireit: pluginSchema,
wrangler: pluginSchema,
xo: pluginSchema,
yarn: pluginSchema,
yorkie: pluginSchema,
});
2 changes: 2 additions & 0 deletions packages/knip/src/types/PluginNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export type PluginName =
| 'wireit'
| 'wrangler'
| 'xo'
| 'yarn'
| 'yorkie';

export const pluginNames = [
Expand Down Expand Up @@ -173,5 +174,6 @@ export const pluginNames = [
'wireit',
'wrangler',
'xo',
'yarn',
'yorkie',
] as const;
23 changes: 23 additions & 0 deletions packages/knip/test/plugins/yarn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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/yarn');

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

assert(issues.files.size === 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just saying, baseCounters contains files: 0 which is essentially the same assertion (no need to change though).


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