init
This commit is contained in:
commit
3d91068e08
71 changed files with 4490 additions and 0 deletions
36
build/tools/buildConfig.js
Normal file
36
build/tools/buildConfig.js
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
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 currentPath = process.cwd();
|
||||
const baseDir = currentPath + '/../';
|
||||
const data = JSON.parse(fs.readFileSync(currentPath + '/../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,
|
||||
$rawDir: __dirname,
|
||||
$forWatch: data['src'].replace(/(\$dir)/gm, '../')
|
||||
})
|
||||
config.dir = baseDir;
|
||||
config.src = src;
|
||||
config.out = out;
|
||||
config.js = data.js || [];
|
||||
config.scss = data.scss || [];
|
||||
config.icons = data.icons || [];
|
||||
this.js = config.js;
|
||||
this.scss = config.scss;
|
||||
this.icons = config.icons;
|
||||
}
|
||||
}
|
||||
24
build/tools/file-includer.js
Normal file
24
build/tools/file-includer.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
const fs = require('fs');
|
||||
|
||||
function findFiles(dir, file, helper) {
|
||||
file = helper.replaceVariables(file);
|
||||
const d = fs.readFileSync(file).toString("UTF8");
|
||||
const split = d.split("\n");
|
||||
let files = [];
|
||||
for (let file of split) {
|
||||
if (file.startsWith("--") || file.trim() === "" || file.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
if (file.startsWith("@import")) {
|
||||
let iFile = file.split("@import")[1].trim();
|
||||
files.push(...this.findFiles(dir, `${dir}/${iFile}.path`, helper));
|
||||
} else {
|
||||
files.push(`${dir}/${helper.replaceVariables(file)}.js`);
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
findFiles
|
||||
};
|
||||
57
build/tools/helperUnit.js
Normal file
57
build/tools/helperUnit.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
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;
|
||||
}
|
||||
|
||||
replaceAbsolutePath(string) {
|
||||
string = string.replace(this.config["$dir"], this.config["$forWatch"])
|
||||
return string;
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new HelperUnit(this.config);
|
||||
}
|
||||
}
|
||||
module.exports = HelperUnit;
|
||||
71
build/tools/iconSprite.js
Normal file
71
build/tools/iconSprite.js
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
const path = require('path'),
|
||||
SVGSpriter = require('svg-sprite'),
|
||||
fs = require('fs'),
|
||||
fsExtra = require('node-fs-extra');
|
||||
|
||||
let toMergeData = [];
|
||||
|
||||
function buildIconSprites(config) {
|
||||
config.spriter = {
|
||||
shape: {
|
||||
id: {
|
||||
generator: function (name) {
|
||||
return "vt-" + name.replace('.svg', '');
|
||||
}
|
||||
}
|
||||
},
|
||||
svg: {
|
||||
xmlDeclaration: true,
|
||||
doctypeDeclaration: true
|
||||
},
|
||||
mode: {
|
||||
symbol: true,
|
||||
defs: true
|
||||
}
|
||||
}
|
||||
|
||||
builder.init(config);
|
||||
builder.generateFromIcons();
|
||||
}
|
||||
|
||||
let builder = {
|
||||
init: function (config) {
|
||||
this.config = config;
|
||||
},
|
||||
generateFromIcons: function () {
|
||||
const spriter = new SVGSpriter(this.config.spriter);
|
||||
this.readFiles(this.config.input, spriter);
|
||||
spriter.compile(function (error, result) {
|
||||
if (error !== null) {
|
||||
throw new Error(error);
|
||||
}
|
||||
builder.writeData(result.symbol.sprite.contents.toString());
|
||||
});
|
||||
},
|
||||
|
||||
writeData: function (data) {
|
||||
let out = builder.config.out + builder.config.config.name + ".svg";
|
||||
let dirname = path.dirname(out);
|
||||
if (!fs.existsSync(dirname)) {
|
||||
fsExtra.mkdirpSync(dirname);
|
||||
}
|
||||
fs.writeFileSync(out, data);
|
||||
},
|
||||
|
||||
readFiles: function (dir, spriter) {
|
||||
let files = fs.readdirSync(dir);
|
||||
for (let file of files) {
|
||||
const split = file.split('.');
|
||||
if (split[split.length - 1] === 'svg') {
|
||||
let filePath = path.resolve(dir, file);
|
||||
spriter.add(
|
||||
filePath,
|
||||
file,
|
||||
fs.readFileSync(filePath, {encoding: 'utf-8'})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.buildIconSprites = buildIconSprites;
|
||||
Loading…
Add table
Add a link
Reference in a new issue