diff --git a/deploy.js b/deploy.js new file mode 100644 index 0000000..ad948cd --- /dev/null +++ b/deploy.js @@ -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); diff --git a/package.json b/package.json index e60a9db..37f860a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/version.js b/version.js new file mode 100644 index 0000000..9aa7076 --- /dev/null +++ b/version.js @@ -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);