Skip to content

Commit

Permalink
Versioning node script
Browse files Browse the repository at this point in the history
  • Loading branch information
jshjohnson committed Feb 9, 2017
1 parent ad68fb2 commit 3900230
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
51 changes: 51 additions & 0 deletions deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs'),
path = require('path'),
config = {
files: ['bower.json', 'package.json', 'index.html']
};

/**
* Convert node arguments into an object
* @return {Object} Arguments
*/
const argvToObject = () => {
const args = {};
let arg = null;
process.argv.forEach((val, index) => {
if(/^--/.test(val)) {
arg = {
index: index,
name: val.replace(/^--/, '')
}
return;
}

if(arg && ((arg.index+1 === index ))) {
args[arg.name] = val;
}
});

return args;
};

/**
* Loop through passed files updating version number
* @param {Object} config
*/
const updateVersion = (config) => {
const args = argvToObject();
const currentVersion = args.current;
const newVersion = args.new;
config.files.forEach((file) => {
const filePath = path.join(__dirname, file);
const regex = new RegExp(currentVersion, 'g');

let contents = fs.readFileSync(filePath, 'utf-8');
contents = contents.replace(regex, newVersion);
fs.writeFileSync(filePath, contents);
});

console.log(`Updated version to ${newVersion}`);
};

updateVersion(config);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"js:build": "concurrently --prefix-colors yellow,green \"webpack --minimize --config webpack.config.prod.js\" \"webpack --config webpack.config.prod.js\"",
"js:test": "./node_modules/karma/bin/karma start --single-run --no-auto-watch tests/karma.config.js",
"js:test:watch": "./node_modules/karma/bin/karma start --auto-watch --no-single-run tests/karma.config.js",
"preversion": "npm run js:build"
"version": "node version.js --current $npm_package_version --new $npm_config_newVersion",
"postversion": "npm run js:build"
},
"repository": {
"type": "git",
Expand Down
50 changes: 50 additions & 0 deletions version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Example usage: npm --newVersion=2.7.2 run version

const fs = require('fs'),
path = require('path'),
config = {
files: ['bower.json', 'package.json', 'index.html']
};

/**
* Convert node arguments into an object
* @return {Object} Arguments
*/
const argvToObject = () => {
const args = {};
let arg = null;
process.argv.forEach((val, index) => {
if(/^--/.test(val)) {
arg = {
index: index,
name: val.replace(/^--/, '')
}
return;
}

if(arg && ((arg.index+1 === index ))) {
args[arg.name] = val;
}
});

return args;
};

const updateVersion = (config) => {
const args = argvToObject();
const currentVersion = args.current;
const newVersion = args.new;
console.log(args);
config.files.forEach((file) => {
const filePath = path.join(__dirname, file);
const regex = new RegExp(currentVersion, 'g');

let contents = fs.readFileSync(filePath, 'utf-8');
contents = contents.replace(regex, newVersion);
fs.writeFileSync(filePath, contents);
});

console.log(`Updated version to ${newVersion}`);
};

updateVersion(config);

0 comments on commit 3900230

Please sign in to comment.