webpack-auto-inject-version/src/main.js

116 lines
3 KiB
JavaScript
Raw Normal View History

2017-04-13 07:32:39 +10:00
/* global define */
2017-04-11 06:33:32 +10:00
import fs from 'fs';
import path from 'path';
import config from 'config';
import log from 'core/log';
2017-04-11 09:11:09 +10:00
import { merge, transform } from 'lodash';
// import sub components
2017-04-13 08:40:22 +10:00
import AutoIncreaseVersion from 'components/auto-increase-version/auto-increase-version';
import InjectAsComment from 'components/inject-as-comment/inject-as-comment';
import InjectByTag from 'components/inject-by-tag/inject-by-tag';
2017-04-11 06:33:32 +10:00
2017-04-13 07:32:39 +10:00
export default class WebpackAutoInject {
2017-04-11 06:33:32 +10:00
/**
* Constructor,
* called on webpack config load
2017-04-11 09:11:09 +10:00
* @param userConfig - config from the webpack config file
2017-04-11 06:33:32 +10:00
*/
2017-04-11 09:11:09 +10:00
constructor(userConfig) {
this.setConfig(userConfig);
let packageFile = JSON.parse(
fs.readFileSync(path.resolve(this.config.PACKAGE_JSON_PATH), 'utf8')
);
2017-04-11 06:33:32 +10:00
this.version = packageFile.version;
log.call('info', 'AIS_START');
this.executeNoneWebpackComponents();
}
2017-04-13 07:32:39 +10:00
/**
* Set config
* - merge userConfig with default config
* - merge above with a protected config
* @param userConfig
*/
2017-04-11 09:11:09 +10:00
setConfig(userConfig) {
this.config = merge(config, userConfig);
// lets convert all components names to lowercase - to prevent issues
2017-04-13 07:32:39 +10:00
this.config.components = transform(this.config.components, (result, val, key) => {
2017-04-11 09:11:09 +10:00
result[key.toLowerCase()] = val;
});
}
2017-04-11 06:33:32 +10:00
/**
* Webpack apply call,
* when webpack is initialized and
* plugin has been called by webpack
* @param compiler
2017-04-13 07:32:39 +10:00
* @protected
2017-04-11 06:33:32 +10:00
*/
2017-04-11 09:11:09 +10:00
async apply(compiler) {
2017-04-11 06:33:32 +10:00
this.compiler = compiler;
2017-04-11 09:11:09 +10:00
await this.executeWebpackComponents();
2017-04-11 06:33:32 +10:00
}
/**
* Execute none webpack components
* - runs as soon as possible,
* > without waiting for webpack init
*/
2017-04-11 09:11:09 +10:00
async executeNoneWebpackComponents() {
await this.executeComponent([AutoIncreaseVersion]);
2017-04-11 06:33:32 +10:00
}
/**
* Execute webpack components
* - runs when webpack is initialized
* and plugins is called by webpack
*/
2017-04-11 09:11:09 +10:00
async executeWebpackComponents() {
if (config.componentsOptions.AutoIncreaseVersion.runInWatchMode) {
await this.executeComponent([AutoIncreaseVersion]);
}
2017-04-11 09:11:09 +10:00
await this.executeComponent([InjectAsComment, InjectByTag]);
2017-04-11 06:33:32 +10:00
}
/**
* Execute components,
* - general layer for comp execution
* - used for both, webpack and non webpack comp
*/
2017-04-11 09:11:09 +10:00
async executeComponent(components) {
2017-04-11 06:33:32 +10:00
// no more components,
// finish
2017-04-13 07:32:39 +10:00
if (!components.length) {
2017-04-11 09:11:09 +10:00
return;
}
2017-04-11 06:33:32 +10:00
2017-04-11 09:11:09 +10:00
// take first component class
let ComponentClass = components.shift();
2017-04-11 06:33:32 +10:00
// if component is disabled, call next component
2017-04-11 09:11:09 +10:00
if (!this.config.components[ComponentClass.componentName.toLowerCase()]) {
await this.executeComponent(components);
2017-04-11 06:33:32 +10:00
return;
}
// execute component
2017-04-11 09:11:09 +10:00
let inst = new ComponentClass(this);
// await for apply to finish
await inst.apply();
// call next tick
await this.executeComponent(components);
2017-04-11 06:33:32 +10:00
}
}
// webpack hack to export class directly,
// - instead of using 'new WebpackAutoInject.default()',
// - with this you can just use WebpackAutoInject();
define(() => {
return WebpackAutoInject;
});