From 8142287a56edc834080f499f24e129b96edc4cbc Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 03:09:24 +0100 Subject: [PATCH 01/23] refactor(cli): port cli to typescript --- @commitlint/cli/package.json | 26 +- .../cli/src/{cli.test.js => cli.test.ts} | 15 +- @commitlint/cli/src/{cli.js => cli.ts} | 224 +++++++++--------- @commitlint/cli/src/help.js | 34 --- @commitlint/cli/src/help.ts | 36 +++ @commitlint/cli/src/types.ts | 19 ++ @commitlint/cli/tsconfig.json | 10 + @commitlint/lint/src/lint.ts | 3 +- @commitlint/types/src/load.ts | 2 +- tsconfig.json | 3 +- yarn.lock | 49 ++-- 11 files changed, 224 insertions(+), 197 deletions(-) rename @commitlint/cli/src/{cli.test.js => cli.test.ts} (97%) rename @commitlint/cli/src/{cli.js => cli.ts} (64%) mode change 100755 => 100644 delete mode 100644 @commitlint/cli/src/help.js create mode 100644 @commitlint/cli/src/help.ts create mode 100644 @commitlint/cli/src/types.ts create mode 100644 @commitlint/cli/tsconfig.json diff --git a/@commitlint/cli/package.json b/@commitlint/cli/package.json index c133b6a3b7..033d2ec939 100644 --- a/@commitlint/cli/package.json +++ b/@commitlint/cli/package.json @@ -11,19 +11,8 @@ "commitlint": "./lib/cli.js" }, "scripts": { - "build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps", "deps": "dep-check", - "pkg": "pkg-check", - "start": "yarn run watch", - "watch": "babel src --out-dir lib --watch --source-maps" - }, - "babel": { - "presets": [ - "babel-preset-commitlint" - ], - "ignore": [ - "**/*.test.js" - ] + "pkg": "pkg-check" }, "engines": { "node": ">=8" @@ -47,12 +36,7 @@ }, "license": "MIT", "devDependencies": { - "@babel/cli": "^7.7.7", - "@babel/core": "^7.7.7", - "@commitlint/test": "8.2.0", - "@commitlint/utils": "^8.3.4", - "babel-preset-commitlint": "^8.2.0", - "cross-env": "7.0.0", + "@types/yargs": "^15.0.3", "execa": "^3.4.0", "fs-extra": "^8.1.0" }, @@ -62,12 +46,10 @@ "@commitlint/load": "^8.3.5", "@commitlint/read": "^8.3.4", "chalk": "3.0.0", - "core-js": "^3.6.1", "get-stdin": "7.0.0", "lodash": "^4.17.15", - "meow": "5.0.0", - "regenerator-runtime": "0.13.3", "resolve-from": "5.0.0", - "resolve-global": "1.0.0" + "resolve-global": "1.0.0", + "yargs": "^15.1.0" } } diff --git a/@commitlint/cli/src/cli.test.js b/@commitlint/cli/src/cli.test.ts similarity index 97% rename from @commitlint/cli/src/cli.test.js rename to @commitlint/cli/src/cli.test.ts index 53d6258a90..d9ed08312e 100644 --- a/@commitlint/cli/src/cli.test.js +++ b/@commitlint/cli/src/cli.test.ts @@ -6,7 +6,12 @@ import fs from 'fs-extra'; const bin = require.resolve('../lib/cli.js'); -const cli = (args, options) => { +interface TestOptions { + cwd: string; + env?: Record; +} + +function cli(args: string[], options: TestOptions) { return (input = '') => { return execa(bin, args, { cwd: options.cwd, @@ -15,10 +20,10 @@ const cli = (args, options) => { reject: false }); }; -}; +} -const gitBootstrap = fixture => git.bootstrap(fixture, __dirname); -const fixBootstrap = fixture => fix.bootstrap(fixture, __dirname); +const gitBootstrap = (fixture: string) => git.bootstrap(fixture, __dirname); +const fixBootstrap = (fixture: string) => fix.bootstrap(fixture, __dirname); test('should throw when called without [input]', async () => { const cwd = await gitBootstrap('fixtures/default'); @@ -423,7 +428,7 @@ test('should work with relative formatter path', async () => { expect(actual.exitCode).toBe(0); }); -async function writePkg(payload, options) { +async function writePkg(payload: unknown, options: TestOptions) { const pkgPath = path.join(options.cwd, 'package.json'); const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8')); const result = merge(pkg, payload); diff --git a/@commitlint/cli/src/cli.js b/@commitlint/cli/src/cli.ts old mode 100755 new mode 100644 similarity index 64% rename from @commitlint/cli/src/cli.js rename to @commitlint/cli/src/cli.ts index 6ad0710816..299839964d --- a/@commitlint/cli/src/cli.js +++ b/@commitlint/cli/src/cli.ts @@ -1,118 +1,111 @@ #!/usr/bin/env node -import 'core-js/stable'; -import 'regenerator-runtime/runtime'; - import load from '@commitlint/load'; import lint from '@commitlint/lint'; import read from '@commitlint/read'; -import meow from 'meow'; -import {merge, pick, isFunction} from 'lodash'; +import merge from 'lodash/merge'; +import isFunction from 'lodash/isFunction'; import stdin from 'get-stdin'; import resolveFrom from 'resolve-from'; import resolveGlobal from 'resolve-global'; +import yargs from 'yargs'; + +import {CliFlags} from './types'; +import { + LintOptions, + LintOutcome, + ParserOptions, + ParserPreset, + QualifiedConfig +} from '@commitlint/types'; -import help from './help'; const pkg = require('../package'); -const flags = { - color: { +const cli = yargs + .option('color', { alias: 'c', default: true, - description: 'toggle colored output', + describe: 'toggle colored output', type: 'boolean' - }, - config: { + }) + .option('config', { alias: 'g', - default: null, - description: 'path to the config file', + describe: 'path to the config file', type: 'string' - }, - cwd: { + }) + .option('cwd', { alias: 'd', default: process.cwd(), - description: 'directory to execute in', + describe: 'directory to execute in', type: 'string' - }, - edit: { + }) + .option('edit', { alias: 'e', - default: false, - description: + // default: false, + describe: 'read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG', type: 'string' - }, - env: { + }) + .option('env', { alias: 'E', - default: null, - description: + // default: null, + describe: 'check message in the file at path given by environment variable value', type: 'string' - }, - extends: { + }) + .option('extends', { alias: 'x', - description: 'array of shareable configurations to extend', - type: 'string' - }, - help: { - alias: 'h', - type: 'boolean', - description: 'display this help message' - }, - 'help-url': { + describe: 'array of shareable configurations to extend', + type: 'array' + }) + .option('help-url', { alias: 'H', type: 'string', - description: 'helpurl in error message' - }, - from: { + describe: 'helpurl in error message' + }) + .option('from', { alias: 'f', - default: null, - description: 'lower end of the commit range to lint; applies if edit=false', + // default: null, + describe: 'lower end of the commit range to lint; applies if edit=false', type: 'string' - }, - format: { + }) + .option('format', { alias: 'o', - default: null, - description: 'output format of the results', + // default: null, + describe: 'output format of the results', type: 'string' - }, - 'parser-preset': { + }) + .option('parser-preset', { alias: 'p', - description: 'configuration preset to use for conventional-commits-parser', + describe: 'configuration preset to use for conventional-commits-parser', type: 'string' - }, - quiet: { + }) + .option('quiet', { alias: 'q', default: false, - description: 'toggle console output', + describe: 'toggle console output', type: 'boolean' - }, - to: { + }) + .option('to', { alias: 't', - default: null, - description: 'upper end of the commit range to lint; applies if edit=false', + // default: null, + describe: 'upper end of the commit range to lint; applies if edit=false', type: 'string' - }, - version: { + }) + .option('version', { alias: 'v', type: 'boolean', - description: 'display version information' - }, - verbose: { + describe: 'display version information' + }) + .option('verbose', { alias: 'V', type: 'boolean', - description: 'enable verbose output for reports without problems' - } -}; - -const cli = meow({ - description: `${pkg.name}@${pkg.version} - ${pkg.description}`, - flags, - help: `[input] reads from stdin if --edit, --env, --from and --to are omitted\n${help( - flags - )}`, - unknown(arg) { - throw new Error(`unknown flags: ${arg}`); - } -}); + describe: 'enable verbose output for reports without problems' + }) + .help( + 'help', + `${pkg.name}@${pkg.version} - ${pkg.description}\n` + + `[input] reads from stdin if --edit, --env, --from and --to are omitted` + ).argv; main(cli).catch(err => setTimeout(() => { @@ -120,17 +113,22 @@ main(cli).catch(err => process.exit(1); } throw err; - }) + }, 0) ); -async function main(options) { - const raw = options.input; - const flags = normalizeFlags(options.flags); +async function main(options: CliFlags) { + const raw = options._; + const flags = normalizeFlags(options); const fromStdin = checkFromStdin(raw, flags); - const range = pick(flags, 'edit', 'from', 'to'); - - const input = await (fromStdin ? stdin() : read(range, {cwd: flags.cwd})); + const input = await (fromStdin + ? stdin() + : read({ + to: flags.to, + from: flags.from, + edit: flags.edit, + cwd: flags.cwd + })); const messages = (Array.isArray(input) ? input : [input]) .filter(message => typeof message === 'string') @@ -138,10 +136,11 @@ async function main(options) { .filter(Boolean); if (messages.length === 0 && !checkFromRepository(flags)) { + // TODO: create custom error?? const err = new Error( '[input] is required: supply via stdin, or --env or --edit or --from and --to' ); - err.type = pkg.name; + (err as any).type = pkg.name; // TODO console.log(`${cli.help}\n`); console.log(err.message); throw err; @@ -150,7 +149,7 @@ async function main(options) { const loadOpts = {cwd: flags.cwd, file: flags.config}; const loaded = await load(getSeed(flags), loadOpts); const parserOpts = selectParserOpts(loaded.parserPreset); - const opts = { + const opts: LintOptions & {parserOpts: ParserOptions} = { parserOpts: {}, plugins: {}, ignores: [], @@ -171,20 +170,19 @@ async function main(options) { const format = loadFormatter(loaded, flags); // Strip comments if reading from `.git/COMMIT_EDIT_MSG` - if (range.edit) { + if (flags.edit) { opts.parserOpts.commentChar = '#'; } const results = await Promise.all( - messages.map(message => lint(message, loaded.rules, opts)) + messages.map(message => lint(message, loaded.rules as any, opts)) ); if (Object.keys(loaded.rules).length === 0) { let input = ''; if (results.length !== 0) { - const originalInput = results[0].input; - input = originalInput; + input = results[0].input; } results.splice(0, results.length, { @@ -206,7 +204,12 @@ async function main(options) { }); } - const report = results.reduce( + const report = results.reduce<{ + valid: boolean; + errorCount: number; + warningCount: number; + results: LintOutcome[]; + }>( (info, result) => { info.valid = result.valid ? info.valid : false; info.errorCount += result.errors.length; @@ -226,8 +229,8 @@ async function main(options) { const output = format(report, { color: flags.color, verbose: flags.verbose, - helpUrl: flags.helpUrl - ? flags.helpUrl.trim() + helpUrl: flags['help-url'] + ? flags['help-url'].trim() : 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint' }); @@ -236,34 +239,35 @@ async function main(options) { } if (!report.valid) { + // TODO: create custom error?? const err = new Error(output); - err.type = pkg.name; + (err as any).type = pkg.name; throw err; } } -function checkFromStdin(input, flags) { +function checkFromStdin(input: string[], flags: CliFlags) { return input.length === 0 && !checkFromRepository(flags); } -function checkFromRepository(flags) { +function checkFromRepository(flags: CliFlags) { return checkFromHistory(flags) || checkFromEdit(flags); } -function checkFromEdit(flags) { +function checkFromEdit(flags: CliFlags) { return Boolean(flags.edit) || flags.env; } -function checkFromHistory(flags) { +function checkFromHistory(flags: CliFlags) { return typeof flags.from === 'string' || typeof flags.to === 'string'; } -function normalizeFlags(flags) { +function normalizeFlags(flags: CliFlags) { const edit = getEditValue(flags); return merge({}, flags, {edit, e: edit}); } -function getEditValue(flags) { +function getEditValue(flags: CliFlags) { if (flags.env) { if (!(flags.env in process.env)) { throw new Error( @@ -282,8 +286,8 @@ function getEditValue(flags) { if (edit === '') { return true; } - if (typeof edit === 'boolean') { - return edit; + if (typeof edit === 'undefined') { + return false; } // The recommended method to specify -e with husky was `commitlint -e $HUSKY_GIT_PARAMS` // This does not work properly with win32 systems, where env variable declarations @@ -311,15 +315,21 @@ function getEditValue(flags) { return edit; } -function getSeed(seed) { - const e = Array.isArray(seed.extends) ? seed.extends : [seed.extends]; - const n = e.filter(i => typeof i === 'string'); +function getSeed( + seed: CliFlags +): { + extends?: string[]; + parserPreset?: string; +} { + const n = (seed.extends || []).filter( + (i): i is string => typeof i === 'string' + ); return n.length > 0 - ? {extends: n, parserPreset: seed.parserPreset} - : {parserPreset: seed.parserPreset}; + ? {extends: n, parserPreset: seed['parser-preset']} + : {parserPreset: seed['parser-preset']}; } -function selectParserOpts(parserPreset) { +function selectParserOpts(parserPreset: ParserPreset) { if (typeof parserPreset !== 'object') { return undefined; } @@ -331,8 +341,10 @@ function selectParserOpts(parserPreset) { return parserPreset.parserOpts; } -function loadFormatter(config, flags) { - const moduleName = flags.format || config.formatter || '@commitlint/format'; +function loadFormatter(config: QualifiedConfig, flags: CliFlags) { + // TODO: validate why formatter is unknown???? + const moduleName: string = + flags.format || (config.formatter as any) || '@commitlint/format'; const modulePath = resolveFrom.silent(__dirname, moduleName) || resolveFrom.silent(flags.cwd, moduleName) || diff --git a/@commitlint/cli/src/help.js b/@commitlint/cli/src/help.js deleted file mode 100644 index 42c0d597c9..0000000000 --- a/@commitlint/cli/src/help.js +++ /dev/null @@ -1,34 +0,0 @@ -module.exports = flags => { - const lines = Object.entries(flags).map(entry => { - const name = entry[0]; - const value = entry[1]; - return [ - [name, value.alias].filter(Boolean), - value.description, - value.default - ].filter(Boolean); - }); - - const longest = lines - .map(line => { - const flags = line[0]; - return flags.reduce((sum, flag) => sum + flag.length, 0); - }) - .sort((a, b) => b - a)[0]; - - return lines - .map(line => { - const flags = line[0]; - const desc = line[1]; - const defaults = line[2]; - const fs = flags.map(flag => - flag.length > 1 ? `--${flag}` : ` -${flag}` - ); - const ds = defaults ? `, defaults to: ${defaults}` : ''; - const length = flags.reduce((sum, flag) => sum + flag.length, 0); - return `${fs.join(',')}${' '.repeat( - Math.max(4 + longest - length, 0) - )}${desc}${ds}`; - }) - .join('\n'); -}; diff --git a/@commitlint/cli/src/help.ts b/@commitlint/cli/src/help.ts new file mode 100644 index 0000000000..4f7e2d64e5 --- /dev/null +++ b/@commitlint/cli/src/help.ts @@ -0,0 +1,36 @@ +// export function help(flags: Record) { +// const lines = Object.entries(flags).map(entry => { +// const name = entry[0]; +// const value = entry[1]; +// return [ +// [name, value.alias].filter(Boolean), +// value.description, +// value.default +// ].filter(Boolean); +// }); +// +// const longest = lines +// .map(line => { +// const flags = line[0]; +// return flags.reduce((sum, flag) => sum + flag.length, 0); +// }) +// .sort((a, b) => b - a)[0]; +// +// return lines +// .map(line => { +// const flags = line[0]; +// const desc = line[1]; +// const defaults = line[2]; +// const fs = flags.map(flag => +// flag.length > 1 ? `--${flag}` : ` -${flag}` +// ); +// const ds = defaults ? `, defaults to: ${defaults}` : ''; +// const length = flags.reduce((sum, flag) => sum + flag.length, 0); +// return `${fs.join(',')}${' '.repeat( +// Math.max(4 + longest - length, 0) +// )}${desc}${ds}`; +// }) +// .join('\n'); +// } + +export const x = 1; diff --git a/@commitlint/cli/src/types.ts b/@commitlint/cli/src/types.ts new file mode 100644 index 0000000000..562513a834 --- /dev/null +++ b/@commitlint/cli/src/types.ts @@ -0,0 +1,19 @@ +export interface CliFlags { + color: boolean; + config?: string; + cwd: string; + edit?: string; + env?: string; + extends?: (string | number)[]; + help?: boolean; + 'help-url'?: string; + from?: string; + format?: string; + 'parser-preset'?: string; + quiet: boolean; + to?: string; + version?: boolean; + verbose?: boolean; + _: string[]; + $0: string; +} diff --git a/@commitlint/cli/tsconfig.json b/@commitlint/cli/tsconfig.json new file mode 100644 index 0000000000..8d5ce01f6d --- /dev/null +++ b/@commitlint/cli/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.shared.json", + "compilerOptions": { + "composite": true, + "rootDir": "./src", + "outDir": "./lib" + }, + "include": ["./src/**/*.ts"], + "exclude": ["./src/**/*.test.ts", "./lib/**/*"] +} diff --git a/@commitlint/lint/src/lint.ts b/@commitlint/lint/src/lint.ts index f216c7996e..3807afe73e 100644 --- a/@commitlint/lint/src/lint.ts +++ b/@commitlint/lint/src/lint.ts @@ -6,6 +6,7 @@ import {buildCommitMesage} from './commit-message'; import { LintRuleConfig, LintOptions, + LintOutcome, LintRuleOutcome, Rule, RuleSeverity @@ -15,7 +16,7 @@ export default async function lint( message: string, rawRulesConfig?: LintRuleConfig, rawOpts?: LintOptions -) { +): Promise { const opts = rawOpts ? rawOpts : {defaultIgnores: undefined, ignores: undefined}; diff --git a/@commitlint/types/src/load.ts b/@commitlint/types/src/load.ts index 6f446b1369..7779136556 100644 --- a/@commitlint/types/src/load.ts +++ b/@commitlint/types/src/load.ts @@ -114,7 +114,7 @@ export type QualifiedRules = Partial>; export interface QualifiedConfig { extends: string[]; formatter: unknown; - rules: Partial; + rules: QualifiedRules; parserPreset: ParserPreset; ignores: ((commit: string) => boolean)[]; defaultIgnores: boolean; diff --git a/tsconfig.json b/tsconfig.json index ca6bc60377..fd1d9d995a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,6 +18,7 @@ {"path": "@commitlint/read"}, {"path": "@commitlint/rules"}, {"path": "@commitlint/lint"}, - {"path": "@commitlint/core"} + {"path": "@commitlint/core"}, + {"path": "@commitlint/cli"} ] } diff --git a/yarn.lock b/yarn.lock index 3392a49727..4d9a5458b5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1996,7 +1996,7 @@ resolved "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-13.1.0.tgz#c563aa192f39350a1d18da36c5a8da382bbd8228" integrity sha512-gCubfBUZ6KxzoibJ+SCUc/57Ms1jz5NjHe4+dI2krNmU5zCPAphyLJYyTOg06ueIyfj+SaCUqmzun7ImlxDcKg== -"@types/yargs@^15.0.0": +"@types/yargs@^15.0.0", "@types/yargs@^15.0.3": version "15.0.3" resolved "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.3.tgz#41453a0bc7ab393e995d1f5451455638edbd2baf" integrity sha512-XCMQRK6kfpNBixHLyHUsGmXrpEmFFxzMrcnSXFMziHd8CoNJo8l16FkHyQq4x+xbM7E2XL83/O78OD8u+iZTdQ== @@ -3448,11 +3448,6 @@ core-js@^2.4.0, core-js@^2.5.0: resolved "https://registry.npmjs.org/core-js/-/core-js-2.6.11.tgz#38831469f9922bded8ee21c9dc46985e0399308c" integrity sha512-5wjnpaT/3dV+XB4borEsnAYQchn00XSgTAWKDkEqv+K8KevjbzmofK6hfJ9TZIlpj2N0xQpazy7PiRQiWHqzWg== -core-js@^3.6.1: - version "3.6.1" - resolved "https://registry.npmjs.org/core-js/-/core-js-3.6.1.tgz#39d5e2e346258cc01eb7d44345b1c3c014ca3f05" - integrity sha512-186WjSik2iTGfDjfdCZAxv2ormxtKgemjC3SI6PL31qOA0j5LhTDVjHChccoc7brwLvpvLPiMyRlcO88C4l1QQ== - core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" @@ -6899,21 +6894,6 @@ meow@4.0.1, meow@^4.0.0: redent "^2.0.0" trim-newlines "^2.0.0" -meow@5.0.0, meow@^5.0.0: - version "5.0.0" - resolved "https://registry.npmjs.org/meow/-/meow-5.0.0.tgz#dfc73d63a9afc714a5e371760eb5c88b91078aa4" - integrity sha512-CbTqYU17ABaLefO8vCU153ZZlprKYWDljcndKKDCFcYQITzWCXZAVk4QMFZPgvzrnUQ3uItnIE/LoUOwrT15Ig== - dependencies: - camelcase-keys "^4.0.0" - decamelize-keys "^1.0.0" - loud-rejection "^1.0.0" - minimist-options "^3.0.1" - normalize-package-data "^2.3.4" - read-pkg-up "^3.0.0" - redent "^2.0.0" - trim-newlines "^2.0.0" - yargs-parser "^10.0.0" - meow@^3.3.0: version "3.7.0" resolved "https://registry.npmjs.org/meow/-/meow-3.7.0.tgz#72cb668b425228290abbfa856892587308a801fb" @@ -6930,6 +6910,21 @@ meow@^3.3.0: redent "^1.0.0" trim-newlines "^1.0.0" +meow@^5.0.0: + version "5.0.0" + resolved "https://registry.npmjs.org/meow/-/meow-5.0.0.tgz#dfc73d63a9afc714a5e371760eb5c88b91078aa4" + integrity sha512-CbTqYU17ABaLefO8vCU153ZZlprKYWDljcndKKDCFcYQITzWCXZAVk4QMFZPgvzrnUQ3uItnIE/LoUOwrT15Ig== + dependencies: + camelcase-keys "^4.0.0" + decamelize-keys "^1.0.0" + loud-rejection "^1.0.0" + minimist-options "^3.0.1" + normalize-package-data "^2.3.4" + read-pkg-up "^3.0.0" + redent "^2.0.0" + trim-newlines "^2.0.0" + yargs-parser "^10.0.0" + merge-stream@^2.0.0: version "2.0.0" resolved "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60" @@ -8333,11 +8328,6 @@ regenerate@^1.4.0: resolved "https://registry.npmjs.org/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11" integrity sha512-1G6jJVDWrt0rK99kBjvEtziZNCICAuvIPkSiUFIQxVP06RCVpq3dmDo2oi6ABpYaDYaTRr67BEhL8r1wgEZZKg== -regenerator-runtime@0.13.3, regenerator-runtime@^0.13.2: - version "0.13.3" - resolved "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz#7cf6a77d8f5c6f60eb73c5fc1955b2ceb01e6bf5" - integrity sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw== - regenerator-runtime@^0.10.5: version "0.10.5" resolved "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658" @@ -8348,6 +8338,11 @@ regenerator-runtime@^0.11.0: resolved "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9" integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg== +regenerator-runtime@^0.13.2: + version "0.13.3" + resolved "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz#7cf6a77d8f5c6f60eb73c5fc1955b2ceb01e6bf5" + integrity sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw== + regenerator-transform@^0.14.0: version "0.14.1" resolved "https://registry.npmjs.org/regenerator-transform/-/regenerator-transform-0.14.1.tgz#3b2fce4e1ab7732c08f665dfdb314749c7ddd2fb" @@ -10289,7 +10284,7 @@ yargs@^14.2.0, yargs@^14.2.2: y18n "^4.0.0" yargs-parser "^15.0.0" -yargs@^15.0.0: +yargs@^15.0.0, yargs@^15.1.0: version "15.1.0" resolved "https://registry.npmjs.org/yargs/-/yargs-15.1.0.tgz#e111381f5830e863a89550bd4b136bb6a5f37219" integrity sha512-T39FNN1b6hCW4SOIk1XyTOWxtXdcen0t+XYrysQmChzSipvhBO8Bj0nK1ozAasdk24dNWuMZvr4k24nz+8HHLg== From 8670c1504adae4b5c0e0aea762f9c6cb4c16dd7d Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 03:14:48 +0100 Subject: [PATCH 02/23] chore(cli): cleanup code --- @commitlint/cli/src/cli.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 299839964d..7f9ab26cca 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -47,7 +47,6 @@ const cli = yargs }) .option('env', { alias: 'E', - // default: null, describe: 'check message in the file at path given by environment variable value', type: 'string' @@ -64,13 +63,11 @@ const cli = yargs }) .option('from', { alias: 'f', - // default: null, describe: 'lower end of the commit range to lint; applies if edit=false', type: 'string' }) .option('format', { alias: 'o', - // default: null, describe: 'output format of the results', type: 'string' }) @@ -87,7 +84,6 @@ const cli = yargs }) .option('to', { alias: 't', - // default: null, describe: 'upper end of the commit range to lint; applies if edit=false', type: 'string' }) From b58696a7d27f8e45576196e07743fd25087bcbd0 Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 03:20:58 +0100 Subject: [PATCH 03/23] fix(cli): cleanup code and add missing dependencies --- @commitlint/cli/package.json | 3 +++ @commitlint/cli/src/cli.test.ts | 4 ++-- @commitlint/cli/src/cli.ts | 4 ++-- @commitlint/cli/src/help.ts | 36 --------------------------------- 4 files changed, 7 insertions(+), 40 deletions(-) delete mode 100644 @commitlint/cli/src/help.ts diff --git a/@commitlint/cli/package.json b/@commitlint/cli/package.json index 033d2ec939..1efccf119a 100644 --- a/@commitlint/cli/package.json +++ b/@commitlint/cli/package.json @@ -36,7 +36,10 @@ }, "license": "MIT", "devDependencies": { + "@types/execa": "^0.9.0", "@types/yargs": "^15.0.3", + "@commitlint/test": "8.2.0", + "@commitlint/utils": "^8.3.4", "execa": "^3.4.0", "fs-extra": "^8.1.0" }, diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index d9ed08312e..a9ca3fa815 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -11,7 +11,7 @@ interface TestOptions { env?: Record; } -function cli(args: string[], options: TestOptions) { +const cli = (args: string[], options: TestOptions) => { return (input = '') => { return execa(bin, args, { cwd: options.cwd, @@ -20,7 +20,7 @@ function cli(args: string[], options: TestOptions) { reject: false }); }; -} +}; const gitBootstrap = (fixture: string) => git.bootstrap(fixture, __dirname); const fixBootstrap = (fixture: string) => fix.bootstrap(fixture, __dirname); diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 7f9ab26cca..f0b4c72c8e 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -103,14 +103,14 @@ const cli = yargs `[input] reads from stdin if --edit, --env, --from and --to are omitted` ).argv; -main(cli).catch(err => +main(cli).catch(err => { setTimeout(() => { if (err.type === pkg.name) { process.exit(1); } throw err; }, 0) -); +}); async function main(options: CliFlags) { const raw = options._; diff --git a/@commitlint/cli/src/help.ts b/@commitlint/cli/src/help.ts deleted file mode 100644 index 4f7e2d64e5..0000000000 --- a/@commitlint/cli/src/help.ts +++ /dev/null @@ -1,36 +0,0 @@ -// export function help(flags: Record) { -// const lines = Object.entries(flags).map(entry => { -// const name = entry[0]; -// const value = entry[1]; -// return [ -// [name, value.alias].filter(Boolean), -// value.description, -// value.default -// ].filter(Boolean); -// }); -// -// const longest = lines -// .map(line => { -// const flags = line[0]; -// return flags.reduce((sum, flag) => sum + flag.length, 0); -// }) -// .sort((a, b) => b - a)[0]; -// -// return lines -// .map(line => { -// const flags = line[0]; -// const desc = line[1]; -// const defaults = line[2]; -// const fs = flags.map(flag => -// flag.length > 1 ? `--${flag}` : ` -${flag}` -// ); -// const ds = defaults ? `, defaults to: ${defaults}` : ''; -// const length = flags.reduce((sum, flag) => sum + flag.length, 0); -// return `${fs.join(',')}${' '.repeat( -// Math.max(4 + longest - length, 0) -// )}${desc}${ds}`; -// }) -// .join('\n'); -// } - -export const x = 1; From 50afa3403234e4fbe03218bf410ff9ace3be462b Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 04:10:59 +0100 Subject: [PATCH 04/23] fix(cli): restore original options array --- @commitlint/cli/src/cli.ts | 164 ++++++++++++++++++----------------- @commitlint/cli/src/types.ts | 2 +- 2 files changed, 85 insertions(+), 81 deletions(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index f0b4c72c8e..73bea7a3d0 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -2,7 +2,6 @@ import load from '@commitlint/load'; import lint from '@commitlint/lint'; import read from '@commitlint/read'; -import merge from 'lodash/merge'; import isFunction from 'lodash/isFunction'; import stdin from 'get-stdin'; import resolveFrom from 'resolve-from'; @@ -21,81 +20,83 @@ import { const pkg = require('../package'); const cli = yargs - .option('color', { - alias: 'c', - default: true, - describe: 'toggle colored output', - type: 'boolean' - }) - .option('config', { - alias: 'g', - describe: 'path to the config file', - type: 'string' - }) - .option('cwd', { - alias: 'd', - default: process.cwd(), - describe: 'directory to execute in', - type: 'string' - }) - .option('edit', { - alias: 'e', - // default: false, - describe: - 'read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG', - type: 'string' - }) - .option('env', { - alias: 'E', - describe: - 'check message in the file at path given by environment variable value', - type: 'string' - }) - .option('extends', { - alias: 'x', - describe: 'array of shareable configurations to extend', - type: 'array' - }) - .option('help-url', { - alias: 'H', - type: 'string', - describe: 'helpurl in error message' - }) - .option('from', { - alias: 'f', - describe: 'lower end of the commit range to lint; applies if edit=false', - type: 'string' - }) - .option('format', { - alias: 'o', - describe: 'output format of the results', - type: 'string' - }) - .option('parser-preset', { - alias: 'p', - describe: 'configuration preset to use for conventional-commits-parser', - type: 'string' - }) - .option('quiet', { - alias: 'q', - default: false, - describe: 'toggle console output', - type: 'boolean' - }) - .option('to', { - alias: 't', - describe: 'upper end of the commit range to lint; applies if edit=false', - type: 'string' - }) - .option('version', { - alias: 'v', - type: 'boolean', - describe: 'display version information' - }) - .option('verbose', { - alias: 'V', - type: 'boolean', - describe: 'enable verbose output for reports without problems' + .options({ + color: { + alias: 'c', + default: true, + description: 'toggle colored output', + type: 'boolean' + }, + config: { + alias: 'g', + description: 'path to the config file', + type: 'string' + }, + cwd: { + alias: 'd', + default: process.cwd(), + description: 'directory to execute in', + type: 'string' + }, + edit: { + alias: 'e', + default: false, + description: + 'read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG', + type: 'string' + }, + env: { + alias: 'E', + description: + 'check message in the file at path given by environment variable value', + type: 'string' + }, + extends: { + alias: 'x', + description: 'array of shareable configurations to extend', + type: 'array' + }, + 'help-url': { + alias: 'H', + type: 'string', + description: 'helpurl in error message' + }, + from: { + alias: 'f', + description: 'lower end of the commit range to lint; applies if edit=false', + type: 'string' + }, + format: { + alias: 'o', + description: 'output format of the results', + type: 'string' + }, + 'parser-preset': { + alias: 'p', + description: 'configuration preset to use for conventional-commits-parser', + type: 'string' + }, + quiet: { + alias: 'q', + default: false, + description: 'toggle console output', + type: 'boolean' + }, + to: { + alias: 't', + description: 'upper end of the commit range to lint; applies if edit=false', + type: 'string' + }, + version: { + alias: 'v', + type: 'boolean', + description: 'display version information' + }, + verbose: { + alias: 'V', + type: 'boolean', + description: 'enable verbose output for reports without problems' + } }) .help( 'help', @@ -242,7 +243,7 @@ async function main(options: CliFlags) { } } -function checkFromStdin(input: string[], flags: CliFlags) { +function checkFromStdin(input: string[], flags: CliFlags): boolean { return input.length === 0 && !checkFromRepository(flags); } @@ -254,13 +255,16 @@ function checkFromEdit(flags: CliFlags) { return Boolean(flags.edit) || flags.env; } -function checkFromHistory(flags: CliFlags) { +function checkFromHistory(flags: CliFlags): boolean { return typeof flags.from === 'string' || typeof flags.to === 'string'; } -function normalizeFlags(flags: CliFlags) { +function normalizeFlags(flags: CliFlags): CliFlags { const edit = getEditValue(flags); - return merge({}, flags, {edit, e: edit}); + return { + ...flags, + edit + }; } function getEditValue(flags: CliFlags) { diff --git a/@commitlint/cli/src/types.ts b/@commitlint/cli/src/types.ts index 562513a834..306b631b8a 100644 --- a/@commitlint/cli/src/types.ts +++ b/@commitlint/cli/src/types.ts @@ -2,7 +2,7 @@ export interface CliFlags { color: boolean; config?: string; cwd: string; - edit?: string; + edit?: string | boolean; env?: string; extends?: (string | number)[]; help?: boolean; From f819a4ce1cb86da5c63f3cf7a2cde62eea4be180 Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 04:14:16 +0100 Subject: [PATCH 05/23] fix(cli): set cli as executable --- @commitlint/cli/src/cli.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 @commitlint/cli/src/cli.ts diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts old mode 100644 new mode 100755 From bb12a02caab83af5ef0faf617b6ff4449345c89d Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 04:34:56 +0100 Subject: [PATCH 06/23] fix(cli): attempt to fix permissions on linux --- @commitlint/cli/cli-bin.js | 2 ++ @commitlint/cli/index.js | 2 +- @commitlint/cli/package.json | 2 +- @commitlint/cli/src/cli.test.ts | 2 +- @commitlint/cli/src/cli.ts | 12 +++++++----- 5 files changed, 12 insertions(+), 8 deletions(-) create mode 100755 @commitlint/cli/cli-bin.js mode change 100755 => 100644 @commitlint/cli/src/cli.ts diff --git a/@commitlint/cli/cli-bin.js b/@commitlint/cli/cli-bin.js new file mode 100755 index 0000000000..4856fd706c --- /dev/null +++ b/@commitlint/cli/cli-bin.js @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require('./lib/cli.js'); diff --git a/@commitlint/cli/index.js b/@commitlint/cli/index.js index 6babb12a58..d8ab7a8f93 100644 --- a/@commitlint/cli/index.js +++ b/@commitlint/cli/index.js @@ -1,3 +1,3 @@ const path = require('path'); -module.exports = path.join(__dirname, 'lib/cli.js'); +module.exports = path.join(__dirname, 'cli-bin.js'); diff --git a/@commitlint/cli/package.json b/@commitlint/cli/package.json index 1efccf119a..658c96bc04 100644 --- a/@commitlint/cli/package.json +++ b/@commitlint/cli/package.json @@ -8,7 +8,7 @@ "!*.test.js*" ], "bin": { - "commitlint": "./lib/cli.js" + "commitlint": "./cli-bin.js" }, "scripts": { "deps": "dep-check", diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index a9ca3fa815..5762f430cf 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -4,7 +4,7 @@ import execa from 'execa'; import merge from 'lodash/merge'; import fs from 'fs-extra'; -const bin = require.resolve('../lib/cli.js'); +const bin = require.resolve('../cli-bin.js'); interface TestOptions { cwd: string; diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts old mode 100755 new mode 100644 index 73bea7a3d0..0a3a99cd2c --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -1,4 +1,3 @@ -#!/usr/bin/env node import load from '@commitlint/load'; import lint from '@commitlint/lint'; import read from '@commitlint/read'; @@ -63,7 +62,8 @@ const cli = yargs }, from: { alias: 'f', - description: 'lower end of the commit range to lint; applies if edit=false', + description: + 'lower end of the commit range to lint; applies if edit=false', type: 'string' }, format: { @@ -73,7 +73,8 @@ const cli = yargs }, 'parser-preset': { alias: 'p', - description: 'configuration preset to use for conventional-commits-parser', + description: + 'configuration preset to use for conventional-commits-parser', type: 'string' }, quiet: { @@ -84,7 +85,8 @@ const cli = yargs }, to: { alias: 't', - description: 'upper end of the commit range to lint; applies if edit=false', + description: + 'upper end of the commit range to lint; applies if edit=false', type: 'string' }, version: { @@ -110,7 +112,7 @@ main(cli).catch(err => { process.exit(1); } throw err; - }, 0) + }, 0); }); async function main(options: CliFlags) { From 27b1ccd7b385cf948c46b0d3b5c877ca4473e669 Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 05:09:46 +0100 Subject: [PATCH 07/23] fix(cli): improve help format and setup version command --- @commitlint/cli/src/cli.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 0a3a99cd2c..4ee25b88e7 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -89,21 +89,23 @@ const cli = yargs 'upper end of the commit range to lint; applies if edit=false', type: 'string' }, - version: { - alias: 'v', - type: 'boolean', - description: 'display version information' - }, verbose: { alias: 'V', type: 'boolean', description: 'enable verbose output for reports without problems' } }) - .help( - 'help', - `${pkg.name}@${pkg.version} - ${pkg.description}\n` + - `[input] reads from stdin if --edit, --env, --from and --to are omitted` + .version( + 'version', + 'display version information', + `${pkg.name}@${pkg.version}` + ) + .alias('v', 'version') + .help('help') + .alias('h', 'help') + .usage(`${pkg.name}@${pkg.version} - ${pkg.description}`) + .usage( + `[input] reads from stdin if --edit, --env, --from and --to are omitted` ).argv; main(cli).catch(err => { From 3d889a09c805cfc0ffc6c526f5e1c16a207cae96 Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 05:18:25 +0100 Subject: [PATCH 08/23] fix(cli): restore condition after refactoring --- @commitlint/cli/src/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 4ee25b88e7..f386df1b62 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -290,7 +290,7 @@ function getEditValue(flags: CliFlags) { if (edit === '') { return true; } - if (typeof edit === 'undefined') { + if (typeof edit === 'boolean') { return false; } // The recommended method to specify -e with husky was `commitlint -e $HUSKY_GIT_PARAMS` From 93f1e633c4f28449c8f4e3c9de26ad056531eea6 Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 05:31:44 +0100 Subject: [PATCH 09/23] chore(cli): add missing todo --- @commitlint/cli/src/cli.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index f386df1b62..b70245cd32 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -176,6 +176,7 @@ async function main(options: CliFlags) { } const results = await Promise.all( + // TODO: validate why those types do not match messages.map(message => lint(message, loaded.rules as any, opts)) ); From 63123de5174e95eb3f825aaa9547095a9c1720e4 Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 05:36:20 +0100 Subject: [PATCH 10/23] chore(cli): add missing type --- @commitlint/cli/src/cli.ts | 11 +++-------- @commitlint/cli/src/types.ts | 5 +++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index b70245cd32..140e29f8dd 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -7,7 +7,7 @@ import resolveFrom from 'resolve-from'; import resolveGlobal from 'resolve-global'; import yargs from 'yargs'; -import {CliFlags} from './types'; +import {CliFlags, Seed} from './types'; import { LintOptions, LintOutcome, @@ -292,7 +292,7 @@ function getEditValue(flags: CliFlags) { return true; } if (typeof edit === 'boolean') { - return false; + return edit; } // The recommended method to specify -e with husky was `commitlint -e $HUSKY_GIT_PARAMS` // This does not work properly with win32 systems, where env variable declarations @@ -320,12 +320,7 @@ function getEditValue(flags: CliFlags) { return edit; } -function getSeed( - seed: CliFlags -): { - extends?: string[]; - parserPreset?: string; -} { +function getSeed(seed: CliFlags): Seed { const n = (seed.extends || []).filter( (i): i is string => typeof i === 'string' ); diff --git a/@commitlint/cli/src/types.ts b/@commitlint/cli/src/types.ts index 306b631b8a..67dcc606a1 100644 --- a/@commitlint/cli/src/types.ts +++ b/@commitlint/cli/src/types.ts @@ -17,3 +17,8 @@ export interface CliFlags { _: string[]; $0: string; } + +export interface Seed { + extends?: string[]; + parserPreset?: string; +} From e4efb344c3523af7bc2c4a145b9682129b4f768c Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 15:49:05 +0100 Subject: [PATCH 11/23] chore: add missing new line in help and update CI doc --- @commitlint/cli/src/cli.ts | 2 +- docs/reference-cli.md | 41 ++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 140e29f8dd..fe01d0bab4 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -103,7 +103,7 @@ const cli = yargs .alias('v', 'version') .help('help') .alias('h', 'help') - .usage(`${pkg.name}@${pkg.version} - ${pkg.description}`) + .usage(`${pkg.name}@${pkg.version} - ${pkg.description}\n`) .usage( `[input] reads from stdin if --edit, --env, --from and --to are omitted` ).argv; diff --git a/docs/reference-cli.md b/docs/reference-cli.md index 44261fc6e2..b73dde86cc 100644 --- a/docs/reference-cli.md +++ b/docs/reference-cli.md @@ -3,21 +3,32 @@ ```bash ❯ npx commitlint --help -@commitlint/cli@8.0.0 - Lint your commit messages +@commitlint/cli@8.3.5 - Lint your commit messages [input] reads from stdin if --edit, --env, --from and --to are omitted ---color, -c toggle colored output, defaults to: true ---config, -g path to the config file ---cwd, -d directory to execute in, defaults to: $CD ---edit, -e read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG ---env, -E check message in the file at path given by environment variable value ---extends, -x array of shareable configurations to extend ---help, -h display this help message ---from, -f lower end of the commit range to lint; applies if edit=false ---format, -o output format of the results ---parser-preset, -p configuration preset to use for conventional-commits-parser ---quiet, -q toggle console output ---to, -t upper end of the commit range to lint; applies if edit=false ---version, -v display version information ---verbose, -V enable verbose output for reports without problems + +Options: + --color, -c toggle colored output [boolean] [default: true] + --config, -g path to the config file [string] + --cwd, -d directory to execute in + [string] [default: "/projects/commitlint"] + --edit, -e read last commit message from the specified file or + fallbacks to ./.git/COMMIT_EDITMSG + [string] [default: false] + --env, -E check message in the file at path given by environment + variable value [string] + --extends, -x array of shareable configurations to extend [array] + --help-url, -H helpurl in error message [string] + --from, -f lower end of the commit range to lint; applies if + edit=false [string] + --format, -o output format of the results [string] + --parser-preset, -p configuration preset to use for + conventional-commits-parser [string] + --quiet, -q toggle console output [boolean] [default: false] + --to, -t upper end of the commit range to lint; applies if + edit=false [string] + --verbose, -V enable verbose output for reports without problems + [boolean] + -v, --version display version information [boolean] + -h, --help Show help [boolean] ``` From 3a17ad815636a2272bf7deac0f8f26eebdb759eb Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 20:20:06 +0100 Subject: [PATCH 12/23] docs: update default of cwd --- docs/reference-cli.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/reference-cli.md b/docs/reference-cli.md index b73dde86cc..3f0b66a6c1 100644 --- a/docs/reference-cli.md +++ b/docs/reference-cli.md @@ -10,8 +10,7 @@ Options: --color, -c toggle colored output [boolean] [default: true] --config, -g path to the config file [string] - --cwd, -d directory to execute in - [string] [default: "/projects/commitlint"] + --cwd, -d directory to execute in [string] [default: cwd] --edit, -e read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG [string] [default: false] From 8b57a0c50a168215fd494e72e8f25fcace66fb4e Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 21:06:33 +0100 Subject: [PATCH 13/23] fix(cli): update published files --- @commitlint/cli/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/@commitlint/cli/package.json b/@commitlint/cli/package.json index 658c96bc04..5dda9698d8 100644 --- a/@commitlint/cli/package.json +++ b/@commitlint/cli/package.json @@ -4,8 +4,8 @@ "description": "Lint your commit messages", "files": [ "index.js", - "lib", - "!*.test.js*" + "cli-bin.js", + "lib" ], "bin": { "commitlint": "./cli-bin.js" From 422b8336c97d3f9036c300d4fc19956d61ff0482 Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 8 Feb 2020 23:51:59 +0100 Subject: [PATCH 14/23] refactor(cli): rename cli-bin to cli --- @commitlint/cli/{cli-bin.js => cli.js} | 0 @commitlint/cli/index.js | 2 +- @commitlint/cli/package.json | 2 +- @commitlint/cli/src/cli.test.ts | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename @commitlint/cli/{cli-bin.js => cli.js} (100%) mode change 100755 => 100644 diff --git a/@commitlint/cli/cli-bin.js b/@commitlint/cli/cli.js old mode 100755 new mode 100644 similarity index 100% rename from @commitlint/cli/cli-bin.js rename to @commitlint/cli/cli.js diff --git a/@commitlint/cli/index.js b/@commitlint/cli/index.js index d8ab7a8f93..bc56366369 100644 --- a/@commitlint/cli/index.js +++ b/@commitlint/cli/index.js @@ -1,3 +1,3 @@ const path = require('path'); -module.exports = path.join(__dirname, 'cli-bin.js'); +module.exports = path.join(__dirname, 'cli.js'); diff --git a/@commitlint/cli/package.json b/@commitlint/cli/package.json index 5dda9698d8..c4d428b443 100644 --- a/@commitlint/cli/package.json +++ b/@commitlint/cli/package.json @@ -8,7 +8,7 @@ "lib" ], "bin": { - "commitlint": "./cli-bin.js" + "commitlint": "./cli.js" }, "scripts": { "deps": "dep-check", diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index 5762f430cf..e5969f2e9d 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -4,7 +4,7 @@ import execa from 'execa'; import merge from 'lodash/merge'; import fs from 'fs-extra'; -const bin = require.resolve('../cli-bin.js'); +const bin = require.resolve('../cli.js'); interface TestOptions { cwd: string; From 3f3d2e071b02a4e27536a6e4c6b7c78228c54f4b Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 9 Feb 2020 00:24:46 +0100 Subject: [PATCH 15/23] refactor(cli): add custom CliError and fix printing help --- @commitlint/cli/src/cli-error.ts | 13 +++++++++++++ @commitlint/cli/src/cli.ts | 19 ++++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 @commitlint/cli/src/cli-error.ts diff --git a/@commitlint/cli/src/cli-error.ts b/@commitlint/cli/src/cli-error.ts new file mode 100644 index 0000000000..757814a1ed --- /dev/null +++ b/@commitlint/cli/src/cli-error.ts @@ -0,0 +1,13 @@ +export class CliError extends Error { + __proto__ = Error; + + public type: string; + + constructor(message: string, type: string) { + super(message); + + this.type = type; + + Object.setPrototypeOf(this, CliError.prototype); + } +} diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index fe01d0bab4..ba111965d1 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -15,6 +15,7 @@ import { ParserPreset, QualifiedConfig } from '@commitlint/types'; +import {CliError} from './cli-error'; const pkg = require('../package'); @@ -106,9 +107,9 @@ const cli = yargs .usage(`${pkg.name}@${pkg.version} - ${pkg.description}\n`) .usage( `[input] reads from stdin if --edit, --env, --from and --to are omitted` - ).argv; + ); -main(cli).catch(err => { +main(cli.argv).catch(err => { setTimeout(() => { if (err.type === pkg.name) { process.exit(1); @@ -137,12 +138,11 @@ async function main(options: CliFlags) { .filter(Boolean); if (messages.length === 0 && !checkFromRepository(flags)) { - // TODO: create custom error?? - const err = new Error( - '[input] is required: supply via stdin, or --env or --edit or --from and --to' + const err = new CliError( + '[input] is required: supply via stdin, or --env or --edit or --from and --to', + pkg.name ); - (err as any).type = pkg.name; // TODO - console.log(`${cli.help}\n`); + yargs.showHelp('log'); console.log(err.message); throw err; } @@ -241,10 +241,7 @@ async function main(options: CliFlags) { } if (!report.valid) { - // TODO: create custom error?? - const err = new Error(output); - (err as any).type = pkg.name; - throw err; + throw new CliError(output, pkg.name); } } From 1f1e6a1643832addfb1bc4078181ae64037da56b Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 9 Feb 2020 00:32:28 +0100 Subject: [PATCH 16/23] chore(cli): fix package.json --- @commitlint/cli/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/@commitlint/cli/package.json b/@commitlint/cli/package.json index c4d428b443..889d50b327 100644 --- a/@commitlint/cli/package.json +++ b/@commitlint/cli/package.json @@ -4,7 +4,7 @@ "description": "Lint your commit messages", "files": [ "index.js", - "cli-bin.js", + "cli.js", "lib" ], "bin": { From a0c4bb3bb87d8b16dd0efb81e6cb70a15e22bb6c Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 9 Feb 2020 01:47:35 +0100 Subject: [PATCH 17/23] refactor(cli): use camelCase cli flag names --- @commitlint/cli/src/cli.ts | 16 ++++++++-------- @commitlint/cli/src/types.ts | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index ba111965d1..18a2eb7545 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -56,7 +56,7 @@ const cli = yargs description: 'array of shareable configurations to extend', type: 'array' }, - 'help-url': { + helpUrl: { alias: 'H', type: 'string', description: 'helpurl in error message' @@ -72,7 +72,7 @@ const cli = yargs description: 'output format of the results', type: 'string' }, - 'parser-preset': { + parserPreset: { alias: 'p', description: 'configuration preset to use for conventional-commits-parser', @@ -231,8 +231,8 @@ async function main(options: CliFlags) { const output = format(report, { color: flags.color, verbose: flags.verbose, - helpUrl: flags['help-url'] - ? flags['help-url'].trim() + helpUrl: flags.helpUrl + ? flags.helpUrl.trim() : 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint' }); @@ -317,13 +317,13 @@ function getEditValue(flags: CliFlags) { return edit; } -function getSeed(seed: CliFlags): Seed { - const n = (seed.extends || []).filter( +function getSeed(flags: CliFlags): Seed { + const n = (flags.extends || []).filter( (i): i is string => typeof i === 'string' ); return n.length > 0 - ? {extends: n, parserPreset: seed['parser-preset']} - : {parserPreset: seed['parser-preset']}; + ? {extends: n, parserPreset: flags.parserPreset} + : {parserPreset: flags.parserPreset}; } function selectParserOpts(parserPreset: ParserPreset) { diff --git a/@commitlint/cli/src/types.ts b/@commitlint/cli/src/types.ts index 67dcc606a1..538d6bb1a2 100644 --- a/@commitlint/cli/src/types.ts +++ b/@commitlint/cli/src/types.ts @@ -6,10 +6,10 @@ export interface CliFlags { env?: string; extends?: (string | number)[]; help?: boolean; - 'help-url'?: string; + helpUrl?: string; from?: string; format?: string; - 'parser-preset'?: string; + parserPreset?: string; quiet: boolean; to?: string; version?: boolean; From 6fea4cf09c96649461c35ed9371deb29631393d7 Mon Sep 17 00:00:00 2001 From: Mario Nebl Date: Sun, 9 Feb 2020 20:43:31 +1100 Subject: [PATCH 18/23] fix: unify rule config types --- @commitlint/cli/cli.js | 0 @commitlint/cli/src/cli.ts | 11 ++- @commitlint/format/package.json | 1 + @commitlint/format/src/format.ts | 38 +++-------- @commitlint/format/tsconfig.json | 3 +- @commitlint/lint/src/lint.test.ts | 4 +- @commitlint/lint/src/lint.ts | 15 ++--- @commitlint/types/src/format.ts | 37 +++++++++++ @commitlint/types/src/index.ts | 1 + @commitlint/types/src/lint.ts | 7 +- @commitlint/types/src/load.ts | 84 ++--------------------- @commitlint/types/src/rules.ts | 107 +++++++++++++++++++++++++++--- yarn.lock | 9 ++- 13 files changed, 178 insertions(+), 139 deletions(-) mode change 100644 => 100755 @commitlint/cli/cli.js create mode 100644 @commitlint/types/src/format.ts diff --git a/@commitlint/cli/cli.js b/@commitlint/cli/cli.js old mode 100644 new mode 100755 diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 18a2eb7545..1d761f1d06 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -13,7 +13,8 @@ import { LintOutcome, ParserOptions, ParserPreset, - QualifiedConfig + QualifiedConfig, + Formatter } from '@commitlint/types'; import {CliError} from './cli-error'; @@ -177,7 +178,7 @@ async function main(options: CliFlags) { const results = await Promise.all( // TODO: validate why those types do not match - messages.map(message => lint(message, loaded.rules as any, opts)) + messages.map(message => lint(message, loaded.rules, opts)) ); if (Object.keys(loaded.rules).length === 0) { @@ -338,10 +339,8 @@ function selectParserOpts(parserPreset: ParserPreset) { return parserPreset.parserOpts; } -function loadFormatter(config: QualifiedConfig, flags: CliFlags) { - // TODO: validate why formatter is unknown???? - const moduleName: string = - flags.format || (config.formatter as any) || '@commitlint/format'; +function loadFormatter(config: QualifiedConfig, flags: CliFlags): Formatter { + const moduleName = flags.format || config.formatter || '@commitlint/format'; const modulePath = resolveFrom.silent(__dirname, moduleName) || resolveFrom.silent(flags.cwd, moduleName) || diff --git a/@commitlint/format/package.json b/@commitlint/format/package.json index d8d8201f69..ceb59d27f5 100644 --- a/@commitlint/format/package.json +++ b/@commitlint/format/package.json @@ -37,6 +37,7 @@ "@commitlint/utils": "^8.3.4" }, "dependencies": { + "@commitlint/types": "^8.3.5", "chalk": "^3.0.0" } } diff --git a/@commitlint/format/src/format.ts b/@commitlint/format/src/format.ts index 050c8fba63..cf75cf824d 100644 --- a/@commitlint/format/src/format.ts +++ b/@commitlint/format/src/format.ts @@ -1,41 +1,19 @@ import chalk from 'chalk'; +import { + ChalkColor, + FormattableReport, + FormatOptions, + FormattableResult, + WithInput +} from '@commitlint/types'; const DEFAULT_SIGNS = [' ', '⚠', '✖'] as const; const DEFAULT_COLORS = ['white', 'yellow', 'red'] as const; -export interface FormattableProblem { - level: 0 | 1 | 2; - name: string; - message: string; -} - -export interface FormattableResult { - errors?: FormattableProblem[]; - warnings?: FormattableProblem[]; -} - -export interface WithInput { - input?: string; -} - -export interface FormattableReport { - results?: (FormattableResult & WithInput)[]; -} - -export type ChalkColor = keyof typeof chalk; - -export interface FormatOptions { - color?: boolean; - signs?: readonly [string, string, string]; - colors?: readonly [ChalkColor, ChalkColor, ChalkColor]; - verbose?: boolean; - helpUrl?: string; -} - export function format( report: FormattableReport = {}, options: FormatOptions = {} -) { +): string { const {results = []} = report; const fi = (result: FormattableResult & WithInput) => formatInput(result, options); diff --git a/@commitlint/format/tsconfig.json b/@commitlint/format/tsconfig.json index 49479bf34f..119e645565 100644 --- a/@commitlint/format/tsconfig.json +++ b/@commitlint/format/tsconfig.json @@ -6,5 +6,6 @@ "outDir": "./lib" }, "include": ["./src"], - "exclude": ["./src/**/*.test.ts", "./lib/**/*"] + "exclude": ["./src/**/*.test.ts", "./lib/**/*"], + "references": [{"path": "../types"}] } diff --git a/@commitlint/lint/src/lint.test.ts b/@commitlint/lint/src/lint.test.ts index c8ff6cc123..521f826b4a 100644 --- a/@commitlint/lint/src/lint.test.ts +++ b/@commitlint/lint/src/lint.test.ts @@ -125,8 +125,8 @@ test('throws for rule with invalid level', async () => { test('throws for rule with out of range level', async () => { const error = lint('type(scope): foo', { - 'type-enum': [-1, 'always'], - 'header-max-length': [3, 'always'] + 'type-enum': [-1, 'always'] as any, + 'header-max-length': [3, 'always'] as any }); await expect(error).rejects.toThrow('rule type-enum must be between 0 and 2'); diff --git a/@commitlint/lint/src/lint.ts b/@commitlint/lint/src/lint.ts index 3807afe73e..8df8cd77a2 100644 --- a/@commitlint/lint/src/lint.ts +++ b/@commitlint/lint/src/lint.ts @@ -4,17 +4,17 @@ import parse from '@commitlint/parse'; import defaultRules from '@commitlint/rules'; import {buildCommitMesage} from './commit-message'; import { - LintRuleConfig, LintOptions, LintOutcome, LintRuleOutcome, Rule, - RuleSeverity + RuleConfigSeverity, + QualifiedRules } from '@commitlint/types'; export default async function lint( message: string, - rawRulesConfig?: LintRuleConfig, + rawRulesConfig?: QualifiedRules, rawOpts?: LintOptions ): Promise { const opts = rawOpts @@ -76,7 +76,7 @@ export default async function lint( const [level] = config; - if (level === RuleSeverity.Disabled && config.length === 1) { + if (level === RuleConfigSeverity.Disabled && config.length === 1) { return null; } @@ -132,10 +132,10 @@ export default async function lint( // Validate against all rules const results = Object.entries(rulesConfig) - .filter(([, [level]]) => level > 0) + .filter(([, config]) => typeof config !== 'undefined' && config[0] > 0) .map(entry => { const [name, config] = entry; - const [level, when, value] = config; + const [level, when, value] = config!; // // Level 0 rules are ignored if (level === 0) { @@ -148,8 +148,7 @@ export default async function lint( throw new Error(`Could not find rule implementation for ${name}`); } - const executableRule = rule as Rule; - const [valid, message] = executableRule(parsed, when, value); + const [valid, message] = rule(parsed, when, value as any); return { level, diff --git a/@commitlint/types/src/format.ts b/@commitlint/types/src/format.ts new file mode 100644 index 0000000000..ba50d8a79f --- /dev/null +++ b/@commitlint/types/src/format.ts @@ -0,0 +1,37 @@ +import chalk from 'chalk'; +import {QualifiedRules} from './load'; +import {RuleConfigSeverity} from './rules'; + +export type Formatter = ( + report: FormattableReport, + options: FormatOptions +) => string; + +export interface FormattableProblem { + level: RuleConfigSeverity; + name: keyof QualifiedRules; + message: string; +} + +export interface FormattableResult { + errors?: FormattableProblem[]; + warnings?: FormattableProblem[]; +} + +export interface WithInput { + input?: string; +} + +export interface FormattableReport { + results?: (FormattableResult & WithInput)[]; +} + +export type ChalkColor = keyof typeof chalk; + +export interface FormatOptions { + color?: boolean; + signs?: readonly [string, string, string]; + colors?: readonly [ChalkColor, ChalkColor, ChalkColor]; + verbose?: boolean; + helpUrl?: string; +} diff --git a/@commitlint/types/src/index.ts b/@commitlint/types/src/index.ts index f562854fed..562a9a6373 100644 --- a/@commitlint/types/src/index.ts +++ b/@commitlint/types/src/index.ts @@ -1,4 +1,5 @@ export * from './ensure'; +export * from './format'; export * from './is-ignored'; export * from './rules'; export * from './lint'; diff --git a/@commitlint/types/src/lint.ts b/@commitlint/types/src/lint.ts index 6ebf81e448..e9f047e9e0 100644 --- a/@commitlint/types/src/lint.ts +++ b/@commitlint/types/src/lint.ts @@ -1,10 +1,11 @@ import {IsIgnoredOptions} from './is-ignored'; -import {RuleConfigTuple, PluginRecords, RuleSeverity} from './load'; +import {PluginRecords} from './load'; import {ParserOptions} from './parse'; +import {RuleConfigSeverity, RuleConfigTuple} from './rules'; export type LintRuleConfig = Record< string, - | Readonly<[RuleSeverity.Disabled]> + | Readonly<[RuleConfigSeverity.Disabled]> | RuleConfigTuple | RuleConfigTuple >; @@ -35,7 +36,7 @@ export interface LintRuleOutcome { /** If the commit is considered valid for the rule */ valid: boolean; /** The "severity" of the rule (1 = warning, 2 = error) */ - level: RuleSeverity; + level: RuleConfigSeverity; /** The name of the rule */ name: string; /** The message returned from the rule, if invalid */ diff --git a/@commitlint/types/src/load.ts b/@commitlint/types/src/load.ts index 7779136556..25c05d7aff 100644 --- a/@commitlint/types/src/load.ts +++ b/@commitlint/types/src/load.ts @@ -1,5 +1,4 @@ -import {TargetCaseType} from './ensure'; -import {Rule, RuleCondition} from './rules'; +import {Rule, RulesConfig, RuleConfigQuality} from './rules'; export type PluginRecords = Record; @@ -14,84 +13,9 @@ export interface LoadOptions { file?: string; } -export enum RuleSeverity { - Disabled = 0, - Warning = 1, - Error = 2 -} - -export type RuleConfigTuple = T extends void - ? Readonly<[RuleSeverity, RuleCondition]> - : Readonly<[RuleSeverity, RuleCondition, T]>; - -export enum RuleConfigQuality { - User, - Qualified -} - -export type QualifiedRuleConfig = - | (() => RuleConfigTuple) - | (() => RuleConfigTuple>) - | RuleConfigTuple; - -export type RuleConfig< - V = RuleConfigQuality.Qualified, - T = void -> = V extends RuleConfigQuality.Qualified - ? RuleConfigTuple - : QualifiedRuleConfig; - -export type CaseRuleConfig = RuleConfig< - V, - TargetCaseType ->; -export type LengthRuleConfig = RuleConfig< - V, - number ->; -export type EnumRuleConfig = RuleConfig< - V, - string[] ->; - -export type RulesConfig = { - 'body-case': CaseRuleConfig; - 'body-empty': RuleConfig; - 'body-leading-blank': RuleConfig; - 'body-max-length': LengthRuleConfig; - 'body-max-line-length': LengthRuleConfig; - 'body-min-length': LengthRuleConfig; - 'footer-empty': RuleConfig; - 'footer-leading-blank': RuleConfig; - 'footer-max-length': LengthRuleConfig; - 'footer-max-line-length': LengthRuleConfig; - 'footer-min-length': LengthRuleConfig; - 'header-case': CaseRuleConfig; - 'header-full-stop': RuleConfig; - 'header-max-length': LengthRuleConfig; - 'header-min-length': LengthRuleConfig; - 'references-empty': RuleConfig; - 'scope-case': CaseRuleConfig; - 'scope-empty': RuleConfig; - 'scope-enum': EnumRuleConfig; - 'scope-max-length': LengthRuleConfig; - 'scope-min-length': LengthRuleConfig; - 'signed-off-by': RuleConfig; - 'subject-case': CaseRuleConfig; - 'subject-empty': RuleConfig; - 'subject-full-stop': RuleConfig; - 'subject-max-length': LengthRuleConfig; - 'subject-min-length': LengthRuleConfig; - 'type-case': CaseRuleConfig; - 'type-empty': RuleConfig; - 'type-enum': EnumRuleConfig; - 'type-max-length': LengthRuleConfig; - 'type-min-length': LengthRuleConfig; -}; - export interface UserConfig { extends?: string[]; - formatter?: unknown; + formatter?: string; rules?: Partial; parserPreset?: string | ParserPreset; ignores?: ((commit: string) => boolean)[]; @@ -101,7 +25,7 @@ export interface UserConfig { export interface UserPreset { extends?: string[]; - formatter?: unknown; + formatter?: string; rules?: Partial; parserPreset?: string | ParserPreset; ignores?: ((commit: string) => boolean)[]; @@ -113,7 +37,7 @@ export type QualifiedRules = Partial>; export interface QualifiedConfig { extends: string[]; - formatter: unknown; + formatter: string; rules: QualifiedRules; parserPreset: ParserPreset; ignores: ((commit: string) => boolean)[]; diff --git a/@commitlint/types/src/rules.ts b/@commitlint/types/src/rules.ts index b064cb4d32..73d0c62d2f 100644 --- a/@commitlint/types/src/rules.ts +++ b/@commitlint/types/src/rules.ts @@ -1,12 +1,6 @@ +import {TargetCaseType} from './ensure'; import {Commit} from './parse'; -/** - * Rules always have a condition. - * It can be either "always" (as tested), or "never" (as tested). - * For example, `header-full-stop` can be enforced as "always" or "never". - */ -export type RuleCondition = 'always' | 'never'; - /** * Rules match the input either as successful or failed. * For example, when `header-full-stop` detects a full stop and is set as "always"; it's true. @@ -20,6 +14,103 @@ export type RuleOutcome = [boolean, string?]; */ export type Rule = ( parsed: Commit, - when?: RuleCondition, + when?: RuleConfigCondition, value?: Value ) => RuleOutcome; + +/** + * Rules always have a severity. + * Severity indicates what to do if the rule is found to be broken + * 0 - Disable this rule + * 1 - Warn for violations + * 2 - Error for violations + */ +export enum RuleConfigSeverity { + Disabled = 0, + Warning = 1, + Error = 2 +} + +/** + * Rules always have a condition. + * It can be either "always" (as tested), or "never" (as tested). + * For example, `header-full-stop` can be enforced as "always" or "never". + */ +export type RuleConfigCondition = 'always' | 'never'; + +export type RuleConfigTuple = T extends void + ? + | Readonly<[RuleConfigSeverity.Disabled]> + | Readonly<[RuleConfigSeverity, RuleConfigCondition]> + : + | Readonly<[RuleConfigSeverity.Disabled]> + | Readonly<[RuleConfigSeverity, RuleConfigCondition, T]>; + +export enum RuleConfigQuality { + User, + Qualified +} + +export type QualifiedRuleConfig = + | (() => RuleConfigTuple) + | (() => RuleConfigTuple>) + | RuleConfigTuple; + +export type RuleConfig< + V = RuleConfigQuality.Qualified, + T = void +> = V extends RuleConfigQuality.Qualified + ? RuleConfigTuple + : QualifiedRuleConfig; + +export type CaseRuleConfig = RuleConfig< + V, + TargetCaseType +>; +export type LengthRuleConfig = RuleConfig< + V, + number +>; +export type EnumRuleConfig = RuleConfig< + V, + string[] +>; + +export type RulesConfig = { + 'body-case': CaseRuleConfig; + 'body-empty': RuleConfig; + 'body-leading-blank': RuleConfig; + 'body-max-length': LengthRuleConfig; + 'body-max-line-length': LengthRuleConfig; + 'body-min-length': LengthRuleConfig; + 'footer-empty': RuleConfig; + 'footer-leading-blank': RuleConfig; + 'footer-max-length': LengthRuleConfig; + 'footer-max-line-length': LengthRuleConfig; + 'footer-min-length': LengthRuleConfig; + 'header-case': CaseRuleConfig; + 'header-full-stop': RuleConfig; + 'header-max-length': LengthRuleConfig; + 'header-min-length': LengthRuleConfig; + 'references-empty': RuleConfig; + 'scope-case': CaseRuleConfig; + 'scope-empty': RuleConfig; + 'scope-enum': EnumRuleConfig; + 'scope-max-length': LengthRuleConfig; + 'scope-min-length': LengthRuleConfig; + 'signed-off-by': RuleConfig; + 'subject-case': CaseRuleConfig; + 'subject-empty': RuleConfig; + 'subject-full-stop': RuleConfig; + 'subject-max-length': LengthRuleConfig; + 'subject-min-length': LengthRuleConfig; + 'type-case': CaseRuleConfig; + 'type-empty': RuleConfig; + 'type-enum': EnumRuleConfig; + 'type-max-length': LengthRuleConfig; + 'type-min-length': LengthRuleConfig; + // Plugins may add their custom rules + [key: string]: AnyRuleConfig; +}; + +export type AnyRuleConfig = RuleConfig | RuleConfig; diff --git a/yarn.lock b/yarn.lock index 4d9a5458b5..fc08bf2455 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1893,6 +1893,13 @@ resolved "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== +"@types/execa@^0.9.0": + version "0.9.0" + resolved "https://registry.npmjs.org/@types/execa/-/execa-0.9.0.tgz#9b025d2755f17e80beaf9368c3f4f319d8b0fb93" + integrity sha512-mgfd93RhzjYBUHHV532turHC2j4l/qxsF/PbfDmprHDEUHmNZGlDn1CEsulGK3AfsPdhkWzZQT/S/k0UGhLGsA== + dependencies: + "@types/node" "*" + "@types/fs-extra@^8.0.1": version "8.0.1" resolved "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-8.0.1.tgz#a2378d6e7e8afea1564e44aafa2e207dadf77686" @@ -8571,7 +8578,7 @@ resolve@1.1.7: resolved "https://registry.npmjs.org/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs= -resolve@1.x, resolve@^1.1.6, resolve@^1.10.0, resolve@^1.3.2, resolve@^1.8.1, resolve@^1.12.0, resolve@^1.13.1: +resolve@1.x, resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.3.2, resolve@^1.8.1: version "1.15.1" resolved "https://registry.npmjs.org/resolve/-/resolve-1.15.1.tgz#27bdcdeffeaf2d6244b95bb0f9f4b4653451f3e8" integrity sha512-84oo6ZTtoTUpjgNEr5SJyzQhzL72gaRodsSfyxC/AXRvwu0Yse9H8eF9IpGo7b8YetZhlI6v7ZQ6bKBFV/6S7w== From 4e122771639f175189646d9807ac09dbf77ccdc1 Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 9 Feb 2020 15:11:59 +0100 Subject: [PATCH 19/23] chore: drop no longer used package (issue after rebase) --- @commitlint/cli/package.json | 1 - yarn.lock | 7 ------- 2 files changed, 8 deletions(-) diff --git a/@commitlint/cli/package.json b/@commitlint/cli/package.json index 889d50b327..6ca3a847a5 100644 --- a/@commitlint/cli/package.json +++ b/@commitlint/cli/package.json @@ -36,7 +36,6 @@ }, "license": "MIT", "devDependencies": { - "@types/execa": "^0.9.0", "@types/yargs": "^15.0.3", "@commitlint/test": "8.2.0", "@commitlint/utils": "^8.3.4", diff --git a/yarn.lock b/yarn.lock index fc08bf2455..7c0265c681 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1893,13 +1893,6 @@ resolved "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== -"@types/execa@^0.9.0": - version "0.9.0" - resolved "https://registry.npmjs.org/@types/execa/-/execa-0.9.0.tgz#9b025d2755f17e80beaf9368c3f4f319d8b0fb93" - integrity sha512-mgfd93RhzjYBUHHV532turHC2j4l/qxsF/PbfDmprHDEUHmNZGlDn1CEsulGK3AfsPdhkWzZQT/S/k0UGhLGsA== - dependencies: - "@types/node" "*" - "@types/fs-extra@^8.0.1": version "8.0.1" resolved "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-8.0.1.tgz#a2378d6e7e8afea1564e44aafa2e207dadf77686" From 17504f2efefa411c756e40e51aea0bb955bbe07a Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 9 Feb 2020 15:41:59 +0100 Subject: [PATCH 20/23] chore: remove todo comment as its already resolved --- @commitlint/cli/src/cli.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 1d761f1d06..eacc9ca1ad 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -177,7 +177,6 @@ async function main(options: CliFlags) { } const results = await Promise.all( - // TODO: validate why those types do not match messages.map(message => lint(message, loaded.rules, opts)) ); From db186ddcc2055935a224fccd833dcd121389e444 Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 9 Feb 2020 16:23:31 +0100 Subject: [PATCH 21/23] fix: enable strict mode to reproduce previous behavior --- @commitlint/cli/src/cli.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index eacc9ca1ad..c55824e728 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -108,7 +108,8 @@ const cli = yargs .usage(`${pkg.name}@${pkg.version} - ${pkg.description}\n`) .usage( `[input] reads from stdin if --edit, --env, --from and --to are omitted` - ); + ) + .strict(); main(cli.argv).catch(err => { setTimeout(() => { From 4246081c510db0a925293aab2da78962a6ab8cc0 Mon Sep 17 00:00:00 2001 From: Armano Date: Tue, 11 Feb 2020 02:17:49 +0100 Subject: [PATCH 22/23] chore: rollback some unnecessary changes --- @commitlint/cli/src/cli.ts | 12 ++++++------ @commitlint/cli/src/types.ts | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index c55824e728..0aafc804de 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -57,7 +57,7 @@ const cli = yargs description: 'array of shareable configurations to extend', type: 'array' }, - helpUrl: { + 'help-url': { alias: 'H', type: 'string', description: 'helpurl in error message' @@ -73,7 +73,7 @@ const cli = yargs description: 'output format of the results', type: 'string' }, - parserPreset: { + 'parser-preset': { alias: 'p', description: 'configuration preset to use for conventional-commits-parser', @@ -232,8 +232,8 @@ async function main(options: CliFlags) { const output = format(report, { color: flags.color, verbose: flags.verbose, - helpUrl: flags.helpUrl - ? flags.helpUrl.trim() + helpUrl: flags['help-url'] + ? flags['help-url'].trim() : 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint' }); @@ -323,8 +323,8 @@ function getSeed(flags: CliFlags): Seed { (i): i is string => typeof i === 'string' ); return n.length > 0 - ? {extends: n, parserPreset: flags.parserPreset} - : {parserPreset: flags.parserPreset}; + ? {extends: n, parserPreset: flags['parser-preset']} + : {parserPreset: flags['parser-preset']}; } function selectParserOpts(parserPreset: ParserPreset) { diff --git a/@commitlint/cli/src/types.ts b/@commitlint/cli/src/types.ts index 538d6bb1a2..67dcc606a1 100644 --- a/@commitlint/cli/src/types.ts +++ b/@commitlint/cli/src/types.ts @@ -6,10 +6,10 @@ export interface CliFlags { env?: string; extends?: (string | number)[]; help?: boolean; - helpUrl?: string; + 'help-url'?: string; from?: string; format?: string; - parserPreset?: string; + 'parser-preset'?: string; quiet: boolean; to?: string; version?: boolean; From 712f4cd6bf6ff47d350643416a0183e0ee7cd33a Mon Sep 17 00:00:00 2001 From: Armano Date: Tue, 11 Feb 2020 03:08:49 +0100 Subject: [PATCH 23/23] test(cli): add unit test for help and version commands --- @commitlint/cli/src/cli.test.ts | 41 +++++++++++++++++++++++++++++++++ @commitlint/cli/src/cli.ts | 3 ++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index e5969f2e9d..0ab9c60560 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -428,6 +428,47 @@ test('should work with relative formatter path', async () => { expect(actual.exitCode).toBe(0); }); +test('should print help', async () => { + const cwd = await gitBootstrap('fixtures/default'); + const actual = await cli(['--help'], {cwd})(); + expect(actual.stdout).toMatchInlineSnapshot(` + "@commitlint/cli@8.3.5 - Lint your commit messages + + [input] reads from stdin if --edit, --env, --from and --to are omitted + + Options: + --color, -c toggle colored output [boolean] [default: true] + --config, -g path to the config file [string] + --cwd, -d directory to execute in + [string] [default: (Working Directory)] + --edit, -e read last commit message from the specified file or + fallbacks to ./.git/COMMIT_EDITMSG + [string] [default: false] + --env, -E check message in the file at path given by environment + variable value [string] + --extends, -x array of shareable configurations to extend [array] + --help-url, -H help url in error message [string] + --from, -f lower end of the commit range to lint; applies if + edit=false [string] + --format, -o output format of the results [string] + --parser-preset, -p configuration preset to use for + conventional-commits-parser [string] + --quiet, -q toggle console output [boolean] [default: false] + --to, -t upper end of the commit range to lint; applies if + edit=false [string] + --verbose, -V enable verbose output for reports without problems + [boolean] + -v, --version display version information [boolean] + -h, --help Show help [boolean]" + `); +}); + +test('should print version', async () => { + const cwd = await gitBootstrap('fixtures/default'); + const actual = await cli(['--version'], {cwd})(); + expect(actual.stdout).toMatch('@commitlint/cli@'); +}); + async function writePkg(payload: unknown, options: TestOptions) { const pkgPath = path.join(options.cwd, 'package.json'); const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8')); diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 0aafc804de..6af2663f88 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -36,6 +36,7 @@ const cli = yargs cwd: { alias: 'd', default: process.cwd(), + defaultDescription: '(Working Directory)', description: 'directory to execute in', type: 'string' }, @@ -60,7 +61,7 @@ const cli = yargs 'help-url': { alias: 'H', type: 'string', - description: 'helpurl in error message' + description: 'help url in error message' }, from: { alias: 'f',