From 87c1056634204d079016d41ab5a206de3257b4ac Mon Sep 17 00:00:00 2001 From: loicduong Date: Thu, 24 Oct 2024 22:12:48 +0700 Subject: [PATCH] chore(projects): use commitlint to check commit --- commitlint.config.cjs | 38 +++++++++ package.json | 4 +- packages/scripts/src/commands/git-commit.ts | 85 --------------------- packages/scripts/src/commands/index.ts | 1 - packages/scripts/src/config/index.ts | 10 --- packages/scripts/src/index.ts | 24 +----- packages/scripts/src/locales/index.ts | 42 ---------- packages/scripts/src/types/index.ts | 2 - 8 files changed, 42 insertions(+), 164 deletions(-) delete mode 100644 packages/scripts/src/commands/git-commit.ts delete mode 100644 packages/scripts/src/locales/index.ts diff --git a/commitlint.config.cjs b/commitlint.config.cjs index 98ee7df..67cf088 100644 --- a/commitlint.config.cjs +++ b/commitlint.config.cjs @@ -1,3 +1,41 @@ module.exports = { extends: ['@commitlint/config-conventional'], + rules: { + 'type-enum': [ + 2, + 'always', + [ + 'feat', // A new feature + 'feat-wip', // Features in development, such as partial code for a certain feature + 'fix', // A bug fix + 'docs', // Documentation only changes + 'typo', // Code or document corrections, such as spelling errors + 'style', // Changes that do not affect the meaning of the code + 'refactor', // A code change that neither fixes a bug nor adds a feature + 'perf', // A code change that improves performance + 'optimize', // A code change that optimizes code quality + 'test', // Adding missing tests or correcting existing tests + 'build', // Changes that affect the build system or external dependencies + 'ci', // Changes to our CI configuration files and scripts + 'chore', // Other changes that don't modify src or test files + 'revert', // Reverts a previous commit + ], + ], + 'scope-enum': [ + 2, + 'always', + [ + 'projects', // project + 'packages', // packages + 'components', // components + 'hooks', // hook functions + 'utils', // utils functions + 'types', // TS declaration + 'styles', // style + 'deps', // project dependencies + 'release', // release project + 'other', // other changes + ], + ], + }, } diff --git a/package.json b/package.json index 490ba69..f1b2cde 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "build:dev": "vite build --mode development", "build:tst": "vite build --mode testing", "cleanup": "sa cleanup", - "commit": "sa git-commit", + "commit": "commitlint --edit", "dev": "vite", "dev:prd": "vite --mode production", "dev:tst": "vite --mode testing", @@ -103,7 +103,7 @@ "vue-tsc": "2.1.6" }, "simple-git-hooks": { - "commit-msg": "pnpm sa git-commit-verify", + "commit-msg": "pnpm commit", "pre-commit": "pnpm typecheck && pnpm lint-staged" }, "lint-staged": { diff --git a/packages/scripts/src/commands/git-commit.ts b/packages/scripts/src/commands/git-commit.ts deleted file mode 100644 index b69394e..0000000 --- a/packages/scripts/src/commands/git-commit.ts +++ /dev/null @@ -1,85 +0,0 @@ -import type { Lang } from '../locales' -import { readFileSync } from 'node:fs' -import path from 'node:path' -import { prompt } from 'enquirer' -import { locales } from '../locales' -import { execCommand } from '../shared' - -interface PromptObject { - types: string - scopes: string - description: string -} - -/** - * Git commit with Conventional Commits standard - * - * @param lang - */ -export async function gitCommit(lang: Lang = 'en-us') { - const { gitCommitMessages, gitCommitTypes, gitCommitScopes } = locales[lang] - - const typesChoices = gitCommitTypes.map(([value, msg]) => { - const nameWithSuffix = `${value}:` - - const message = `${nameWithSuffix.padEnd(12)}${msg}` - - return { - name: value, - message, - } - }) - - const scopesChoices = gitCommitScopes.map(([value, msg]) => ({ - name: value, - message: `${value.padEnd(30)} (${msg})`, - })) - - const result = await prompt([ - { - name: 'types', - type: 'select', - message: gitCommitMessages.types, - choices: typesChoices, - }, - { - name: 'scopes', - type: 'select', - message: gitCommitMessages.scopes, - choices: scopesChoices, - }, - { - name: 'description', - type: 'text', - message: gitCommitMessages.description, - }, - ]) - - const breaking = result.description.startsWith('!') ? '!' : '' - - const description = result.description.replace(/^!/, '').trim() - - const commitMsg = `${result.types}(${result.scopes})${breaking}: ${description}` - - await execCommand('git', ['commit', '-m', commitMsg], { stdio: 'inherit' }) -} - -/** Git commit message verify */ -export async function gitCommitVerify(lang: Lang = 'en-us', ignores: RegExp[] = []) { - const gitPath = await execCommand('git', ['rev-parse', '--show-toplevel']) - - const gitMsgPath = path.join(gitPath, '.git', 'COMMIT_EDITMSG') - - const commitMsg = readFileSync(gitMsgPath, 'utf8').trim() - - if (ignores.some(regExp => regExp.test(commitMsg))) - return - - const REG_EXP = /(?[a-z]+)(?:\((?.+)\))?(?!)?: (?.+)/i - - if (!REG_EXP.test(commitMsg)) { - const errorMsg = locales[lang].gitCommitVerify - - throw new Error(errorMsg) - } -} diff --git a/packages/scripts/src/commands/index.ts b/packages/scripts/src/commands/index.ts index 7ed94fd..a8fe3dd 100644 --- a/packages/scripts/src/commands/index.ts +++ b/packages/scripts/src/commands/index.ts @@ -1,6 +1,5 @@ export * from './changelog' export * from './cleanup' -export * from './git-commit' export * from './release' export * from './router' export * from './update-pkg' diff --git a/packages/scripts/src/config/index.ts b/packages/scripts/src/config/index.ts index 75f7052..f088772 100644 --- a/packages/scripts/src/config/index.ts +++ b/packages/scripts/src/config/index.ts @@ -14,16 +14,6 @@ const defaultOptions: CliOption = { ], ncuCommandArgs: ['--deep', '-u'], changelogOptions: {}, - gitCommitVerifyIgnores: [ - /^((Merge pull request)|(Merge (.*?) into (.*?)|(Merge branch (.*?)))(?:\r?\n)*$)/m, - /^(Merge tag (.*?))(?:\r?\n)*$/m, - /^(R|r)evert (.*)/, - /^(amend|fixup|squash)!/, - /^(Merged (.*?)(in|into) (.*)|Merged PR (.*): (.*))/, - /^Merge remote-tracking branch(\s*)(.*)/, - /^Automatic merge(.*)/, - /^Auto-merged (.*?) into (.*)/, - ], } export async function loadCliOptions(overrides?: Partial, cwd = process.cwd()) { diff --git a/packages/scripts/src/index.ts b/packages/scripts/src/index.ts index d637eb2..698011f 100644 --- a/packages/scripts/src/index.ts +++ b/packages/scripts/src/index.ts @@ -1,11 +1,10 @@ -import type { Lang } from './locales' import cac from 'cac' import { blue, lightGreen } from 'kolorist' import { version } from '../package.json' -import { cleanup, genChangelog, generateRoute, gitCommit, gitCommitVerify, release, updatePkg } from './commands' +import { cleanup, genChangelog, generateRoute, release, updatePkg } from './commands' import { loadCliOptions } from './config' -type Command = 'cleanup' | 'update-pkg' | 'git-commit' | 'git-commit-verify' | 'changelog' | 'release' | 'gen-route' +type Command = 'cleanup' | 'update-pkg' | 'changelog' | 'release' | 'gen-route' type CommandAction = (args?: A) => Promise | void @@ -26,12 +25,6 @@ interface CommandArg { * Multiple values use "," to separate them */ cleanupDir?: string - /** - * display lang of cli - * - * @default 'en-us' - */ - lang?: Lang } export async function setupCli() { @@ -51,7 +44,6 @@ export async function setupCli() { '-c, --cleanupDir ', 'The glob pattern of dirs to cleanup, If not set, it will use the default value, Multiple values use "," to separate them', ) - .option('-l, --lang ', 'display lang of cli', { default: 'en-us', type: [String] }) .help() const commands: CommandWithAction = { @@ -67,18 +59,6 @@ export async function setupCli() { await updatePkg(cliOptions.ncuCommandArgs) }, }, - 'git-commit': { - desc: 'git commit, generate commit message which match Conventional Commits standard', - action: async (args) => { - await gitCommit(args?.lang) - }, - }, - 'git-commit-verify': { - desc: 'verify git commit message, make sure it match Conventional Commits standard', - action: async (args) => { - await gitCommitVerify(args?.lang, cliOptions.gitCommitVerifyIgnores) - }, - }, 'changelog': { desc: 'generate changelog', action: async (args) => { diff --git a/packages/scripts/src/locales/index.ts b/packages/scripts/src/locales/index.ts deleted file mode 100644 index a5d5581..0000000 --- a/packages/scripts/src/locales/index.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { bgRed, green, red, yellow } from 'kolorist' - -export type Lang = 'en-us' - -export const locales = { - 'en-us': { - gitCommitMessages: { - types: 'Please select a type', - scopes: 'Please select a scope', - description: `Please enter a description (add prefix ${yellow('!')} to indicate breaking change)`, - }, - gitCommitTypes: [ - ['feat', 'A new feature'], - ['fix', 'A bug fix'], - ['docs', 'Documentation only changes'], - ['style', 'Changes that do not affect the meaning of the code'], - ['refactor', 'A code change that neither fixes a bug nor adds a feature'], - ['perf', 'A code change that improves performance'], - ['optimize', 'A code change that optimizes code quality'], - ['test', 'Adding missing tests or correcting existing tests'], - ['build', 'Changes that affect the build system or external dependencies'], - ['ci', 'Changes to our CI configuration files and scripts'], - ['chore', 'Other changes that don\'t modify src or test files'], - ['revert', 'Reverts a previous commit'], - ] as [string, string][], - gitCommitScopes: [ - ['projects', 'project'], - ['packages', 'packages'], - ['components', 'components'], - ['hooks', 'hook functions'], - ['utils', 'utils functions'], - ['types', 'TS declaration'], - ['styles', 'style'], - ['deps', 'project dependencies'], - ['release', 'release project'], - ['other', 'other changes'], - ] as [string, string][], - gitCommitVerify: `${bgRed(' ERROR ')} ${red('git commit message must match the Conventional Commits standard!')}\n\n${green( - 'Recommended to use the command `pnpm commit` to generate Conventional Commits compliant commit information.\nGet more info about Conventional Commits, follow this link: https://conventionalcommits.org', - )}`, - }, -} satisfies Record> diff --git a/packages/scripts/src/types/index.ts b/packages/scripts/src/types/index.ts index 1e52673..3347c5b 100644 --- a/packages/scripts/src/types/index.ts +++ b/packages/scripts/src/types/index.ts @@ -26,6 +26,4 @@ export interface CliOption { * @link https://github.com/soybeanjs/changelog */ changelogOptions: Partial - /** The ignore pattern list of git commit verify */ - gitCommitVerifyIgnores: RegExp[] }