UNPKG

i18n-xy

Version:

自动提取React项目中的中文字符串并进行国际化的CLI工具

181 lines (172 loc) 5.75 kB
import { Options } from 'fast-glob'; interface I18nConfig { locale: string; displayLanguage?: string; outputDir: string; tempDir?: string; include: string[]; exclude?: string[]; keyGeneration?: { maxChineseLength?: number; hashLength?: number; maxRetryCount?: number; reuseExistingKey?: boolean; duplicateKeySuffix?: 'hash'; keyPrefix?: string; separator?: string; pinyinOptions?: { toneType?: 'none' | 'symbol' | 'num'; type?: 'string' | 'array'; }; }; output?: { prettyJson?: boolean; localeFileName?: string; }; logging?: { enabled?: boolean; level?: 'minimal' | 'normal' | 'verbose'; }; replacement?: { functionName?: string; quoteType?: 'single' | 'double'; useOriginalTextAsKey?: boolean; templateString?: { enabled?: boolean; preserveExpressions?: boolean; splitStrategy?: 'smart' | 'aggressive' | 'conservative'; }; autoImport?: { enabled?: boolean; insertPosition?: 'top' | 'afterImports'; imports?: { [filePattern: string]: { importStatement: string; }; }; }; }; translation?: { enabled?: boolean; provider?: 'baidu' | 'custom'; defaultSourceLang?: string; defaultTargetLang?: string; concurrency?: number; retryTimes?: number; retryDelay?: number; batchDelay?: number; baidu?: { appid?: string; key?: string; }; custom?: { endpoint?: string; apiKey?: string; }; }; [key: string]: unknown; } declare class ConfigManagerClass { private config; init(config: I18nConfig): void; get(): I18nConfig; reset(): void; isInitialized(): boolean; } declare const ConfigManager: ConfigManagerClass; /** * 配置一致性检查工具 * 用于验证所有配置项是否正确使用,没有硬编码值 */ declare class ConfigValidator { /** * 检查配置的一致性和完整性 */ static validateConfigUsage(): { isValid: boolean; errors: string[]; warnings: string[]; }; /** * 检查是否存在配置与代码不一致的问题 */ static checkConfigConsistency(): void; } declare function loadConfig(customConfigPath?: string): I18nConfig; declare class Logger { private static temporaryLevel; private static getLogLevel; /** * 临时设置日志级别(用于进度显示期间) */ static setTemporaryLevel(level: string | null): void; private static shouldLog; static success(message: string, level?: 'minimal' | 'normal' | 'verbose'): void; static info(message: string, level?: 'minimal' | 'normal' | 'verbose'): void; static warn(message: string, level?: 'minimal' | 'normal' | 'verbose'): void; static error(message: string, level?: 'minimal' | 'normal' | 'verbose'): void; static verbose(message: string): void; } /** * 读取文件内容 */ declare function readFile(filePath: string, encoding?: BufferEncoding): Promise<string>; /** * 写入文件内容 */ declare function writeFile(filePath: string, data: string | Buffer): Promise<void>; /** * 写入文件内容,支持 tempDir 配置 * @param filePath 原始文件路径 * @param data 文件内容 * @param tempDir 临时目录,如果配置则写入到临时目录,否则写入原始位置 */ declare function writeFileWithTempDir(filePath: string, data: string | Buffer, tempDir?: string): Promise<void>; /** * 创建目录 */ declare function ensureDir(dirPath: string): Promise<void>; /** * 遍历目录下所有文件,支持glob模式和过滤参数 */ declare function findFiles(pattern: string | string[], options?: Options): Promise<string[]>; /** * 根据 include/exclude 参数筛选目标文件,所有路径以 CLI 执行目录为根目录 * @param include 需要包含的文件模式(glob数组或字符串) * @param exclude 需要排除的文件模式(glob数组或字符串) */ declare function findTargetFiles(include: string[], exclude?: string[]): Promise<string[]>; /** * 检查文件是否存在 * @param filePath 文件路径 */ declare function fileExists(filePath: string): boolean; /** * 同步读取 JSON 文件内容 * @param filePath JSON 文件路径 */ declare function readJsonSync(filePath: string): unknown; /** * 异步读取 JSON 文件内容 * @param filePath JSON 文件路径 * @param defaultValue 文件不存在时的默认值 */ declare function readJson<T = unknown>(filePath: string, defaultValue?: T): Promise<T>; /** * 异步写入 JSON 文件内容 * @param filePath JSON 文件路径 * @param data 要写入的数据 * @param pretty 是否格式化输出,默认为 true */ declare function writeJson(filePath: string, data: unknown, pretty?: boolean): Promise<void>; interface CLIWrapperOptions<T> { configRequired?: boolean; validator?: (config: I18nConfig) => void; preProcess?: (options: T) => T; } /** * CLI统一包裹函数 * 统一处理配置加载、验证、错误处理等逻辑 */ declare function withCLI<T extends Record<string, any>>(handler: (options: T, config: I18nConfig) => Promise<void>, wrapperOptions?: CLIWrapperOptions<T>): (options: T) => Promise<void>; export { ConfigManager, ConfigValidator, type I18nConfig, Logger, ensureDir, fileExists, findFiles, findTargetFiles, loadConfig, readFile, readJson, readJsonSync, withCLI, writeFile, writeFileWithTempDir, writeJson };