用nodejs写了一个发布脚本,在最后执行npm run publush
后整体脚本意外的重新执行了一次,
脚本通过执行npm run publish 开始执行, 我测试了多次npm run publush不论是在package.json中的script上写还是js文件里写都是会重新执行, 照片如下
代码 "publish": "node publish.js",
#! /usr/bin/env node
import chalk from "chalk";
import inquirer from "inquirer";
import shell from "shelljs";
const getSelectAnswer = async ({ key, list, tipMessage, defaultValue }) => {
const value = await inquirer
.prompt({
name: key,
type: "list",
choices: list.length ? list : [""],
message: tipMessage,
default: defaultValue
})
.catch(error => {
console.log(error);
});
return value[key] || "";
};
if (shell.exec("git status").stdout.indexOf("working tree clean") === -1) {
console.log(chalk.bold.red("---请先提交git暂存区代码---"));
shell.exit(1);
}
const main = async () => {
console.error("开始执行main");
const type = await getSelectAnswer({
key: "type",
list: [
{
name: "修复/ 0.0.(X)",
value: 3
},
{
name: "增加功能 / 0.(X).0",
value: 2
},
{
name: "升级版本 / (X).0.0 ",
value: 1
}
],
tipMessage: "请选版本升级方式(将会修改package.json中version字段)"
});
if (type === 1) {
shell.exec("npm version major");
} else if (type === 2) {
shell.exec("npm version minor");
} else {
shell.exec("npm version patch");
}
shell.exec("git push origin --tag");
shell.exec("git push origin test");
shell.exec("npm publish");
};
main();