diff --git a/@commitlint/load/src/load.test.ts b/@commitlint/load/src/load.test.ts index 80e71147d9..2686d1aa13 100644 --- a/@commitlint/load/src/load.test.ts +++ b/@commitlint/load/src/load.test.ts @@ -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) } @@ -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}); diff --git a/@commitlint/load/src/load.ts b/@commitlint/load/src/load.ts index 63fec9b908..a02e32c7b3 100644 --- a/@commitlint/load/src/load.ts +++ b/@commitlint/load/src/load.ts @@ -24,6 +24,12 @@ import {pickConfig} from './utils/pick-config'; const w = (_: unknown, b: ArrayLike | null | undefined | false) => Array.isArray(b) ? b : undefined; +const generateLocalId = () => { + let id = 0; + + return () => `local-${id++}`; +}; + export default async function load( seed: UserConfig = {}, options: LoadOptions = {} @@ -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; } }); } diff --git a/docs/reference-plugins.md b/docs/reference-plugins.md index 6782fb49d5..e43f8a9dda 100644 --- a/docs/reference-plugins.md +++ b/docs/reference-plugins.md @@ -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