From 308d62fc8663aba216342b7d7d67d02407a64f3c Mon Sep 17 00:00:00 2001 From: rkiel Date: Thu, 18 Jul 2019 15:40:37 -0400 Subject: [PATCH] refactor --- bin/creature-start | 98 +++++++++------------------------------------- js/branch.js | 17 ++++++++ js/commander.js | 20 +++++++--- js/git.js | 2 +- js/immutable.js | 2 +- js/shell.js | 14 +++++++ js/validate.js | 23 +++++++++++ 7 files changed, 90 insertions(+), 86 deletions(-) create mode 100644 js/branch.js create mode 100644 js/validate.js diff --git a/bin/creature-start b/bin/creature-start index b118f2b..1edd6aa 100755 --- a/bin/creature-start +++ b/bin/creature-start @@ -7,109 +7,49 @@ const immutable = path("js/immutable"); const git = path("js/git"); const shell = path("js/shell"); const commander = path("js/commander"); - -function setStandardBranches(dp) { - return immutable.set(dp, "standardBranches", ["master", "release"]); -} +const branch = path("js/branch"); function validateCurrentBranch(dp) { - if (!dp.standardBranches.includes(dp.currentBranch)) { - throw `invalid base branch: ${dp.currentBranch}`; + if (branch.isNonStandard(dp.branch.current)) { + throw `invalid base branch: ${dp.branch.current}`; } else { return dp; } } function validateFeatureName(dp) { - if (dp.standardBranches.includes(dp.featureName)) { - throw `invalid feature branch: ${dp.featureName}`; + if (branch.isStandard(commander.featureName())) { + throw `invalid feature branch: ${commander.featureName()}`; } else { return dp; } } -function setFeatureName(dp) { - const fn = commander.args().join("-"); - return Object.assign({}, dp, { featureName: fn }); -} - -function setPrefix(dp) { - const p = process.env.FEATURE_USER || process.env.USER; - return Object.assign({}, dp, { prefix: p }); -} - function setFeatureBranch(dp) { - const fb = `${dp.prefix}-${dp.currentBranch}-${dp.featureName}`; - return Object.assign({}, dp, { featureBranch: fb }); -} - -function gitFetch(dp) { - const cmd = "git fetch origin -p && git fetch origin --tags"; - console.log(cmd); - return shell.something(cmd, dp); -} - -function setMergeBranch(dp) { - return immutable.set( - dp, - "mergeBranch", - ["origin", dp.currentBranch].join("/") - ); -} - -function gitMerge(dp) { - const cmd = `git merge ${dp.mergeBranch}`; - console.log(cmd); - return shell.something(cmd, dp); -} - -function gitBranch(dp) { - const cmd = `git branch ${dp.featureBranch}`; - console.log(cmd); - return shell.something(cmd, dp); -} - -function gitCheckout(dp) { - const cmd = `git checkout ${dp.featureBranch}`; - console.log(cmd); - return shell.something(cmd, dp); -} - -function gitPush(dp) { - const cmd = `git push -u origin ${dp.featureBranch}`; - console.log(cmd); - return shell.something(cmd, dp); + const fb = [ + commander.prefix(), + dp.branch.current, + commander.featureName() + ].join("-"); + return immutable.set(dp, "branch.feature", fb); } function createFeatureBranch(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}` - ]; - return _.reduce( - cmds, - (accum, elem) => { - return accum - .then(() => console.log(elem)) - .then(() => console.log()) - .then(() => shell.run(elem)); - }, - Promise.resolve({}) - ); + return shell.pipeline([ + "git fetch --prune --prune-tags --tags origin", + `git merge origin/${dp.branch.current}`, + "git push", + `git checkout -b ${dp.branch.feature}`, + `git push -u origin ${dp.branch.feature}` + ]); } function toPromise(dp) { return Promise.resolve(dp); } -const x = toPromise({ program: commander.parse() }) - .then(setStandardBranches) - .then(setFeatureName) +const x = toPromise(commander.parse()) .then(validateFeatureName) - .then(setPrefix) .then(git.setCurrentBranch) .then(validateCurrentBranch) .then(setFeatureBranch) diff --git a/js/branch.js b/js/branch.js new file mode 100644 index 0000000..a316b2a --- /dev/null +++ b/js/branch.js @@ -0,0 +1,17 @@ +let lib; + +function standard() { + return ["master", "release"]; +} + +function isStandard(b) { + return lib.standard().includes(b); +} + +function isNonStandard(b) { + return !lib.isStandard(b); +} + +lib = { standard, isStandard, isNonStandard }; + +module.exports = lib; diff --git a/js/commander.js b/js/commander.js index a9b6028..11aea3b 100644 --- a/js/commander.js +++ b/js/commander.js @@ -4,17 +4,27 @@ const something = program.parse(process.argv); let lib; -function parse() { - return something; -} - function args() { return something.args; } +function featureName() { + return args().join("-"); +} + +function parse() { + return { program: something }; +} + +function prefix() { + return process.env.FEATURE_USER || process.env.USER; +} + lib = { args, - parse + parse, + featureName, + prefix }; module.exports = lib; diff --git a/js/git.js b/js/git.js index 97ade9c..abe701b 100644 --- a/js/git.js +++ b/js/git.js @@ -9,7 +9,7 @@ let lib; function setCurrentBranch(dp) { const cmd = "git rev-parse --abbrev-ref HEAD"; - return shell.capture(cmd).then(x => immutable.set(dp, "currentBranch", x)); + return shell.capture(cmd).then(x => immutable.set(dp, "branch.current", x)); } lib = { diff --git a/js/immutable.js b/js/immutable.js index c3c3e89..62f5fb3 100644 --- a/js/immutable.js +++ b/js/immutable.js @@ -3,7 +3,7 @@ const _ = require("lodash"); let lib; function set(dp, path, value) { - return _.assign({}, dp, _.set({}, path, value)); + return _.set(_.assign({}, dp), path, value); } lib = { diff --git a/js/shell.js b/js/shell.js index e32a5ec..53e7553 100644 --- a/js/shell.js +++ b/js/shell.js @@ -23,10 +23,24 @@ function something(cmd, dp) { return lib.run(cmd).then(lib._something(dp)); } +function pipeline(cmds) { + return _.reduce( + cmds, + (accum, elem) => { + return accum + .then(() => console.log()) + .then(() => console.log(elem)) + .then(() => exec(elem)); + }, + Promise.resolve({}) + ).then(() => console.log()); +} + lib = { _something, run, capture, + pipeline, something: _.curry(something) }; diff --git a/js/validate.js b/js/validate.js new file mode 100644 index 0000000..e14ed0d --- /dev/null +++ b/js/validate.js @@ -0,0 +1,23 @@ +const branch = path("js/branch"); + +let lib; + +function validateCurrentBranch(dp) { + if (branch.isNonStandard(dp.branch.current)) { + throw `invalid base branch: ${dp.branch.current}`; + } else { + return dp; + } +} + +function validateFeatureName(dp) { + if (branch.isStandard(commander.featureName())) { + throw `invalid feature branch: ${commander.featureName()}`; + } else { + return dp; + } +} + +lib = { validateCurrentBranch, validateFeatureName }; + +module.exports = lib;