Skip to content

Commit

Permalink
feat: add local plugins support (#1692)
Browse files Browse the repository at this point in the history
* feat: add local plugins support

* docs: adding plugin reference

* test: local plugin"
"
  • Loading branch information
EmaSuriano authored Jun 18, 2020
1 parent 9e953c6 commit 7b29c48
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
22 changes: 22 additions & 0 deletions @commitlint/load/src/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
8 changes: 6 additions & 2 deletions @commitlint/load/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion @commitlint/types/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface UserConfig {
parserPreset?: string | ParserPreset;
ignores?: ((commit: string) => boolean)[];
defaultIgnores?: boolean;
plugins?: string[];
plugins?: (string | Plugin)[];
}

export interface UserPreset {
Expand Down
35 changes: 35 additions & 0 deletions docs/reference-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 7b29c48

Please sign in to comment.