Skip to content

Commit

Permalink
Feat: update func with Docs 1.4.8
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaoh committed Jan 15, 2024
1 parent ab12a95 commit 51ad615
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 62 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ npm i js-xcmd -g
xcmd -h

const {
rmRf,
isDirExistResult,
isDirExist,
emptyDir,
copyDir,
copyFile,
deleteDir,
Expand All @@ -38,6 +40,8 @@ npm link
npm run bind
npm unlink
npm run unbind

npm run i:local
```

## Others
Expand Down
34 changes: 24 additions & 10 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: 2023-12-29 14:42:16
* @LastEditTime: 2024-01-15 18:02:47
* @Description: 命令处理文件
* @FilePath: \js-xcmd\bin\xcmd.js
*/
Expand Down Expand Up @@ -137,7 +137,7 @@ program
.action((dirSrc, dirTarget) => {
console.log('----------Copying----------');
console.log({ dirSrc, dirTarget });
copyDir(null, dirSrc, dirTarget);
copyDir(dirSrc, dirTarget);
console.log('----------Successful----------');
});

Expand All @@ -164,6 +164,8 @@ program
program
.option('rm-rf <path>', 'rm rf')
.command('rm-rf <path>')
// .option('rmrf <path>', 'rmrf')
// .command('rmrf <path>')
.action((path) => {
console.log('----------RimRaf----------');
console.log({ path });
Expand Down Expand Up @@ -244,15 +246,25 @@ program
});

program
.option('update-time', 'update package.json time')
.command('update-time')
.action(() => {
.option('update-time [filePath]', 'update package.json time [filePath]')
.command('update-time [filePath]')
// .option('time [filePath]', 'update package.json time [filePath]')
// .command('time [filePath]')
.action((filePath) => {
try {
console.log(new Date().toLocaleString());
} catch (e) {
console.log('IOS', e);
}
console.log('----------Updating----------');
let pkgVal = JSON.parse(getFileContent('./package.json'));
// console.log(pkgVal);
pkgVal['time'] = getTimeCode();
console.log(pkgVal['time']);
setFileContent('./package.json', JSON.stringify(pkgVal, '', 2));
const packageFilePath = filePath || './package.json';
const packageData = getFileContent(packageFilePath);
const packageJson = JSON.parse(packageData);

packageJson.time = getTimeCode();
console.log({ time: packageJson.time });
setFileContent(packageFilePath, JSON.stringify(packageJson, '', 2));

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

Expand Down Expand Up @@ -343,6 +355,8 @@ program
program
.option('list [global]', 'list [global]')
.command('list [global]')
// .option('ls [global]', 'ls [global]')
// .command('ls [global]')
.action((global) => {
let cmdStr = '';
if (global) {
Expand Down
6 changes: 5 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
* @Author: HxB
* @Date: 2022-04-28 13:54:21
* @LastEditors: DoubleAm
* @LastEditTime: 2022-08-03 18:48:38
* @LastEditTime: 2024-01-15 17:53:39
* @Description: 导出一些方法,或许后面可以用到。
* @FilePath: \js-xcmd\main.js
*/

const {
rmRf,
isDirExistResult,
isDirExist,
emptyDir,
copyDir,
copyFile,
deleteDir,
Expand All @@ -27,8 +29,10 @@ const {
const { cmd } = require('./utils/cmd');

module.exports = {
rmRf,
isDirExistResult,
isDirExist,
emptyDir,
copyDir,
copyFile,
deleteDir,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-xcmd",
"version": "1.4.6",
"version": "1.4.8",
"description": "XCmd library for node.js.",
"main": "main.js",
"bin": {
Expand Down Expand Up @@ -36,5 +36,5 @@
"node-cmd": "^5.0.0",
"rimraf": "^5.0.5"
},
"time": "0518011528042022"
"time": "2717021815012024"
}
118 changes: 69 additions & 49 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: 2023-12-29 14:03:06
* @LastEditTime: 2024-01-15 17:51:49
* @Description: 文件处理工具
* @FilePath: \js-xcmd\utils\files.js
*/
Expand All @@ -13,25 +13,25 @@ const fsExtra = require('fs-extra');

/**
* 判断目录是否存在,返回结果。(boolean)
* @param {*} path
* @param {*} dirPath
* @returns
*/
const isDirExistResult = (path) => {
return fs.existsSync(path);
const isDirExistResult = (dirPath) => {
return fs.existsSync(dirPath);
};

/**
* 判断目录是否存在,不存在则创建目录。
*/
const isDirExist = (path) => {
// fs.access(path, function (err) {
const isDirExist = (dirPath) => {
// fs.access(dirPath, function (err) {
// if (err) {
// // 目录不存在时创建目录
// fs.mkdirSync(path);
// fs.mkdirSync(dirPath);
// }
// });
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
};

Expand All @@ -40,37 +40,22 @@ const isDirExist = (path) => {
* @param src {String} 要复制的目录
* @param target {String} 复制到目标目录
*/
const copyDir = (err, src, target) => {
if (err) {
console.log({ 'copyDir error': err });
return;
const copyDir = (src, target) => {
if (!fs.existsSync(target)) {
fs.mkdirSync(target, { recursive: true });
}

fs.readdir(src, function (err, paths) {
if (err) {
console.log({ 'copyDir error': err });
return;
}
const paths = fs.readdirSync(src);
paths.forEach((filename) => {
const srcPath = path.join(src, filename);
const targetPath = path.join(target, filename);
const stat = fs.statSync(srcPath);

paths.forEach(function (path) {
let _src = src + '/' + path;
let _target = target + '/' + path;
fs.stat(_src, function (err, stat) {
if (err) {
console.log({ 'copyDir error': err });
return;
}

// 判断是文件还是目录
if (stat.isFile()) {
fs.writeFileSync(_target, fs.readFileSync(_src));
} else if (stat.isDirectory()) {
// 当是目录是,递归复制。
isDirExist(_target);
copyDir(null, _src, _target);
}
});
});
if (stat.isFile()) {
fs.copyFileSync(srcPath, targetPath);
} else if (stat.isDirectory()) {
copyDir(srcPath, targetPath);
}
});
};

Expand Down Expand Up @@ -98,12 +83,12 @@ const copyFile = (src, target) => {

/**
* 删除文件夹
* @param {*} path
* @param {*} dirPath
*/
const deleteDir = (path) => {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file) {
var curPath = path + '/' + file;
const deleteDir = (dirPath) => {
if (fs.existsSync(dirPath)) {
fs.readdirSync(dirPath).forEach(function (file) {
let curPath = dirPath + '/' + file;
if (fs.statSync(curPath).isDirectory()) {
// recurse
deleteDir(curPath);
Expand All @@ -112,7 +97,7 @@ const deleteDir = (path) => {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
fs.rmdirSync(dirPath);
} else {
console.log('目录不存在');
}
Expand Down Expand Up @@ -146,11 +131,11 @@ const emptyDir = (dirPath) => {

/**
* rm-rf 目录或者文件
* @param {*} path 路径或者路径数组
* @param {*} dirPath 路径或者路径数组
* @param {*} opts https://www.npmjs.com/package/rimraf
*/
const rmRf = (path, opts) => {
rimraf(path, opts);
const rmRf = (dirPath, opts) => {
rimraf(dirPath, opts);
};

/**
Expand All @@ -159,7 +144,12 @@ const rmRf = (path, opts) => {
*/
const addDir = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
try {
fs.mkdirSync(dir, { recursive: true });
console.log('目录创建成功');
} catch (err) {
console.log({ 'addDir error': err });
}
} else {
console.log('目录已存在');
}
Expand All @@ -184,7 +174,22 @@ const addFile = (filePath, content = '') => {
* @param {*} newPath
*/
const renameDir = (oldPath, newPath) => {
fs.renameSync(oldPath, newPath);
try {
fs.renameSync(oldPath, newPath);
console.log('目录重命名成功');
} catch (err) {
if (err.code === 'ENOENT') {
try {
fs.mkdirSync(path.dirname(newPath), { recursive: true });
fs.renameSync(oldPath, newPath);
console.log('目录重命名成功');
} catch (e) {
console.log({ 'renameDir error': e });
}
} else {
console.log({ 'renameDir error': err });
}
}
};

/**
Expand All @@ -193,7 +198,22 @@ const renameDir = (oldPath, newPath) => {
* @param {*} newPath
*/
const renameFile = (oldPath, newPath) => {
fs.renameSync(oldPath, newPath);
try {
fs.renameSync(oldPath, newPath);
console.log('文件重命名成功');
} catch (err) {
if (err.code === 'ENOENT') {
try {
fs.mkdirSync(path.dirname(newPath), { recursive: true });
fs.renameSync(oldPath, newPath);
console.log('文件重命名成功');
} catch (e) {
console.log({ 'renameFile error': e });
}
} else {
console.log({ 'renameFile error': err });
}
}
};

/**
Expand Down

0 comments on commit 51ad615

Please sign in to comment.