Skip to content

Latest commit

History

History
466 lines (378 loc) Β· 9.52 KB

File metadata and controls

466 lines (378 loc) Β· 9.52 KB

API

@commitlint/format

Format commitlint reports

Install

npm install --save @commitlint/format

Usage

  • Signature
typeProblem={/* * Level of the problem hint | warning | error */level: 0|1|2;/* * Name of the problem to annotate the message with */name: string;/* * Message to print */message: string;}typeReport={results: ReportResult[];}typeReportResult={errors: Problem[];warnings: Problem[];}typeformatOptions={/** * Color the output **/color: boolean=true;/** * Signs to use as decoration for messages with severy 0, 1, 2 **/signs: readonly[string;string;string]=[' ','⚠','βœ–'];/** * Colors to use for messages with severy 0, 1, 2 **/colors: readonly[string;string;string]=['white','yellow','red'];/** * Print summary and inputs for reports without problems **/verbose: boolean=false;/** * URL to print as help for reports with problems **/helpUrl: string;}format(report?: Report={},options?: formatOptions={})=>string[];
  • Example
constformat=require('@commitlint/format').default;format();// => [ '\u001b[1m\u001b[32mβœ”\u001b[39m found 0 problems, 0 warnings\u001b[22m' ]format({results: [{warnings: [{level: 0,name: 'some-hint',message: 'This will not show up as it has level 0',},{level: 1,name: 'some-warning',message: 'This will show up yellow as it has level 1',},],errors: [{level: 2,name: 'some-error',message: 'This will show up red as it has level 2',},],},],},{color: false,});/* => [ 'βœ– This will show up red as it has level 2 [some-error]', ' This will not show up as it has level 0 [some-hint]', '⚠ This will show up yellow as it has level 1 [some-warning]', 'βœ– found 1 problems, 2 warnings'] */

@commitlint/load

Load shared commitlint configuration

Install

npm install --save @commitlint/load

Usage

  • Signature
/** * How to handle violation of rule * 0 - ignore * 1 - warn * 2 - throw */typeRuleLevel=0|1|2;/* * Application of rule * always - positive * never - negative */typeRuleCondition='always'|'never';/* * Additional, optional options to pass to rule */typeRuleOption=any;/** * Basic complete rule definition */typePrimitiveRule=[RuleLevel,RuleCondition,RuleOption?];/* * Async rules are resolved during config lookup. * They can be used to set up linting rules based on e.g. the project fs */typeAsyncRule=Promise<PrimitiveRule>;/* * Function rules are executed during config lookup. * They can be used to set up linting rules based on e.g. the project fs */typeFunctionRule=()=>PrimitiveRule;/* * Async function rules are executed and awaited during config lookup. * They can be used to set up linting rules based on e.g. the project fs */typeAsyncFunctionRule()=>Promise<PrimitiveRule>;/* * Polymorphic rule struct */typeRule=PrimitiveRule|FunctionRule|AsyncFunctionRule;/* * Parser preset for conventional commits */typeParserPreset={name: string;path: string;opts: any;};typeSeed={/* * ids resolveable from cwd or configuration file. * Imported and merged into configuration * with increasing precedence, with top level config taking the highest. */extends?: string[];/* * id resolveable from cwd or configuration file. * Imported and expanded to {ParserPreset}. * Top level parserPresets override presets in extended configuration. */parserPreset?: string;/** * Initial map of rules to check against */rules?: {[ruleName: string]: Rule};/** * URL to print as help for reports with problems */helpUrl?: string;};typeConfig={/* * Relatives path to all extendend configurations. */extends: string[];/* * Expanded parser preset, if any */parserPreset?: ParserPreset;/* * Merged map of rules to check against */rules: {[ruleName: string]: Rule};/** * URL to print as help for reports with problems */helpUrl?: string;};typeLoadOptions={/* * Path to the config file to load. */file?: string;/* * The cwd to use when loading config from file parameter. */cwd: string;};load(seed: Seed={},options?: LoadOptions={cwd: process.cwd()})=>Promise<Config>;
  • Example
constload=require('@commitlint/load').default;load({rules: {'body-leading-blank': [2,'always'],},}).then((config)=>console.log(config));// => { extends: [], rules: { 'body-leading-blank': [ 2, 'always' ] }}load({extends: ['./package']}).then((config)=>console.log(config));// => { extends: ['./package', './package-b'], rules: {}}load({parserPreset: './parser-preset.js'}).then((config)=>console.log(config));// => { extends: [], rules: {}, parserPreset: {name: './parser-preset.js', path: './parser-preset.js', opts: {}}}load({},{file: '.commitlintrc.yml',cwd: process.cwd()}).then((config)=>console.log(config));// => { extends: [], rules: { 'body-leading-blank': [ 1, 'always' ] }, formatter: '@commitlint/format', plugins: {}}

@commitlint/read

Read commit messages from a specified range or disk

Install

npm install --save @commitlint/read

Usage

  • Signature
typeRange={/* Lower end of the commit range to read */from: string;/* Upper end of the commit range to read */to: string;/* Wether (boolean) to read from ./.git/COMMIT_EDITMSG or where to read from (string) */edit?: boolean|string;};read(range: Range)=>Promise<string[]>
  • Example
// git commit -m "I did something"constread=require('@commitlint/read').default;read({edit: true}).then((messages)=>console.log(messages));// => ['I did something\n\n']read({edit: './git/GITGUI_EDITMESSAGE'}).then((messages)=>console.log(messages));// => ['I did something via git gui\n\n']read({from: 'HEAD~2'}).then((messages)=>console.log(messages));// => ['I did something\n\n', 'Initial commit\n\n']read({from: 'HEAD~2',to: 'HEAD~1'}).then((messages)=>console.log(messages));// => ['Initial commit\n\n']

lint

Lint a string against commitlint rules

Install

npm install --save @commitlint/lint

Usage

  • Signature
typeRuleLevel=0|1|2;typeRuleCondition='always'|'never';typeRuleOption=any;typePrimitiveRule=[RuleLevel,RuleCondition,RuleOption?];typeAsyncRule=Promise<PrimitiveRule>;typeFunctionRule=()=>PrimitiveRule;typeAsyncFunctionRule()=>Promise<PrimitiveRule>;typeRule=PrimitiveRule|FunctionRule|AsyncFunctionRule;typeProblem={level: number;valid: boolean;name: string;message: string;}typeReport={valid: boolean;errors: Problem[];warnings: Problem[];}typeOptions={parserOpts?: any;};lint(message: string,rules: {[ruleName: string]: Rule},opts?: Options)=>Promise<Report>;
  • Basic Example
constlint=require('@commitlint/lint').default;lint('foo: bar').then((report)=>console.log(report));// => { valid: true, errors: [], warnings: [] }lint('foo: bar',{'type-enum': [1,'always',['foo']]}).then((report)=>console.log(report));// => { valid: true, errors: [], warnings: [] }lint('foo: bar',{'type-enum': [1,'always',['bar']]}).then((report)=>console.log(report));/* => { valid: true, errors: [], warnings: [ { level: 1, valid: false, name: 'type-enum', message: 'type must be one of [bar]' } ] } */constopts={parserOpts: {headerPattern: /^(\w*)-(\w*)/,headerCorrespondence: ['type','scope'],},};lint('foo-bar',{'type-enum': [2,'always',['foo']]},opts).then((report)=>console.log(report));// => { valid: true, errors: [], warnings: [] }
  • Load configuration
constload=require('@commitlint/load').default;constlint=require('@commitlint/lint').default;constCONFIG={extends: ['@commitlint/config-conventional'],};load(CONFIG).then((opts)=>lint('foo: bar',opts.rules,opts.parserPreset ? {parserOpts: opts.parserPreset.parserOpts} : {})).then((report)=>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: [] } */
  • Read git history
constlint=require('@commitlint/lint').default;constread=require('@commitlint/read').default;constRULES={'type-enum': [2,'always',['foo']],};constcheck=(commit)=>lint(commit,RULES);read({to: 'HEAD',from: 'HEAD~2'}).then((commits)=>Promise.all(commits.map(check)));
  • Simplfied last-commit checker
constload=require('@commitlint/load').default;constread=require('@commitlint/read').default;constlint=require('@commitlint/lint').default;Promise.all([load(),read({from: 'HEAD~1'})]).then((tasks)=>{const[{rules, parserPreset},[commit]]=tasks;returnlint(commit,rules,parserPreset ? {parserOpts: parserPreset.parserOpts} : {});}).then((report)=>console.log(JSON.stringify(result.valid)));