Skip to content

Commit

Permalink
Feat: v1.5.8 update-v
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaoh committed Jun 26, 2024
1 parent 278b73d commit 2d7070b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ const {
getFileName,
getFileSize,
getFileExt,
setFileContent,
getFileContent,
setFileContent,
getJSONFileObj,
getPath,
getFullPath,
getResolvePath,
getAllFilePath,
cmd,
node2es6,
sortJSON
sortJSON,
mergeObj,
versionUpgrade
} = require('js-xcmd');
```

Expand Down
21 changes: 19 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-06-20 16:49:52
* @LastEditTime: 2024-06-26 09:43:35
* @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, mergeObj } = require('../utils/tools');
const { node2es6, sortJSON, mergeObj, versionUpgrade } = require('../utils/tools');
const nodeCmd = require('node-cmd');

// http://patorjk.com/software/taag/
Expand Down Expand Up @@ -265,6 +265,23 @@ program
console.log('----------Successful----------');
});

program
.option('update-v [filePath]', 'update package.json version [filePath]')
.command('update-v [filePath]')
.description('更新 json 中的 version 字段')
.action((filePath) => {
console.log('----------Updating----------');
const packageFilePath = filePath || './package.json';
const packageJson = getJSONFileObj(packageFilePath);

console.log('old version: ' + packageJson.version);
packageJson.version = versionUpgrade(packageJson.version);
console.log('new version: ' + packageJson.version);
setFileContent(packageFilePath, JSON.stringify(packageJson, '', 2));

console.log('----------Successful----------');
});

program
.option('update-time [filePath]', 'update package.json time [filePath]')
.command('update-time [filePath]')
Expand Down
15 changes: 11 additions & 4 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-17 13:53:52
* @LastEditTime: 2024-06-26 09:48:50
* @Description: 导出一些方法,或许后面可以用到。
* @FilePath: \js-xcmd\main.js
*/
Expand All @@ -25,11 +25,14 @@ const {
getFileExt,
getFileContent,
setFileContent,
getJSONFileObj,
getPath,
getFullPath
getFullPath,
getResolvePath,
getAllFilePath
} = require('./utils/files');
const { cmd } = require('./utils/cmd');
const { node2es6, sortJSON } = require('./utils/tools');
const { node2es6, sortJSON, mergeObj, versionUpgrade } = require('./utils/tools');

module.exports = {
rmRf,
Expand All @@ -49,10 +52,14 @@ module.exports = {
getFileExt,
getFileContent,
setFileContent,
getJSONFileObj,
getPath,
getFullPath,
getResolvePath,
getAllFilePath,
cmd,
node2es6,
sortJSON,
mergeObj
mergeObj,
versionUpgrade
};
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.7",
"version": "1.5.8",
"description": "XCmd library for node.js.",
"main": "main.js",
"bin": {
Expand Down
40 changes: 38 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-17 13:52:49
* @LastEditTime: 2024-06-26 09:42:31
* @Description: 转化 commonjs 为 es6 modules
* @FilePath: \js-xcmd\utils\tools.js
*/
Expand Down Expand Up @@ -74,8 +74,44 @@ const mergeObj = (...args) => {
return Object.assign({}, ...[...args].map((i) => (i ? i : {})));
};

/**
* 版本号升级算法
* @example
* versionUpgrade('0.0.1'); /// '0.0.2'
* versionUpgrade('0.0.0.9'); /// '0.0.0.10'
* versionUpgrade('0.0.0.9', 9); /// '0.0.1.0'
* versionUpgrade('0.0.9.9', 9); /// '0.1.0.0'
* @param version 版本号
* @param maxVersionCode 最大版本号
* @returns
*/
const versionUpgrade = (version, maxVersionCode = 99) => {
if (maxVersionCode == 0) {
maxVersionCode = 99;
}
let tempVersionArr = version.split('.').map((v) => Number(v));
const nan = tempVersionArr.some((v) => isNaN(v));
if (nan) {
return version;
}
tempVersionArr = tempVersionArr.reverse();
let check = true;
tempVersionArr.forEach((v, i) => {
if (check) {
if (v >= maxVersionCode) {
tempVersionArr[i] = 0;
} else {
check = false;
tempVersionArr[i] = tempVersionArr[i] + 1;
}
}
});
return tempVersionArr.reverse().join('.');
};

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

0 comments on commit 2d7070b

Please sign in to comment.