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
# Installation
npm i webpack-auto-inject-version --save-dev
## What
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
Add plugin to your webpack configuration.
AIV can also auto inject your version number into html by using special code ( <{version}> ).<br><br>
Example:
```html
<span>My awesome project | <{version}></span>
```
Require it by:
var WebpackAutoInject = require('webpack-auto-inject-version');
<br>
## 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: [
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!
<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 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;

View file

@ -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() + " ";

View file

@ -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;
}());

16
dist/config.js vendored
View file

@ -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'
}
]
};

31
dist/main.js vendored
View file

@ -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;
}());

View file

@ -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"
}
}

View file

@ -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);
}
}
}
module.exports = IncVersion;

View file

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

View file

@ -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(); })
}
}

View file

@ -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'
}
]
}

View file

@ -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();
}
}