Skip to content

Commit

Permalink
Feat: update cmd and file func adn npm/yarn cmd.
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaoh committed Dec 29, 2023
1 parent 93f6fcb commit ab12a95
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 7 deletions.
118 changes: 115 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: 2023-06-15 14:34:01
* @LastEditTime: 2023-12-29 14:42:16
* @Description: 命令处理文件
* @FilePath: \js-xcmd\bin\xcmd.js
*/
Expand All @@ -22,7 +22,9 @@ const {
addDir,
addFile,
renameDir,
renameFile
renameFile,
rmRf,
emptyDir
} = require('../utils/files');
const nodeCmd = require('node-cmd');
const { cmd } = require('../utils/cmd');
Expand Down Expand Up @@ -149,6 +151,26 @@ program
console.log('----------Successful----------');
});

program
.option('empty-dir <dir>', 'empty dir')
.command('empty-dir <dir>')
.action((dir) => {
console.log('----------Emptying----------');
console.log({ dir });
emptyDir(dir);
console.log('----------Successful----------');
});

program
.option('rm-rf <path>', 'rm rf')
.command('rm-rf <path>')
.action((path) => {
console.log('----------RimRaf----------');
console.log({ path });
rmRf(path);
console.log('----------Successful----------');
});

program
.option('delete-file <file>', 'delete file')
.command('delete-file <file>')
Expand Down Expand Up @@ -243,11 +265,101 @@ program
console.log(rimrafCmd);
nodeCmd.run(rimrafCmd, (err, data, stderr) => {
if (err) return console.log(`%c出错啦!${data}`, 'color:red;');

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

program
.option('npm-clean', 'npm-clean')
.command('npm-clean')
.action(() => {
console.log('----------Npm-CacheClean-Start----------');
const cmdStr = 'npm cache clean --force';
console.log({ cmdStr });
nodeCmd.run(cmdStr, (err, data, stderr) => {
if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
console.log(data);
console.log('----------Npm-CacheClean-End----------');
});
});

program
.option('yarn-clean', 'yarn-clean')
.command('yarn-clean')
.action(() => {
console.log('----------Yarn-CacheClean-Start----------');
const cmdStr = 'yarn cache clean --force';
console.log({ cmdStr });
nodeCmd.run(cmdStr, (err, data, stderr) => {
if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
console.log(data);
console.log('----------Yarn-CacheClean-End----------');
});
});

program
.option('i', 'npm i --ignore-scripts')
.command('i')
.action(() => {
console.log('----------Npm-Install-IgnoreScripts-Start----------');
const cmdStr = 'npm i --ignore-scripts';
console.log({ cmdStr });
nodeCmd.run(cmdStr, (err, data, stderr) => {
if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
console.log(data);
console.log('----------Npm-Install-IgnoreScripts-End----------');
});
});

program
.option('eslint', 'eslint ./ --fix --ext .ts,.tsx,.js,.jsx,.vue')
.command('eslint')
.action(() => {
console.log('----------Eslint-Start----------');
const cmdStr = 'eslint ./ --fix --ext .ts,.tsx,.js,.jsx,.vue';
console.log({ cmdStr });
nodeCmd.run(cmdStr, (err, data, stderr) => {
if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
console.log(data);
console.log('----------Eslint-End----------');
});
});

program
.option('prettier', 'prettier --write ./**/*.{ts,tsx,js,jsx,vue,html,css,scss,less}')
.command('prettier')
.action(() => {
console.log('----------Prettier-Start----------');
const cmdStr = 'prettier --write ./**/*.{ts,tsx,js,jsx,vue,html,css,scss,less}';
console.log({ cmdStr });
nodeCmd.run(cmdStr, (err, data, stderr) => {
if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
console.log(data);
console.log('----------Prettier-End----------');
});
});

program
.option('list [global]', 'list [global]')
.command('list [global]')
.action((global) => {
let cmdStr = '';
if (global) {
console.log('----------Npm-List-Global-Start----------');
cmdStr = 'npm list -g --depth 0';
} else {
console.log('----------Npm-List-Start----------');
cmdStr = 'npm list --depth 0';
}
console.log({ cmdStr });
nodeCmd.run(cmdStr, (err, data, stderr) => {
if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
console.log(data);
console.log('----------Npm-List-End----------');
});
});

// xcmd run "git status"
program
.option('run <cmdStr>', 'run cmd')
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-xcmd",
"version": "1.4.5",
"version": "1.4.6",
"description": "XCmd library for node.js.",
"main": "main.js",
"bin": {
Expand All @@ -9,7 +9,8 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"bind": "npm link",
"unbind": "npm unlink"
"unbind": "npm unlink",
"i:local": " npm i -g js-xcmd ../js-xcmd"
},
"repository": {
"type": "git",
Expand All @@ -31,7 +32,9 @@
"dependencies": {
"commander": "^9.2.0",
"download-git-repo": "^3.0.2",
"node-cmd": "^5.0.0"
"fs-extra": "^11.2.0",
"node-cmd": "^5.0.0",
"rimraf": "^5.0.5"
},
"time": "0518011528042022"
}
29 changes: 28 additions & 1 deletion utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
* @Author: HxB
* @Date: 2022-04-25 17:49:14
* @LastEditors: DoubleAm
* @LastEditTime: 2022-08-03 18:47:26
* @LastEditTime: 2023-12-29 14:03:06
* @Description: 文件处理工具
* @FilePath: \js-xcmd\utils\files.js
*/
const path = require('path');
const fs = require('fs');
const { rimraf } = require('rimraf');
const fsExtra = require('fs-extra');

/**
* 判断目录是否存在,返回结果。(boolean)
Expand Down Expand Up @@ -128,6 +130,29 @@ const deleteFile = (filePath) => {
}
};

/**
* 清空目录
* @param {*} dirPath
*/
const emptyDir = (dirPath) => {
try {
// fsExtra.removeSync(dirPath);
fsExtra.emptyDirSync(dirPath);
console.log('文件夹已成功清空');
} catch (err) {
console.error('清空文件夹时出错:', err);
}
};

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

/**
* 新建目录
* @param {*} dir
Expand Down Expand Up @@ -217,8 +242,10 @@ const setFileContent = (filePath, content) => {
};

module.exports = {
rmRf,
isDirExistResult,
isDirExist,
emptyDir,
copyDir,
copyFile,
deleteDir,
Expand Down

0 comments on commit ab12a95

Please sign in to comment.