Finalizing first beta release

This commit is contained in:
radswiat 2016-10-20 09:32:26 +01:00
parent 004c1932d9
commit 1729614a00
12 changed files with 306 additions and 71 deletions

109
README.md
View file

@ -1,25 +1,102 @@
# In development # In development
# Installation ## What
npm i webpack-auto-inject-version --save-dev AIV can inject version number for all your bundle files (css,js,html).<br><br>
Example js:
```js
// [AIV] Build version: 1.0.10
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
```
<br><br>
Example html:
```html
<!-- [AIV] Build version: 1.0.10 -->
<!DOCTYPE html>
<html lang="en">
```
# Usage AIV can also auto inject your version number into html by using special code ( <{version}> ).<br><br>
Add plugin to your webpack configuration. Example:
```html
<span>My awesome project | <{version}></span>
```
Require it by: <br>
var WebpackAutoInject = require('webpack-auto-inject-version');
## Install
```console
$ npm install webpack-auto-inject-version --save-dev
```
<br>
## 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: [ plugins: [
new WebpackAutoInject(options) new WebpackAutoInject({
]
# Options
NOT SUPPORTED YET!
autoIncrease : boolean, autoIncrease : boolean,
injectIntoHtml : boolean, injectIntoHtml : boolean,
injectIntoHtmlRegex : regex,
injectIntoAnyFile : boolean
})
]
# Auto Increase Version }
Option: autoIncrease : true ```
- run webpack with --release major|minor|patch
DO NOT RUN IT WITH WATCH! <br>
## Options
By default you don't need to pass any options, all options from Usage section are set by default.<br><br>
<br>
### autoIncrease
Auto increase package.json number. <br>
This option requires extra argument to be sent to webpack build. <br>
Arguments: --major --minor --patch<br><br>
<br>
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
<br>
### injectIntoHtml
Inject version number ( increased if autoIncrease is set correctly ) into HTML template<br>
For this to work you need to place <{version}> inside your html file.<br><br>
Example:
```html
<span>My awesome project | <{version}></span>
```
Default: true
<br>
### injectIntoHtmlRegex
Regex to find your html file, where injectIntoHtml should try to find your <{version}> tag.<br>
Default: /^index\.html$/
<br>
### injectIntoAnyFile
This will inject your version file as a comment into any css,js,html file.<br>
Default: true

View file

@ -1,26 +1,68 @@
var semver = require('semver'); var semver = require('semver');
var config = require('../config'); 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 () { 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 () { IncVersion.prototype.openPackageFile = function () {
return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8')); return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
}; };
IncVersion.prototype.closePackageFile = function (content) { IncVersion.prototype.closePackageFile = function (newVersion) {
fs.writeFile("/tmp/test", content, function (err) { var _this = this;
this.packageFile.version = newVersion;
fs.writeFile(path.normalize(config.PATH_PACKAGE), JSON.stringify(this.packageFile, null, 4), function (err) {
if (err) { if (err) {
_this.reject(err);
return console.log(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 () { IncVersion.prototype.major = function () {
this.openPackageFile(); var newVersion = semver.inc(this.packageFile.version, 'major');
this.closePackageFile(); this.closePackageFile(newVersion);
}; };
IncVersion.prototype.minor = function () { IncVersion.prototype.minor = function () {
var newVersion = semver.inc(this.packageFile.version, 'minor');
this.closePackageFile(newVersion);
}; };
IncVersion.prototype.patch = function () { IncVersion.prototype.patch = function () {
var newVersion = semver.inc(this.packageFile.version, 'patch');
this.closePackageFile(newVersion);
}; };
return IncVersion; return IncVersion;
}()); }());
module.exports = IncVersion;

View file

@ -27,6 +27,7 @@ var InjectIntoAnyFile = (function () {
} }
cb(); cb();
}); });
return new Promise(function (resolve, reject) { resolve(); });
}; };
InjectIntoAnyFile.prototype.injectIntoCss = function (asset) { InjectIntoAnyFile.prototype.injectIntoCss = function (asset) {
var modAsset = "/** [" + config.SHORT + "] Build version: " + this.context.version + " **/ " + endOfLine + " " + asset.source() + " "; var modAsset = "/** [" + config.SHORT + "] Build version: " + this.context.version + " **/ " + endOfLine + " " + asset.source() + " ";

View file

@ -7,7 +7,7 @@ var InjectIntoHtml = (function () {
var _this = this; var _this = this;
this.context.compiler.plugin('emit', function (compilation, cb) { this.context.compiler.plugin('emit', function (compilation, cb) {
var _loop_1 = function() { var _loop_1 = function() {
if (/^index\.html$/.test(basename)) { if (_this.context.options.injectIntoHtmlRegex.test(basename)) {
var asset = compilation.assets[basename]; var asset = compilation.assets[basename];
var modFile_1 = asset.source().replace(/(\<\{version\}\>)/g, _this.context.version); var modFile_1 = asset.source().replace(/(\<\{version\}\>)/g, _this.context.version);
asset.source = function () { return modFile_1; }; asset.source = function () { return modFile_1; };
@ -18,6 +18,7 @@ var InjectIntoHtml = (function () {
} }
cb(); cb();
}); });
return new Promise(function (resolve, reject) { resolve(); });
}; };
return InjectIntoHtml; return InjectIntoHtml;
}()); }());

16
dist/config.js vendored
View file

@ -1,5 +1,19 @@
module.exports = { module.exports = {
NAME: 'Auto Inject Version', NAME: 'Auto Inject Version',
SHORT: 'AIV', 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'
}
]
}; };

31
dist/main.js vendored
View file

@ -2,29 +2,42 @@ var chalk = require('chalk');
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var config = require('./config'); var config = require('./config');
var Promise = require('bluebird');
var u = require('./core/utils');
'use strict'; 'use strict';
var WebpackAutoInject = (function () { var WebpackAutoInject = (function () {
function WebpackAutoInject(options) { 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')); var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
this.version = packageFile.version; this.version = packageFile.version;
} }
WebpackAutoInject.prototype.apply = function (compiler) { WebpackAutoInject.prototype.apply = function (compiler) {
this.compiler = compiler; this.compiler = compiler;
if (this.options.injectIntoHtml) { this.components = config.COMPONENTS;
var comp_1 = new (require('./components/auto-inc-version'))(this); this.executeComponents();
comp_1.apply(); };
WebpackAutoInject.prototype.executeComponents = function () {
var _this = this;
if (!this.components.length) {
console.log(chalk.bgRed('AIS: DONE!'));
return;
} }
if (this.options.injectIntoHtml) { var comp = this.components.shift();
var comp_2 = new (require('./components/inject-into-html'))(this); if (this.options[comp.option]) {
comp_2.apply(); 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 = { WebpackAutoInject.options = {
autoIncrease: true, autoIncrease: true,
injectIntoHtml: true, injectIntoHtml: true,
injectIntoHtmlRegex: /^index\.html$/,
injectIntoAnyFile: true
}; };
return WebpackAutoInject; return WebpackAutoInject;
}()); }());

View file

@ -1,6 +1,6 @@
{ {
"name": "webpack-auto-inject-version", "name": "webpack-auto-inject-version",
"version": "0.0.19", "version": "0.1.0",
"description": "Webpack plugin for auto inject version from package.json", "description": "Webpack plugin for auto inject version from package.json",
"main": "dist/main.js", "main": "dist/main.js",
"scripts": { "scripts": {
@ -13,6 +13,7 @@
"typings": "^1.4.0" "typings": "^1.4.0"
}, },
"dependencies": { "dependencies": {
"bluebird": "^3.4.6",
"semver": "^5.3.0" "semver": "^5.3.0"
} }
} }

View file

@ -1,30 +1,93 @@
var semver = require('semver'); var semver = require('semver');
var config = require('../config'); 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{ 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() { private openPackageFile() {
return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8')); return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
} }
private closePackageFile(content) { /**
fs.writeFile("/tmp/test", content, function(err) { * Close & save package file
if(err) {return console.log(err);} * @param newVersion
console.log("The file was saved!"); */
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(); * Increase major
*/
this.closePackageFile(); 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);
} }
} }
module.exports = IncVersion;

View file

@ -34,6 +34,7 @@ class InjectIntoAnyFile{
} }
cb(); cb();
}); });
return new Promise((resolve, reject) => { resolve(); })
} }
injectIntoCss(asset) { injectIntoCss(asset) {

View file

@ -15,7 +15,7 @@ class InjectIntoHtml{
apply() { apply() {
this.context.compiler.plugin('emit', (compilation, cb) => { this.context.compiler.plugin('emit', (compilation, cb) => {
for ( var basename in compilation.assets ) { for ( var basename in compilation.assets ) {
if(/^index\.html$/.test(basename)) { if(this.context.options.injectIntoHtmlRegex.test(basename)) {
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, this.context.version);
asset.source = () => modFile; asset.source = () => modFile;
@ -23,6 +23,7 @@ class InjectIntoHtml{
} }
cb(); cb();
}); });
return new Promise((resolve, reject) => { resolve(); })
} }
} }

View file

@ -1,5 +1,19 @@
module.exports = { module.exports = {
NAME : 'Auto Inject Version', NAME : 'Auto Inject Version',
SHORT : 'AIV', 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'
}
]
} }

View file

@ -3,6 +3,8 @@ var chalk = require('chalk');
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var config = require('./config'); var config = require('./config');
var Promise = require('bluebird');
var u = require('./core/utils');
'use strict'; 'use strict';
@ -11,14 +13,17 @@ class WebpackAutoInject{
private options; private options;
private compiler; private compiler;
private version; private version;
private components;
static options = { static options = {
autoIncrease : true, autoIncrease : true,
injectIntoHtml : true, injectIntoHtml : true,
injectIntoHtmlRegex : /^index\.html$/,
injectIntoAnyFile : true
} }
constructor(options) { 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')); var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
this.version = packageFile.version; this.version = packageFile.version;
} }
@ -27,24 +32,26 @@ class WebpackAutoInject{
this.compiler = compiler; this.compiler = compiler;
// Component: auto-inc-version this.components = config.COMPONENTS;
// if: autoIncrease : true
if(this.options.injectIntoHtml) { this.executeComponents();
let comp = new (require('./components/auto-inc-version'))(this);
comp.apply();
} }
private executeComponents() {
// Component: Inject-into-html if(!this.components.length) { console.log(chalk.bgRed('AIS: DONE!')); return;}
// if: injectIntoHtml : true
if(this.options.injectIntoHtml) { let comp = this.components.shift();
let comp = new (require('./components/inject-into-html'))(this);
comp.apply(); if(this.options[comp.option]) {
let inst = new (require(comp.path))(this);
inst.apply().then(() => {
this.executeComponents();
}, (err) => {console.log(err);})
}else{
this.executeComponents();
} }
let comp = new (require('./components/inject-into-any-file'))(this);
comp.apply();
} }
} }