Skip to content

Commit

Permalink
Docs: 1.5.1 增加 node 转换与 json 排序。
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaoh committed May 11, 2024
1 parent dae7890 commit 7b12df3
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const {
getFileExt,
getFileContent,
setFileContent,
cmd
getPath,
getFullPath,
cmd,
node2es6,
sortJSON
} = require('js-xcmd');
```

Expand Down
30 changes: 27 additions & 3 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-04-13 10:47:58
* @LastEditTime: 2024-05-11 18:47:15
* @Description: 命令处理文件
* @FilePath: \js-xcmd\bin\xcmd.js
*/
Expand All @@ -24,10 +24,12 @@ const {
renameDir,
renameFile,
rmRf,
emptyDir
emptyDir,
getFullPath
} = require('../utils/files');
const nodeCmd = require('node-cmd');
const { cmd } = require('../utils/cmd');
const { node2es6, sortJSON } = require('../utils/tools');
const nodeCmd = require('node-cmd');

// http://patorjk.com/software/taag/
const logo = () => {
Expand Down Expand Up @@ -487,4 +489,26 @@ program
});
});

program
.option('node2es6 <filePath> <outputPath>', 'node2es6 <filePath> <outputPath>')
.command('node2es6 <filePath> <outputPath>')
.description('将 CommonJS 文件转换为 ES6 模块')
.action((filePath, outputPath) => {
filePath = getFullPath(filePath); // 获取当前运行目录
const obj = require(filePath);
const result = node2es6(obj);
setFileContent(outputPath, result);
});

program
.option('sort-json <jsonPath> [outputPath]', 'sort-json <jsonPath> [outputPath]')
.command('sort-json <jsonPath> [outputPath]')
.action((jsonPath, outputPath) => {
outputPath = outputPath ? outputPath : jsonPath;
const jsonString = getFileContent(jsonPath);
const obj = JSON.parse(jsonString);
const result = sortJSON(obj);
setFileContent(outputPath, result);
});

program.parse(process.argv);
13 changes: 10 additions & 3 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-01-15 17:53:39
* @LastEditTime: 2024-05-11 18:48:09
* @Description: 导出一些方法,或许后面可以用到。
* @FilePath: \js-xcmd\main.js
*/
Expand All @@ -24,9 +24,12 @@ const {
getFileSize,
getFileExt,
getFileContent,
setFileContent
setFileContent,
getPath,
getFullPath
} = require('./utils/files');
const { cmd } = require('./utils/cmd');
const { node2es6, sortJSON } = require('./utils/tools');

module.exports = {
rmRf,
Expand All @@ -46,5 +49,9 @@ module.exports = {
getFileExt,
getFileContent,
setFileContent,
cmd
getPath,
getFullPath,
cmd,
node2es6,
sortJSON
};
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.0",
"version": "1.5.1",
"description": "XCmd library for node.js.",
"main": "main.js",
"bin": {
Expand Down
22 changes: 20 additions & 2 deletions utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: HxB
* @Date: 2022-04-25 17:49:14
* @LastEditors: DoubleAm
* @LastEditTime: 2024-01-15 17:51:49
* @LastEditTime: 2024-05-11 18:32:15
* @Description: 文件处理工具
* @FilePath: \js-xcmd\utils\files.js
*/
Expand Down Expand Up @@ -261,6 +261,22 @@ const setFileContent = (filePath, content) => {
fs.writeFileSync(filePath, content, 'utf-8');
};

/**
* 获取 path
* @param {*} filePath
*/
const getPath = (filePath) => {
return path.join(__dirname, filePath);
};

/**
* 获取当前目录相对 path 的完整路径
* @param {*} filePath
*/
const getFullPath = (filePath) => {
return path.join(process.cwd(), filePath);
};

module.exports = {
rmRf,
isDirExistResult,
Expand All @@ -278,5 +294,7 @@ module.exports = {
getFileSize,
getFileExt,
getFileContent,
setFileContent
setFileContent,
getPath,
getFullPath
};
76 changes: 76 additions & 0 deletions utils/tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* @Author: HxB
* @Date: 2024-05-11 17:59:32
* @LastEditors: DoubleAm
* @LastEditTime: 2024-05-11 18:47:05
* @Description: 转化 commonjs 为 es6 modules
* @FilePath: \js-xcmd\utils\tools.js
*/

const node2es6 = (transferObj) => {
console.log('---转换 Dev 开始---');

let es6ModuleContent = '\n/* eslint-disable */\n// @ts-nocheck\n';

Object.keys(transferObj).forEach((i) => {
const val = transferObj[i];
const type = typeof val;
if (val === undefined) {
return;
}
console.log(`(${type}) export const ${i}`);
es6ModuleContent += `\nexport const ${i} = ${
type === 'function' ? val.toString() : JSON.stringify(val, null, 2)
};\n`;
});

console.log('---转换 Dev 完成---');

return es6ModuleContent;
};

const sortJSON = (obj) => {
console.log('---JSON 排序开始---');

// 去重并获取对象的键数组
const keys = Object.keys(obj);

// 按键的首字符进行排序
const sortedKeys = keys.sort((a, b) => a[0].localeCompare(b[0]));

// 构建分类和排序后的对象
const sortedAndGroupedObject = sortedKeys.reduce((result, key) => {
const firstChar = key[0];
if (!result[firstChar]) {
result[firstChar] = [];
}
result[firstChar].push(key);
return result;
}, {});

// 在每个分类中按键的长度进行排序
for (const group in sortedAndGroupedObject) {
sortedAndGroupedObject[group].sort((a, b) => a.length - b.length);
}

// 拼接分类后的键数组
const finalKeys = Object.values(sortedAndGroupedObject).flat();

// 构建排序后的对象
const sortedObject = finalKeys.reduce((result, key) => {
result[key] = obj[key];
return result;
}, {});

// 转换为 JSON 字符串
const sortedJsonString = JSON.stringify(sortedObject, null, 2);

console.log('---JSON 排序完成---');

return sortedJsonString;
};

module.exports = {
node2es6,
sortJSON
};

0 comments on commit 7b12df3

Please sign in to comment.