Skip to content

Commit

Permalink
yarn.config.cjs support (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoo0122 authored Dec 2, 2024
1 parent 7950bf3 commit 759503d
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 0 deletions.
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 { _firstGlob } 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 }) => Boolean(await _firstGlob({ cwd, patterns: ['yarn.lock'] }));

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);

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

0 comments on commit 759503d

Please sign in to comment.