Skip to content

Commit

Permalink
feat: add option cwd
Browse files Browse the repository at this point in the history
With the new option `cwd` it is possible to define a custom working directory.
This is usefull if you want to pump e.g a sub-component or simply for testing.

Closes vojtajina#198
  • Loading branch information
mojoaxel committed Jan 22, 2017
1 parent 4addcb2 commit 348f5b1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ In your project's Gruntfile, add a section named `bump` to the data object passe
grunt.initConfig({
bump: {
options: {
cwd: '.',
files: ['package.json'],
updateConfigs: [],
commit: true,
Expand All @@ -62,11 +63,18 @@ grunt.initConfig({

### Options

#### options.cwd
Type: `String`
Default value: `.`

It is possible to define a custom working directory (cwd). All operations get executed in this directory. All `options.files` entries are relative to this directory.


#### options.files
Type: `Array`
Default value: `['package.json']`

Maybe you wanna bump 'component.json' instead? Or maybe both: `['package.json', 'component.json']`? Can be either a list of files to bump (an array of files) or a grunt glob (e.g., `['*.json']`).
Maybe you wanna bump 'component.json' instead? Or maybe both: `['package.json', 'component.json']`? Can be either a list of files to bump (an array of files) or a grunt glob (e.g., `['*.json']`). All paths are relative to the `options.cwd` directory.

#### options.updateConfigs
Type: `Array`
Expand Down
40 changes: 34 additions & 6 deletions tasks/bump.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var fs = require('fs');
var path = require('path');
var semver = require('semver');
var exec = require('child_process').exec;

Expand All @@ -8,6 +10,7 @@ module.exports = function(grunt) {
var DESC = 'Increment the version, commit, tag and push.';
grunt.registerTask('bump', DESC, function(versionType, incOrCommitOnly) {
var opts = this.options({
cwd: '.',
bumpVersion: true,
commit: true,
commitFiles: ['package.json'], // '-a' for all files
Expand All @@ -30,6 +33,24 @@ module.exports = function(grunt) {
versionType: false
});

// check if cwd exists
if (!fs.existsSync(opts.cwd)) {
grunt.fatal('The "cwd" requested does not exist');
}

// set cwd as the working directory for exec
var execOpts = {
cwd: path.resolve(opts.cwd)
};

// set grunts default working directory
var originalCwd;
if (opts.cwd != '.') {
originalCwd = process.cwd();
grunt.verbose.writeln('Changing working directory: ' + opts.cwd);
grunt.file.setBase(opts.cwd);
}

if (versionType === 'bump-only' || versionType === 'commit-only') {
incOrCommitOnly = versionType;
versionType = '';
Expand Down Expand Up @@ -60,6 +81,11 @@ module.exports = function(grunt) {
var next = function() {
if (!queue.length) {
grunt.config.set('bump.version', globalVersion);

// reset original cwd
if (originalCwd) {
grunt.file.setBase(originalCwd);
}
return done();
}
queue.shift()();
Expand Down Expand Up @@ -90,7 +116,7 @@ module.exports = function(grunt) {

// GET VERSION FROM GIT
runIf(opts.bumpVersion && versionType === 'git', function() {
exec('git describe ' + opts.gitDescribeOptions, function(err, stdout) {
exec('git describe ' + opts.gitDescribeOptions, execOpts, function(err, stdout) {
if (err) {
grunt.fatal('Can not get a version number using `git describe`');
}
Expand Down Expand Up @@ -190,8 +216,10 @@ module.exports = function(grunt) {
grunt.log.ok('bump-dry: ' + cmd);
next();
} else {
exec(cmd, function(err, stdout, stderr) {
exec(cmd, execOpts, function(err, stdout, stderr) {
if (err) {
grunt.verbose.error(err);
grunt.verbose.error(stdout);
grunt.fatal('Can not create the commit:\n ' + stderr);
}
grunt.log.ok('Committed as "' + commitMessage + '"');
Expand All @@ -211,7 +239,7 @@ module.exports = function(grunt) {
grunt.log.ok('bump-dry: ' + cmd);
next();
} else {
exec(cmd , function(err, stdout, stderr) {
exec(cmd , execOpts, function(err, stdout, stderr) {
if (err) {
grunt.fatal('Can not create the tag:\n ' + stderr);
}
Expand All @@ -232,7 +260,7 @@ module.exports = function(grunt) {
grunt.log.ok('bump-dry: ' + cmd);
next();
} else {
exec(cmd, function(err, stdout, stderr) {
exec(cmd, execOpts, function(err, stdout, stderr) {
if (err) {
grunt.fatal(
'Can not push to the git default settings:\n ' + stderr
Expand All @@ -246,7 +274,7 @@ module.exports = function(grunt) {
return;
}

exec('git rev-parse --abbrev-ref HEAD', function(err, ref, stderr) {
exec('git rev-parse --abbrev-ref HEAD', execOpts, function(err, ref, stderr) {
if (err) {
grunt.fatal('Can not get ref for HEAD:\n' + stderr);
}
Expand All @@ -268,7 +296,7 @@ module.exports = function(grunt) {
grunt.log.ok('bump-dry: ' + cmd);
next();
} else {
exec(cmd, function(err, stdout, stderr) {
exec(cmd, execOpts, function(err, stdout, stderr) {
if (err) {
grunt.fatal('Can not push to ' + opts.pushTo + ':\n ' + stderr);
}
Expand Down

0 comments on commit 348f5b1

Please sign in to comment.