Skip to content

Commit

Permalink
Feat: 增加 check-i18n 方法,优化 sort-json。
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaoh committed May 16, 2024
1 parent 4949f88 commit 7d5d807
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 20 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ npm i js-xcmd -g
## Use

```bash
xcmd -h
xcmd -h # 查看帮助说明

# 代码使用
const {
rmRf,
isDirExistResult,
Expand All @@ -27,10 +28,13 @@ const {
getFileName,
getFileSize,
getFileExt,
getFileContent,
setFileContent,
getFileContent,
getJSONFileObj,
getPath,
getFullPath,
getResolvePath,
getAllFilePath,
cmd,
node2es6,
sortJSON
Expand All @@ -50,6 +54,6 @@ npm run i:local

## Others

* [Issue](https://github.com/pandaoh/js-xcmd/issues)
* [Pull Request](https://github.com/pandaoh/js-xcmd/pulls)
* [Issue](https://github.com/biugle/js-xcmd/issues)
* [Pull Request](https://github.com/biugle/js-xcmd/pulls)
* [hxbpandaoh@163.com](mailto:hxbpandaoh@163.com)
80 changes: 72 additions & 8 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-13 17:11:47
* @LastEditTime: 2024-05-16 17:26:23
* @Description: 命令处理文件
* @FilePath: \js-xcmd\bin\xcmd.js
*/
Expand All @@ -15,8 +15,9 @@ const pkg = require('../package.json');
const {
copyDir,
copyFile,
getFileContent,
setFileContent,
getFileContent,
getJSONFileObj,
deleteDir,
deleteFile,
addDir,
Expand All @@ -25,7 +26,9 @@ const {
renameFile,
rmRf,
emptyDir,
getFullPath
getFullPath,
getResolvePath,
getAllFilePath
} = require('../utils/files');
const { cmd } = require('../utils/cmd');
const { node2es6, sortJSON } = require('../utils/tools');
Expand Down Expand Up @@ -86,7 +89,7 @@ program
.action((dir) => {
console.log('----------Clone Template----------');
console.log(logLogo(logo));
download('github:pandaoh/vue-admin', !dir ? 'vue-admin' : dir, function (err) {
download('github:biugle/vue-admin', !dir ? 'vue-admin' : dir, function (err) {
console.log(err ? err : '----------Successful----------');
});
});
Expand Down Expand Up @@ -276,8 +279,7 @@ program
}
console.log('----------Updating----------');
const packageFilePath = filePath || './package.json';
const packageData = getFileContent(packageFilePath);
const packageJson = JSON.parse(packageData);
const packageJson = getJSONFileObj(packageFilePath);

packageJson.time = getTimeCode();
console.log({ time: packageJson.time });
Expand Down Expand Up @@ -535,10 +537,72 @@ program
.description('将指定 json 文件排序去重并输出')
.action((jsonPath, outputPath) => {
outputPath = outputPath ? outputPath : jsonPath;
const jsonString = getFileContent(jsonPath);
const obj = JSON.parse(jsonString);
const obj = getJSONFileObj(jsonPath);
const result = sortJSON(obj);
setFileContent(outputPath, result);
});

program
.option('check-i18n [dirPath] [depth] [isExited]', 'check-i18n [dirPath] [depth] [isExited]')
.command('check-i18n [dirPath] [depth] [isExited]')
.description('检查 i18n 目录下的 json 文件 Key 是否有差异')
.action((dirPath = './src/locales/', depth = 0, isExited = false) => {
console.log(`${dirPath} 目录检查中...`);
const i18nDirectory = getResolvePath(dirPath);
const files = getAllFilePath(i18nDirectory, depth == 'true' ? true : depth, ['json']);
console.log(`${i18nDirectory} 目录检查完成...`, files);
if (files.length === 0) {
console.error(`${i18nDirectory} 目录下没有 json 文件`);
return;
}

const getKeys = (filePath) => {
const jsonContent = getJSONFileObj(filePath);
return Object.keys(jsonContent);
};

let baseFile = files[0];
let baseKeys = getKeys(baseFile);

for (let i = 1; i < files.length; i++) {
const currentFile = files[i];
const currentKeys = getKeys(currentFile);

if (currentKeys.length > baseKeys.length) {
baseFile = currentFile;
baseKeys = currentKeys;
}
}

let isValid = true;

for (let i = 0; i < files.length; i++) {
const currentFile = files[i];

if (currentFile !== baseFile) {
const currentKeys = getKeys(currentFile);
const missingKeys = baseKeys.filter((key) => !currentKeys.includes(key));

if (missingKeys.length > 0) {
isValid = false;
console.info(`\n\n${currentFile} 相比基准【${baseFile}】缺失 key: ${missingKeys.join(', ')}\n\n`);
}
}
}

if (isValid) {
console.log('所有文件 key 一致,通过校验!');

files.forEach((jsonPath) => {
console.log(`正在排序 ${jsonPath}`);
const obj = getJSONFileObj(jsonPath);
const result = sortJSON(obj);
setFileContent(jsonPath, result);
});
} else {
console.error('校验未通过,存在缺失的 key。');
isExited && process.exit(1);
}
});

program.parse(process.argv);
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-xcmd",
"version": "1.5.2",
"version": "1.5.3",
"description": "XCmd library for node.js.",
"main": "main.js",
"bin": {
Expand All @@ -14,7 +14,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/pandaoh/js-xcmd.git"
"url": "git+https://github.com/biugle/js-xcmd.git"
},
"keywords": [
"cmd",
Expand All @@ -26,9 +26,9 @@
"blog": "http://a.biugle.cn",
"license": "ISC",
"bugs": {
"url": "https://github.com/pandaoh/js-xcmd/issues"
"url": "https://github.com/biugle/js-xcmd/issues"
},
"homepage": "https://github.com/pandaoh/js-xcmd#readme",
"homepage": "https://github.com/biugle/js-xcmd#readme",
"dependencies": {
"commander": "^9.2.0",
"download-git-repo": "^3.0.2",
Expand Down
63 changes: 61 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-05-11 18:32:15
* @LastEditTime: 2024-05-16 16:48:18
* @Description: 文件处理工具
* @FilePath: \js-xcmd\utils\files.js
*/
Expand Down Expand Up @@ -252,6 +252,21 @@ const getFileContent = (filePath) => {
return content;
};

/**
* 获取 JSON 文件内容对象
* @param {*} filePath
* @returns
*/
const getJSONFileObj = (filePath) => {
try {
const fileContent = getFileContent(filePath);
return JSON.parse(fileContent);
} catch (e) {
console.log({ getJSONFileObjError: e, filePath });
return null;
}
};

/**
* 修改文件内容
* @param {*} filePath
Expand All @@ -277,6 +292,47 @@ const getFullPath = (filePath) => {
return path.join(process.cwd(), filePath);
};

/**
* 处理绝对路径
* @param {*} filePath
*/
const getResolvePath = (filePath) => {
return path.resolve(filePath);
};

/**
* 获取指定目录下的文件,`depth = true` 查所有。
* @param {*} directory
* @param {*} depth
* @param {*} fileExtensions
* @returns
*/
function getAllFilePath(directory, depth = 0, fileExtensions = []) {
const files = [];

function traverseDirectory(currentDir, currentDepth) {
const entries = fs.readdirSync(currentDir, { withFileTypes: true });

entries.forEach((entry) => {
const fullPath = path.join(currentDir, entry.name);

if (entry.isDirectory()) {
if (depth === true || currentDepth < depth) {
traverseDirectory(fullPath, currentDepth + 1);
}
} else {
if (fileExtensions.length === 0 || fileExtensions.some((i) => path.extname(fullPath).includes(i))) {
files.push(fullPath);
}
}
});
}

traverseDirectory(directory, 0);

return files;
}

module.exports = {
rmRf,
isDirExistResult,
Expand All @@ -295,6 +351,9 @@ module.exports = {
getFileExt,
getFileContent,
setFileContent,
getJSONFileObj,
getPath,
getFullPath
getFullPath,
getResolvePath,
getAllFilePath
};
4 changes: 2 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-11 18:47:05
* @LastEditTime: 2024-05-16 17:28:34
* @Description: 转化 commonjs 为 es6 modules
* @FilePath: \js-xcmd\utils\tools.js
*/
Expand Down Expand Up @@ -50,7 +50,7 @@ const sortJSON = (obj) => {

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

// 拼接分类后的键数组
Expand Down

0 comments on commit 7d5d807

Please sign in to comment.