From 7b29c48321b513e091849fbb2cc2bf0e6ebb94a6 Mon Sep 17 00:00:00 2001 From: Ema Suriano Date: Thu, 18 Jun 2020 11:23:50 +0200 Subject: [PATCH] feat: add local plugins support (#1692) * feat: add local plugins support * docs: adding plugin reference * test: local plugin" " --- @commitlint/load/src/load.test.ts | 22 +++++++++++++++++++ @commitlint/load/src/load.ts | 8 +++++-- @commitlint/types/src/load.ts | 2 +- docs/reference-plugins.md | 35 +++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/@commitlint/load/src/load.test.ts b/@commitlint/load/src/load.test.ts index bf633a8c82..80e71147d9 100644 --- a/@commitlint/load/src/load.test.ts +++ b/@commitlint/load/src/load.test.ts @@ -63,6 +63,28 @@ test('plugins should be loaded from seed', async () => { }); }); +test('plugins should be loaded from local', async () => { + const actual = await load({ + plugins: [ + { + rules: { + test: () => [true, 'asd'] + } + } + ] + }); + + expect(actual.plugins).toEqual( + expect.objectContaining({ + local: { + rules: { + test: 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 ae083b0692..63fec9b908 100644 --- a/@commitlint/load/src/load.ts +++ b/@commitlint/load/src/load.ts @@ -86,8 +86,12 @@ export default async function load( // resolve plugins if (Array.isArray(config.plugins)) { - config.plugins.forEach((pluginKey: string) => { - loadPlugin(preset.plugins, pluginKey, process.env.DEBUG === 'true'); + config.plugins.forEach(plugin => { + if (typeof plugin === 'string') { + loadPlugin(preset.plugins, plugin, process.env.DEBUG === 'true'); + } else { + preset.plugins.local = plugin; + } }); } diff --git a/@commitlint/types/src/load.ts b/@commitlint/types/src/load.ts index 6f446b1369..d6feebca38 100644 --- a/@commitlint/types/src/load.ts +++ b/@commitlint/types/src/load.ts @@ -96,7 +96,7 @@ export interface UserConfig { parserPreset?: string | ParserPreset; ignores?: ((commit: string) => boolean)[]; defaultIgnores?: boolean; - plugins?: string[]; + plugins?: (string | Plugin)[]; } export interface UserPreset { diff --git a/docs/reference-plugins.md b/docs/reference-plugins.md index a5b1bd0b0c..6782fb49d5 100644 --- a/docs/reference-plugins.md +++ b/docs/reference-plugins.md @@ -43,6 +43,41 @@ Recommended keywords: Add these keywords into your `package.json` file to make it easy for others to find. +## 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. + +### Usage Example + +```js +// commitlint.config.js +module.exports = { + rules: { + 'hello-world-rule': [2, 'always'] + }, + plugins: [ + { + rules: { + 'hello-world-rule': ({subject}) => { + const HELLO_WORLD = 'Hello World'; + return [ + subject.includes(HELLO_WORLD), + `Your subject should contain ${HELLO_WORLD} message` + ]; + } + } + } + ] +}; +``` + +### Usage Example + +```bash +> echo "feat: random subject" | commitlint # fails +> echo "feat: Hello World" | commitlint # passes +``` + ## Further Reading - [npm Developer Guide](https://docs.npmjs.com/misc/developers)