diff --git a/.gitignore b/.gitignore index 1676a24..7940c4c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules .DS_Store *.orig .tmp +npm-debug.log diff --git a/gulp/tasks/release.js b/gulp/tasks/release.js new file mode 100644 index 0000000..54e6a40 --- /dev/null +++ b/gulp/tasks/release.js @@ -0,0 +1,84 @@ +'use strict'; + +var gulp = require('gulp'); +var $ = require('gulp-load-plugins')(); +var logger = require('gulplog'); +var inquirer = require('inquirer'); +var exec = require('child_process').spawn; + +var allowedBranch = 'master'; +var bumpTypeQuestion = { + type: 'list', + name: 'bump', + message: 'What type of bump would you like to do?', + default: 'minor', + choices: ['patch', 'minor', 'major'] +}; + +// Makes sure we do this on the master branch +var assertBranchAllowed = function(done) { + $.git.revParse({args:'--abbrev-ref HEAD', quiet: true}, function(err, currentBranch) { + if (currentBranch != allowedBranch) { + logger.error('ERR: You need to be on the %s branch to be able to deploy', allowedBranch); + process.exit(1); + } + done(); + }); +} +assertBranchAllowed.displayName = 'Verify branch'; + +// Asks for the bump type +var askBumpType = function(done) { + inquirer.prompt([bumpTypeQuestion], function( answers ) { + if (answers !== null && answers[bumpTypeQuestion.name] !== null) { + bumpVersion(answers[bumpTypeQuestion.name], done); + } else { + logger.error('ERR: There was an error with the provided bump type'); + process.exit(1); + } + }); +} +askBumpType.displayName = 'Ask bump type'; + +// Bumps the version of the project +var bumpVersion = function(bumpType, done) { + var p = exec('npm', ['version', bumpType], { cwd: process.cwd()}); + var stderr = ''; + + p.stderr.on('data', function(buf) { + stderr += buf; + }); + + p.on('close', function(code){ + if (code !== 0) { + logger.error('ERR: There was an error running \'npm version\' && git push && git push --tags:'); + logger.error('Details:'); + logger.error('%s', stderr); + process.exit(1); + } else { + done(); + } + }); +} +bumpVersion.displayName = 'Bump Version'; + +// Display the next release steps +var displayNextReleaseSteps = function(done) { + logger.info('The project now has a new version and was pushed to master'); + logger.info('Next steps:'); + logger.info(' 1. Got to github and create a pull request from master to release'); + logger.info(' 2. Somebody else should review the PR and merge it'); + logger.info(' 3. The CI will publish it and create a release on github'); + done(); +} +bumpVersion.displayName = 'Display next release steps'; + +// Bumps the version on package.json and commits it locally and pushes to master +var releaseTask = function(done) { + return new Promise(gulp.series(assertBranchAllowed, askBumpType, displayNextReleaseSteps)); +} +releaseTask.description = 'Bumps the npm version'; + +module.exports = function(gulp, $, config) { + return releaseTask; +}; diff --git a/gulpfile.js b/gulpfile.js index c7876e8..aff6f01 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -66,6 +66,13 @@ gulp.task( gulp.task('deploy', t.getTask('deploy')); +///////////// +// Release // +///////////// + +gulp.task('release', t.getTask('release')); + + ///////////// // Others // ///////////// diff --git a/package.json b/package.json index 35c0681..6bd0165 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,16 @@ { "name": "project-name", "description": "", - "version": "0.0.0", + "version": "0.3.0", "main": "dist/index.html", "scripts": { "start": "./node_modules/.bin/gulp", "build": "./node_modules/.bin/gulp build", "test": "./node_modules/.bin/gulp test", "serve": "./node_modules/.bin/gulp serve", - "deploy": "./node_modules/.bin/gulp deploy" + "deploy": "./node_modules/.bin/gulp deploy", + "release": "./node_modules/.bin/gulp release", + "postversion": "git push && git push --tags" }, "dependencies": { "sensible": "0.5.4" @@ -23,12 +25,14 @@ "gulp-a11y": "^0.1.1", "gulp-accessibility": "^1.2.1", "gulp-autoprefixer": "^2.2.0", + "gulp-bump": "^1.0.0", "gulp-changed": "^1.2.1", "gulp-cli": "github:gulpjs/gulp-cli#4.0", "gulp-concat": "^2.5.2", "gulp-css-globbing": "^0.1.7", "gulp-data": "^1.2.0", "gulp-eslint": "^1.0.0", + "gulp-git": "^1.6.0", "gulp-jade": "^1.0.0", "gulp-jade-globbing": "0.1.6", "gulp-load-plugins": "^0.10.0", @@ -38,6 +42,8 @@ "gulp-sourcemaps": "^1.5.2", "gulp-sync": "^0.1.4", "gulp-util": "^3.0.4", + "gulplog": "^1.0.0", + "inquirer": "^0.11.0", "jade": "^1.11.0", "jade-glob-include": "^1.0.1", "jade-inline-file": "^0.1.0",