#!/usr/bin/env node import commander from 'commander'; import IntlHelper from './index'; import appConfig from '../package.json'; const { Command } = commander; const program = new Command(); program.version(appConfig.version || '0.0.0', '-v, --version', '输出版本号'); program .command('extract ') .option('-p, --pattern ', 'find glob pattern') .option('-o, --output ', 'output file') .action((targetDir: string, { output, pattern }) => { try { const app = new IntlHelper(process.cwd()); app.extract(targetDir, output, pattern); } catch (error) { // eslint-disable-next-line no-console console.error('[runner] 提取内容失败:', error); } }); program .command('inject ') .option('-p, --pattern ', 'find glob pattern') .option('-f, --file ', 'locale file', './locales/zh-CN.js') .option('-o, --output ', 'output dir') .action((targetDir, { file, output, pattern }) => { try { const app = new IntlHelper(process.cwd()); app.inject(targetDir, file, output, pattern); } catch (error) { // eslint-disable-next-line no-console console.error('[runner] 内容替换失败:', error); } }); program .command('translate ') .option('-o, --output ', 'output path') .option('--appid ', '翻译 api appId') .option('--key ', '翻译 api appKey') .option('--engine ', '翻译引擎类型') .action((localeFile, { output, appid, key, engine }) => { try { if (!appid || !key) { throw Error('请传入 --appid 和 --key 参数'); } const app = new IntlHelper(process.cwd(), { baiduAppId: appid, baiduAppKey: key, engine, }); app.translate(localeFile, output); } catch (error) { // eslint-disable-next-line no-console console.error('[runner] 内容翻译失败:', error); } }); program .command('danger ') .option('-p, --pattern ', 'find glob pattern') .option('-o, --output ', 'output dir', './output') .option('--appid ', '翻译 api appId') .option('--key ', '翻译 api appKey') .option('--engine ', '翻译引擎类型') .action((targetDir, { output, appid, key, pattern, engine }) => { try { if (!appid || !key) { throw Error('请传入 --appid 和 --key 参数'); } const app = new IntlHelper(process.cwd(), { baiduAppId: appid, baiduAppKey: key, engine, }); app.replaceContent(targetDir, output, pattern); } catch (error) { // eslint-disable-next-line no-console console.error('[runner] 内容翻译失败:', error); } }); // TODO: // 待翻译内容地图(可以指定展示层级,类似 tree -L 3) // 加载已有翻译内容(递归分析合并的配置,将 ... 扩展运算符的内容一起合并) // 为已有的翻译内容增加 defaultMessage // 为英文内容增加中文注释 // 增加只是翻译 git staged 范围的文件 // 增加测试用例 // TODO:BUG // 忽略 id 自动包含中文的内容 // 翻译时跳过已有的翻译内容,只翻译新增 program.parse(process.argv);