init commit
This commit is contained in:
commit
c5d0f2d1b8
9 changed files with 322 additions and 0 deletions
31
tools/buildConfig.js
Normal file
31
tools/buildConfig.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
const fs = require('fs'),
|
||||
HelperUnit = require('./helperUnit'),
|
||||
helper = new HelperUnit(),
|
||||
config = {};
|
||||
|
||||
module.exports = {
|
||||
config: config,
|
||||
helper: helper,
|
||||
prepare: function () {
|
||||
if (!fs.existsSync(__dirname + '/../../buildConfig.json')) {
|
||||
console.error("Cannot find Config JSON");
|
||||
process.exit(40);
|
||||
}
|
||||
const baseDir = __dirname + '/../..';
|
||||
const data = JSON.parse(fs.readFileSync(__dirname + '/../../buildConfig.json').toString());
|
||||
const src = data['src'].replace(/(\$dir)/gm, baseDir);
|
||||
const out = data['out'].replace(/(\$dir)/gm, baseDir);
|
||||
helper.setConfig({
|
||||
$dir: baseDir,
|
||||
$out: out,
|
||||
$src: src,
|
||||
})
|
||||
config.dir = baseDir;
|
||||
config.src = src;
|
||||
config.out = out;
|
||||
config.js = data.js || [];
|
||||
config.scss = data.scss || [];
|
||||
this.js = config.js;
|
||||
this.scss = config.scss;
|
||||
}
|
||||
}
|
||||
52
tools/helperUnit.js
Normal file
52
tools/helperUnit.js
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
let inConfig = {
|
||||
$dir: __dirname,
|
||||
$out: __dirname,
|
||||
$src: __dirname,
|
||||
$: __dirname
|
||||
}
|
||||
|
||||
class HelperUnit {
|
||||
constructor(config = null) {
|
||||
this.config = config || inConfig;
|
||||
this.regEx = {};
|
||||
this.buildRegex();
|
||||
}
|
||||
|
||||
static setGlobalConfig(config) {
|
||||
inConfig = config;
|
||||
}
|
||||
|
||||
addConfigItem(name, value, regexOnly = false) {
|
||||
if (!regexOnly) {
|
||||
this.config[name] = value;
|
||||
}
|
||||
let replace = name.replace("$", "\\$");
|
||||
this.regEx[name] = new RegExp(`(${replace})`, "gm");
|
||||
}
|
||||
|
||||
setConfig(config) {
|
||||
this.config = config;
|
||||
this.buildRegex();
|
||||
}
|
||||
|
||||
buildRegex() {
|
||||
const keys = Object.keys(this.config);
|
||||
this.regEx = {};
|
||||
for (const key of keys) {
|
||||
this.addConfigItem(key, null, true);
|
||||
}
|
||||
}
|
||||
|
||||
replaceVariables(string) {
|
||||
const keys = Object.keys(this.config);
|
||||
for (const key of keys) {
|
||||
string = string.replace(this.regEx[key], this.config[key]);
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new HelperUnit(this.config);
|
||||
}
|
||||
}
|
||||
module.exports = HelperUnit;
|
||||
Loading…
Add table
Add a link
Reference in a new issue