Improvments and refactoring

This commit is contained in:
radswiat 2016-10-21 08:32:35 +01:00
parent 13ad8e603c
commit 7974472559
14 changed files with 379 additions and 112 deletions

View file

@ -1,6 +1,13 @@
# In development # In development
## What ## What
Auto Inject Version (AIV) can:
- inject version from package.json into every bundle file as a comment ( at the top )
- inject version from package.json into any place in your HTML by special tag <{version}>
- inject version from package.json into any place in CSS/JS file by special tag <{version}>
- auto increase version by --major, --minor, --patch and then inject as chosen
## Desc
AIV can inject version number for all your bundle files (css,js,html).<br><br> AIV can inject version number for all your bundle files (css,js,html).<br><br>
Example js: Example js:
```js ```js
@ -38,29 +45,20 @@ $ npm install webpack-auto-inject-version --save-dev
var WebpackAutoInject = require('webpack-auto-inject-version'); var WebpackAutoInject = require('webpack-auto-inject-version');
module.exports = { module.exports = {
plugins: [ plugins: [
new WebpackAutoInject({ new WebpackAutoInject(options)
autoIncrease : boolean,
injectIntoHtml : boolean,
injectIntoHtmlRegex : regex,
injectIntoAnyFile : boolean
})
] ]
} }
``` ```
<br> <br>
## Options ## Options
By default you don't need to pass any options, all options from Usage section are set by default.<br><br>
<br>
### autoIncrease ### autoIncrease
Auto increase package.json number. <br> Auto increase package.json number. <br>
This option requires extra argument to be sent to webpack build. <br> This option requires extra argument to be sent to webpack build. <br>
It happens before anything else to make sure that your new version is injected into your files.<br>
Arguments: --major --minor --patch<br><br> Arguments: --major --minor --patch<br><br>
<br> <br>
@ -76,27 +74,32 @@ Default: true
<br> <br>
### injectIntoHtml ### injectByTag
Inject version number ( increased if autoIncrease is set correctly ) into HTML template<br> Inject version number into your file<br>
For this to work you need to place <{version}> inside your html file.<br><br> Version will replace the <{version}> tag.<br>
Example:
```html ```html
<span>My awesome project | <{version}></span> <span>My awesome project | <{version}></span>
``` ```
```js
var version = '<{version}>';
```
Default: true Default: true
<br> <br>
### injectIntoHtmlRegex ### injectByTagFileRegex
Regex to find your html file, where injectIntoHtml should try to find your <{version}> tag.<br> Regex against file name. If match, injectByTag will try to find version tag and replace it.
Only html files: /^(.){1,}\.html$/ <br>
Only js files: ^(.){1,}\.js$ <br>
Any file: (.){1,} <br>
Default: /^index\.html$/ Default: /^index\.html$/
<br> <br>
### injectIntoAnyFile ### injectAsComment
This will inject your version file as a comment into any css,js,html file.<br> This will inject your version as a comment into any css,js,html file.<br>
Default: true Default: true

View file

@ -5,6 +5,7 @@ var fs = require('fs');
var u = require('../core/utils'); var u = require('../core/utils');
var chalk = require('chalk'); var chalk = require('chalk');
var Promise = require('bluebird'); var Promise = require('bluebird');
var log = require('../core/log');
var IncVersion = (function () { var IncVersion = (function () {
function IncVersion(context) { function IncVersion(context) {
this.context = context; this.context = context;
@ -19,7 +20,6 @@ var IncVersion = (function () {
}; };
IncVersion.prototype.start = function () { IncVersion.prototype.start = function () {
this.packageFile = this.openPackageFile(); this.packageFile = this.openPackageFile();
var argv = process.argv;
if (u.isArgv('major')) { if (u.isArgv('major')) {
this.major(); this.major();
} }
@ -30,8 +30,6 @@ var IncVersion = (function () {
this.patch(); this.patch();
} }
else { 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(); this.reject();
} }
}; };
@ -46,8 +44,9 @@ var IncVersion = (function () {
_this.reject(err); _this.reject(err);
return console.log(err); return console.log(err);
} }
console.log(''); log.info("autoIncVersion : new version : " + newVersion);
console.log(chalk.bgGreen("[@] " + config.SHORT + " OK > ") + ' package.json updated : ' + _this.packageFile.version); log.info('package.json updated!');
_this.context.version = newVersion;
_this.resolve(); _this.resolve();
}); });
}; };

49
dist/components/inject-as-comment.js vendored Normal file
View file

@ -0,0 +1,49 @@
var chalk = require('chalk');
var path = require('path');
var endOfLine = require('os').EOL;
var config = require('../config');
var log = require('../core/log');
'use strict';
var InjectAsComment = (function () {
function InjectAsComment(context) {
this.context = context;
}
InjectAsComment.prototype.apply = function () {
var _this = this;
this.context.compiler.plugin('emit', function (compilation, cb) {
for (var basename in compilation.assets) {
var ext = path.extname(basename);
var asset = compilation.assets[basename];
switch (ext) {
case '.js':
_this.injectIntoJs(asset);
break;
case '.html':
_this.injectIntoHtml(asset);
break;
case '.css':
_this.injectIntoCss(asset);
break;
case 'default': break;
}
log.info("InjectAsComment : match : " + basename + " : injected : " + _this.context.version);
}
cb();
});
return new Promise(function (resolve, reject) { resolve(); });
};
InjectAsComment.prototype.injectIntoCss = function (asset) {
var modAsset = "/** [" + config.SHORT + "] Build version: " + this.context.version + " **/ " + endOfLine + " " + asset.source() + " ";
asset.source = function () { return modAsset; };
};
InjectAsComment.prototype.injectIntoHtml = function (asset) {
var modAsset = "<!-- [" + config.SHORT + "] Build version: " + this.context.version + " --> " + endOfLine + " " + asset.source() + " ";
asset.source = function () { return modAsset; };
};
InjectAsComment.prototype.injectIntoJs = function (asset) {
var modAsset = "// [" + config.SHORT + "] Build version: " + this.context.version + " " + endOfLine + " " + asset.source() + " ";
asset.source = function () { return modAsset; };
};
return InjectAsComment;
}());
module.exports = InjectAsComment;

31
dist/components/inject-by-tag.js vendored Normal file
View file

@ -0,0 +1,31 @@
var log = require('../core/log');
'use strict';
var InjectByTag = (function () {
function InjectByTag(context) {
this.context = context;
}
InjectByTag.prototype.apply = function () {
var _this = this;
this.context.compiler.plugin('emit', function (compilation, cb) {
var _loop_1 = function() {
if (_this.context.options.injectByTagFileRegex.test(basename)) {
var replaced_1 = 0;
var asset = compilation.assets[basename];
var modFile_1 = asset.source().replace(/(\<\{version\}\>)/g, function () {
replaced_1++;
return _this.context.version;
});
asset.source = function () { return modFile_1; };
log.info("InjectByTag : match : " + basename + " : replaced : " + replaced_1);
}
};
for (var basename in compilation.assets) {
_loop_1();
}
cb();
});
return new Promise(function (resolve, reject) { resolve(); });
};
return InjectByTag;
}());
module.exports = InjectByTag;

25
dist/config.js vendored
View file

@ -2,18 +2,23 @@ module.exports = {
NAME: 'Auto Inject Version', NAME: 'Auto Inject Version',
SHORT: 'AIV', SHORT: 'AIV',
PATH_PACKAGE: './package.json', PATH_PACKAGE: './package.json',
COMPONENTS: [ NON_WEBPACK_COMPONENTS: [
{ {
option: 'autoIncrease', option: 'autoIncrease',
path: './components/auto-inc-version' path: './components/auto-inc-version'
},
{
option: 'injectIntoHtml',
path: './components/inject-into-html'
},
{
option: 'injectIntoAnyFile',
path: './components/inject-into-any-file'
} }
] ],
WEBPACK_COMPONENTS: [
{
option: 'injectByTag',
path: './components/inject-by-tag'
},
{
option: 'injectAsComment',
path: './components/inject-as-comment'
}
],
LOGS_TEXT: {
AIS_START: 'Auto inject version started'
}
}; };

47
dist/core/log.js vendored Normal file
View file

@ -0,0 +1,47 @@
var config = require('../config');
var chalk = require('chalk');
var endOfLine = require('os').EOL;
var u = require('./utils');
var Log = (function () {
function Log() {
this.logLevel = 3;
this.getLogLevel();
}
Log.prototype.getLogLevel = function () {
if (u.isArgv('aiv-log-full')) {
this.logLevel = 3;
}
else if (u.isArgv('aiv-log-none')) {
this.logLevel = 0;
}
};
Log.prototype.getHead = function () {
return endOfLine + chalk.bgYellow.black('[AIV] : ');
};
Log.prototype.getText = function (id) {
return config.LOGS_TEXT[id];
};
Log.prototype.call = function (type, msgId) {
if (typeof this[type] === 'function') {
this[type](this.getText(msgId));
}
};
Log.prototype.error = function (msg) {
if (this.logLevel < 3)
return;
console.log(this.getHead() + " " + chalk.red('error') + " : " + msg);
};
Log.prototype.info = function (msg) {
if (!this.logLevel)
return;
console.log(this.getHead() + " " + chalk.blue('info') + " : " + msg);
};
Log.prototype.warn = function (msg) {
if (!this.logLevel)
return;
console.log(this.getHead() + " " + chalk.yellow('warn') + " : " + msg);
};
return Log;
}());
var log = new Log();
module.exports = log;

43
dist/main.js vendored
View file

@ -4,40 +4,49 @@ var path = require('path');
var config = require('./config'); var config = require('./config');
var Promise = require('bluebird'); var Promise = require('bluebird');
var u = require('./core/utils'); var u = require('./core/utils');
var log = require('./core/log');
'use strict'; 'use strict';
var WebpackAutoInject = (function () { var WebpackAutoInject = (function () {
function WebpackAutoInject(options) { function WebpackAutoInject(options) {
this.options = u.merge(WebpackAutoInject.options, options); this.options = u.merge(WebpackAutoInject.options, options);
var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8')); var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
this.version = packageFile.version; this.version = packageFile.version;
log.call('info', 'AIS_START');
this.executeNoneWebpackComponents();
} }
WebpackAutoInject.prototype.apply = function (compiler) { WebpackAutoInject.prototype.apply = function (compiler) {
this.compiler = compiler; this.compiler = compiler;
this.components = config.COMPONENTS; this.executeWebpackComponents();
this.executeComponents();
}; };
WebpackAutoInject.prototype.executeComponents = function () { WebpackAutoInject.prototype.executeNoneWebpackComponents = function () {
this.executeComponents(config.NON_WEBPACK_COMPONENTS, function () {
});
};
WebpackAutoInject.prototype.executeWebpackComponents = function () {
this.executeComponents(config.WEBPACK_COMPONENTS, function () {
});
};
WebpackAutoInject.prototype.executeComponents = function (components, done) {
var _this = this; var _this = this;
if (!this.components.length) { if (!components.length) {
console.log(chalk.bgRed('AIS: DONE!')); done();
return; return;
} }
var comp = this.components.shift(); var comp = components.shift();
if (this.options[comp.option]) { if (!this.options[comp.option]) {
var inst = new (require(comp.path))(this); this.executeComponents(components, done);
inst.apply().then(function () { return;
_this.executeComponents();
}, function (err) { console.log(err); });
}
else {
this.executeComponents();
} }
var inst = new (require(comp.path))(this);
inst.apply().then(function () {
_this.executeComponents(components, done);
}, function (err) { _this.executeComponents(components, done); });
}; };
WebpackAutoInject.options = { WebpackAutoInject.options = {
autoIncrease: true, autoIncrease: true,
injectIntoHtml: true, injectAsComment: true,
injectIntoHtmlRegex: /^index\.html$/, injectByTag: true,
injectIntoAnyFile: true injectByTagFileRegex: /^index\.html$/
}; };
return WebpackAutoInject; return WebpackAutoInject;
}()); }());

View file

@ -10,11 +10,12 @@
"author": "Radoslaw Swiat", "author": "Radoslaw Swiat",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"chalk": "^1.1.3",
"typings": "^1.4.0" "typings": "^1.4.0"
}, },
"dependencies": { "dependencies": {
"bluebird": "^3.4.6", "bluebird": "^3.4.6",
"semver": "^5.3.0" "semver": "^5.3.0",
"chalk": "^1.1.3"
} }
} }

View file

@ -1,10 +1,11 @@
var semver = require('semver'); const semver = require('semver');
var config = require('../config'); const config = require('../config');
var path = require('path'); const path = require('path');
var fs = require('fs'); const fs = require('fs');
var u = require('../core/utils'); const u = require('../core/utils');
var chalk = require('chalk'); const chalk = require('chalk');
var Promise = require('bluebird'); const Promise = require('bluebird');
const log = require('../core/log');
class IncVersion{ class IncVersion{
@ -28,7 +29,6 @@ class IncVersion{
*/ */
private start() { private start() {
this.packageFile = this.openPackageFile(); this.packageFile = this.openPackageFile();
let argv = process.argv;
if( u.isArgv('major') ) { if( u.isArgv('major') ) {
this.major(); this.major();
} }
@ -37,8 +37,6 @@ class IncVersion{
}else if( u.isArgv('patch') ) { }else if( u.isArgv('patch') ) {
this.patch(); this.patch();
}else { }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(); this.reject();
} }
} }
@ -59,8 +57,9 @@ class IncVersion{
this.packageFile.version = newVersion; this.packageFile.version = newVersion;
fs.writeFile(path.normalize(config.PATH_PACKAGE), JSON.stringify(this.packageFile, null, 4), (err) => { fs.writeFile(path.normalize(config.PATH_PACKAGE), JSON.stringify(this.packageFile, null, 4), (err) => {
if(err) {this.reject(err); return console.log(err);} if(err) {this.reject(err); return console.log(err);}
console.log(''); log.info(`autoIncVersion : new version : ${newVersion}`)
console.log(chalk.bgGreen(`[@] ${config.SHORT} OK > `)+' package.json updated : ' + this.packageFile.version); log.info('package.json updated!');
this.context.version = newVersion;
this.resolve(); this.resolve();
}); });
} }

View file

@ -1,8 +1,9 @@
/// <reference path='../../typings/index.d.ts' /> /// <reference path='../../typings/index.d.ts' />
var chalk = require('chalk'); const chalk = require('chalk');
var path = require('path'); const path = require('path');
var endOfLine = require('os').EOL; const endOfLine = require('os').EOL;
var config = require('./../config'); const config = require('../config');
const log = require('../core/log');
'use strict'; 'use strict';
@ -11,7 +12,7 @@ var config = require('./../config');
* - done by parsing html file, * - done by parsing html file,
* > replace: <{version}> * > replace: <{version}>
*/ */
class InjectIntoAnyFile{ class InjectAsComment{
constructor(private context) {} constructor(private context) {}
@ -30,7 +31,9 @@ class InjectIntoAnyFile{
case '.css' : case '.css' :
this.injectIntoCss(asset); this.injectIntoCss(asset);
break; break;
case 'default': break;
} }
log.info(`InjectAsComment : match : ${basename} : injected : ${this.context.version}`);
} }
cb(); cb();
}); });
@ -53,4 +56,4 @@ class InjectIntoAnyFile{
} }
} }
module.exports = InjectIntoAnyFile; module.exports = InjectAsComment;

View file

@ -1,4 +1,5 @@
/// <reference path='../../typings/index.d.ts' /> /// <reference path='../../typings/index.d.ts' />
const log = require('../core/log');
'use strict'; 'use strict';
@ -7,18 +8,24 @@
* - done by parsing html file, * - done by parsing html file,
* > replace: <{version}> * > replace: <{version}>
*/ */
class InjectIntoHtml{ class InjectByTag{
constructor(private context) {} constructor(private context) {}
apply() { apply() {
this.context.compiler.plugin('emit', (compilation, cb) => { this.context.compiler.plugin('emit', (compilation, cb) => {
// for every output file
for ( var basename in compilation.assets ) { for ( var basename in compilation.assets ) {
if(this.context.options.injectIntoHtmlRegex.test(basename)) { // only if match regex
if(this.context.options.injectByTagFileRegex.test(basename)) {
let replaced = 0;
let asset = compilation.assets[basename]; let asset = compilation.assets[basename];
let modFile = asset.source().replace(/(\<\{version\}\>)/g, this.context.version); let modFile = asset.source().replace(/(\<\{version\}\>)/g, () => {
replaced++;
return this.context.version;
});
asset.source = () => modFile; asset.source = () => modFile;
log.info(`InjectByTag : match : ${basename} : replaced : ${replaced}`);
} }
} }
cb(); cb();
@ -27,4 +34,4 @@ class InjectIntoHtml{
} }
} }
module.exports = InjectIntoHtml; module.exports = InjectByTag;

View file

@ -2,18 +2,23 @@ module.exports = {
NAME : 'Auto Inject Version', NAME : 'Auto Inject Version',
SHORT : 'AIV', SHORT : 'AIV',
PATH_PACKAGE : './package.json', PATH_PACKAGE : './package.json',
COMPONENTS : [ NON_WEBPACK_COMPONENTS : [
{ {
option : 'autoIncrease', option : 'autoIncrease',
path : './components/auto-inc-version' path : './components/auto-inc-version'
},
{
option : 'injectIntoHtml',
path : './components/inject-into-html'
},
{
option : 'injectIntoAnyFile',
path : './components/inject-into-any-file'
} }
] ],
WEBPACK_COMPONENTS : [
{
option : 'injectByTag',
path : './components/inject-by-tag'
},
{
option : 'injectAsComment',
path : './components/inject-as-comment'
}
],
LOGS_TEXT : {
AIS_START : 'Auto inject version started'
}
} }

67
src/core/log.ts Normal file
View file

@ -0,0 +1,67 @@
const config = require('../config');
const chalk = require('chalk');
const endOfLine = require('os').EOL;
const u = require('./utils');
class Log{
private logLevel = 3; // default 1
constructor() {
this.getLogLevel();
}
private getLogLevel() {
if(u.isArgv('aiv-log-full')){
this.logLevel = 3;
}else if(u.isArgv('aiv-log-none')) {
this.logLevel = 0;
}
}
/**
* Get console log head
* @returns {string}
*/
private getHead() {
return endOfLine + chalk.bgYellow.black('[AIV] : ')
}
/**
* Get log text by ID from config file
*/
private getText(id) {
return config.LOGS_TEXT[id];
}
/**
* Call any type
* @param type
* @param msg
*/
call(type, msgId) {
if(typeof this[type] === 'function') {
this[type](this.getText(msgId));
}
}
error (msg) {
if(this.logLevel < 3) return;
console.log(`${this.getHead()} ${chalk.red('error')} : ${msg}`);
}
info (msg) {
if(!this.logLevel) return;
console.log(`${this.getHead()} ${chalk.blue('info')} : ${msg}`);
}
warn (msg) {
if(!this.logLevel) return;
console.log(`${this.getHead()} ${chalk.yellow('warn')} : ${msg}`);
}
}
var log = new Log();
module.exports = log;

View file

@ -1,10 +1,11 @@
/// <reference path='../typings/index.d.ts' /> /// <reference path='../typings/index.d.ts' />
var chalk = require('chalk'); const chalk = require('chalk');
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const path = require('path');
var config = require('./config'); const config = require('./config');
var Promise = require('bluebird'); const Promise = require('bluebird');
var u = require('./core/utils'); const u = require('./core/utils');
const log = require('./core/log');
'use strict'; 'use strict';
@ -13,45 +14,86 @@ class WebpackAutoInject{
private options; private options;
private compiler; private compiler;
private version; private version;
private components;
/**
* Default options
*/
static options = { static options = {
autoIncrease : true, autoIncrease : true,
injectIntoHtml : true, injectAsComment : true,
injectIntoHtmlRegex : /^index\.html$/, injectByTag : true,
injectIntoAnyFile : true injectByTagFileRegex : /^index\.html$/
} }
/**
* Constructor,
* called on webpack config load
* @param options
*/
constructor(options) { constructor(options) {
this.options = u.merge(WebpackAutoInject.options, options); this.options = u.merge(WebpackAutoInject.options, options);
var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8')); var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
this.version = packageFile.version; this.version = packageFile.version;
log.call('info', 'AIS_START');
this.executeNoneWebpackComponents();
} }
/**
* Webpack apply call,
* when webpack is initialized and
* plugin has been called by webpack
* @param compiler
*/
protected apply(compiler) { protected apply(compiler) {
this.compiler = compiler; this.compiler = compiler;
this.executeWebpackComponents();
this.components = config.COMPONENTS;
this.executeComponents();
} }
private executeComponents() { /**
* Execute none webpack components
* - runs as soon as possible,
* > without waiting for webpack init
*/
private executeNoneWebpackComponents() {
this.executeComponents(config.NON_WEBPACK_COMPONENTS, () => {
});
}
if(!this.components.length) { console.log(chalk.bgRed('AIS: DONE!')); return;} /**
* Execute webpack components
* - runs when webpack is initialized
* and plugins is called by webpack
*/
private executeWebpackComponents() {
this.executeComponents(config.WEBPACK_COMPONENTS, () => {
});
}
let comp = this.components.shift(); /**
* Execute components,
* - general layer for comp execution
* - used for both, webpack and non webpack comp
*/
private executeComponents(components, done) {
if(this.options[comp.option]) { // no more components,
let inst = new (require(comp.path))(this); // finish
inst.apply().then(() => { if(!components.length) { done(); return;}
this.executeComponents();
}, (err) => {console.log(err);}) // take first component
}else{ let comp = components.shift();
this.executeComponents();
// if component is disabled, call next component
if(!this.options[comp.option]) {
this.executeComponents(components, done);
return;
} }
// execute component
let inst = new (require(comp.path))(this);
inst.apply().then(() => {
this.executeComponents(components, done);
}, (err) => {this.executeComponents(components, done);})
} }
} }