Skip to content

Commit

Permalink
Support plugin config at root level (resolves #260)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Sep 24, 2023
1 parent 14f3e98 commit 0918da3
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 18 deletions.
1 change: 1 addition & 0 deletions fixtures/plugin-config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1;
1 change: 1 addition & 0 deletions fixtures/plugin-config/index.vitest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1;
3 changes: 3 additions & 0 deletions fixtures/plugin-config/knip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"vitest": { "entry": ["**/*.vitest.ts"] }
}
1 change: 1 addition & 0 deletions fixtures/plugin-config/node_modules/vitest/index.js

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

4 changes: 4 additions & 0 deletions fixtures/plugin-config/node_modules/vitest/package.json

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

12 changes: 12 additions & 0 deletions fixtures/plugin-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@fixtures/plugin-config",
"scripts": {
"test": "vitest"
},
"devDependencies": {
"vitest": "*"
},
"workspaces": [
"packages/*"
]
}
6 changes: 6 additions & 0 deletions fixtures/workspaces-plugin-config/knip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"vitest": { "entry": ["**/*.vitest.ts"] },
"workspaces": {
"packages/*": {}
}
}

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.

6 changes: 6 additions & 0 deletions fixtures/workspaces-plugin-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@workspaces-plugin-config/root",
"workspaces": [
"packages/*"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest';

test('it', () => {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@workspaces-plugin-config/backend",
"scripts": {
"test": "vitest"
},
"devDependencies": {
"vitest": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@workspaces-plugin-config/frontend",
"scripts": {
"test": "vitest"
},
"devDependencies": {
"vitest": "*"
}
}
39 changes: 24 additions & 15 deletions src/ConfigurationChief.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ import { _dirGlob } from './util/glob.js';
import { _load } from './util/loader.js';
import { getKeysByValue } from './util/object.js';
import { join, relative, toPosix } from './util/path.js';
import { toCamelCase } from './util/plugin.js';
import { normalizePluginConfig, toCamelCase } from './util/plugin.js';
import { byPathDepth } from './util/workspace.js';
import type { SyncCompilers, AsyncCompilers } from './types/compilers.js';
import type { RawConfiguration, Configuration, PluginName, WorkspaceConfiguration } from './types/config.js';
import type {
RawConfiguration,
RawPluginConfiguration,
Configuration,
PluginName,
PluginsConfiguration,
WorkspaceConfiguration,
} from './types/config.js';
import type { PackageJson } from '@npmcli/package-json';

const {
Expand Down Expand Up @@ -176,6 +183,15 @@ export class ConfigurationChief {

const defaultWorkspaceConfig = getDefaultWorkspaceConfig(extensions);

const rootPluginConfigs: Partial<PluginsConfiguration> = {};

for (const [name, pluginConfig] of Object.entries(rawLocalConfig)) {
const pluginName = toCamelCase(name) as PluginName;
if (PLUGIN_NAMES.includes(pluginName)) {
rootPluginConfigs[pluginName] = normalizePluginConfig(pluginConfig as RawPluginConfiguration);
}
}

const workspaces = Object.entries(initialWorkspaces)
.filter(([workspaceName]) => !ignoreWorkspaces.includes(workspaceName))
.reduce(
Expand All @@ -195,22 +211,15 @@ export class ConfigurationChief {
ignoreDependencies: arrayify(workspaceConfig.ignoreDependencies),
};

for (const [name, pluginConfig] of Object.entries(rootPluginConfigs)) {
const pluginName = toCamelCase(name) as PluginName;
if (pluginConfig) workspaces[workspaceName][pluginName] = pluginConfig;
}

for (const [name, pluginConfig] of Object.entries(workspaceConfig)) {
const pluginName = toCamelCase(name) as PluginName;
if (PLUGIN_NAMES.includes(pluginName)) {
if (pluginConfig === false) {
workspaces[workspaceName][pluginName] = false;
} else {
const isObject = typeof pluginConfig !== 'string' && !Array.isArray(pluginConfig);
const config = isObject ? arrayify(pluginConfig.config) : pluginConfig ? arrayify(pluginConfig) : null;
const entry = isObject && 'entry' in pluginConfig ? arrayify(pluginConfig.entry) : null;
const project = isObject && 'project' in pluginConfig ? arrayify(pluginConfig.project) : entry;
workspaces[workspaceName][pluginName] = {
config,
entry,
project,
};
}
workspaces[workspaceName][pluginName] = normalizePluginConfig(pluginConfig as RawPluginConfiguration);
}
}
return workspaces;
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigurationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const reportConfigSchema = z.object({
exclude: z.array(z.string()).optional(),
});

const pluginSchema = z.union([
export const pluginSchema = z.union([
z.literal(false),
globSchema,
z.object({
Expand Down
6 changes: 4 additions & 2 deletions src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { z } from 'zod';
import { ConfigurationValidator } from '../ConfigurationValidator.js';
import { ConfigurationValidator, pluginSchema } from '../ConfigurationValidator.js';
import * as Plugins from '../plugins/index.js';
import type { Rules } from './issues.js';
import type { SyncCompilers, AsyncCompilers } from '../types/compilers.js';

export type RawConfiguration = z.infer<typeof ConfigurationValidator>;

export type RawPluginConfiguration = z.infer<typeof pluginSchema>;

type NormalizedGlob = string[];

export type PluginName = keyof typeof Plugins;
Expand All @@ -18,7 +20,7 @@ export type PluginConfiguration =
}
| false;

type PluginsConfiguration = Record<PluginName, PluginConfiguration>;
export type PluginsConfiguration = Record<PluginName, PluginConfiguration>;

interface BaseWorkspaceConfiguration {
entry: NormalizedGlob;
Expand Down
14 changes: 14 additions & 0 deletions src/util/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { _load as load } from './loader.js';
import { arrayify } from './array.js';
import type { RawPluginConfiguration } from 'src/types/config.js';

export const toCamelCase = (name: string) =>
name.toLowerCase().replace(/(-[a-z])/g, group => group.toUpperCase().replace('-', ''));
Expand All @@ -14,3 +16,15 @@ export const hasDependency = (dependencies: Set<string>, values: (string | RegEx
}
return false;
});

export const normalizePluginConfig = (pluginConfig: RawPluginConfiguration) => {
if (pluginConfig === false) {
return false;
} else {
const isObject = typeof pluginConfig !== 'string' && !Array.isArray(pluginConfig);
const config = isObject ? arrayify(pluginConfig.config) : pluginConfig ? arrayify(pluginConfig) : null;
const entry = isObject && 'entry' in pluginConfig ? arrayify(pluginConfig.entry) : null;
const project = isObject && 'project' in pluginConfig ? arrayify(pluginConfig.project) : entry;
return { config, entry, project };
}
};
21 changes: 21 additions & 0 deletions tests/plugin-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from 'node:assert/strict';
import test from 'node:test';
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/plugin-config');

test('Use root plugin config', async () => {
const { counters } = await main({
...baseArguments,
cwd,
});

assert.deepEqual(counters, {
...baseCounters,
total: 2,
processed: 2,
});
});
21 changes: 21 additions & 0 deletions tests/workspaces-plugin-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from 'node:assert/strict';
import test from 'node:test';
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/workspaces-plugin-config');

test('Use root plugin config in workspaces', async () => {
const { counters } = await main({
...baseArguments,
cwd,
});

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

0 comments on commit 0918da3

Please sign in to comment.