From 438416bd4178b37689c9169ba9390768aa525409 Mon Sep 17 00:00:00 2001 From: rkiel Date: Sat, 20 Jul 2019 08:11:08 -0400 Subject: [PATCH] finish end --- bin/creature-end | 64 ++++++++++++++---------------------------------- js/commander.js | 5 ++-- js/git.js | 28 ++++++++++++++++++++- js/validate.js | 19 +++++++++++++- 4 files changed, 66 insertions(+), 50 deletions(-) diff --git a/bin/creature-end b/bin/creature-end index fa0ece7..e05d755 100755 --- a/bin/creature-end +++ b/bin/creature-end @@ -1,59 +1,31 @@ #!/usr/bin/env node const _ = require("lodash"); + const path = x => require("../" + x); -const immutable = path("js/immutable"); const git = path("js/git"); const shell = path("js/shell"); - -const commander = require("commander"); -const program = new commander.Command(); - -program.parse(process.argv); +const commander = path("js/commander"); +const validate = path("js/validate"); function endFeatureBranch(dp) { const cmds = [ - "git fetch origin -p", - "git fetch origin --tags", - `git merge origin/${dp.currentBranch}`, - `git checkout -b ${dp.featureBranch}`, - `git push -u origin ${dp.featureBranch}` + `git checkout ${dp.branch.standard}`, + `git branch -d ${dp.branch.current}` ]; - return _.reduce( - cmds, - (accum, elem) => { - return accum - .then(() => console.log(elem)) - .then(() => console.log()) - .then(() => shell.run(elem)); - }, - Promise.resolve({}) - ); -} -// function setBranchParts(dp) { -// def parse_branch (branch) -// parts = branch.split('-') -// error "invalid branch: user-standard-feature" unless parts.size > 2 -// -// user = parts.shift -// standard = parts.shift -// error "invalid user: #{user}" unless [ENV['FEATURE_USER'],ENV['USER']].include? user -// error "invalid branch: #{standard}" unless standard_branches.include? standard or standard =~ release_branch_pattern -// feature = parts.join('-') -// { user: user, standard: standard, feature: feature } -// } - -function toPromise(dp) { - return Promise.resolve(dp); + const remoteBranch = _.get(dp, "branch.remote", false); + if (remoteBranch) { + cmds.push(`git push origin :${remoteBranch}`); + } + return shell.pipeline(cmds); } -const x = toPromise({ program }) - .then(setStandardBranches) - .then(setFeatureName) - .then(validateFeatureName) - .then(setPrefix) - .then(setCurrentBranch) - .then(validateCurrentBranch) - .then(setFeatureBranch) - .then(createFeatureBranch) +commander + .start() + .then(git.setCurrentBranch) + .then(git.parseCurrentBranch) + .then(validate.mustBeFeatureBranch) + .then(git.setStandardBranch) + .then(git.isBranchRemote) + .then(endFeatureBranch) .catch(err => console.error(err)); diff --git a/js/commander.js b/js/commander.js index fb018eb..fdf7636 100644 --- a/js/commander.js +++ b/js/commander.js @@ -25,7 +25,7 @@ function toPromise(dp) { } function echo(dp) { - console.log(JSON.stringify(dp)); + console.log(JSON.stringify(dp, null, 2)); return dp; } @@ -38,7 +38,8 @@ lib = { parse, featureName, prefix, - start + start, + echo }; module.exports = lib; diff --git a/js/git.js b/js/git.js index 1742ac1..75f5b84 100644 --- a/js/git.js +++ b/js/git.js @@ -22,9 +22,35 @@ function setFeatureBranch(dp) { return immutable.set(dp, "branch.feature", fb); } +function parseCurrentBranch(dp) { + const parts = dp.branch.current.split("-"); + return immutable.set(dp, "branch.parts", parts); +} + +function setStandardBranch(dp) { + const parts = _.get(dp, "branch.parts", []); + return immutable.set(dp, "branch.standard", parts[1]); +} + +function isBranchRemote(dp) { + const branch = _.get(dp, "branch.current"); + const cmd = `git branch -r|grep origin|grep -v 'HEAD'|grep ${branch}`; + return shell + .capture(cmd) + .then(x => x.trim()) + .then(x => + x === "" + ? immutable.set(dp, "branch.remote", false) + : immutable.set(dp, "branch.remote", branch) + ); +} + lib = { setCurrentBranch, - setFeatureBranch + setFeatureBranch, + setStandardBranch, + parseCurrentBranch, + isBranchRemote }; module.exports = lib; diff --git a/js/validate.js b/js/validate.js index 25c879f..c1333eb 100644 --- a/js/validate.js +++ b/js/validate.js @@ -28,6 +28,23 @@ function featureIsNotStandardBranch(dp) { } } -lib = { currentIsStandardBranch, featureIsNotStandardBranch }; +function mustBeFeatureBranch(dp) { + const parts = dp.branch.parts; + if ( + parts.length > 2 && + commander.prefix() === parts[0] && + branch.isStandard(parts[1]) + ) { + return dp; + } else { + throw `ERROR: ${dp.branch.current} is not a feature branch`; + } +} + +lib = { + currentIsStandardBranch, + mustBeFeatureBranch, + featureIsNotStandardBranch +}; module.exports = lib;