webpack-auto-inject-version
Advanced tools
Comparing version 0.0.19 to 0.1.0
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; |
@@ -30,2 +30,3 @@ var chalk = require('chalk'); | ||
}); | ||
return new Promise(function (resolve, reject) { resolve(); }); | ||
}; | ||
@@ -32,0 +33,0 @@ InjectIntoAnyFile.prototype.injectIntoCss = function (asset) { |
@@ -10,3 +10,3 @@ 'use strict'; | ||
var _loop_1 = function() { | ||
if (/^index\.html$/.test(basename)) { | ||
if (_this.context.options.injectIntoHtmlRegex.test(basename)) { | ||
var asset = compilation.assets[basename]; | ||
@@ -22,2 +22,3 @@ var modFile_1 = asset.source().replace(/(\<\{version\}\>)/g, _this.context.version); | ||
}); | ||
return new Promise(function (resolve, reject) { resolve(); }); | ||
}; | ||
@@ -24,0 +25,0 @@ return InjectIntoHtml; |
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' | ||
} | ||
] | ||
}; |
@@ -5,6 +5,8 @@ var chalk = require('chalk'); | ||
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')); | ||
@@ -15,12 +17,21 @@ this.version = packageFile.version; | ||
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); }); | ||
} | ||
var comp = new (require('./components/inject-into-any-file'))(this); | ||
comp.apply(); | ||
else { | ||
this.executeComponents(); | ||
} | ||
}; | ||
@@ -30,2 +41,4 @@ WebpackAutoInject.options = { | ||
injectIntoHtml: true, | ||
injectIntoHtmlRegex: /^index\.html$/, | ||
injectIntoAnyFile: true | ||
}; | ||
@@ -32,0 +45,0 @@ return WebpackAutoInject; |
{ | ||
"name": "webpack-auto-inject-version", | ||
"version": "0.0.19", | ||
"version": "0.1.0", | ||
"description": "Webpack plugin for auto inject version from package.json", | ||
@@ -16,4 +16,5 @@ "main": "dist/main.js", | ||
"dependencies": { | ||
"bluebird": "^3.4.6", | ||
"semver": "^5.3.0" | ||
} | ||
} |
109
README.md
# 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> | ||
And add to plugins array as one of the last items ( further = better ). | ||
## Install | ||
```console | ||
$ npm install webpack-auto-inject-version --save-dev | ||
``` | ||
<br> | ||
## Usage | ||
```js | ||
var WebpackAutoInject = require('webpack-auto-inject-version'); | ||
module.exports = { | ||
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 |
@@ -1,6 +0,50 @@ | ||
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() { | ||
@@ -10,22 +54,41 @@ 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(); | ||
/** | ||
* Increase major | ||
*/ | ||
private major() { | ||
let newVersion = semver.inc(this.packageFile.version, 'major'); | ||
this.closePackageFile(newVersion); | ||
} | ||
this.closePackageFile(); | ||
/** | ||
* Increase minor | ||
*/ | ||
private minor() { | ||
let newVersion = semver.inc(this.packageFile.version, 'minor'); | ||
this.closePackageFile(newVersion); | ||
} | ||
public minor() { | ||
/** | ||
* Increase patch | ||
*/ | ||
private patch() { | ||
let newVersion = semver.inc(this.packageFile.version, 'patch'); | ||
this.closePackageFile(newVersion); | ||
} | ||
} | ||
public patch() { | ||
} | ||
} | ||
module.exports = IncVersion; |
@@ -37,2 +37,3 @@ /// <reference path='../../typings/index.d.ts' /> | ||
}); | ||
return new Promise((resolve, reject) => { resolve(); }) | ||
} | ||
@@ -39,0 +40,0 @@ |
@@ -18,3 +18,3 @@ /// <reference path='../../typings/index.d.ts' /> | ||
for ( var basename in compilation.assets ) { | ||
if(/^index\.html$/.test(basename)) { | ||
if(this.context.options.injectIntoHtmlRegex.test(basename)) { | ||
let asset = compilation.assets[basename]; | ||
@@ -27,2 +27,3 @@ let modFile = asset.source().replace(/(\<\{version\}\>)/g, this.context.version); | ||
}); | ||
return new Promise((resolve, reject) => { resolve(); }) | ||
} | ||
@@ -29,0 +30,0 @@ } |
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' | ||
} | ||
] | ||
} |
@@ -6,2 +6,4 @@ /// <reference path='../typings/index.d.ts' /> | ||
var config = require('./config'); | ||
var Promise = require('bluebird'); | ||
var u = require('./core/utils'); | ||
@@ -15,10 +17,13 @@ 'use strict'; | ||
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')); | ||
@@ -32,20 +37,22 @@ this.version = packageFile.version; | ||
// 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(); | ||
// 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(); | ||
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(); | ||
} | ||
} | ||
@@ -52,0 +59,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
191244
3935
102
2
4
+ Addedbluebird@^3.4.6
+ Addedbluebird@3.7.2(transitive)