From 51ad6157945674eefa7b2a8e66dc27a92c547991 Mon Sep 17 00:00:00 2001 From: doubleam Date: Mon, 15 Jan 2024 18:08:22 +0800 Subject: [PATCH] Feat: update func with Docs 1.4.8 --- README.md | 4 ++ bin/xcmd.js | 34 +++++++++----- main.js | 6 ++- package.json | 4 +- utils/files.js | 118 +++++++++++++++++++++++++++++-------------------- 5 files changed, 104 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index c2c1418..fd79b46 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,10 @@ npm i js-xcmd -g xcmd -h const { + rmRf, isDirExistResult, isDirExist, + emptyDir, copyDir, copyFile, deleteDir, @@ -38,6 +40,8 @@ npm link npm run bind npm unlink npm run unbind + +npm run i:local ``` ## Others diff --git a/bin/xcmd.js b/bin/xcmd.js index de50eb7..302b9f1 100644 --- a/bin/xcmd.js +++ b/bin/xcmd.js @@ -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 */ @@ -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----------'); }); @@ -164,6 +164,8 @@ program program .option('rm-rf ', 'rm rf') .command('rm-rf ') + // .option('rmrf ', 'rmrf') + // .command('rmrf ') .action((path) => { console.log('----------RimRaf----------'); console.log({ path }); @@ -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----------'); }); @@ -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) { diff --git a/main.js b/main.js index 2f34719..d7d7c96 100644 --- a/main.js +++ b/main.js @@ -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, @@ -27,8 +29,10 @@ const { const { cmd } = require('./utils/cmd'); module.exports = { + rmRf, isDirExistResult, isDirExist, + emptyDir, copyDir, copyFile, deleteDir, diff --git a/package.json b/package.json index 7081829..5e3c066 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -36,5 +36,5 @@ "node-cmd": "^5.0.0", "rimraf": "^5.0.5" }, - "time": "0518011528042022" + "time": "2717021815012024" } \ No newline at end of file diff --git a/utils/files.js b/utils/files.js index 4607cd1..ec064e6 100644 --- a/utils/files.js +++ b/utils/files.js @@ -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 */ @@ -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 }); } }; @@ -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); + } }); }; @@ -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); @@ -112,7 +97,7 @@ const deleteDir = (path) => { fs.unlinkSync(curPath); } }); - fs.rmdirSync(path); + fs.rmdirSync(dirPath); } else { console.log('目录不存在'); } @@ -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); }; /** @@ -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('目录已存在'); } @@ -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 }); + } + } }; /** @@ -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 }); + } + } }; /**