UNPKG

gtw

Version:

git

80 lines (68 loc) 2.47 kB
const cp = require("child_process"); const util = require("util"); const exec = util.promisify(cp.exec); const colors = require("colors"); async function check() { const { stdout: diff } = await exec("git diff", { stdio: "inherit" }); if (diff) { console.log(diff); console.log(colors.yellow("There are file changes, please commit first")); process.exit(1); } else { const { stdout: status } = await exec("git status", { stdio: "inherit" }); if (!isClean(status)) { console.log(status); console.log( colors.yellow( "There are changes that need to be committed, please commit first" ) ); process.exit(1); } else { const args = process.argv; 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[4]}`)) { console.log( colors.yellow("There is a remote branch with the same name") ); console.log( `Please execute "git checkout ${args[4]}" or "wg checkout ${args[4]}" to switch branches` ); process.exit(1); } } if (args[3] === "-b") { const { stdout: currentBranch } = await exec( "git rev-parse --abbrev-ref HEAD" ); if (args[4].startsWith("release/")) { if (!isMaster(currentBranch)) { 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[4]} master` ); process.exit(1); } } else if (args[4].startsWith("feature/")) { if (!isMaster(currentBranch)) { console.log( colors.yellow( "❗️The source branch is not a master. Are you sure to do this?" ) ); } } } } } } const isClean = (status) => status.includes("nothing to commit, working tree clean"); const isMaster = (branch) => branch.replace(/[\r\n]/g, "") === "master"; check();