使用nodejs 前端方便处理dev prod 切换 git提交

依赖包

  • npm install chalk 多变的颜色
  • npm install shelljs shell执行命令
  • npm install dayjs 时间格式化
  • npm install commander 命令行参数获取
  • npm install readline-sync 同步逐行读取输入内容

代码 deployment.js

/*
 * @Author: 周凯
 * @Date: 2020-10-15 14:48:12
 * @LastEditTime: 2020-10-15 16:23:48
 */
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

/*
 * @Author: 周凯
 * @Date: 2020-10-15 15:47:01
 * @LastEditTime: 2020-10-15 17:13:55
 */

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("代码提交完成~"));
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值