Commit 058def7e authored by 马旭烽's avatar 马旭烽

feat: 添加 list 命令 (缓存命令功能)

parent 3663c23e
......@@ -8,7 +8,8 @@
"test": "ts-node bin/i18-cli.ts",
"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": "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"
},
"files": [
"bin",
......
import path from 'path';
export enum LanguageNameTypeEnum {
/** 中文 */
......@@ -44,4 +45,9 @@ export interface CenterLanguageItemTypeV1 {
[key: string]: string,
}
export type SheettRowItemKey_V1 = '唯一标识'|'翻译字段'|'建议长度';
\ No newline at end of file
export type SheettRowItemKey_V1 = '唯一标识'|'翻译字段'|'建议长度';
/** 包路径 */
export const PACKAGE_PATH = path.join(__dirname, '..', '..');
/** 生成默认配置目录 */
export const I18_GEN_PACKAGE_PATH = path.join(PACKAGE_PATH, '.i18');
\ No newline at end of file
......@@ -5,6 +5,7 @@ import { registerUseProgram } from './utils/commanderUtli';
import { normalizeFilePath } from './utils/fileUtil';
import { getOutputFilePath } from './utils/pathUtil';
import { checkLenObjToNoAllowArrayV1, getLenObjToMetaLenObj, getLenRatio } from './helper/i18';
import listCommand from './order/list';
import { LanguageNameTypeEnum, } from './constants';
program
......@@ -114,6 +115,7 @@ const newProgram = registerUseProgram<{}>(program, [
console.log('执行结束');
});
},
listCommand,
], {});
newProgram.parse(process.argv);
\ No newline at end of file
import path from 'path';
import fp from 'fs/promises';
import { I18_GEN_PACKAGE_PATH } from '../constants'
enum ClassStatus {
uninit = 0,
init = 1,
}
export interface CommandOption {
/** 中文导入路径 */
zhCNPath: string
/** 其它语言导入路径 */
otherLenPath: string
/** 其它导入语言类型 */
lenType: string
/** lenRatio */
lenRatio?: string;
}
interface ManagerJson {
list: CommandOption[]
}
const initJson: ManagerJson = {
list: [],
}
const FILE_PATH = path.join(I18_GEN_PACKAGE_PATH, 'list.manager.json');
const getKey = (item: CommandOption) => item.zhCNPath + item.otherLenPath + item.lenType
const getKeys = (items: CommandOption[]) => items.map(getKey);
export class ListManagerModal {
status: ClassStatus
constructor(){
this.status = ClassStatus.uninit;
}
async checkIsInit() {
try {
await fp.stat(FILE_PATH)
this.status = ClassStatus.init;
return true;
} catch (error) {
return false
}
}
async generateInitFile() {
return await fp.writeFile(FILE_PATH, JSON.stringify(initJson, null , 2));
}
async init() {
const isInit = await this.checkIsInit();
if (!isInit) {
await this.generateInitFile();
};
}
async readContent(): Promise<ManagerJson> {
if (ClassStatus.uninit == this.status) {
await this.init();
}
const content = await fp.readFile(FILE_PATH, { encoding: 'utf8' });
const format = (content: string) => JSON.parse(content);
return format(content);
}
private writeContent(data: ManagerJson) {
return fp.writeFile(FILE_PATH, JSON.stringify(data, null, 2));
}
async addItems (items: CommandOption[]) {
const originData = await this.readContent();
const newData = { ...originData };
newData.list = originData.list.concat(...items);
await this.writeContent(newData);
return this;
}
async removeItems (items: CommandOption[]) {
const filterKeys = getKeys(items);
const originData = await this.readContent();
const newData = { ...originData };
newData.list = originData.list.filter((originItem) => !filterKeys.includes(getKey(originItem)));
await this.writeContent(newData);
return this;
}
}
\ No newline at end of file
import path from 'path';
import childProcess from 'child_process';
import {
LanguageNameTypeEnum,
} from '../constants';
import { ListManagerModal, CommandOption } from '../modal/ListManagerModal';
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();
/**
* 管理输入输出命令
* @param program
* @returns
*/
const listCommand: useProgramType<any> = (program) => {
return program
.command('list',)
.option('-a,--action <action>', '设置输入输出批操作 see/add/remove/run')
.option('-zh,--zhCNPath <zhCNPath>', '设置中文路径')
.description('设置语言路径')
.action(async (args ) => {
const { action, zhCNPath } = args;
const isActionType = checkIsActionType(action);
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', [''])
})
}
})
};
export default listCommand;
\ 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