From f5d0fcfe6ab23fe59b34c12201ac8e166412c937 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Tue, 30 Jan 2018 21:09:42 -0800 Subject: [PATCH] feat: only allow releases from master branch This change only permits releases from the master branch. Along the same lines, this change also: * Errors-out quickly if the user appears to be outside a git repo * Removes the dependency on upstream tracking (assumes you're pushing to `origin master`) Fixes #2 Fixes #4 --- index.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 01b6b24..d7cfd0e 100755 --- a/index.js +++ b/index.js @@ -3,6 +3,8 @@ require('shelljs/global'); var path = require('path'); +var RELEASE_BRANCH = 'master'; + // npm version (bump version, commit, make tag) // echo npm publish // - if successful, push commit & tag @@ -13,12 +15,31 @@ function usage() { echo(' Usage: node ' + process.argv[1] + ' '); } +function gitBranch() { + var output = exec('git rev-parse --abbrev-ref HEAD', { silent: true }); + if (output.code) { + throw new Error('Unable to fetch git branch'); + } + return output.stdout.trimRight(); +} + config.silent = true; function run(version) { config.silent = false; config.fatal = true; try { - // TODO(nfischer): only allow releases from master branch (issue #4) + var currentBranch = gitBranch(); + if (currentBranch !== RELEASE_BRANCH) { + echo('Please switch to the release branch: ' + RELEASE_BRANCH); + echo('Currently on: ' + currentBranch); + exit(1); + } + } catch (e) { + echo('Are you in a git repo?'); + exit(1); + } + + try { echo('Publishing new ' + version + ' version'); echo(''); exec('npm version ' + version); @@ -71,9 +92,8 @@ function run(version) { config.silent = false; try { - // TODO(nfischer): this currently requires upstream tracking (issue #2) - exec('git push'); - exec('git push --tags'); + exec('git push origin ' + RELEASE_BRANCH); + exec('git push --tags origin ' + RELEASE_BRANCH); } catch (e) { echo(''); echo('Version has been released, but commit/tag could not be pushed.');