gtw
Version:
git
64 lines (54 loc) • 1.84 kB
JavaScript
const cp = require("child_process");
const util = require("util");
const exec = util.promisify(cp.exec);
const colors = require("colors");
async function check() {
const args = process.argv;
if (
typeof args[3] !== "undefined"
? isRelease(args[3]) || isFeature(args[3])
: false
) {
const { stdout: remote } = await exec("git remote");
if (!remote.startsWith("fatal")) {
const origin = remote.replace(/[\r\n]/g, "");
await exec(`git fetch ${origin}`);
const { stdout: originBranchList } = await exec(`git branch -r`);
if (originBranchList.includes(`${origin}/${args[3]}`)) {
console.log(
colors.yellow("There is a remote branch with the same name")
);
console.log(
`Please execute "git branch ${args[3]}" or "wg branch ${args[3]}" to switch branches`
);
process.exit(1);
}
}
const { stdout: currentBranch } = await exec(
"git rev-parse --abbrev-ref HEAD"
);
if (
currentBranch.replace(/[\r\n]/g, "") !== "master" &&
args[4] !== "master"
) {
if (isRelease(args[3])) {
console.log(colors.red("❌ Please create a release on the master"));
console.log(colors.red("❌ Please create a release on the master"));
console.log(colors.red("❌ Please create a release on the master"));
console.log(
`\nPlease execute the command: \n\n wg branch ${args[3]} master`
);
process.exit(1);
} else if (isFeature(args[3])) {
console.log(
colors.yellow(
"❗️The source branch is not a master. Are you sure to do this?"
)
);
}
}
}
}
const isRelease = (branch) => /^release\//.test(branch);
const isFeature = (branch) => /^feature\//.test(branch);
check();