feat(scripts): 增加--lang参数,支持中文、英文

This commit is contained in:
chufan 2024-07-15 11:15:28 +08:00
parent 7373852590
commit 9e98bc8415
5 changed files with 124 additions and 47 deletions

View File

@ -1,9 +1,10 @@
import path from 'node:path'; import path from 'node:path';
import { readFileSync } from 'node:fs'; import { readFileSync } from 'node:fs';
import { prompt } from 'enquirer'; import { prompt } from 'enquirer';
import { bgRed, green, red, yellow } from 'kolorist'; import { bgRed, green, red } from 'kolorist';
import { execCommand } from '../shared'; import { execCommand } from '../shared';
import type { CliOption } from '../types'; import { getLocalLanguage } from '../i18n';
import type { LangTypeEnum } from '../types';
interface PromptObject { interface PromptObject {
types: string; types: string;
@ -14,15 +15,11 @@ interface PromptObject {
/** /**
* Git commit with Conventional Commits standard * Git commit with Conventional Commits standard
* *
* @param gitCommitTypes
* @param gitCommitScopes
* @param lang * @param lang
*/ */
export async function gitCommit( export async function gitCommit(lang?: LangTypeEnum) {
gitCommitTypes: CliOption['gitCommitTypes'], const { gitCommitMessages, gitCommitTypes, gitCommitScopes } = getLocalLanguage(lang);
gitCommitScopes: CliOption['gitCommitScopes'],
lang?: string
) {
const typesChoices = gitCommitTypes.map(([value, msg]) => { const typesChoices = gitCommitTypes.map(([value, msg]) => {
const nameWithSuffix = `${value}:`; const nameWithSuffix = `${value}:`;
@ -43,22 +40,19 @@ export async function gitCommit(
{ {
name: 'types', name: 'types',
type: 'select', type: 'select',
message: lang === 'en-us' ? 'Please select a type' : '请选择提交类型', message: gitCommitMessages.types,
choices: typesChoices choices: typesChoices
}, },
{ {
name: 'scopes', name: 'scopes',
type: 'select', type: 'select',
message: lang === 'en-us' ? 'Please select a scope' : '请选择提交范围', message: gitCommitMessages.scopes,
choices: scopesChoices choices: scopesChoices
}, },
{ {
name: 'description', name: 'description',
type: 'text', type: 'text',
message: message: gitCommitMessages.description
lang === 'en-us'
? `Please enter a description (add prefix ${yellow('!')} to indicate breaking change)`
: '请输入描述信息(!开头表示破坏性改动)'
} }
]); ]);

View File

@ -1,6 +1,9 @@
import process from 'node:process'; import process from 'node:process';
import { loadConfig } from 'c12'; import { loadConfig } from 'c12';
import type { CliOption } from '../types'; import type { CliOption } from '../types';
import { getLocalLanguage } from '../i18n';
const defaultLang = getLocalLanguage();
const defaultOptions: CliOption = { const defaultOptions: CliOption = {
cwd: process.cwd(), cwd: process.cwd(),
@ -12,32 +15,8 @@ const defaultOptions: CliOption = {
'**/node_modules', '**/node_modules',
'!node_modules/**' '!node_modules/**'
], ],
gitCommitTypes: [ gitCommitTypes: defaultLang.gitCommitTypes,
['feat', 'A new feature'], gitCommitScopes: defaultLang.gitCommitScopes,
['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']
],
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']
],
ncuCommandArgs: ['--deep', '-u'], ncuCommandArgs: ['--deep', '-u'],
changelogOptions: {} changelogOptions: {}
}; };

View File

@ -0,0 +1,96 @@
import { yellow } from 'kolorist';
import { LangTypeEnum } from '../types';
interface GitCommitMessages {
types: string;
scopes: string;
description: string;
}
interface LocalConfig {
gitCommitMessages: GitCommitMessages;
gitCommitTypes: [string, string][];
gitCommitScopes: [string, string][];
}
interface Locals {
[LangTypeEnum.CHINESE]: LocalConfig;
[LangTypeEnum.ENGLISH]: LocalConfig;
}
const locales: Locals = {
'zh-cn': {
gitCommitMessages: {
types: '请选择提交类型',
scopes: '请选择提交范围',
description: `请输入描述信息(${yellow('!')}开头表示破坏性改动`
},
gitCommitTypes: [
['feat', '新功能'],
['fix', '修复Bug'],
['docs', '只更新文档'],
['style', '修改代码风格,不影响代码含义的变更'],
['refactor', '代码重构,既不修复 bug 也不添加功能的代码变更'],
['perf', '可提高性能的代码更改'],
['optimize', '优化代码质量的代码更改'],
['test', '添加缺失的测试或更正现有测'],
['build', '影响构建系统或外部依赖项的更改'],
['ci', '对 CI 配置文件和脚本的更改'],
['chore', '没有修改src或测试文件的其他变更'],
['revert', '还原先前的提交']
],
gitCommitScopes: [
['projects', '项目'],
['packages', '包'],
['components', '组件'],
['hooks', '钩子函数'],
['utils', '工具函数'],
['types', 'TS类型声明'],
['styles', '代码风格'],
['deps', '项目依赖'],
['release', '发布项目新版本'],
['other', '其他的变更']
]
},
'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']
],
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']
]
}
};
/**
*
*
* @param lang
*/
export function getLocalLanguage(lang?: LangTypeEnum) {
return locales[lang ?? LangTypeEnum.ENGLISH];
}

View File

@ -3,6 +3,7 @@ import { blue, lightGreen } from 'kolorist';
import { version } from '../package.json'; import { version } from '../package.json';
import { cleanup, genChangelog, generateRoute, gitCommit, gitCommitVerify, release, updatePkg } from './commands'; import { cleanup, genChangelog, generateRoute, gitCommit, gitCommitVerify, release, updatePkg } from './commands';
import { loadCliOptions } from './config'; import { loadCliOptions } from './config';
import { LangTypeEnum } from './types';
type Command = 'cleanup' | 'update-pkg' | 'git-commit' | 'git-commit-verify' | 'changelog' | 'release' | 'gen-route'; type Command = 'cleanup' | 'update-pkg' | 'git-commit' | 'git-commit-verify' | 'changelog' | 'release' | 'gen-route';
@ -27,11 +28,11 @@ interface CommandArg {
cleanupDir?: string; cleanupDir?: string;
/** Support for different language prompts, and the default is en-us */ /** Support for different language prompts, and the default is en-us */
lang?: string; lang?: LangTypeEnum;
} }
/** 支持的语言 */ /** 支持的语言 */
enum SaCliLanguage { export enum SaCliLanguage {
Chinese = 'zh-cn', Chinese = 'zh-cn',
English = 'en-us' English = 'en-us'
} }
@ -53,9 +54,9 @@ export async function setupCli() {
'-c, --cleanupDir <dir>', '-c, --cleanupDir <dir>',
'The glob pattern of dirs to cleanup, If not set, it will use the default value, Multiple values use "," to separate them' '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', 'Support for different language prompts, and the default is en-us', { .option('-l, --lang <lang>', 'Support for different language prompts, and the default is en-us', {
default: SaCliLanguage.English, default: LangTypeEnum.ENGLISH,
type: [String] type: [LangTypeEnum]
}) })
.help(); .help();
@ -75,7 +76,8 @@ export async function setupCli() {
'git-commit': { 'git-commit': {
desc: 'git commit, generate commit message which match Conventional Commits standard', desc: 'git commit, generate commit message which match Conventional Commits standard',
action: async args => { action: async args => {
await gitCommit(cliOptions.gitCommitTypes, cliOptions.gitCommitScopes, args?.lang); // 选择中文后,不支持语言
await gitCommit(args?.lang);
} }
}, },
'git-commit-verify': { 'git-commit-verify': {

View File

@ -31,3 +31,9 @@ export interface CliOption {
*/ */
changelogOptions: Partial<ChangelogOption>; changelogOptions: Partial<ChangelogOption>;
} }
/** 语言 */
export enum LangTypeEnum {
CHINESE = 'zh-cn',
ENGLISH = 'en-us'
}