Skip to content

Commit

Permalink
Fixes to config files + Cosmiconfig, Lilconfig and Unconfig generatio…
Browse files Browse the repository at this point in the history
…n. (#700)
  • Loading branch information
joshmeads authored and webpro committed Jun 29, 2024
1 parent 62090b4 commit 8c31e96
Show file tree
Hide file tree
Showing 23 changed files with 124 additions and 59 deletions.
6 changes: 1 addition & 5 deletions packages/knip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@
"create-plugin": "bun ./scripts/create-new-plugin.ts",
"postcreate-plugin": "biome format --write schema.json schema-jsonc.json src/ConfigurationValidator.ts"
},
"files": [
"dist",
"schema.json",
"schema-jsonc.json"
],
"files": ["dist", "schema.json", "schema-jsonc.json"],
"dependencies": {
"@ericcornelissen/bash-parser": "0.5.3",
"@nodelib/fs.walk": "2.0.0",
Expand Down
10 changes: 3 additions & 7 deletions packages/knip/src/plugins/commitlint/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import { hasDependency, toCosmiconfig } from '#p/util/plugin.js';
import type { CommitLintConfig } from './types.js';

// https://commitlint.js.org
// https://github.com/conventional-changelog/commitlint#config
// https://github.com/conventional-changelog/commitlint/blob/master/%40commitlint/load/src/utils/load-config.ts

const title = 'commitlint';

const enablers = ['@commitlint/cli'];

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

const config = [
'.commitlintrc',
'.commitlintrc.{json,yaml,yml,js,cjs,ts,cts}',
'commitlint.config.{js,cjs,ts,cts}',
'package.json',
];
const config = ['package.json', 'package.yaml', ...toCosmiconfig('commitlint', { additionalExtensions: ['cts'] })];

const resolveConfig: ResolveConfig<CommitLintConfig> = async config => {
const extendsConfigs = config.extends
Expand Down
9 changes: 6 additions & 3 deletions packages/knip/src/plugins/graphql-codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ import { get } from '#p/util/object.js';
import { isInternal } from '#p/util/path.js';
import { hasDependency } from '#p/util/plugin.js';
import { toEntryPattern } from '#p/util/protocols.js';
import { isConfigurationOutput, isGraphqlConfigTypes, isGraphqlProjectsConfigTypes } from './types.js';
import type {
ConfiguredPlugin,
GraphqlCodegenTypes,
GraphqlConfigTypes,
GraphqlProjectsConfigTypes,
PresetNames,
} from './types.js';
import { isConfigurationOutput, isGraphqlConfigTypes, isGraphqlProjectsConfigTypes } from './types.js';

// Both use Cosmiconfig with custom searchPlaces - not using helper as a result
// Codegen:
// https://the-guild.dev/graphql/codegen/docs/config-reference/codegen-config
// https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/src/config.ts
// Config:
// https://the-guild.dev/graphql/config/docs/user/usage#config-search-places

const title = 'GraphQL Codegen';

Expand All @@ -26,11 +30,10 @@ const packageJsonPath: Plugin['packageJsonPath'] = manifest => get(manifest, 'co
const config = [
'package.json',
// graphql-codegen config files
'codegen.{json,yml,yaml,js,ts,mjs,cts}',
'codegen.{json,yml,yaml,js,ts}',
'.codegenrc.{json,yml,yaml,js,ts}',
'codegen.config.js',
// graphql-config config files
// https://the-guild.dev/graphql/config/docs/user/usage#config-search-places
'.graphqlrc',
'.graphqlrc.{json,yml,yaml,toml,js,ts}',
'graphql.config.{json,yml,yaml,toml,js,cjs,ts}',
Expand Down
11 changes: 5 additions & 6 deletions packages/knip/src/plugins/lint-staged/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js';
import { getDependenciesFromScripts, hasDependency } from '#p/util/plugin.js';
import { getDependenciesFromScripts, hasDependency, toLilconfig } from '#p/util/plugin.js';
import type { LintStagedConfig } from './types.js';

// https://github.com/okonet/lint-staged
Expand All @@ -13,12 +13,11 @@ const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependenc
const packageJsonPath = 'lint-staged';

const config = [
'.lintstagedrc',
'.lintstagedrc.json',
'.lintstagedrc.{yml,yaml}',
'.lintstagedrc.{js,mjs,cjs}',
'lint-staged.config.{js,mjs,cjs}',
'package.json',
'package.yaml',
'package.yml',
...toLilconfig('lint-staged'),
...toLilconfig('lintstaged'),
];

const resolveConfig: ResolveConfig<LintStagedConfig> = async (config, options) => {
Expand Down
11 changes: 2 additions & 9 deletions packages/knip/src/plugins/linthtml/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js';
import { isInternal } from '#p/util/path.js';
import { hasDependency } from '#p/util/plugin.js';
import { hasDependency, toCosmiconfig } from '#p/util/plugin.js';
import { toEntryPattern } from '#p/util/protocols.js';
import type { PluginConfig } from './types.js';

Expand All @@ -14,14 +14,7 @@ const enablers = ['@linthtml/linthtml'];

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

const config = [
'.linthtmlrc',
'.linthtmlrc.json',
'.linthtmlrc.yml',
'.linthtmlrc.{js,cjs}',
'linthtml.config.js',
'package.json',
];
const config = ['package.json', ...toCosmiconfig('linthtml')];

const resolveConfig: ResolveConfig<PluginConfig> = config => {
const extensions = [config.extends ?? []]
Expand Down
4 changes: 2 additions & 2 deletions packages/knip/src/plugins/lockfile-lint/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EnablerPatterns } from '#p/types/config.js';
import type { IsPluginEnabled, Plugin } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import { hasDependency, toCosmiconfig } from '#p/util/plugin.js';

// https://github.com/lirantal/lockfile-lint/blob/main/packages/lockfile-lint/README.md

Expand All @@ -10,7 +10,7 @@ const enablers: EnablerPatterns = ['lockfile-lint'];

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

const config = ['.lockfile-lintrc', '.lockfile-lint.{js,toml}', 'lockfile-lint.config.js', 'package.json'];
const config = ['package.json', ...toCosmiconfig('lockfile-lint', { additionalExtensions: ['toml'] })];

export default {
title,
Expand Down
4 changes: 2 additions & 2 deletions packages/knip/src/plugins/npm-package-json-lint/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import { hasDependency, toCosmiconfig } from '#p/util/plugin.js';
import type { NpmPkgJsonLintConfig } from './types.js';

// https://npmpackagejsonlint.org/docs/
Expand All @@ -12,7 +12,7 @@ const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependenc

const packageJsonPath = 'npmpackagejsonlint';

const config = ['.npmpackagejsonlintrc.json', 'npmpackagejsonlint.config.js', 'package.json'];
const config = ['package.json', ...toCosmiconfig('npmpackagejsonlint')];

const resolveConfig: ResolveConfig<NpmPkgJsonLintConfig> = localConfig => {
return localConfig?.extends ? [localConfig.extends] : [];
Expand Down
11 changes: 9 additions & 2 deletions packages/knip/src/plugins/postcss/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import { hasDependency, toLilconfig } from '#p/util/plugin.js';
import type { PostCSSConfig } from './types.js';

// https://github.com/postcss/postcss-load-config/blob/main/src/index.js#L110
// Additionally postcss.config.json is loaded by nextjs

const title = 'PostCSS';

const enablers = ['postcss', 'postcss-cli', 'next'];

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

const config = ['postcss.config.{cjs,js}', 'postcss.config.json', 'package.json'];
const config = [
'package.json',
'postcss.config.json',
...toLilconfig('postcss', { configDir: false, additionalExtensions: ['ts', 'mts', 'cts', 'yaml', 'yml'] }),
];

const resolveConfig: ResolveConfig<PostCSSConfig> = config => {
return config.plugins
Expand Down
5 changes: 3 additions & 2 deletions packages/knip/src/plugins/prettier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { hasDependency } from '#p/util/plugin.js';
import type { PrettierConfig } from './types.js';

// https://prettier.io/docs/en/configuration.html
// https://github.com/prettier/prettier/blob/main/src/config/prettier-config/config-searcher.js

const title = 'Prettier';

Expand All @@ -13,9 +14,9 @@ const isEnabled: IsPluginEnabled = ({ dependencies, config }) =>

const config = [
'.prettierrc',
'.prettierrc.{json,js,cjs,mjs,yml,yaml}',
'.prettierrc.{json,js,cjs,mjs,yml,yaml,toml,json5}',
'prettier.config.{js,cjs,mjs}',
'package.json',
'package.{json,yaml}',
];

const resolveConfig: ResolveConfig<PrettierConfig> = config => {
Expand Down
4 changes: 3 additions & 1 deletion packages/knip/src/plugins/release-it/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { getDependenciesFromScripts, hasDependency } from '#p/util/plugin.js';
import type { ReleaseItConfig } from './types.js';

// https://github.com/release-it/release-it/blob/master/docs/plugins.md#using-a-plugin
// Uses CosmiConfig but with custom searchPlaces
// https://github.com/release-it/release-it/blob/main/lib/config.js

const title = 'Release It';

Expand All @@ -12,7 +14,7 @@ const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependenc

const packageJsonPath = 'release-it';

const config = ['.release-it.json', '.release-it.{js,cjs}', '.release-it.{yml,yaml}', 'package.json'];
const config = ['.release-it.{json,js,cjs,ts,yml,yaml,toml}', 'package.json'];

const resolveConfig: ResolveConfig<ReleaseItConfig> = (config, options) => {
const plugins = config.plugins ? Object.keys(config.plugins) : [];
Expand Down
4 changes: 2 additions & 2 deletions packages/knip/src/plugins/semantic-release/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import { hasDependency, toCosmiconfig } from '#p/util/plugin.js';
import type { SemanticReleaseConfig } from './types.js';

// https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#configuration-file
Expand All @@ -12,7 +12,7 @@ const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependenc

const packageJsonPath = 'release';

const config = ['.releaserc', '.releaserc.{yaml,yml,json,js,cjs}', 'release.config.{js,cjs}', 'package.json'];
const config = ['package.json', ...toCosmiconfig('release')];

const resolveConfig: ResolveConfig<SemanticReleaseConfig> = config => {
const plugins = (config?.plugins ?? []).map(plugin => (Array.isArray(plugin) ? plugin[0] : plugin));
Expand Down
9 changes: 7 additions & 2 deletions packages/knip/src/plugins/size-limit/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import type { EnablerPatterns } from '#p/types/config.js';
import type { IsPluginEnabled, Plugin } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import { hasDependency, toLilconfig } from '#p/util/plugin.js';

// https://github.com/ai/size-limit
// Uses lilconfig but with custom searchPlaces
// https://github.com/ai/size-limit/blob/main/packages/size-limit/get-config.js

const title = 'size-limit';

const enablers: EnablerPatterns = ['size-limit'];

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

const config: string[] = ['.size-limit.{json,js,cjs,ts}', 'package.json'];
const config = [
'package.json',
...toLilconfig('size-limit', { configDir: false, additionalExtensions: ['ts', 'mts', 'cts'], rcSuffix: '' }),
];

export default {
title,
Expand Down
4 changes: 2 additions & 2 deletions packages/knip/src/plugins/stylelint/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '#p/types/plugins.js';
import { isInternal } from '#p/util/path.js';
import { hasDependency } from '#p/util/plugin.js';
import { hasDependency, toCosmiconfig } from '#p/util/plugin.js';
import type { BaseStyleLintConfig, StyleLintConfig } from './types.js';

// https://stylelint.io/user-guide/configure/
Expand All @@ -11,7 +11,7 @@ const enablers = ['stylelint'];

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

const config = ['.stylelintrc', '.stylelintrc.{cjs,js,json,yaml,yml}', 'stylelint.config.{cjs,mjs,js}'];
const config = ['package.json', ...toCosmiconfig('stylelint')];

const resolve = (config: StyleLintConfig | BaseStyleLintConfig): string[] => {
const extend = config.extends ? [config.extends].flat().filter(id => !isInternal(id)) : [];
Expand Down
7 changes: 5 additions & 2 deletions packages/knip/src/plugins/syncpack/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { EnablerPatterns } from '#p/types/config.js';
import type { IsPluginEnabled, Plugin } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import { get } from '#p/util/object.js';
import { hasDependency, toCosmiconfig } from '#p/util/plugin.js';

// link to syncpack docs https://jamiemason.github.io/syncpack/config/syncpackrc/

Expand All @@ -10,11 +11,13 @@ const enablers: EnablerPatterns = ['syncpack'];

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

const config = ['.syncpackrc', '.syncpackrc.{json,yaml,yml,js,cjs}', '.syncpack.config.{js,cjs}'];
const config = ['package.json', ...toCosmiconfig('syncpack')];
const packageJsonPath: Plugin['packageJsonPath'] = manifest => get(manifest, 'syncpack');

export default {
title,
enablers,
isEnabled,
config,
packageJsonPath,
} satisfies Plugin;
1 change: 1 addition & 0 deletions packages/knip/src/plugins/tailwind/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { IsPluginEnabled, Plugin } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';

// https://tailwindcss.com/docs/configuration
// Tailwinds lilconfig dependency is only used for postcss

const title = 'Tailwind';

Expand Down
3 changes: 2 additions & 1 deletion packages/knip/src/plugins/tsup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { toProductionEntryPattern } from '#p/util/protocols.js';
import type { TsupConfig } from './types.js';

// https://paka.dev/npm/tsup/api
// https://github.com/egoist/tsup/blob/dev/src/load.ts

const title = 'tsup';

const enablers = ['tsup'];

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

const config = ['tsup.config.{js,ts,cjs,json}', 'package.json'];
const config = ['tsup.config.{js,ts,cjs,mjs,json}', 'package.json'];

const resolveConfig: ResolveConfig<TsupConfig> = async config => {
if (typeof config === 'function') config = await config({});
Expand Down
9 changes: 5 additions & 4 deletions packages/knip/src/plugins/typedoc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { hasDependency } from '#p/util/plugin.js';
import type { TypeDocConfig } from './types.js';

// https://typedoc.org/guides/overview/
// https://github.com/TypeStrong/typedoc/blob/9f0fb048399c7a1273dc452d01cca92b34f4675b/src/lib/utils/options/readers/typedoc.ts#L168

const title = 'TypeDoc';

Expand All @@ -13,10 +14,10 @@ const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependenc
const packageJsonPath = 'typedocOptions';

const config = [
'typedoc.{js,cjs,json,jsonc}',
'typedoc.config.{js,cjs}',
'.config/typedoc.{js,cjs,json,jsonc}',
'.config/typedoc.config.{js,cjs}',
'typedoc.{js,cjs,mjs,json,jsonc}',
'typedoc.config.{js,cjs,mjs}',
'.config/typedoc.{js,cjs,mjs,json,jsonc}',
'.config/typedoc.config.{js,cjs,mjs}',
'package.json',
'tsconfig.json',
];
Expand Down
5 changes: 3 additions & 2 deletions packages/knip/src/plugins/unocss/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { EnablerPatterns } from '#p/types/config.js';
import type { IsPluginEnabled, Plugin } from '#p/types/plugins.js';
import { hasDependency } from '#p/util/plugin.js';
import { hasDependency, toUnconfig } from '#p/util/plugin.js';

// https://unocss.dev/guide/config-file
// https://github.com/unocss/unocss/blob/main/packages/config/src/index.ts

const title = 'UnoCSS';

const enablers: EnablerPatterns = ['unocss'];

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

const config: string[] = ['uno.config.{js,ts,mjs,mts}', 'unocss.config.{js,ts,mjs,mts}'];
const config = [...toUnconfig('uno.config'), ...toUnconfig('unocss.config')];

export default {
title,
Expand Down
6 changes: 4 additions & 2 deletions packages/knip/src/plugins/xo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getDependenciesDeep } from '../eslint/helpers.js';
import type { XOConfig } from './types.js';

// link to xo docs: https://github.com/xojs/xo#config
// Uses custom cosmiconfig search paths
// https://github.com/xojs/xo/blob/ee9f0a3d72d55df098fc321c4d54a1ea3804e226/lib/constants.js

const title = 'xo';

Expand All @@ -14,9 +16,9 @@ const isEnabled: IsPluginEnabled = ({ dependencies, config }) =>
hasDependency(dependencies, enablers) || 'xo' in config;

const packageJsonPath = 'xo';
const config: string[] = ['{.,}xo-config.{js,cjs,json,}', 'package.json'];
const config = ['package.json', '.xo-config', '.xo-config.{js,cjs,json}', 'xo.config.{js,cjs}'];

const entry: string[] = ['{.,}xo-config.{js,cjs}'];
const entry: string[] = ['.xo-config.{js,cjs}', 'xo.config.{js,cjs}'];

const resolveConfig: ResolveConfig<XOConfig> = async (config, options) => {
const dependencies = await getDependenciesDeep(config, options);
Expand Down
Loading

0 comments on commit 8c31e96

Please sign in to comment.