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

feat: allow multiple local plugins #1980

Closed
Show file tree
Hide file tree
Changes from all 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
34 changes: 33 additions & 1 deletion @commitlint/load/src/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test('plugins should be loaded from local', async () => {

expect(actual.plugins).toEqual(
expect.objectContaining({
local: {
'local-0': {
rules: {
test: expect.any(Function)
}
Expand All @@ -85,6 +85,38 @@ test('plugins should be loaded from local', async () => {
);
});

test('multiple local plugins should be supported', async () => {
const actual = await load({
plugins: [
{
rules: {
test1: () => [true, 'asd']
}
},
{
rules: {
test2: () => [true, 'fgh']
}
}
]
});

expect(actual.plugins).toEqual(
expect.objectContaining({
'local-0': {
rules: {
test1: expect.any(Function)
}
},
'local-1': {
rules: {
test2: expect.any(Function)
}
}
})
);
});

test('plugins should be loaded from config', async () => {
const cwd = await gitBootstrap('fixtures/extends-plugins');
const actual = await load({}, {cwd});
Expand Down
10 changes: 9 additions & 1 deletion @commitlint/load/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import {pickConfig} from './utils/pick-config';
const w = <T>(_: unknown, b: ArrayLike<T> | null | undefined | false) =>
Array.isArray(b) ? b : undefined;

const generateLocalId = () => {
let id = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this redefined every time you run the method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes but it is only run once. The function that is run multiple times is this one:

return () => `local-${id++}`;

It is a closure :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was considering to just declare id on row 95 and do the logic for generating an id in row 101. Do you think the code is clearer that way?


return () => `local-${id++}`;
};

export default async function load(
seed: UserConfig = {},
options: LoadOptions = {}
Expand Down Expand Up @@ -86,11 +92,13 @@ export default async function load(

// resolve plugins
if (Array.isArray(config.plugins)) {
const localIdGenerator = generateLocalId();

config.plugins.forEach(plugin => {
if (typeof plugin === 'string') {
loadPlugin(preset.plugins, plugin, process.env.DEBUG === 'true');
} else {
preset.plugins.local = plugin;
preset.plugins[localIdGenerator()] = plugin;
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion docs/reference-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Add these keywords into your `package.json` file to make it easy for others to f

## Local Plugins

In case you want to develop your plugins locally without the need to publish to `npm`, you can send declare your plugins inside your project locally. Please be aware that you can declare **only one** local plugin.
In case you want to develop your plugins locally without the need to publish to `npm`, you can send declare your plugins inside your project locally. This is also a great way to include specific rules in a [shareable configuration](https://commitlint.js.org/#/reference-configuration?id=shareable-configuration).

### Usage Example

Expand Down