Skip to content

Commit

Permalink
Add react-cosmos plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jul 19, 2024
1 parent efd7d2a commit 2de3924
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 0 deletions.
Empty file.
16 changes: 16 additions & 0 deletions packages/knip/fixtures/plugins/react-cosmos/cosmos.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json.schemastore.org/cosmos-config",
"plugins": [
"react-cosmos-plugin-open-fixture",
"react-cosmos-plugin-boolean-input",
"react-cosmos-plugin-webpack",
"./src/my-cosmos-plugin.ts"
],
"webpack": {
"configPath": "./webpack.config.cosmos.js",
"overridePath": "./webpack.override.js"
},
"vite": {
"configPath": "./tools/vite.config.js"
}
}
Empty file.

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

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

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

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

Empty file.

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

16 changes: 16 additions & 0 deletions packages/knip/fixtures/plugins/react-cosmos/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@fixtures/react-cosmos",
"version": "*",
"scripts": {
"cosmos": "cosmos",
"cosmos-export": "cosmos-export",
"cosmos-native": "cosmos-native"
},
"devDependencies": {
"react-cosmos": "*",
"react-cosmos-native": "*",
"react-cosmos-plugin-boolean-input": "*",
"react-cosmos-plugin-open-fixture": "*",
"react-cosmos-plugin-webpack": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function MyDecorator() {}
Empty file.
4 changes: 4 additions & 0 deletions packages/knip/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@
"title": "Prettier plugin configuration (https://knip.dev/reference/plugins/prettier)",
"$ref": "#/definitions/plugin"
},
"react-cosmos": {
"title": "react-cosmos plugin configuration (https://knip.dev/reference/plugins/react-cosmos)",
"$ref": "#/definitions/plugin"
},
"release-it": {
"title": "Release It plugin configuration (https://knip.dev/reference/plugins/release-it)",
"$ref": "#/definitions/plugin"
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/ConfigurationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const pluginsSchema = z.object({
'playwright-ct': pluginSchema,
postcss: pluginSchema,
prettier: pluginSchema,
'react-cosmos': pluginSchema,
'release-it': pluginSchema,
remark: pluginSchema,
remix: pluginSchema,
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export { default as playwright } from './playwright/index.js';
export { default as playwrightCt } from './playwright-ct/index.js';
export { default as postcss } from './postcss/index.js';
export { default as prettier } from './prettier/index.js';
export { default as reactCosmos } from './react-cosmos/index.js';
export { default as releaseIt } from './release-it/index.js';
export { default as remark } from './remark/index.js';
export { default as remix } from './remix/index.js';
Expand Down
49 changes: 49 additions & 0 deletions packages/knip/src/plugins/react-cosmos/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { EnablerPatterns } from '#p/types/config.js';
import type { IsPluginEnabled, Plugin, ResolveConfig, ResolveEntryPaths } from '#p/types/plugins.js';
import { hasDependency, resolveEntry } from '#p/util/plugin.js';
import { join } from '../../util/path.js';
import { toEntryPattern } from '../../util/protocols.js';
import type { ReactCosmosConfig } from './types.js';

// https://reactcosmos.org/docs

const title = 'React Cosmos';

const enablers: EnablerPatterns = ['react-cosmos'];

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

const config = ['cosmos.config.json'];

const ext = '{js,jsx,ts,tsx,md,mdx}';

const fixtureEntry = [`**/*.fixture.${ext}`, `__fixtures__/**/*.${ext}`, `**/fixture.${ext}`];

const decoratorEntry = ['**/cosmos.decorator.{jsx,tsx}'];

const entry = [...fixtureEntry, ...decoratorEntry];

const resolveEntryPaths: ResolveEntryPaths<ReactCosmosConfig> = async localConfig => {
const { fixturesDir, fixtureFileSuffix } = localConfig;
const entries = [
join(fixturesDir ?? '__fixtures__', `**/*.${ext}`),
join(fixturesDir ?? '', `**/*.${fixtureFileSuffix ?? 'fixture'}.${ext}`),
join(fixturesDir ?? '', `**/${fixtureFileSuffix ?? 'fixture'}.${ext}`),
];
return [...entries, ...decoratorEntry].map(toEntryPattern);
};

const resolveConfig: ResolveConfig<ReactCosmosConfig> = async (localConfig, options) => {
const dependencies = (localConfig?.plugins ?? []).map(specifier => resolveEntry(options, specifier));
return [...dependencies];
};

export default {
title,
enablers,
isEnabled,
config,
entry,
resolveConfig,
resolveEntryPaths,
} satisfies Plugin;
5 changes: 5 additions & 0 deletions packages/knip/src/plugins/react-cosmos/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type ReactCosmosConfig = {
plugins?: string[];
fixtureFileSuffix?: string;
fixturesDir?: string;
};
21 changes: 21 additions & 0 deletions packages/knip/test/plugins/react-cosmos.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/react-cosmos');

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

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

0 comments on commit 2de3924

Please sign in to comment.