自动化脚本之打标签

前言

项目每个迭代我们一般会打个阶段性标签,commit的多找上个标签是到哪个版本还是稍微麻烦一点,然后还要手动提交,那怎么解决以上问题呢?写自动化脚本(esm版)😏

实践

const release = async () => {
  inquirer
    .prompt([
      {
        type: "list",
        name: "versionType",
        message: "请选择版本类型:",
        choices: versionChoices,
      },
      {
        type: "input",
        name: "customVersion",
        message: "Enter the custom version:",
        when: (answers) => answers.versionType === "custom",
        validate: (input) => {
          if (/^\d+\.\d+\.\d+$/.test(input)) {
            return true;
          }
          return "请输入有效的版本,例如 1.2.3";
        },
      },
    ])
    .then(({ versionType, customVersion }) => {
      const newVersion =
        versionType === "custom"
          ? customVersion
          : incrementVersion(currentVersion, versionType);

      const newTag = `v${newVersion}`;
      if (tag) {
        const existingTags = spawnSync("git", ["tag"], {
          encoding: "utf8",
        }).stdout.split("\n");
        if (existingTags.includes(newTag)) {
          console.log(picocolors.green(`标签${newTag}已经存在`));
          process.exit(0);
        }
      }
      try {
        runGitCommand("tag", [newTag]);
        runGitCommand("push", ["origin", "--tags"]);
        console.log(picocolors.blue(`成功创建和推送标签 ${newTag}`));
        process.exit(0);
      } catch (error) {
        console.error(
          picocolors.red("创建和推送标签出错:"),
          picocolors.green(error)
        );
        process.exit(1);
      }
    });
};
release();

工具

inquirer

Inquirer.js 提供了一种简洁、直观的方式来创建交互式命令行界面,它可以提示用户输入各种类型的信息,如文本、数字、密码等,还可以让用户从列表中选择选项、确认操作等。通过 Inquirer,开发者可以更好地与用户进行交互,根据用户的输入来执行不同的操作或生成相应的输出

让用户从列表中选择一个选项,例如选择喜欢的动物

inquirer.prompt([
  {
    type: 'list',
    name: 'animal',
    message: 'What is your favorite color?',
    choices: ['cat', 'dog', 'rabbit', 'sheep']
  }
])
.then(answers => {
  console.log(`You like ${answers.animal}.`);
});

picocolors

Picocolors 专注于提供简洁的 API 来实现彩色文本输出,它支持多种常见的颜色和样式,并且在不同的终端环境中具有较好的兼容性。通过使用 Picocolors,开发者可以轻松地为命令行应用程序、脚本等添加彩色的日志输出、提示信息等,增强用户体验。

使用 Picocolors 提供的颜色函数来输出彩色文本。例如,输出蓝色的文本

console.log(pc.blue('This text is red'));

最后

自动化打标签大功告成啦(终于不用去手打命令和push)😎 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊 程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值