Skip to content

Commit

Permalink
Feat: 增加合并 json 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaoh committed May 17, 2024
1 parent 7d5d807 commit ae0c5e8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
29 changes: 27 additions & 2 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-16 17:26:23
* @LastEditTime: 2024-05-17 14:04:56
* @Description: 命令处理文件
* @FilePath: \js-xcmd\bin\xcmd.js
*/
Expand All @@ -31,7 +31,7 @@ const {
getAllFilePath
} = require('../utils/files');
const { cmd } = require('../utils/cmd');
const { node2es6, sortJSON } = require('../utils/tools');
const { node2es6, sortJSON, mergeObj } = require('../utils/tools');
const nodeCmd = require('node-cmd');

// http://patorjk.com/software/taag/
Expand Down Expand Up @@ -605,4 +605,29 @@ program
}
});

program
.option('merge-json [filePaths...]', 'merge-json [filePaths...]')
.command('merge-json [filePaths...]')
.description('合并指定 JSON 文件内容,并去重排序。')
.action((filePaths) => {
if (!filePaths || filePaths.length <= 1) {
console.error('请提供需要合并的 JSON 文件路径,至少 2 个!');
return;
}

const objArgs = filePaths.map((filePath) => {
filePath = getResolvePath(filePath);
return getJSONFileObj(filePath);
});

const mergedData = mergeObj(...objArgs);
const mergedFilePath = getFullPath('./xcmd-auto-merge.json');

console.log(`正在排序 ${mergedFilePath}`);
const result = sortJSON(mergedData);
setFileContent(mergedFilePath, result);

console.log(`JSON 文件合并完成,并保存为 【${mergedFilePath}】。`);
});

program.parse(process.argv);
5 changes: 3 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: HxB
* @Date: 2022-04-28 13:54:21
* @LastEditors: DoubleAm
* @LastEditTime: 2024-05-11 18:48:09
* @LastEditTime: 2024-05-17 13:53:52
* @Description: 导出一些方法,或许后面可以用到。
* @FilePath: \js-xcmd\main.js
*/
Expand Down Expand Up @@ -53,5 +53,6 @@ module.exports = {
getFullPath,
cmd,
node2es6,
sortJSON
sortJSON,
mergeObj
};
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.3",
"version": "1.5.4",
"description": "XCmd library for node.js.",
"main": "main.js",
"bin": {
Expand Down
9 changes: 7 additions & 2 deletions utils/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: HxB
* @Date: 2024-05-11 17:59:32
* @LastEditors: DoubleAm
* @LastEditTime: 2024-05-16 17:28:34
* @LastEditTime: 2024-05-17 13:52:49
* @Description: 转化 commonjs 为 es6 modules
* @FilePath: \js-xcmd\utils\tools.js
*/
Expand Down Expand Up @@ -70,7 +70,12 @@ const sortJSON = (obj) => {
return sortedJsonString;
};

const mergeObj = (...args) => {
return Object.assign({}, ...[...args].map((i) => (i ? i : {})));
};

module.exports = {
node2es6,
sortJSON
sortJSON,
mergeObj
};

0 comments on commit ae0c5e8

Please sign in to comment.