Skip to content

Commit 7b29c48

Browse files
authoredJun 18, 2020
feat: add local plugins support (#1692)
* feat: add local plugins support * docs: adding plugin reference * test: local plugin" "
1 parent 9e953c6 commit 7b29c48

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed
 

‎@commitlint/load/src/load.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ test('plugins should be loaded from seed', async () => {
6363
});
6464
});
6565

66+
test('plugins should be loaded from local', async () => {
67+
const actual = await load({
68+
plugins: [
69+
{
70+
rules: {
71+
test: () => [true, 'asd']
72+
}
73+
}
74+
]
75+
});
76+
77+
expect(actual.plugins).toEqual(
78+
expect.objectContaining({
79+
local: {
80+
rules: {
81+
test: expect.any(Function)
82+
}
83+
}
84+
})
85+
);
86+
});
87+
6688
test('plugins should be loaded from config', async () => {
6789
const cwd = await gitBootstrap('fixtures/extends-plugins');
6890
const actual = await load({}, {cwd});

‎@commitlint/load/src/load.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ export default async function load(
8686

8787
// resolve plugins
8888
if (Array.isArray(config.plugins)) {
89-
config.plugins.forEach((pluginKey: string) => {
90-
loadPlugin(preset.plugins, pluginKey, process.env.DEBUG === 'true');
89+
config.plugins.forEach(plugin => {
90+
if (typeof plugin === 'string') {
91+
loadPlugin(preset.plugins, plugin, process.env.DEBUG === 'true');
92+
} else {
93+
preset.plugins.local = plugin;
94+
}
9195
});
9296
}
9397

‎@commitlint/types/src/load.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export interface UserConfig {
9696
parserPreset?: string | ParserPreset;
9797
ignores?: ((commit: string) => boolean)[];
9898
defaultIgnores?: boolean;
99-
plugins?: string[];
99+
plugins?: (string | Plugin)[];
100100
}
101101

102102
export interface UserPreset {

‎docs/reference-plugins.md

+35
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ Recommended keywords:
4343

4444
Add these keywords into your `package.json` file to make it easy for others to find.
4545

46+
## Local Plugins
47+
48+
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.
49+
50+
### Usage Example
51+
52+
```js
53+
// commitlint.config.js
54+
module.exports = {
55+
rules: {
56+
'hello-world-rule': [2, 'always']
57+
},
58+
plugins: [
59+
{
60+
rules: {
61+
'hello-world-rule': ({subject}) => {
62+
const HELLO_WORLD = 'Hello World';
63+
return [
64+
subject.includes(HELLO_WORLD),
65+
`Your subject should contain ${HELLO_WORLD} message`
66+
];
67+
}
68+
}
69+
}
70+
]
71+
};
72+
```
73+
74+
### Usage Example
75+
76+
```bash
77+
> echo "feat: random subject" | commitlint # fails
78+
> echo "feat: Hello World" | commitlint # passes
79+
```
80+
4681
## Further Reading
4782

4883
- [npm Developer Guide](https://docs.npmjs.com/misc/developers)

0 commit comments

Comments
 (0)
Please sign in to comment.