Skip to content

Commit

Permalink
Add a new option: the jsonFiles to reference json files with the vers…
Browse files Browse the repository at this point in the history
…ion property to update
  • Loading branch information
Julien Roche committed Sep 23, 2016
1 parent 370ca3b commit 400114c
Show file tree
Hide file tree
Showing 16 changed files with 453 additions and 42 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,27 @@ is called before create a git commit / tag and pushing.
"git-push": false,
"git-commit-message": "Release version: %s",
"git-tag-message": "v%s",
"increment": "minor"
"increment": "minor",
"jsonFiles": []
}
````

jsonFiles is a list of JSON files with the "version" property to update. The file path is relative to the package.json file.

An example which includes bower:

````json
{
"force-preid": true,
"nogit-commit": false,
"nogit-tag": true,
"git-push": false,
"git-commit-message": "Release version: %s",
"git-tag-message": "v%s",
"increment": "minor",
"jsonFiles": [
"bower.json"
]
}
````

Expand Down
13 changes: 12 additions & 1 deletion lib-es5/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,25 @@ module.exports = function () {
}

_createClass(GitUtils, null, [{
key: 'createCommit',
key: 'addFile',

/**
* @param {string} filePath
* @returns {Promise}
*/
value: function addFile(filePath) {
return Utils.promisedExec('git add ' + filePath);
}

/**
* Create a commit git
* @param {string} packageVersion
* @param {string} [label]
* @returns {Promise}
*/

}, {
key: 'createCommit',
value: function createCommit(packageVersion, label) {
return Utils.promisedExec('git commit --all --message "' + GitUtils.createCommitLabel(packageVersion, label) + '"');
}
Expand Down
3 changes: 2 additions & 1 deletion lib-es5/rc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var RC_OPTIONS = {
'git-push': false,
'git-commit-message': messages.GIT_COMMIT_MESSAGE,
'git-tag-message': messages.GIT_TAG_MESSAGE,
'increment': 'patch'
'increment': 'patch',
'jsonFiles': []
};

/**
Expand Down
84 changes: 82 additions & 2 deletions lib-es5/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ var _createClass = function () { function defineProperties(target, props) { for

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

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

var versionOptionsAnalyzer = require('./cli-params');
var rcOptionsRetriever = require('./rc');

// Constants & variables
// Here the class
module.exports = function () {

var Utils = function () {
function Utils() {
_classCallCheck(this, Utils);
}
Expand Down Expand Up @@ -69,7 +72,84 @@ module.exports = function () {
}
});
}

/**
* @param {string} filePath
* @returns {Promise}
*/

}, {
key: 'readFile',
value: function readFile(filePath) {
return new Promise(function (resolve, reject) {
fs.readFile(filePath, function (err, content) {
if (err) {
reject(err);
} else {
resolve(content.toString());
}
});
});
}

/**
* @param {string} content
* @param {string} propertyName
* @param {string} value
* @returns {string}
*/

}, {
key: 'replaceJsonProperty',
value: function replaceJsonProperty(content, propertyName, value) {
var propertyToFound = '"' + propertyName + '"';
var indexPropertyToFound = content.indexOf(propertyToFound);

if (indexPropertyToFound >= 0) {
var startIndex = content.indexOf('"', indexPropertyToFound + propertyToFound.length);
var startExtract = content.substr(0, startIndex + 1);
var endExtract = content.substr(startIndex + 1);
endExtract = endExtract.substr(endExtract.indexOf('"'));

return '' + startExtract + value + endExtract;
}

return content;
}

/**
* @param {string} content
* @param {string} value
* @returns {string}
*/

}, {
key: 'replaceJsonVersionProperty',
value: function replaceJsonVersionProperty(content, value) {
return Utils.replaceJsonProperty(content, 'version', value);
}

/**
* @param {string} filePath
* @returns {Promise}
*/

}, {
key: 'writeFile',
value: function writeFile(filePath, content) {
return new Promise(function (resolve, reject) {
fs.writeFile(filePath, content, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
}]);

return Utils;
}();
}();

module.exports = Utils;
41 changes: 41 additions & 0 deletions lib-es5/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ var ERRORS = Object.freeze({
* @property {string} [preid]
* @property {string} [git-commit-message]
* @property {string} [git-tag-message]
* @property {string[]} [jsonFiles] Relative paths from the package.json to update files with a "version" property
*/

// Here the class
Expand Down Expand Up @@ -215,6 +216,8 @@ var VersionUtils = function () {
return VersionUtils.doPrenpmVersionRunScriptIfNeeded(packageJson);
}).then(function () {
return VersionUtils.updatePackageVersion(packageJsonVersion);
}).then(function () {
return VersionUtils.updateJsonFilesIfNeeded(options, packageJsonVersion);
}).then(function () {
return VersionUtils.doPostnpmVersionRunScriptIfNeeded(packageJson);
}).then(function () {
Expand Down Expand Up @@ -696,6 +699,44 @@ var VersionUtils = function () {
return packageVersion;
});
}

/**
* @param {string} packageVersion
* @param {string} jsonFilePath
* @returns {Promise}
*/

}, {
key: 'updateJsonFile',
value: function updateJsonFile(packageVersion, jsonFilePath) {
var filePath = path.resolve(path.join(process.cwd(), jsonFilePath));

return Utils.readFile(filePath).then(function (jsonContent) {
return Utils.replaceJsonVersionProperty(jsonContent, packageVersion);
}).then(function (newJsonContent) {
return Utils.writeFile(filePath, newJsonContent);
}).then(function () {
return GitUtils.addFile(jsonFilePath);
});
}

/**
* @param {VersionOptions} options
* @param {string} packageVersion
* @returns {Promise}
*/

}, {
key: 'updateJsonFilesIfNeeded',
value: function updateJsonFilesIfNeeded(options, packageVersion) {
if (options && options.jsonFiles && options.jsonFiles.length > 0) {
return Promise.all(options.jsonFiles.map(function (jsonFilePath) {
return VersionUtils.updateJsonFile(packageVersion, jsonFilePath);
}));
}

return Promise.resolve();
}
}]);

return VersionUtils;
Expand Down
8 changes: 8 additions & 0 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ const REGEX = {

// Here the class
module.exports = class GitUtils {
/**
* @param {string} filePath
* @returns {Promise}
*/
static addFile(filePath) {
return Utils.promisedExec(`git add ${filePath}`);
}

/**
* Create a commit git
* @param {string} packageVersion
Expand Down
3 changes: 2 additions & 1 deletion lib/rc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const RC_OPTIONS = {
'git-push': false,
'git-commit-message': messages.GIT_COMMIT_MESSAGE,
'git-tag-message': messages.GIT_TAG_MESSAGE,
'increment': 'patch'
'increment': 'patch',
'jsonFiles': []
};

/**
Expand Down
74 changes: 71 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
'use strict';

// Imports
const fs = require('fs');
const exec = require('child_process').exec;

const versionOptionsAnalyzer = require('./cli-params');
const rcOptionsRetriever = require('./rc');


// Constants & variables
// Here the class
module.exports = class Utils {
class Utils {
/**
* Load the application parameters
* @param {string[]} cliParams
Expand Down Expand Up @@ -56,4 +57,71 @@ module.exports = class Utils {
}
});
}
};

/**
* @param {string} filePath
* @returns {Promise}
*/
static readFile(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, content) => {
if (err) {
reject(err);

} else {
resolve(content.toString());
}
});
});
}

/**
* @param {string} content
* @param {string} propertyName
* @param {string} value
* @returns {string}
*/
static replaceJsonProperty(content, propertyName, value) {
let propertyToFound = `"${propertyName}"`;
let indexPropertyToFound = content.indexOf(propertyToFound);

if (indexPropertyToFound >= 0) {
let startIndex = content.indexOf('"', indexPropertyToFound + propertyToFound.length);
let startExtract = content.substr(0, startIndex + 1);
let endExtract = content.substr(startIndex + 1);
endExtract = endExtract.substr(endExtract.indexOf('"'));

return `${startExtract}${value}${endExtract}`;
}

return content;
}

/**
* @param {string} content
* @param {string} value
* @returns {string}
*/
static replaceJsonVersionProperty(content, value) {
return Utils.replaceJsonProperty(content, 'version', value);
}

/**
* @param {string} filePath
* @returns {Promise}
*/
static writeFile(filePath, content) {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, content, (err) => {
if (err) {
reject(err);

} else {
resolve();
}
});
});
}
}

module.exports = Utils;
Loading

0 comments on commit 400114c

Please sign in to comment.