Commit 2c1b30b5 authored by 马旭烽's avatar 马旭烽

feat: i18命令批处理

parent 225d13d6
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
"dev:comment": "启动开发环境", "dev:comment": "启动开发环境",
"importLen-dev": "ts-node-dev --respawn --transpile-only src/index.ts importLen -zh mock/input/zhCN/V1.ts -len mock/input/en/V1.ts -lenType enUs", "importLen-dev": "ts-node-dev --respawn --transpile-only src/index.ts importLen -zh mock/input/zhCN/V1.ts -len mock/input/en/V1.ts -lenType enUs",
"importLen": "ts-node src/index.ts importLen -zh mock/input/zhCN/V1.ts -len mock/input/en/V1.ts -lenType enUs", "importLen": "ts-node src/index.ts importLen -zh mock/input/zhCN/V1.ts -len mock/input/en/V1.ts -lenType enUs",
"listAdd": "ts-node src/index.ts list -a add -zh mock/input/zhCN/v2.ts" "listAdd": "ts-node src/index.ts list -a add -zh mock/input/zhCN/v2.ts",
"listRun": "ts-node src/index.ts list -a run ",
"importLenTs": "ts-node src/index.ts importLen -zh mock/input/zhCN/V1.ts --otherLenPath mock/input/en/V1.ts -lenType enUs"
}, },
"files": [ "files": [
"bin", "bin",
......
...@@ -51,3 +51,5 @@ export type SheettRowItemKey_V1 = '唯一标识'|'翻译字段'|'建议长度'; ...@@ -51,3 +51,5 @@ export type SheettRowItemKey_V1 = '唯一标识'|'翻译字段'|'建议长度';
export const PACKAGE_PATH = path.join(__dirname, '..', '..'); export const PACKAGE_PATH = path.join(__dirname, '..', '..');
/** 生成默认配置目录 */ /** 生成默认配置目录 */
export const I18_GEN_PACKAGE_PATH = path.join(PACKAGE_PATH, '.i18'); export const I18_GEN_PACKAGE_PATH = path.join(PACKAGE_PATH, '.i18');
/** 脚本路径 */
export const I18_SCRIPT_PATH = path.join(PACKAGE_PATH, 'src', 'index.ts');
\ No newline at end of file
import { program } from 'commander'; import { program, } from 'commander';
import { ImportModal } from './modal/ImportModal';
import { ExportModal } from './modal/ExportModal';
import { registerUseProgram } from './utils/commanderUtli'; import { registerUseProgram } from './utils/commanderUtli';
import { normalizeFilePath } from './utils/fileUtil'; import importLenCommand from './order/len';
import { getOutputFilePath } from './utils/pathUtil';
import { checkLenObjToNoAllowArrayV1, getLenObjToMetaLenObj, getLenRatio } from './helper/i18';
import listCommand from './order/list'; import listCommand from './order/list';
import { LanguageNameTypeEnum, } from './constants';
program program
.name('i18命令行工具') .name('i18命令行工具')
...@@ -14,107 +9,8 @@ program ...@@ -14,107 +9,8 @@ program
.usage('<command>[option]') .usage('<command>[option]')
.version('0.0.0'); .version('0.0.0');
const importModal = new ImportModal();
const exportModal = new ExportModal();
const newProgram = registerUseProgram<{}>(program, [ const newProgram = registerUseProgram<{}>(program, [
(program) => { importLenCommand,
return program
.command('importLen')
.description('导入i18语言包,导出xlsx包,语言包')
.option('-zh,--zhCNPath <zhCNPath>','导入中文包路径')
.option('-len, --otherLenPath <otherLenPath>', '导入其它包语言路径')
.option('-lenType, --otherLenType <otherLenType>', '导入其它语言类型')
.option('-lenRatio, --otherLenRatio [otherLenRatio]', '导入其它语言系数,可选')
.option('-d,--debug', '测试打印部分执行路径', false)
.action(async (args, _program) => {
let { zhCNPath, otherLenPath, otherLenType, otherlenRdio: otherLenRdio, debug } = args;
debug && console.log('命令行打印参数', args)
//check is Allow Or Return
if(!(zhCNPath && otherLenPath&&otherLenType)){
throw new TypeError('导入参数缺失');
};
zhCNPath = await normalizeFilePath(zhCNPath);
otherLenPath = await normalizeFilePath(otherLenPath);
if(debug){
console.log('中文路径', zhCNPath , );
console.log('其它语言路径', otherLenPath,);
console.log('其它语言类型', otherLenPath);
}
const zhCNMap = await importModal.importFileToJSObj_V1(zhCNPath);
const otherLenMap = await importModal.importFileToJSObj_V1(otherLenPath);
const loseKeys = Object.keys(zhCNMap).filter(key => !otherLenMap[key]);
console.log('语言包缺失内容关键地图',loseKeys);
if(debug){
console.log('对应的中文包解析', zhCNMap, typeof zhCNMap);
console.log('对应其它语言解析', otherLenMap);
console.log(otherLenPath, otherLenType, otherLenRdio)
}
const zhCNRatio = getLenRatio(LanguageNameTypeEnum.zhCN);
otherLenRdio = otherLenRdio || getLenRatio(otherLenType, otherLenRdio);
if(debug){
console.log('中文长度系数', zhCNRatio);
console.log('其它语言长度系数', otherLenRdio)
}
const zhCNMetaMap = getLenObjToMetaLenObj(zhCNMap, zhCNRatio);
const otherLenMetaMap = getLenObjToMetaLenObj(otherLenMap, otherLenRdio, zhCNMap);
if(debug){
console.log('中文元数据', zhCNMetaMap);
console.log('其它元数据', otherLenMetaMap);
}
const generateZhCNJsonFilePath = await getOutputFilePath(`${'zhCN'}.json`);
const generateXlsxFilePath = await getOutputFilePath(`${otherLenType}.xlsx`);
const generateJsonFilePath = await getOutputFilePath(`${otherLenType}.json`);
const checkNoAlowArray = checkLenObjToNoAllowArrayV1(otherLenMetaMap);
console.log(['生成文件路径', generateXlsxFilePath, generateJsonFilePath].join('\n'));
console.log('不达标长度', checkNoAlowArray);
await Promise.all([
exportModal.exportJSObjToJSONV1(zhCNMap, generateZhCNJsonFilePath),
// 其它语言包
exportModal.epportJSObjToXlsxV1(otherLenMetaMap, generateXlsxFilePath),
exportModal.exportJSObjToJSONV1(otherLenMap, generateJsonFilePath),
]);
if(loseKeys.length > 1) {
const outputPath = await getOutputFilePath('缺少键值.json');
await exportModal.exportJSObjToJSONV1(
{loseKeys}
,
outputPath
);
}
if(checkNoAlowArray.length > 0) {
const outputPath = await getOutputFilePath(`检验不通过.json`);
await exportModal.exportJSObjToJSONV1(
{checkLenObjToNoAllowArrayV1}
,
outputPath
);
};
console.log('执行结束');
});
},
listCommand, listCommand,
], {}); ], {});
......
import path from 'path'; import { listWork } from '../work/list';
import childProcess from 'child_process';
import {
LanguageNameTypeEnum,
} from '../constants';
import { ListManagerModal, CommandOption } from '../modal/ListManagerModal';
import type { useProgramType } from '../utils/commanderUtli'; import type { useProgramType } from '../utils/commanderUtli';
enum ActionType {
see = 'see',
add = 'add',
remove = 'remove',
run = 'run',
}
const checkIsActionType = (action: unknown): action is ActionType => {
return [
ActionType.see,
ActionType.add,
ActionType.remove,
ActionType.run,
].includes(action as ActionType);
};
const listMandagerModal = new ListManagerModal();
/** /**
* 管理输入输出命令 * 管理输入输出命令
...@@ -39,35 +16,7 @@ const listCommand: useProgramType<any> = (program) => { ...@@ -39,35 +16,7 @@ const listCommand: useProgramType<any> = (program) => {
.action(async (args ) => { .action(async (args ) => {
const { action, zhCNPath } = args; const { action, zhCNPath } = args;
const isActionType = checkIsActionType(action); await listWork({ action, zhCNPath })
const iszhCNPath = !!zhCNPath;
const isAllowed = isActionType && iszhCNPath;
if (!isAllowed) {
throw new TypeError('命令参数错误', );
}
switch (action) {
case ActionType.add:
const dirPath = path.dirname(zhCNPath);
const genInputPath = (lenType: LanguageNameTypeEnum) => path.join(dirPath, '..',lenType);
const lenTypes = [ LanguageNameTypeEnum.enUS, LanguageNameTypeEnum.koKR ];
const lenTypeCommands: CommandOption[] = lenTypes.map(lenType => ({
zhCNPath,
otherLenPath: genInputPath(lenType),
lenType,
}))
await listMandagerModal.addItems(lenTypeCommands)
break;
case ActionType.run:
const { list } = await listMandagerModal.readContent();
list.forEach(item => {
childProcess.spawn('node', [''])
})
}
}) })
}; };
......
import path from 'path';
import {
LanguageNameTypeEnum,
} from '../constants';
import { ListManagerModal, CommandOption } from '../modal/ListManagerModal';
import { importLenWrok } from './len';
export enum ActionType {
see = 'see',
add = 'add',
remove = 'remove',
run = 'run',
}
interface AgrementsType {
action: ActionType,
zhCNPath: string,
}
const checkIsActionType = (action: unknown): action is ActionType => {
return [
ActionType.see,
ActionType.add,
ActionType.remove,
ActionType.run,
].includes(action as ActionType);
};
const listMandagerModal = new ListManagerModal();
export const listWork = async (args: AgrementsType) => {
const { action, zhCNPath } = args;
const isActionType = checkIsActionType(action);
const iszhCNPath = ActionType.add !== action || !!zhCNPath;
const isAllowed = isActionType && iszhCNPath;
if (!isAllowed) {
throw new TypeError('命令参数错误' + action + '|' + zhCNPath, );
}
switch (action) {
case ActionType.add:
const dirPath = path.dirname(zhCNPath);
const fileName = path.basename(zhCNPath);
const genInputPath = (lenType: LanguageNameTypeEnum) => path.join(dirPath, '..', lenType, fileName);
const lenTypes = [ LanguageNameTypeEnum.enUS, LanguageNameTypeEnum.koKR ];
const lenTypeCommands: CommandOption[] = lenTypes.map(lenType => ({
zhCNPath,
otherLenPath: genInputPath(lenType),
lenType,
}))
await listMandagerModal.addItems(lenTypeCommands)
break;
case ActionType.run:
const { list } = await listMandagerModal.readContent();
console.log('准备执行清单')
for await (const item of list) {
importLenWrok({
zhCNPath: item.zhCNPath,
otherLenPath: item.otherLenPath,
otherLenType: item.lenType,
debug: true,
})
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment