UNPKG

@modea/modea-increment-version

Version:

Increments version and build numbers in specified files

140 lines (120 loc) 5.94 kB
const util = require('util'); const exec = util.promisify(require('child_process').exec) const NODE_MODULES_PATH = "./node_modules/" const MERGE_SCRIPT_PATH = "@modea/modea-increment-version/src/increment/git/merge.sh"; const BRANCH_CREATE_SCRIPT_PATH = "@modea/modea-increment-version/src/increment/git/branch.sh" const INCREMENT_BRANCH_NAME = "Increment" async function pushChangesToGit(gitConfiguration, incrementType) { const commands = [] const pushCommand = formulatePushCommand(gitConfiguration.simplePush, INCREMENT_BRANCH_NAME, gitConfiguration.description) if (gitConfiguration.simplePush) { // execute only the push command executeCommands([{command: pushCommand, exitOnErr: true, failureMessage: "Failed to push increment changes"}]); return } commands.push({command: pushCommand, exitOnErr: true, failureMessage: `Failed to push increment changes onto increment branch`}); const incrementPRcommand = { command: createIncrementPRCommand(gitConfiguration), exitOnErr: true, failureMessage: `Failed to create or merge PR from increment branch onto ${gitConfiguration.currentBranch}`} commands.push(incrementPRcommand); if (incrementType !== 'build') { const releaseBranchCommand = { command: createReleaseBranchCommand(gitConfiguration), exitOnErr: true, failureMessage: `Failed to create ${gitConfiguration.branchName}`} commands.push(releaseBranchCommand); // add all pr commands for (const command of createPrCommands(gitConfiguration)) { commands.push({command: command, exitOnErr: false, failureMessage: "Failed to create or merge increment PR's"}); } // add all sync pr commands for (const command of createMergePRCommands(gitConfiguration)) { commands.push({command: command, exitOnErr: false, failureMessage: "Failed to create or merge sync PR's"}); } } // now execute them and print failure messages if they exist const errors = await executeCommands(commands) if (Array.isArray(errors) && errors.length) { exitWithError(errors.join(', ')) } } function formulatePushCommand(isSimple, branchName, description) { const gitAdd = 'git add .'; const gitCommit = `git commit -m "${description}"`; const simplePush = 'git push'; // if simple push just run git add git commit git push, // else run checkout branch, git add git commit git push --set-upstream origin branch if (isSimple) { return `${gitAdd} && ${gitCommit} && ${simplePush}` } else { const checkoutBranch = `git checkout -b ${branchName}`; const pushToBranch = `git push --set-upstream origin ${branchName}`; return `${checkoutBranch} && ${gitAdd} && ${gitCommit} && ${pushToBranch}`; } } function createIncrementPRCommand(gitConfiguration) { const scriptLocation = getScriptLocation(gitConfiguration, MERGE_SCRIPT_PATH) return formulatePrCommand(scriptLocation, INCREMENT_BRANCH_NAME, gitConfiguration.currentBranch, "Increment", gitConfiguration.description, "fast_forward", true) } function createReleaseBranchCommand(gitConfiguration) { const scriptLocation = getScriptLocation(gitConfiguration, BRANCH_CREATE_SCRIPT_PATH) return `sh ${scriptLocation} ${gitConfiguration.branchName} ${gitConfiguration.currentBranch}` } function createPrCommands(gitConfiguration) { const commands = [] const scriptLocation = getScriptLocation(gitConfiguration, MERGE_SCRIPT_PATH); for (const prBranch of gitConfiguration.pr.branches) { // don't create a PR on a branch that has already been PRed if (prBranch.name !== gitConfiguration.currentBranch) { const command = formulatePrCommand(scriptLocation, gitConfiguration.branchName, prBranch.name, `${gitConfiguration.branchName}`, gitConfiguration.description, prBranch.mergeStrategy, false) commands.push(command) } } return commands; } function formulatePrCommand(scriptLocation, fromBranch, toBranch, title, description, mergeStrategy, closeSourceBranch) { const rawDescription = description.replace(/\n/g, "\\n").replace(/\t/g, "\\t") return `sh ${scriptLocation} ${fromBranch} ${toBranch} "${title}" "${rawDescription}" ${mergeStrategy} ${closeSourceBranch}` } function createMergePRCommands(gitConfiguration) { const commands = [] const scriptLocation = getScriptLocation(gitConfiguration, MERGE_SCRIPT_PATH); for (const prBranch of gitConfiguration.pr.branches) { // We're creating PR's from other branches to current branch if (prBranch.name !== gitConfiguration.currentBranch) { const command = formulatePrCommand(scriptLocation, prBranch.name, gitConfiguration.currentBranch, `Sync with ${prBranch.name}`, `Syncing branch ${gitConfiguration.currentBranch} with ${prBranch.name} after increment changes`, "merge_commit", false); commands.push(command) } } return commands; } async function executeCommands(commands) { const errors = [] for (const command of commands) { try { const {stdout, stderr} = await exec(command.command) console.log(stdout, stderr) } catch (err) { console.log('\x1b[31m', err.stdout, err.stderr) if (command.exitOnErr) { exitWithError(command.failureMessage) return } else { errors.push(command.failureMessage) } } } return errors } function getScriptLocation(gitConfiguration, path) { if (gitConfiguration.packageLocation) { return gitConfiguration.packageLocation + path } else { return NODE_MODULES_PATH + path } } function exitWithError(failureMessage) { console.log('\x1b[31m', failureMessage); process.exit(-1); } module.exports = { pushChangesToGit };