依赖包
npm install chalk
多变的颜色npm install shelljs
shell执行命令npm install dayjs
时间格式化npm install commander
命令行参数获取npm install readline-sync
同步逐行读取输入内容
代码 deployment.js
const path = require("path");
const fs = require("fs");
const chalk = require("chalk");
const defaultName = "index";
const filePath = FunFilePath(defaultName);
const name = process.argv[2];
try {
if (name === "prod" || name === "dev") {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
console.log(chalk.blue("index 成功删除文件!"));
}
fs.copyFileSync(FunFilePath(`index-${name}`), FunFilePath(`index`));
console.log("文件处理完成-----------------");
console.log(chalk.blue("文件处理完成-----------------"));
} else {
console.log(chalk.red("请输入正确的命令"));
throw "请输入正确的命令";
}
} catch (err) {
console.log(chalk.red("处理失败!"));
}
function FunFilePath(val) {
return path.join(__dirname, `./src/config/${val}.ts`);
}
代码 gitpush
const program = require("commander");
const chalk = require("chalk");
const readlineSync = require("readline-sync");
const shell = require("shelljs");
const dayjs = require("dayjs");
program
.version("0.1.0")
.option("-p, --peppers", "添加到生产环境")
.parse(process.argv);
console.log(chalk.red("输入内容为:"));
let name = readlineSync.question("请输入提交描述?");
if (program.peppers) {
console.log(" - 生产");
name = `${name} - prod time:${dayjs(Date.now())}`;
console.log("name", name);
}
if (!shell.which("git")) {
shell.echo("没有安装git");
shell.exit(1);
}
if (shell.exec("git add -A").code !== 0) {
shell.echo("错误: 代码提交失败 git add -A!~");
shell.exit(1);
}
if (shell.exec(`git commit -m ${name}`).code !== 0) {
shell.echo("错误: 代码提交失败 git add commit -m!~");
shell.exit(1);
}
if (shell.exec(`git push origin master`).code !== 0) {
shell.echo("错误: 代码提交失败 git push origin master!~");
shell.exit(1);
} else {
console.log(chalk.blue("代码提交完成~"));
}