Skip to content

Commit 3900230

Browse files
committed
Versioning node script
1 parent ad68fb2 commit 3900230

3 files changed

Lines changed: 103 additions & 1 deletion

File tree

deploy.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const fs = require('fs'),
2+
path = require('path'),
3+
config = {
4+
files: ['bower.json', 'package.json', 'index.html']
5+
};
6+
7+
/**
8+
* Convert node arguments into an object
9+
* @return {Object} Arguments
10+
*/
11+
const argvToObject = () => {
12+
const args = {};
13+
let arg = null;
14+
process.argv.forEach((val, index) => {
15+
if(/^--/.test(val)) {
16+
arg = {
17+
index: index,
18+
name: val.replace(/^--/, '')
19+
}
20+
return;
21+
}
22+
23+
if(arg && ((arg.index+1 === index ))) {
24+
args[arg.name] = val;
25+
}
26+
});
27+
28+
return args;
29+
};
30+
31+
/**
32+
* Loop through passed files updating version number
33+
* @param {Object} config
34+
*/
35+
const updateVersion = (config) => {
36+
const args = argvToObject();
37+
const currentVersion = args.current;
38+
const newVersion = args.new;
39+
config.files.forEach((file) => {
40+
const filePath = path.join(__dirname, file);
41+
const regex = new RegExp(currentVersion, 'g');
42+
43+
let contents = fs.readFileSync(filePath, 'utf-8');
44+
contents = contents.replace(regex, newVersion);
45+
fs.writeFileSync(filePath, contents);
46+
});
47+
48+
console.log(`Updated version to ${newVersion}`);
49+
};
50+
51+
updateVersion(config);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"js:build": "concurrently --prefix-colors yellow,green \"webpack --minimize --config webpack.config.prod.js\" \"webpack --config webpack.config.prod.js\"",
1414
"js:test": "./node_modules/karma/bin/karma start --single-run --no-auto-watch tests/karma.config.js",
1515
"js:test:watch": "./node_modules/karma/bin/karma start --auto-watch --no-single-run tests/karma.config.js",
16-
"preversion": "npm run js:build"
16+
"version": "node version.js --current $npm_package_version --new $npm_config_newVersion",
17+
"postversion": "npm run js:build"
1718
},
1819
"repository": {
1920
"type": "git",

version.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Example usage: npm --newVersion=2.7.2 run version
2+
3+
const fs = require('fs'),
4+
path = require('path'),
5+
config = {
6+
files: ['bower.json', 'package.json', 'index.html']
7+
};
8+
9+
/**
10+
* Convert node arguments into an object
11+
* @return {Object} Arguments
12+
*/
13+
const argvToObject = () => {
14+
const args = {};
15+
let arg = null;
16+
process.argv.forEach((val, index) => {
17+
if(/^--/.test(val)) {
18+
arg = {
19+
index: index,
20+
name: val.replace(/^--/, '')
21+
}
22+
return;
23+
}
24+
25+
if(arg && ((arg.index+1 === index ))) {
26+
args[arg.name] = val;
27+
}
28+
});
29+
30+
return args;
31+
};
32+
33+
const updateVersion = (config) => {
34+
const args = argvToObject();
35+
const currentVersion = args.current;
36+
const newVersion = args.new;
37+
console.log(args);
38+
config.files.forEach((file) => {
39+
const filePath = path.join(__dirname, file);
40+
const regex = new RegExp(currentVersion, 'g');
41+
42+
let contents = fs.readFileSync(filePath, 'utf-8');
43+
contents = contents.replace(regex, newVersion);
44+
fs.writeFileSync(filePath, contents);
45+
});
46+
47+
console.log(`Updated version to ${newVersion}`);
48+
};
49+
50+
updateVersion(config);

0 commit comments

Comments
 (0)