Lint a string against commitlint rules
npm install --save @commitlint/lint
type RuleLevel = 0 | 1 | 2;
type RuleCondition = 'always' | 'never';
type RuleOption = any;
type PrimitiveRule = [RuleLevel, RuleCondition, RuleOption?];
type AsyncRule = Promise<PrimitiveRule>;
type FunctionRule = () => PrimitiveRule;
type AsyncFunctionRule = () => Promise<PrimitiveRule>;
type Rule = PrimitiveRule | FunctionRule | AsyncFunctionRule;
type Problem = {
level: number;
valid: boolean;
name: string;
message: string;
}
type Report = {
valid: boolean;
errors: Problem[];
warnings: Problem[];
}
type Options = {
parserOpts?: any;
};
lint(message: string, rules: {[ruleName: string]: Rule}, opts?: Options) => Promise<Report>;
import lint from '@commitlint/lint';
const report = await lint('foo: bar');
console.log(report);
// => { valid: true, errors: [], warnings: [] }
const report = await lint('foo: bar', {'type-enum': [1, 'always', ['foo']]});
console.log(report);
// => { valid: true, errors: [], warnings: [] }
const report = await lint('foo: bar', {'type-enum': [1, 'always', ['bar']]});
console.log(report);
/* =>
{
valid: false,
errors: [],
warnings: [
{
level: 1,
valid: false,
name: 'type-enum',
message: 'type must be one of [bar]'
}
]
}
*/
const opts = {
parserOpts: {
headerPattern: /^(\w*)-(\w*)/,
headerCorrespondence: ['type', 'scope'],
},
};
const report = await lint(
'foo-bar',
{'type-enum': [2, 'always', ['foo']]},
opts
);
console.log(report);
// => { valid: true, errors: [], warnings: [] }
import load from '@commitlint/load';
import lint from '@commitlint/lint';
const CONFIG = {
extends: ['@commitlint/config-conventional'],
};
const opts = await load(CONFIG);
const report = await lint(
'foo: bar',
opts.rules,
opts.parserPreset ? {parserOpts: opts.parserPreset.parserOpts} : {}
);
console.log(report);
/* =>
{
valid: false,
errors: [
{
level: 2,
valid: false,
name: 'type-enum',
message: 'type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]'
}
],
warnings: []
}
*/
import lint from '@commitlint/lint';
import read from '@commitlint/read';
const RULES = {
'type-enum': [2, 'always', ['foo']],
};
const commits = await read({to: 'HEAD', from: 'HEAD~2'});
console.info(commits.map((commit) => lint(commit, RULES)));
import load from '@commitlint/load';
import read from '@commitlint/read';
import lint from '@commitlint/lint';
const {rules, parserPreset} = load();
const [commit] = await read({from: 'HEAD~1'});
const report = await lint(
commit,
rules,
parserPreset ? {parserOpts: parserPreset.parserOpts} : {}
);
console.log(JSON.stringify(result.valid));