Skip to content

Commit

Permalink
- new: dry-run option, only logs the commands without changing anything
Browse files Browse the repository at this point in the history
- some cleanup
  • Loading branch information
stbaer committed Jan 8, 2017
1 parent 6fb9fb0 commit b55a449
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 49 deletions.
31 changes: 11 additions & 20 deletions bin/release.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
#! /usr/bin/env node

const readJsonSync = require('read-json-sync');
const semver = require('semver');
const taggedVersions = require('tagged-versions');
const inquirer = require('inquirer');
const args = require('args');
const loudRejection = require('loud-rejection');

const shellEx = require('../lib/ex').shellEx;
const execSh = require('../lib/ex').execSh;
const branchesUpToDate = require('../lib/check-branches-up-to-date');
const branchesUpToDate = require('../lib/branches-up-to-date');
const bumpVersions = require('../lib/bump-versions');
const handleError = require('../lib/handle-error');
const spinner = require('../lib/spinner');
const prompts = require('../lib/prompts');
const getTagNotes = require('../lib/history');
const writeHistoryFile = require('../lib/history').writeHistoryFile;

args.option('message', 'enter a custom tag message');
args.option('skip-build', `skip build before release`);
const config = require('../lib/config').config;
const flags = require('../lib/config').flags;

const flags = args.parse(process.argv);
const config = readJsonSync('./package.json').releaseConfig || {};

config.versionFiles = config.versionFiles || ['package.json'];
config.buildCommand = (config.buildCommand === 'undefined') ? 'npm run build' : config.buildCommand;
config.productionBranchName = config.productionBranchName || 'master';
config.developBranchName = config.developBranchName || 'develop';
config.historyFile = config.historyFile || false;
let currentVersion;
let newVersion;

loudRejection();

const onGitFlowReleaseFinished = () => {
inquirer.prompt(prompts.pushThemAll)
.then(answer => {
Expand All @@ -46,9 +39,9 @@ const onVersionsBumped = () => {
if (flags.m) {
tagMessage = `-m "${flags.m}" `;
}

if (config.historyFile) {
const historyText = getTagNotes(currentVersion, newVersion);
shellEx(`echo "${historyText}\n$(cat ${config.historyFile})" > ${config.historyFile}`);
writeHistoryFile(currentVersion, newVersion, config.historyFile);
commitCommand += 'updated History.md;';
}

Expand All @@ -61,8 +54,7 @@ const onVersionsBumped = () => {
shellEx(`${commitCommand}"`);

execSh(`git flow release finish ${tagMessage} ${newVersion}`)
.then(onGitFlowReleaseFinished)
.catch(handleError);
.then(onGitFlowReleaseFinished);
};

const onRealeaseTypeChosen = choice => {
Expand All @@ -71,8 +63,7 @@ const onRealeaseTypeChosen = choice => {

shellEx(`git flow release start ${newVersion}`);
bumpVersions(config.versionFiles, newVersion)
.then(onVersionsBumped)
.catch(handleError);
.then(onVersionsBumped);
};

const onLastVersionResult = res => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ module.exports = branches => {
spinner.fail(`Error: ${branch} not up to date with origin`);
}
});
shellEx('sleep 0.5', {silent: true}, false);
spinner.succeed();
};
4 changes: 3 additions & 1 deletion lib/bump-versions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const updateJson = require('update-json');
const handleError = require('./handle-error');

/**
*
Expand All @@ -16,5 +17,6 @@ module.exports = (versionFiles, newVersion) => {
})
);
});
return Promise.all(promises);
return Promise.all(promises)
.catch(handleError);
};
25 changes: 25 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const readJsonSync = require('read-json-sync');
const args = require('args');
const log = require('./log');

args.option('message', 'enter a custom tag message');
args.option('dry-run', 'only show the commands that would be executed without changing anything', false);
args.option('bump-files', 'a list of .json files where the version field should be bumped', ['package.json']);
args.option('skip-build', `skip build before finishing release`);

const flags = args.parse(process.argv);
const pkgConfig = readJsonSync('./package.json').releaseConfig || {};
const configDefault = {
versionFiles: ['package.json'],
productionBranchName: 'master',
developBranchName: 'develop',
buildCommand: false,
historyFile: false
};
const config = Object.assign({}, configDefault, pkgConfig);

flags.d && console.log('\nflags:', flags, '\n\nConfig: \n', config, '\n');

module.exports.config = config;

module.exports.flags = flags;
30 changes: 20 additions & 10 deletions lib/ex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const shell = require('shelljs');
const exec = require('exec-sh');
const handleError = require('./handle-error');
const log = require('./log');
const flags = require('./config').flags;

/**
*
Expand All @@ -17,29 +18,38 @@ const logCommand = cmd => log(`\nExecuting command: ${cmd}`, 'yellow');
* @returns {Object}
*/
const shellEx = (command, sjsOpts = {silent: true}, showCommand = true) => {
showCommand && logCommand(command);
(showCommand || flags.d) && logCommand(command);

const runCommand = shell.exec(command, sjsOpts);
runCommand.code !== 0 && handleError(runCommand.stderr);
if (!flags.d) {
const runCommand = shell.exec(command, sjsOpts);
runCommand.code !== 0 && handleError(runCommand.stderr);
return runCommand;
}else {
return {
stdout: ''
};
}

return runCommand;
};

/**
*
* @param {String} command - the shell command to execute
* @param [opts] - [options={silent: true}]
* @param [showCommand=true] - print the command
* @returns {Object}
*/
const execSh = (command, opts = {silent: true}, showCommand = true) => {
return new Promise((resolve, reject) => {
showCommand && logCommand(command);
(showCommand || flags.d) && logCommand(command);

exec(command, err => {
err && reject(err);
return new Promise(resolve => {
if (flags.d) {
resolve(true);
});
} else {
exec(command, err => {
err && handleError(err);
resolve(true);
});
}
});
};

Expand Down
31 changes: 15 additions & 16 deletions lib/history.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
const shellEx = require('../lib/ex').shellEx;
const dateFormat = require('dateformat');
const shellEx = require('./ex').shellEx;
const handleError = require('./handle-error');

const getDate = () => {
const d = new Date();
const getHistoryString = (currentVersion, newVersion) => {
const logCommand = shellEx(`git log ${currentVersion}..HEAD --pretty=format:" * %s"`);
const date = dateFormat(new Date(), 'longDate');

const months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
if (logCommand.code !== 0) {
handleError(logCommand.stderr);
}

return `${months[d.getMonth()]} ${d.getDate()}, ${d.getFullYear()}`;
};

module.exports = (currentVersion, newVersion) => {
const logCommand = shellEx(`git log ${currentVersion}..HEAD --pretty=format:" * %s"`);
const date = getDate();
return `###${newVersion} — *${date}*\n\n${logCommand.stdout}\n`;
};

if (logCommand.code !== 0) {
handleError(logCommand.stderr);
}

return `###${newVersion} — *${date}*\n\n${logCommand.stdout}\n`;
const writeHistoryFile = (currentVersion, newVersion, file) => {
const historyText = getHistoryString(currentVersion, newVersion);
shellEx(`echo "${historyText}\n$(cat ${file})" > ${file}`);
};

module.exports.writeHistoryFile = writeHistoryFile;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gf-release",
"version": "1.1.4",
"version": "1.1.5",
"description": "git flow release node script",
"main": "index.js",
"bin": {
Expand Down Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"args": "^2.2.1",
"chalk": "^1.1.3",
"dateformat": "^2.0.0",
"exec-sh": "^0.2.0",
"inquirer": "^2.0.0",
"ora": "^0.4.0",
Expand Down

0 comments on commit b55a449

Please sign in to comment.