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: add local plugins support #1692

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)