diff --git a/README.md b/README.md index e519477..7c4d4cd 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,102 @@ # In development -# Installation -npm i webpack-auto-inject-version --save-dev +## What +AIV can inject version number for all your bundle files (css,js,html).

+Example js: +```js +// [AIV] Build version: 1.0.10 +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +``` +

+Example html: +```html + + + +``` -# Usage -Add plugin to your webpack configuration. +AIV can also auto inject your version number into html by using special code ( <{version}> ).

+Example: +```html +My awesome project | <{version}> +``` -Require it by: - var WebpackAutoInject = require('webpack-auto-inject-version'); +
+ +## Install + +```console +$ npm install webpack-auto-inject-version --save-dev +``` +
+ +## Usage + +```js +var WebpackAutoInject = require('webpack-auto-inject-version'); + +module.exports = { -And add to plugins array as one of the last items ( further = better ). plugins: [ - new WebpackAutoInject(options) + new WebpackAutoInject({ + autoIncrease : boolean, + injectIntoHtml : boolean, + injectIntoHtmlRegex : regex, + injectIntoAnyFile : boolean + }) ] -# Options -NOT SUPPORTED YET! - autoIncrease : boolean, - injectIntoHtml : boolean, +} +``` -# Auto Increase Version -Option: autoIncrease : true -- run webpack with --release major|minor|patch -DO NOT RUN IT WITH WATCH! \ No newline at end of file +
+ +## Options +By default you don't need to pass any options, all options from Usage section are set by default.

+ +
+ +### autoIncrease +Auto increase package.json number.
+This option requires extra argument to be sent to webpack build.
+Arguments: --major --minor --patch

+ +
+ +Example for package.json run type, npm run start => ( 1.2.10 to 2.0.0 ) +```json + "version" : "1.2.10", + "scripts": { + "start": "webpack --major" + } +``` +Default: true + +
+ +### injectIntoHtml +Inject version number ( increased if autoIncrease is set correctly ) into HTML template
+For this to work you need to place <{version}> inside your html file.

+Example: +```html +My awesome project | <{version}> +``` +Default: true + + +
+ + +### injectIntoHtmlRegex +Regex to find your html file, where injectIntoHtml should try to find your <{version}> tag.
+Default: /^index\.html$/ + + +
+ + +### injectIntoAnyFile +This will inject your version file as a comment into any css,js,html file.
+Default: true \ No newline at end of file diff --git a/dist/components/auto-inc-version.js b/dist/components/auto-inc-version.js index 1d997a1..28d591f 100644 --- a/dist/components/auto-inc-version.js +++ b/dist/components/auto-inc-version.js @@ -1,26 +1,68 @@ var semver = require('semver'); var config = require('../config'); +var path = require('path'); +var fs = require('fs'); +var u = require('../core/utils'); +var chalk = require('chalk'); +var Promise = require('bluebird'); var IncVersion = (function () { - function IncVersion() { + function IncVersion(context) { + this.context = context; } + IncVersion.prototype.apply = function () { + var _this = this; + return new Promise(function (resolve, reject) { + _this.resolve = resolve; + _this.reject = reject; + _this.start(); + }); + }; + IncVersion.prototype.start = function () { + this.packageFile = this.openPackageFile(); + var argv = process.argv; + if (u.isArgv('major')) { + this.major(); + } + else if (u.isArgv('minor')) { + this.minor(); + } + else if (u.isArgv('patch')) { + this.patch(); + } + else { + console.log(chalk.bgRed("[@] " + config.SHORT + " error > ") + ' --major --minor --patch missing in arguments. '); + console.log(chalk.bgRed("[@] " + config.SHORT + " how to> ") + ' webpack -w --major'); + this.reject(); + } + }; IncVersion.prototype.openPackageFile = function () { return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8')); }; - IncVersion.prototype.closePackageFile = function (content) { - fs.writeFile("/tmp/test", content, function (err) { + IncVersion.prototype.closePackageFile = function (newVersion) { + var _this = this; + this.packageFile.version = newVersion; + fs.writeFile(path.normalize(config.PATH_PACKAGE), JSON.stringify(this.packageFile, null, 4), function (err) { if (err) { + _this.reject(err); return console.log(err); } - console.log("The file was saved!"); + console.log(''); + console.log(chalk.bgGreen("[@] " + config.SHORT + " OK > ") + ' package.json updated : ' + _this.packageFile.version); + _this.resolve(); }); }; IncVersion.prototype.major = function () { - this.openPackageFile(); - this.closePackageFile(); + var newVersion = semver.inc(this.packageFile.version, 'major'); + this.closePackageFile(newVersion); }; IncVersion.prototype.minor = function () { + var newVersion = semver.inc(this.packageFile.version, 'minor'); + this.closePackageFile(newVersion); }; IncVersion.prototype.patch = function () { + var newVersion = semver.inc(this.packageFile.version, 'patch'); + this.closePackageFile(newVersion); }; return IncVersion; }()); +module.exports = IncVersion; diff --git a/dist/components/inject-into-any-file.js b/dist/components/inject-into-any-file.js index dbc9d14..4c45825 100644 --- a/dist/components/inject-into-any-file.js +++ b/dist/components/inject-into-any-file.js @@ -27,6 +27,7 @@ var InjectIntoAnyFile = (function () { } cb(); }); + return new Promise(function (resolve, reject) { resolve(); }); }; InjectIntoAnyFile.prototype.injectIntoCss = function (asset) { var modAsset = "/** [" + config.SHORT + "] Build version: " + this.context.version + " **/ " + endOfLine + " " + asset.source() + " "; diff --git a/dist/components/inject-into-html.js b/dist/components/inject-into-html.js index a50c7be..892a534 100644 --- a/dist/components/inject-into-html.js +++ b/dist/components/inject-into-html.js @@ -7,7 +7,7 @@ var InjectIntoHtml = (function () { var _this = this; this.context.compiler.plugin('emit', function (compilation, cb) { var _loop_1 = function() { - if (/^index\.html$/.test(basename)) { + if (_this.context.options.injectIntoHtmlRegex.test(basename)) { var asset = compilation.assets[basename]; var modFile_1 = asset.source().replace(/(\<\{version\}\>)/g, _this.context.version); asset.source = function () { return modFile_1; }; @@ -18,6 +18,7 @@ var InjectIntoHtml = (function () { } cb(); }); + return new Promise(function (resolve, reject) { resolve(); }); }; return InjectIntoHtml; }()); diff --git a/dist/config.js b/dist/config.js index 86db92e..b21f989 100644 --- a/dist/config.js +++ b/dist/config.js @@ -1,5 +1,19 @@ module.exports = { NAME: 'Auto Inject Version', SHORT: 'AIV', - PATH_PACKAGE: './package.json' + PATH_PACKAGE: './package.json', + COMPONENTS: [ + { + option: 'autoIncrease', + path: './components/auto-inc-version' + }, + { + option: 'injectIntoHtml', + path: './components/inject-into-html' + }, + { + option: 'injectIntoAnyFile', + path: './components/inject-into-any-file' + } + ] }; diff --git a/dist/main.js b/dist/main.js index 99969a7..e367a0e 100644 --- a/dist/main.js +++ b/dist/main.js @@ -2,29 +2,42 @@ var chalk = require('chalk'); var fs = require('fs'); var path = require('path'); var config = require('./config'); +var Promise = require('bluebird'); +var u = require('./core/utils'); 'use strict'; var WebpackAutoInject = (function () { function WebpackAutoInject(options) { - this.options = WebpackAutoInject.options; + this.options = u.merge(WebpackAutoInject.options, options); var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8')); this.version = packageFile.version; } WebpackAutoInject.prototype.apply = function (compiler) { this.compiler = compiler; - if (this.options.injectIntoHtml) { - var comp_1 = new (require('./components/auto-inc-version'))(this); - comp_1.apply(); + this.components = config.COMPONENTS; + this.executeComponents(); + }; + WebpackAutoInject.prototype.executeComponents = function () { + var _this = this; + if (!this.components.length) { + console.log(chalk.bgRed('AIS: DONE!')); + return; } - if (this.options.injectIntoHtml) { - var comp_2 = new (require('./components/inject-into-html'))(this); - comp_2.apply(); + var comp = this.components.shift(); + if (this.options[comp.option]) { + var inst = new (require(comp.path))(this); + inst.apply().then(function () { + _this.executeComponents(); + }, function (err) { console.log(err); }); + } + else { + this.executeComponents(); } - var comp = new (require('./components/inject-into-any-file'))(this); - comp.apply(); }; WebpackAutoInject.options = { autoIncrease: true, injectIntoHtml: true, + injectIntoHtmlRegex: /^index\.html$/, + injectIntoAnyFile: true }; return WebpackAutoInject; }()); diff --git a/package.json b/package.json index 57b8f74..408efb5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-auto-inject-version", - "version": "0.0.19", + "version": "0.1.0", "description": "Webpack plugin for auto inject version from package.json", "main": "dist/main.js", "scripts": { @@ -13,6 +13,7 @@ "typings": "^1.4.0" }, "dependencies": { + "bluebird": "^3.4.6", "semver": "^5.3.0" } } diff --git a/src/components/auto-inc-version.ts b/src/components/auto-inc-version.ts index ce8b306..474df9f 100644 --- a/src/components/auto-inc-version.ts +++ b/src/components/auto-inc-version.ts @@ -1,30 +1,93 @@ -var semver = require('semver'); -var config = require('../config'); +var semver = require('semver'); +var config = require('../config'); +var path = require('path'); +var fs = require('fs'); +var u = require('../core/utils'); +var chalk = require('chalk'); +var Promise = require('bluebird'); class IncVersion{ + private packageFile; + private resolve; + private reject; + + constructor(private context) {} + + public apply() { + return new Promise((resolve, reject) => { + this.resolve = resolve; + this.reject = reject; + this.start(); + }); + } + + /** + * Start version increase + * - decide scenario: major, minor, patch + */ + private start() { + this.packageFile = this.openPackageFile(); + let argv = process.argv; + if( u.isArgv('major') ) { + this.major(); + } + else if( u.isArgv('minor') ) { + this.minor(); + }else if( u.isArgv('patch') ) { + this.patch(); + }else { + console.log(chalk.bgRed(`[@] ${config.SHORT} error > `)+' --major --minor --patch missing in arguments. '); + console.log(chalk.bgRed(`[@] ${config.SHORT} how to> `)+' webpack -w --major'); + this.reject(); + } + } + + /** + * Open package file + * @returns {any} + */ private openPackageFile() { return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8')); } - private closePackageFile(content) { - fs.writeFile("/tmp/test", content, function(err) { - if(err) {return console.log(err);} - console.log("The file was saved!"); + /** + * Close & save package file + * @param newVersion + */ + private closePackageFile(newVersion) { + this.packageFile.version = newVersion; + fs.writeFile(path.normalize(config.PATH_PACKAGE), JSON.stringify(this.packageFile, null, 4), (err) => { + if(err) {this.reject(err); return console.log(err);} + console.log(''); + console.log(chalk.bgGreen(`[@] ${config.SHORT} OK > `)+' package.json updated : ' + this.packageFile.version); + this.resolve(); }); } - public major() { - this.openPackageFile(); - - this.closePackageFile(); + /** + * Increase major + */ + private major() { + let newVersion = semver.inc(this.packageFile.version, 'major'); + this.closePackageFile(newVersion); } - public minor() { - + /** + * Increase minor + */ + private minor() { + let newVersion = semver.inc(this.packageFile.version, 'minor'); + this.closePackageFile(newVersion); } - public patch() { - + /** + * Increase patch + */ + private patch() { + let newVersion = semver.inc(this.packageFile.version, 'patch'); + this.closePackageFile(newVersion); } -} \ No newline at end of file +} + +module.exports = IncVersion; \ No newline at end of file diff --git a/src/components/inject-into-any-file.ts b/src/components/inject-into-any-file.ts index 6ff222a..660c84e 100644 --- a/src/components/inject-into-any-file.ts +++ b/src/components/inject-into-any-file.ts @@ -34,6 +34,7 @@ class InjectIntoAnyFile{ } cb(); }); + return new Promise((resolve, reject) => { resolve(); }) } injectIntoCss(asset) { diff --git a/src/components/inject-into-html.ts b/src/components/inject-into-html.ts index 864cf66..fd0538d 100644 --- a/src/components/inject-into-html.ts +++ b/src/components/inject-into-html.ts @@ -15,7 +15,7 @@ class InjectIntoHtml{ apply() { this.context.compiler.plugin('emit', (compilation, cb) => { for ( var basename in compilation.assets ) { - if(/^index\.html$/.test(basename)) { + if(this.context.options.injectIntoHtmlRegex.test(basename)) { let asset = compilation.assets[basename]; let modFile = asset.source().replace(/(\<\{version\}\>)/g, this.context.version); asset.source = () => modFile; @@ -23,6 +23,7 @@ class InjectIntoHtml{ } cb(); }); + return new Promise((resolve, reject) => { resolve(); }) } } diff --git a/src/config.ts b/src/config.ts index de63ad6..7d20b63 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,19 @@ module.exports = { NAME : 'Auto Inject Version', SHORT : 'AIV', - PATH_PACKAGE : './package.json' + PATH_PACKAGE : './package.json', + COMPONENTS : [ + { + option : 'autoIncrease', + path : './components/auto-inc-version' + }, + { + option : 'injectIntoHtml', + path : './components/inject-into-html' + }, + { + option : 'injectIntoAnyFile', + path : './components/inject-into-any-file' + } + ] } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 9f71c39..0fe16fa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,8 @@ var chalk = require('chalk'); var fs = require('fs'); var path = require('path'); var config = require('./config'); +var Promise = require('bluebird'); +var u = require('./core/utils'); 'use strict'; @@ -11,14 +13,17 @@ class WebpackAutoInject{ private options; private compiler; private version; + private components; static options = { - autoIncrease : true, - injectIntoHtml : true, + autoIncrease : true, + injectIntoHtml : true, + injectIntoHtmlRegex : /^index\.html$/, + injectIntoAnyFile : true } constructor(options) { - this.options = WebpackAutoInject.options; + this.options = u.merge(WebpackAutoInject.options, options); var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8')); this.version = packageFile.version; } @@ -27,24 +32,26 @@ class WebpackAutoInject{ this.compiler = compiler; - // Component: auto-inc-version - // if: autoIncrease : true - if(this.options.injectIntoHtml) { - let comp = new (require('./components/auto-inc-version'))(this); - comp.apply(); + this.components = config.COMPONENTS; + + this.executeComponents(); + + } + + private executeComponents() { + + if(!this.components.length) { console.log(chalk.bgRed('AIS: DONE!')); return;} + + let comp = this.components.shift(); + + if(this.options[comp.option]) { + let inst = new (require(comp.path))(this); + inst.apply().then(() => { + this.executeComponents(); + }, (err) => {console.log(err);}) + }else{ + this.executeComponents(); } - - - // Component: Inject-into-html - // if: injectIntoHtml : true - if(this.options.injectIntoHtml) { - let comp = new (require('./components/inject-into-html'))(this); - comp.apply(); - } - - let comp = new (require('./components/inject-into-any-file'))(this); - comp.apply(); - } }