Long awaited improvements, in progress

This commit is contained in:
radswiat 2017-04-10 21:33:32 +01:00
parent cfd2f2decb
commit 05e79420c4
29 changed files with 781 additions and 4402 deletions

17
.babelrc Normal file
View File

@ -0,0 +1,17 @@
{
"babelrc": false,
"presets": [
"es2015",
"stage-2"
],
"plugins": [
["module-resolver", {
"root": ["./src/"],
"alias": {
"config": "./config",
"core": "./core"
}
}],
"transform-runtime"
]
}

0
demo/package.json Normal file
View File

3
demo/webpack.conf.js Normal file
View File

@ -0,0 +1,3 @@
/**
* Created by 608234548 on 10/04/2017.
*/

View File

@ -1,67 +0,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 log = require('../core/log');
var IncVersion = (function () {
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();
if (u.isArgv('major')) {
this.major();
}
else if (u.isArgv('minor')) {
this.minor();
}
else if (u.isArgv('patch')) {
this.patch();
}
else {
this.reject();
}
};
IncVersion.prototype.openPackageFile = function () {
return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
};
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);
}
log.info("autoIncVersion : new version : " + newVersion);
log.info('package.json updated!');
_this.context.version = newVersion;
_this.resolve();
});
};
IncVersion.prototype.major = function () {
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

@ -1,49 +0,0 @@
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;

View File

@ -1,31 +0,0 @@
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;

View File

@ -1,46 +0,0 @@
var chalk = require('chalk');
var path = require('path');
var endOfLine = require('os').EOL;
var config = require('./../config');
'use strict';
var InjectIntoAnyFile = (function () {
function InjectIntoAnyFile(context) {
this.context = context;
}
InjectIntoAnyFile.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;
}
}
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() + " ";
asset.source = function () { return modAsset; };
};
InjectIntoAnyFile.prototype.injectIntoHtml = function (asset) {
var modAsset = "<!-- [" + config.SHORT + "] Build version: " + this.context.version + " --> " + endOfLine + " " + asset.source() + " ";
asset.source = function () { return modAsset; };
};
InjectIntoAnyFile.prototype.injectIntoJs = function (asset) {
var modAsset = "// [" + config.SHORT + "] Build version: " + this.context.version + " " + endOfLine + " " + asset.source() + " ";
asset.source = function () { return modAsset; };
};
return InjectIntoAnyFile;
}());
module.exports = InjectIntoAnyFile;

View File

@ -1,25 +0,0 @@
'use strict';
var InjectIntoHtml = (function () {
function InjectIntoHtml(context) {
this.context = context;
}
InjectIntoHtml.prototype.apply = function () {
var _this = this;
this.context.compiler.plugin('emit', function (compilation, cb) {
var _loop_1 = function() {
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; };
}
};
for (var basename in compilation.assets) {
_loop_1();
}
cb();
});
return new Promise(function (resolve, reject) { resolve(); });
};
return InjectIntoHtml;
}());
module.exports = InjectIntoHtml;

24
dist/config.js vendored
View File

@ -1,24 +0,0 @@
module.exports = {
NAME: 'Auto Inject Version',
SHORT: 'AIV',
PATH_PACKAGE: './package.json',
NON_WEBPACK_COMPONENTS: [
{
option: 'autoIncrease',
path: './components/auto-inc-version'
}
],
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
View File

@ -1,47 +0,0 @@
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;

21
dist/core/utils.js vendored
View File

@ -1,21 +0,0 @@
var Utils = (function () {
function Utils() {
}
Utils.isArgv = function (arg) {
return Boolean(process.argv.find(function (item) {
return item.substr(0, 2) === '--' && item.indexOf(arg) > -1;
}));
};
Utils.merge = function (obj1, obj2) {
var obj3 = {};
for (var attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (var attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
};
return Utils;
}());
module.exports = Utils;

472
dist/index-bundle.js vendored Normal file
View File

@ -0,0 +1,472 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 11);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
module.exports = require("babel-runtime/helpers/classCallCheck");
/***/ }),
/* 1 */
/***/ (function(module, exports) {
module.exports = require("babel-runtime/helpers/createClass");
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
module.exports = {
NAME: 'Auto Inject Version',
SHORT: 'AIV',
PATH_PACKAGE: './package.json',
NON_WEBPACK_COMPONENTS: [{
option: 'autoIncrease',
path: './components/auto-inc-version'
}],
WEBPACK_COMPONENTS: [{
option: 'injectByTag',
path: './components/inject-by-tag'
}, {
option: 'injectAsComment',
path: './components/inject-as-comment'
}],
LOGS_TEXT: {
AIS_START: 'Auto inject version started'
}
};
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var _classCallCheck2 = __webpack_require__(0);
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = __webpack_require__(1);
var _createClass3 = _interopRequireDefault(_createClass2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Utils = function () {
function Utils() {
(0, _classCallCheck3.default)(this, Utils);
}
(0, _createClass3.default)(Utils, null, [{
key: 'isArgv',
value: function isArgv(arg) {
return Boolean(process.argv.find(function (item) {
return item.substr(0, 2) === '--' && item.indexOf(arg) > -1;
}));
}
/**
* Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
* @param obj1
* @param obj2
* @returns obj3 a new object based on obj1 and obj2
*/
}, {
key: 'merge',
value: function merge(obj1, obj2) {
var obj3 = {};
for (var attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (var attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
}
}]);
return Utils;
}();
module.exports = Utils;
/***/ }),
/* 4 */
/***/ (function(module, exports) {
module.exports = require("chalk");
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _classCallCheck2 = __webpack_require__(0);
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = __webpack_require__(1);
var _createClass3 = _interopRequireDefault(_createClass2);
var _chalk = __webpack_require__(4);
var _chalk2 = _interopRequireDefault(_chalk);
var _fs = __webpack_require__(8);
var _fs2 = _interopRequireDefault(_fs);
var _path = __webpack_require__(10);
var _path2 = _interopRequireDefault(_path);
var _config = __webpack_require__(2);
var _config2 = _interopRequireDefault(_config);
var _utils = __webpack_require__(3);
var _utils2 = _interopRequireDefault(_utils);
var _log = __webpack_require__(6);
var _log2 = _interopRequireDefault(_log);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var WebpackAutoInject = function () {
/**
* Constructor,
* called on webpack config load
* @param options
*/
function WebpackAutoInject(options) {
(0, _classCallCheck3.default)(this, WebpackAutoInject);
this.options = _utils2.default.merge(WebpackAutoInject.options, options);
var packageFile = JSON.parse(_fs2.default.readFileSync(_path2.default.normalize(_config2.default.PATH_PACKAGE), 'utf8'));
this.version = packageFile.version;
_log2.default.call('info', 'AIS_START');
this.executeNoneWebpackComponents();
}
/**
* Webpack apply call,
* when webpack is initialized and
* plugin has been called by webpack
* @param compiler
*/
/**
* Default options
*/
(0, _createClass3.default)(WebpackAutoInject, [{
key: 'apply',
value: function apply(compiler) {
this.compiler = compiler;
this.executeWebpackComponents();
}
/**
* Execute none webpack components
* - runs as soon as possible,
* > without waiting for webpack init
*/
}, {
key: 'executeNoneWebpackComponents',
value: function executeNoneWebpackComponents() {
this.executeComponents(_config2.default.NON_WEBPACK_COMPONENTS, function () {});
}
/**
* Execute webpack components
* - runs when webpack is initialized
* and plugins is called by webpack
*/
}, {
key: 'executeWebpackComponents',
value: function executeWebpackComponents() {
this.executeComponents(_config2.default.WEBPACK_COMPONENTS, function () {});
}
/**
* Execute components,
* - general layer for comp execution
* - used for both, webpack and non webpack comp
*/
}, {
key: 'executeComponents',
value: function executeComponents(components, done) {
var _this = this;
// no more components,
// finish
if (!components.length) {
done();return;
}
// take first component
var comp = components.shift();
// if component is disabled, call next component
if (!this.options[comp.option]) {
this.executeComponents(components, done);
return;
}
// execute component
var inst = new (!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()))(this);
inst.apply().then(function () {
_this.executeComponents(components, done);
}, function (err) {
_this.executeComponents(components, done);
});
}
}]);
return WebpackAutoInject;
}();
WebpackAutoInject.options = {
autoIncrease: true,
injectAsComment: true,
injectByTag: true,
injectByTagFileRegex: /^index\.html$/
};
exports.default = WebpackAutoInject;
/***/ }),
/* 6 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _classCallCheck2 = __webpack_require__(0);
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = __webpack_require__(1);
var _createClass3 = _interopRequireDefault(_createClass2);
var _config = __webpack_require__(2);
var _config2 = _interopRequireDefault(_config);
var _chalk = __webpack_require__(4);
var _chalk2 = _interopRequireDefault(_chalk);
var _utils = __webpack_require__(3);
var _utils2 = _interopRequireDefault(_utils);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var endOfLine = __webpack_require__(9).EOL;
var Log = function () {
// default 1
function Log() {
(0, _classCallCheck3.default)(this, Log);
this.logLevel = 3;
this.getLogLevel();
}
(0, _createClass3.default)(Log, [{
key: 'getLogLevel',
value: function 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}
*/
}, {
key: 'getHead',
value: function getHead() {
return endOfLine + _chalk2.default.bgYellow.black('[AIV] : ');
}
/**
* Get log text by ID from config file
*/
}, {
key: 'getText',
value: function getText(id) {
return _config2.default.LOGS_TEXT[id];
}
/**
* Call any type
* @param type
* @param msg
*/
}, {
key: 'call',
value: function call(type, msgId) {
if (typeof this[type] === 'function') {
this[type](this.getText(msgId));
}
}
}, {
key: 'error',
value: function error(msg) {
if (this.logLevel < 3) return;
console.log(this.getHead() + ' ' + _chalk2.default.red('error') + ' : ' + msg);
}
}, {
key: 'info',
value: function info(msg) {
if (!this.logLevel) return;
console.log(this.getHead() + ' ' + _chalk2.default.blue('info') + ' : ' + msg);
}
}, {
key: 'warn',
value: function warn(msg) {
if (!this.logLevel) return;
console.log(this.getHead() + ' ' + _chalk2.default.yellow('warn') + ' : ' + msg);
}
}]);
return Log;
}();
exports.default = new Log();
/***/ }),
/* 7 */
/***/ (function(module, exports) {
function webpackEmptyContext(req) {
throw new Error("Cannot find module '" + req + "'.");
}
webpackEmptyContext.keys = function() { return []; };
webpackEmptyContext.resolve = webpackEmptyContext;
module.exports = webpackEmptyContext;
webpackEmptyContext.id = 7;
/***/ }),
/* 8 */
/***/ (function(module, exports) {
module.exports = require("fs");
/***/ }),
/* 9 */
/***/ (function(module, exports) {
module.exports = require("os");
/***/ }),
/* 10 */
/***/ (function(module, exports) {
module.exports = require("path");
/***/ }),
/* 11 */
/***/ (function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(5);
/***/ })
/******/ ]);

53
dist/main.js vendored
View File

@ -1,53 +0,0 @@
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');
var log = require('./core/log');
'use strict';
var WebpackAutoInject = (function () {
function 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;
log.call('info', 'AIS_START');
this.executeNoneWebpackComponents();
}
WebpackAutoInject.prototype.apply = function (compiler) {
this.compiler = compiler;
this.executeWebpackComponents();
};
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;
if (!components.length) {
done();
return;
}
var comp = components.shift();
if (!this.options[comp.option]) {
this.executeComponents(components, done);
return;
}
var inst = new (require(comp.path))(this);
inst.apply().then(function () {
_this.executeComponents(components, done);
}, function (err) { _this.executeComponents(components, done); });
};
WebpackAutoInject.options = {
autoIncrease: true,
injectAsComment: true,
injectByTag: true,
injectByTagFileRegex: /^index\.html$/
};
return WebpackAutoInject;
}());
module.exports = WebpackAutoInject;

View File

@ -5,12 +5,28 @@
"description": "Webpack plugin for auto inject version from package.json",
"main": "dist/main.js",
"scripts": {
"build": "tsc -w"
"start": "babel-node tools/compile.js"
},
"author": "Radoslaw Swiat",
"license": "ISC",
"devDependencies": {
"typings": "^1.4.0"
"babel-cli": "^6.10.1",
"babel-core": "^6.24.1",
"babel-eslint": "^6.0.0",
"babel-loader": "^6.2.4",
"babel-plugin-module-resolver": "^2.4.0",
"babel-plugin-transform-runtime": "^6.12.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-node5": "^11.0.1",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-2": "^6.22.0",
"eslint": "^2.7.0",
"eslint-config-airbnb": "^6.2.0",
"eslint-loader": "^1.5.0",
"eslint-plugin-babel": "^3.2.0",
"gutil": "^1.6.4",
"webpack": "^2.3.3",
"webpack-node-externals": "^1.5.4"
},
"dependencies": {
"bluebird": "^3.4.6",

24
src/config.js Normal file
View File

@ -0,0 +1,24 @@
module.exports = {
NAME : 'Auto Inject Version',
SHORT : 'AIV',
PATH_PACKAGE : './package.json',
NON_WEBPACK_COMPONENTS : [
{
option : 'autoIncrease',
path : './components/auto-inc-version'
}
],
WEBPACK_COMPONENTS : [
{
option : 'injectByTag',
path : './components/inject-by-tag'
},
{
option : 'injectAsComment',
path : './components/inject-as-comment'
}
],
LOGS_TEXT : {
AIS_START : 'Auto inject version started'
}
}

View File

@ -1,24 +0,0 @@
module.exports = {
NAME : 'Auto Inject Version',
SHORT : 'AIV',
PATH_PACKAGE : './package.json',
NON_WEBPACK_COMPONENTS : [
{
option : 'autoIncrease',
path : './components/auto-inc-version'
}
],
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.js Normal file
View File

@ -0,0 +1,67 @@
import config from 'config';
import chalk from 'chalk';
import utils from 'core/utils';
const endOfLine = require('os').EOL;
class Log{
logLevel = 3; // default 1
constructor() {
this.getLogLevel();
}
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}
*/
getHead() {
return endOfLine + chalk.bgYellow.black('[AIV] : ')
}
/**
* Get log text by ID from config file
*/
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}`);
}
}
export default new Log();

View File

@ -1,67 +0,0 @@
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;

24
src/core/utils.js Normal file
View File

@ -0,0 +1,24 @@
class Utils{
static isArgv(arg) {
return Boolean(process.argv.find(function(item) {
return item.substr(0, 2) === '--' && item.indexOf(arg) > -1;
}));
}
/**
* Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
* @param obj1
* @param obj2
* @returns obj3 a new object based on obj1 and obj2
*/
static merge(obj1,obj2){
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
}
}
module.exports = Utils;

View File

@ -1,24 +0,0 @@
class Utils{
static isArgv(arg) {
return Boolean(process.argv.find(function(item) {
return item.substr(0, 2) === '--' && item.indexOf(arg) > -1;
}));
}
/**
* Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
* @param obj1
* @param obj2
* @returns obj3 a new object based on obj1 and obj2
*/
static merge(obj1,obj2){
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
}
}
module.exports = Utils;

90
src/main.js Normal file
View File

@ -0,0 +1,90 @@
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import config from 'config';
import u from 'core/utils';
import log from 'core/log';
export default class WebpackAutoInject{
/**
* Default options
*/
static options = {
autoIncrease: true,
injectAsComment: true,
injectByTag: true,
injectByTagFileRegex: /^index\.html$/
};
/**
* Constructor,
* called on webpack config load
* @param options
*/
constructor(options) {
this.options = u.merge(WebpackAutoInject.options, options);
let packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
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
*/
apply(compiler) {
this.compiler = compiler;
this.executeWebpackComponents();
}
/**
* Execute none webpack components
* - runs as soon as possible,
* > without waiting for webpack init
*/
executeNoneWebpackComponents() {
this.executeComponents(config.NON_WEBPACK_COMPONENTS, () => {
});
}
/**
* Execute webpack components
* - runs when webpack is initialized
* and plugins is called by webpack
*/
executeWebpackComponents() {
this.executeComponents(config.WEBPACK_COMPONENTS, () => {
});
}
/**
* Execute components,
* - general layer for comp execution
* - used for both, webpack and non webpack comp
*/
executeComponents(components, done) {
// no more components,
// finish
if(!components.length) { done(); return;}
// take first component
let comp = components.shift();
// 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);})
}
}

View File

@ -1,101 +0,0 @@
/// <reference path='../typings/index.d.ts' />
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const config = require('./config');
const Promise = require('bluebird');
const u = require('./core/utils');
const log = require('./core/log');
'use strict';
class WebpackAutoInject{
private options;
private compiler;
private version;
/**
* Default options
*/
static options = {
autoIncrease : true,
injectAsComment : true,
injectByTag : true,
injectByTagFileRegex : /^index\.html$/
}
/**
* Constructor,
* called on webpack config load
* @param options
*/
constructor(options) {
this.options = u.merge(WebpackAutoInject.options, options);
var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
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) {
this.compiler = compiler;
this.executeWebpackComponents();
}
/**
* Execute none webpack components
* - runs as soon as possible,
* > without waiting for webpack init
*/
private executeNoneWebpackComponents() {
this.executeComponents(config.NON_WEBPACK_COMPONENTS, () => {
});
}
/**
* Execute webpack components
* - runs when webpack is initialized
* and plugins is called by webpack
*/
private executeWebpackComponents() {
this.executeComponents(config.WEBPACK_COMPONENTS, () => {
});
}
/**
* Execute components,
* - general layer for comp execution
* - used for both, webpack and non webpack comp
*/
private executeComponents(components, done) {
// no more components,
// finish
if(!components.length) { done(); return;}
// take first component
let comp = components.shift();
// 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);})
}
}
module.exports = WebpackAutoInject;

16
tools/compile.js Normal file
View File

@ -0,0 +1,16 @@
import webpack from 'webpack';
import gutil from 'gutil';
import webpackConfig from './webpack.conf';
function run() {
console.log('compiling');
let compiler = webpack(webpackConfig);
compiler.run((err, stats) => {
gutil.log('[webpack:build]', stats.toString({
chunks: false, // Makes the build much quieter
colors: true
}));
});
}
run();

50
tools/webpack.conf.js Normal file
View File

@ -0,0 +1,50 @@
import path from 'path';
const webpack = require('webpack');
import nodeExternals from 'webpack-node-externals';
export default {
target: 'node',
externals: [nodeExternals()],
entry: {
index: [
'./src/main.js'
]
},
resolve: {
extensions: ['.js']
},
output: {
filename: '[name]-bundle.js',
path: path.resolve(process.cwd(), 'dist')
},
module: {
// rules: [
// { // eslint feature
// enforce: 'pre',
// test: /\.js$/,
// loader: 'eslint-loader',
// exclude: /node_modules/
// }
// ],
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
include: [
path.resolve('src')
]
},
{
test: /\.json$/,
loader: 'json-loader'
},
{
test: /\.txt$/,
loader: 'raw-loader'
}
]
},
plugins: [
]
};

View File

@ -1,17 +0,0 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"outDir": "./dist"
},
"filesGlob": [
"./src/"
]
}

View File

@ -1,5 +0,0 @@
{
"globalDependencies": {
"node": "registry:dt/node#6.0.0+20161019125345"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +0,0 @@
{
"resolution": "main",
"tree": {
"src": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/988a48ab2cfff3243868d70d836332a118d9f060/node/node.d.ts",
"raw": "registry:dt/node#6.0.0+20161019125345",
"typings": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/988a48ab2cfff3243868d70d836332a118d9f060/node/node.d.ts"
}
}

1
typings/index.d.ts vendored
View File

@ -1 +0,0 @@
/// <reference path="globals/node/index.d.ts" />