Skip to content

Commit

Permalink
Feat: 增加自动填充 key 以及目录 json 合并功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaoh committed May 24, 2024
1 parent ae0c5e8 commit e6f4e60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
37 changes: 30 additions & 7 deletions bin/xcmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @Author: HxB
* @Date: 2022-04-25 16:27:06
* @LastEditors: DoubleAm
* @LastEditTime: 2024-05-17 14:04:56
* @LastEditTime: 2024-05-24 10:26:54
* @Description: 命令处理文件
* @FilePath: \js-xcmd\bin\xcmd.js
*/
Expand Down Expand Up @@ -543,10 +543,10 @@ program
});

program
.option('check-i18n [dirPath] [depth] [isExited]', 'check-i18n [dirPath] [depth] [isExited]')
.command('check-i18n [dirPath] [depth] [isExited]')
.option('check-i18n [dirPath] [depth] [addKeys] [isExited]', 'check-i18n [dirPath] [depth] [addKeys] [isExited]')
.command('check-i18n [dirPath] [depth] [addKeys] [isExited]')
.description('检查 i18n 目录下的 json 文件 Key 是否有差异')
.action((dirPath = './src/locales/', depth = 0, isExited = false) => {
.action((dirPath = './src/locales/', depth = 0, addKeys = false, isExited = false) => {
console.log(`${dirPath} 目录检查中...`);
const i18nDirectory = getResolvePath(dirPath);
const files = getAllFilePath(i18nDirectory, depth == 'true' ? true : depth, ['json']);
Expand Down Expand Up @@ -587,6 +587,15 @@ program
isValid = false;
console.info(`\n\n${currentFile} 相比基准【${baseFile}】缺失 key: ${missingKeys.join(', ')}\n\n`);
}

if (addKeys && missingKeys.length) {
console.log(`正在补充 ${currentFile} 缺失的 key: ${missingKeys.join(', ')}`);
const obj = getJSONFileObj(currentFile);
missingKeys.forEach((key) => {
obj[key] = '';
});
setFileContent(currentFile, JSON.stringify(obj, null, 2));
}
}
}

Expand All @@ -600,7 +609,7 @@ program
setFileContent(jsonPath, result);
});
} else {
console.error('校验未通过,存在缺失的 key。');
console.info(addKeys ? '校验未通过,已补充缺失的 key,请前往修改。' : '校验未通过,存在缺失的 key。');
isExited && process.exit(1);
}
});
Expand All @@ -610,10 +619,24 @@ program
.command('merge-json [filePaths...]')
.description('合并指定 JSON 文件内容,并去重排序。')
.action((filePaths) => {
if (!filePaths || filePaths.length <= 1) {
console.error('请提供需要合并的 JSON 文件路径,至少 2 个!');
if (!filePaths) {
console.error('请提供需要合并的 JSON 文件路径,或者文件目录!');
return;
}
if (filePaths.length === 1) {
if (`${filePaths[0]}`.includes('json')) {
console.error('请提供至少 2 个 JSON 文件!');
return;
}
const i18nDirectory = getResolvePath(filePaths[0]);
const files = getAllFilePath(i18nDirectory, true, ['json']);
console.log(`${i18nDirectory} 目录检查完成...`, files);
if (files.length === 0) {
console.error(`${i18nDirectory} 目录下没有 json 文件`);
return;
}
filePaths = files;
}

const objArgs = filePaths.map((filePath) => {
filePath = getResolvePath(filePath);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-xcmd",
"version": "1.5.4",
"version": "1.5.5",
"description": "XCmd library for node.js.",
"main": "main.js",
"bin": {
Expand Down

0 comments on commit e6f4e60

Please sign in to comment.