Skip to content

Commit 06971f3

Browse files
mmdaplhonghuangdc
authored andcommitted
feat(packages): @sa/scripts: command gitCommit support chinese (#548)
1 parent f4eeb2e commit 06971f3

File tree

5 files changed

+102
-50
lines changed

5 files changed

+102
-50
lines changed

packages/scripts/src/commands/git-commit.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from 'node:path';
22
import { readFileSync } from 'node:fs';
33
import { prompt } from 'enquirer';
4-
import { bgRed, green, red, yellow } from 'kolorist';
54
import { execCommand } from '../shared';
6-
import type { CliOption } from '../types';
5+
import { locales } from '../locales';
6+
import type { Lang } from '../locales';
77

88
interface PromptObject {
99
types: string;
@@ -14,13 +14,11 @@ interface PromptObject {
1414
/**
1515
* Git commit with Conventional Commits standard
1616
*
17-
* @param gitCommitTypes
18-
* @param gitCommitScopes
17+
* @param lang
1918
*/
20-
export async function gitCommit(
21-
gitCommitTypes: CliOption['gitCommitTypes'],
22-
gitCommitScopes: CliOption['gitCommitScopes']
23-
) {
19+
export async function gitCommit(lang: Lang = 'en-us') {
20+
const { gitCommitMessages, gitCommitTypes, gitCommitScopes } = locales[lang];
21+
2422
const typesChoices = gitCommitTypes.map(([value, msg]) => {
2523
const nameWithSuffix = `${value}:`;
2624

@@ -41,19 +39,19 @@ export async function gitCommit(
4139
{
4240
name: 'types',
4341
type: 'select',
44-
message: 'Please select a type',
42+
message: gitCommitMessages.types,
4543
choices: typesChoices
4644
},
4745
{
4846
name: 'scopes',
4947
type: 'select',
50-
message: 'Please select a scope',
48+
message: gitCommitMessages.scopes,
5149
choices: scopesChoices
5250
},
5351
{
5452
name: 'description',
5553
type: 'text',
56-
message: `Please enter a description (add prefix ${yellow('!')} to indicate breaking change)`
54+
message: gitCommitMessages.description
5755
}
5856
]);
5957

@@ -67,7 +65,7 @@ export async function gitCommit(
6765
}
6866

6967
/** Git commit message verify */
70-
export async function gitCommitVerify() {
68+
export async function gitCommitVerify(lang: Lang = 'en-us') {
7169
const gitPath = await execCommand('git', ['rev-parse', '--show-toplevel']);
7270

7371
const gitMsgPath = path.join(gitPath, '.git', 'COMMIT_EDITMSG');
@@ -77,10 +75,8 @@ export async function gitCommitVerify() {
7775
const REG_EXP = /(?<type>[a-z]+)(?:\((?<scope>.+)\))?(?<breaking>!)?: (?<description>.+)/i;
7876

7977
if (!REG_EXP.test(commitMsg)) {
80-
throw new Error(
81-
`${bgRed(' ERROR ')} ${red('git commit message must match the Conventional Commits standard!')}\n\n${green(
82-
'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'
83-
)}`
84-
);
78+
const errorMsg = locales[lang].gitCommitVerify;
79+
80+
throw new Error(errorMsg);
8581
}
8682
}

packages/scripts/src/config/index.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,6 @@ const defaultOptions: CliOption = {
1212
'**/node_modules',
1313
'!node_modules/**'
1414
],
15-
gitCommitTypes: [
16-
['feat', 'A new feature'],
17-
['fix', 'A bug fix'],
18-
['docs', 'Documentation only changes'],
19-
['style', 'Changes that do not affect the meaning of the code'],
20-
['refactor', 'A code change that neither fixes a bug nor adds a feature'],
21-
['perf', 'A code change that improves performance'],
22-
['optimize', 'A code change that optimizes code quality'],
23-
['test', 'Adding missing tests or correcting existing tests'],
24-
['build', 'Changes that affect the build system or external dependencies'],
25-
['ci', 'Changes to our CI configuration files and scripts'],
26-
['chore', "Other changes that don't modify src or test files"],
27-
['revert', 'Reverts a previous commit']
28-
],
29-
gitCommitScopes: [
30-
['projects', 'project'],
31-
['packages', 'packages'],
32-
['components', 'components'],
33-
['hooks', 'hook functions'],
34-
['utils', 'utils functions'],
35-
['types', 'TS declaration'],
36-
['styles', 'style'],
37-
['deps', 'project dependencies'],
38-
['release', 'release project'],
39-
['other', 'other changes']
40-
],
4115
ncuCommandArgs: ['--deep', '-u'],
4216
changelogOptions: {}
4317
};

packages/scripts/src/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { blue, lightGreen } from 'kolorist';
33
import { version } from '../package.json';
44
import { cleanup, genChangelog, generateRoute, gitCommit, gitCommitVerify, release, updatePkg } from './commands';
55
import { loadCliOptions } from './config';
6+
import type { Lang } from './locales';
67

78
type Command = 'cleanup' | 'update-pkg' | 'git-commit' | 'git-commit-verify' | 'changelog' | 'release' | 'gen-route';
89

@@ -18,13 +19,19 @@ interface CommandArg {
1819
/** Generate changelog by total tags */
1920
total?: boolean;
2021
/**
21-
* The glob pattern of dirs to cleanup
22+
* The glob pattern of dirs to clean up
2223
*
2324
* If not set, it will use the default value
2425
*
2526
* Multiple values use "," to separate them
2627
*/
2728
cleanupDir?: string;
29+
/**
30+
* display lang of cli
31+
*
32+
* @default 'en-us'
33+
*/
34+
lang?: Lang;
2835
}
2936

3037
export async function setupCli() {
@@ -44,6 +51,7 @@ export async function setupCli() {
4451
'-c, --cleanupDir <dir>',
4552
'The glob pattern of dirs to cleanup, If not set, it will use the default value, Multiple values use "," to separate them'
4653
)
54+
.option('-l, --lang <lang>', 'display lang of cli', { default: 'en-us', type: [String] })
4755
.help();
4856

4957
const commands: CommandWithAction<CommandArg> = {
@@ -61,8 +69,8 @@ export async function setupCli() {
6169
},
6270
'git-commit': {
6371
desc: 'git commit, generate commit message which match Conventional Commits standard',
64-
action: async () => {
65-
await gitCommit(cliOptions.gitCommitTypes, cliOptions.gitCommitScopes);
72+
action: async args => {
73+
await gitCommit(args?.lang);
6674
}
6775
},
6876
'git-commit-verify': {

packages/scripts/src/locales/index.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { bgRed, green, red, yellow } from 'kolorist';
2+
3+
export type Lang = 'zh-cn' | 'en-us';
4+
5+
export const locales = {
6+
'zh-cn': {
7+
gitCommitMessages: {
8+
types: '请选择提交类型',
9+
scopes: '请选择提交范围',
10+
description: `请输入描述信息(${yellow('!')}开头表示破坏性改动`
11+
},
12+
gitCommitTypes: [
13+
['feat', '新功能'],
14+
['fix', '修复Bug'],
15+
['docs', '只更新文档'],
16+
['style', '修改代码风格,不影响代码含义的变更'],
17+
['refactor', '代码重构,既不修复 bug 也不添加功能的代码变更'],
18+
['perf', '可提高性能的代码更改'],
19+
['optimize', '优化代码质量的代码更改'],
20+
['test', '添加缺失的测试或更正现有测'],
21+
['build', '影响构建系统或外部依赖项的更改'],
22+
['ci', '对 CI 配置文件和脚本的更改'],
23+
['chore', '没有修改src或测试文件的其他变更'],
24+
['revert', '还原先前的提交']
25+
] as [string, string][],
26+
gitCommitScopes: [
27+
['projects', '项目'],
28+
['packages', '包'],
29+
['components', '组件'],
30+
['hooks', '钩子函数'],
31+
['utils', '工具函数'],
32+
['types', 'TS类型声明'],
33+
['styles', '代码风格'],
34+
['deps', '项目依赖'],
35+
['release', '发布项目新版本'],
36+
['other', '其他的变更']
37+
] as [string, string][],
38+
gitCommitVerify: `${bgRed(' 错误 ')} ${red('git 提交信息必须符合 Conventional Commits 标准!')}\n\n${green(
39+
'推荐使用命令 `pnpm commit` 生成符合 Conventional Commits 标准的提交信息。\n获取有关 Conventional Commits 的更多信息,请访问此链接: https://conventionalcommits.org'
40+
)}`
41+
},
42+
'en-us': {
43+
gitCommitMessages: {
44+
types: 'Please select a type',
45+
scopes: 'Please select a scope',
46+
description: `Please enter a description (add prefix ${yellow('!')} to indicate breaking change)`
47+
},
48+
gitCommitTypes: [
49+
['feat', 'A new feature'],
50+
['fix', 'A bug fix'],
51+
['docs', 'Documentation only changes'],
52+
['style', 'Changes that do not affect the meaning of the code'],
53+
['refactor', 'A code change that neither fixes a bug nor adds a feature'],
54+
['perf', 'A code change that improves performance'],
55+
['optimize', 'A code change that optimizes code quality'],
56+
['test', 'Adding missing tests or correcting existing tests'],
57+
['build', 'Changes that affect the build system or external dependencies'],
58+
['ci', 'Changes to our CI configuration files and scripts'],
59+
['chore', "Other changes that don't modify src or test files"],
60+
['revert', 'Reverts a previous commit']
61+
] as [string, string][],
62+
gitCommitScopes: [
63+
['projects', 'project'],
64+
['packages', 'packages'],
65+
['components', 'components'],
66+
['hooks', 'hook functions'],
67+
['utils', 'utils functions'],
68+
['types', 'TS declaration'],
69+
['styles', 'style'],
70+
['deps', 'project dependencies'],
71+
['release', 'release project'],
72+
['other', 'other changes']
73+
] as [string, string][],
74+
gitCommitVerify: `${bgRed(' ERROR ')} ${red('git commit message must match the Conventional Commits standard!')}\n\n${green(
75+
'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'
76+
)}`
77+
}
78+
} satisfies Record<Lang, Record<string, unknown>>;

packages/scripts/src/types/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ export interface CliOption {
1414
* ```
1515
*/
1616
cleanupDirs: string[];
17-
/** Git commit types */
18-
gitCommitTypes: [string, string][];
19-
/** Git commit scopes */
20-
gitCommitScopes: [string, string][];
2117
/**
2218
* Npm-check-updates command args
2319
*

0 commit comments

Comments
 (0)