chore: format all common files (#431)

This commit is contained in:
tecvan
2025-07-31 21:46:47 +08:00
committed by GitHub
parent 8136ddd82d
commit 40088b0a05
60 changed files with 658 additions and 288 deletions

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Command } from 'commander';
import { CliOptions } from '../types/config';
@@ -14,7 +30,11 @@ export const createProgram = (): Command => {
program
.requiredOption('-r, --root <directory>', '需要处理的根目录')
.option('-e, --exts <extensions>', '文件扩展名,用逗号分隔 (例: ts,js,go)', '')
.option(
'-e, --exts <extensions>',
'文件扩展名,用逗号分隔 (例: ts,js,go)',
'',
)
.option('--access-key-id <key>', '火山引擎 Access Key ID')
.option('--secret-access-key <key>', '火山引擎 Secret Access Key')
.option('--region <region>', '火山引擎服务区域', 'cn-beijing')
@@ -48,7 +68,7 @@ export const parseOptions = (program: Command): CliOptions => {
dryRun: options.dryRun,
verbose: options.verbose,
output: options.output,
config: options.config
config: options.config,
};
};

View File

@@ -1,4 +1,25 @@
import { AppConfig, CliOptions, TranslationConfig, ProcessingConfig } from '../types/config';
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
AppConfig,
CliOptions,
TranslationConfig,
ProcessingConfig,
} from '../types/config';
import { deepMerge } from '../utils/fp';
/**
@@ -13,28 +34,63 @@ const DEFAULT_CONFIG: AppConfig = {
targetLanguage: 'en',
maxRetries: 3,
timeout: 30000,
concurrency: 3
concurrency: 3,
},
processing: {
defaultExtensions: [
'ts', 'tsx', 'js', 'jsx', 'go', 'md', 'txt', 'json',
'yaml', 'yml', 'toml', 'ini', 'conf', 'config',
'sh', 'bash', 'zsh', 'fish', 'py', 'css', 'scss', 'sass', 'less',
'html', 'htm', 'xml', 'php', 'rb', 'rs', 'java', 'c', 'h',
'cpp', 'cxx', 'cc', 'hpp', 'cs', 'thrift'
'ts',
'tsx',
'js',
'jsx',
'go',
'md',
'txt',
'json',
'yaml',
'yml',
'toml',
'ini',
'conf',
'config',
'sh',
'bash',
'zsh',
'fish',
'py',
'css',
'scss',
'sass',
'less',
'html',
'htm',
'xml',
'php',
'rb',
'rs',
'java',
'c',
'h',
'cpp',
'cxx',
'cc',
'hpp',
'cs',
'thrift',
],
outputFormat: 'console'
outputFormat: 'console',
},
git: {
ignorePatterns: ['node_modules/**', '.git/**', 'dist/**', 'build/**'],
includeUntracked: false
}
includeUntracked: false,
},
};
/**
* Load configuration from file
*/
export const loadConfigFromFile = async (configPath: string): Promise<Partial<AppConfig>> => {
export const loadConfigFromFile = async (
configPath: string,
): Promise<Partial<AppConfig>> => {
try {
const fs = await import('fs/promises');
const configContent = await fs.readFile(configPath, 'utf-8');
@@ -48,11 +104,19 @@ export const loadConfigFromFile = async (configPath: string): Promise<Partial<Ap
/**
* Create configuration from command line options
*/
export const createConfigFromOptions = (options: CliOptions): Partial<AppConfig> => {
export const createConfigFromOptions = (
options: CliOptions,
): Partial<AppConfig> => {
const config: Partial<AppConfig> = {};
// translation configuration
if (options.accessKeyId || options.secretAccessKey || options.region || options.sourceLanguage || options.targetLanguage) {
if (
options.accessKeyId ||
options.secretAccessKey ||
options.region ||
options.sourceLanguage ||
options.targetLanguage
) {
config.translation = {} as Partial<TranslationConfig>;
if (options.accessKeyId) {
config.translation!.accessKeyId = options.accessKeyId;
@@ -90,10 +154,9 @@ export const createConfigFromOptions = (options: CliOptions): Partial<AppConfig>
* merge configuration
*/
export const mergeConfigs = (...configs: Partial<AppConfig>[]): AppConfig => {
return configs.reduce(
(merged, config) => deepMerge(merged, config),
{ ...DEFAULT_CONFIG }
) as AppConfig;
return configs.reduce((merged, config) => deepMerge(merged, config), {
...DEFAULT_CONFIG,
}) as AppConfig;
};
/**
@@ -118,41 +181,65 @@ export const loadConfig = async (options: CliOptions): Promise<AppConfig> => {
/**
* verify configuration
*/
export const validateConfig = (config: AppConfig): { valid: boolean; errors: string[] } => {
export const validateConfig = (
config: AppConfig,
): { valid: boolean; errors: string[] } => {
const errors: string[] = [];
// Verify Volcano Engine Access Key ID
if (!config.translation.accessKeyId) {
errors.push('火山引擎 Access Key ID 未设置请通过环境变量VOLC_ACCESS_KEY_ID或--access-key-id参数提供');
errors.push(
'火山引擎 Access Key ID 未设置请通过环境变量VOLC_ACCESS_KEY_ID或--access-key-id参数提供',
);
}
// Verify Volcano Engine Secret Access Key
if (!config.translation.secretAccessKey) {
errors.push('火山引擎 Secret Access Key 未设置请通过环境变量VOLC_SECRET_ACCESS_KEY或--secret-access-key参数提供');
errors.push(
'火山引擎 Secret Access Key 未设置请通过环境变量VOLC_SECRET_ACCESS_KEY或--secret-access-key参数提供',
);
}
// validation area
const validRegions = ['cn-beijing', 'ap-southeast-1', 'us-east-1'];
if (!validRegions.includes(config.translation.region)) {
console.warn(`未知的区域: ${config.translation.region},建议使用: ${validRegions.join(', ')}`);
console.warn(
`未知的区域: ${config.translation.region},建议使用: ${validRegions.join(
', ',
)}`,
);
}
// Verify language code
const validLanguages = ['zh', 'en', 'ja', 'ko', 'fr', 'de', 'es', 'pt', 'ru'];
if (!validLanguages.includes(config.translation.sourceLanguage)) {
console.warn(`未知的源语言: ${config.translation.sourceLanguage},建议使用: ${validLanguages.join(', ')}`);
console.warn(
`未知的源语言: ${
config.translation.sourceLanguage
},建议使用: ${validLanguages.join(', ')}`,
);
}
if (!validLanguages.includes(config.translation.targetLanguage)) {
console.warn(`未知的目标语言: ${config.translation.targetLanguage},建议使用: ${validLanguages.join(', ')}`);
console.warn(
`未知的目标语言: ${
config.translation.targetLanguage
},建议使用: ${validLanguages.join(', ')}`,
);
}
// validation concurrency
if (config.translation.concurrency < 1 || config.translation.concurrency > 10) {
if (
config.translation.concurrency < 1 ||
config.translation.concurrency > 10
) {
errors.push('并发数应该在1-10之间');
}
// verification timeout
if (config.translation.timeout < 1000 || config.translation.timeout > 300000) {
if (
config.translation.timeout < 1000 ||
config.translation.timeout > 300000
) {
errors.push('超时时间应该在1000-300000毫秒之间');
}
@@ -162,7 +249,10 @@ export const validateConfig = (config: AppConfig): { valid: boolean; errors: str
/**
* Print configuration information
*/
export const printConfigInfo = (config: AppConfig, verbose: boolean = false): void => {
export const printConfigInfo = (
config: AppConfig,
verbose: boolean = false,
): void => {
console.log('🔧 当前配置:');
console.log(` 区域: ${config.translation.region}`);
console.log(` 源语言: ${config.translation.sourceLanguage}`);
@@ -172,11 +262,23 @@ export const printConfigInfo = (config: AppConfig, verbose: boolean = false): vo
console.log(` 输出格式: ${config.processing.outputFormat}`);
if (verbose) {
console.log(` Access Key ID: ${config.translation.accessKeyId ? '已设置' : '未设置'}`);
console.log(` Secret Access Key: ${config.translation.secretAccessKey ? '已设置' : '未设置'}`);
console.log(
` Access Key ID: ${
config.translation.accessKeyId ? '已设置' : '未设置'
}`,
);
console.log(
` Secret Access Key: ${
config.translation.secretAccessKey ? '已设置' : '未设置'
}`,
);
console.log(` 超时时间: ${config.translation.timeout}ms`);
console.log(` 默认扩展名: ${config.processing.defaultExtensions.join(', ')}`);
console.log(
` 默认扩展名: ${config.processing.defaultExtensions.join(', ')}`,
);
console.log(` 忽略模式: ${config.git.ignorePatterns.join(', ')}`);
console.log(` 包含未跟踪文件: ${config.git.includeUntracked ? '是' : '否'}`);
console.log(
` 包含未跟踪文件: ${config.git.includeUntracked ? '是' : '否'}`,
);
}
};