new-themes-switch
Version:
Toolset for switch multiple themes in application based on webpack
166 lines (134 loc) • 4.35 kB
JavaScript
;
var path = require('path');
var fs = require('fs-extra');
function collectFiles(filePath, results, fileFilter, dirFilter) {
var files = fs.readdirSync(filePath);
if (!files) {
console.warn(err);
} else {
files.forEach(function (filename) {
var filedir = path.posix.join(filePath, filename);
var stats = fs.statSync(filedir);
if (!stats) {
console.warn("".concat(filedir, ": invalid file stats."));
} else {
var isFile = stats.isFile();
var isDir = stats.isDirectory();
if (isFile) {
if (fileFilter && typeof fileFilter === 'function') {
if (fileFilter(filedir)) {
results.push(filedir);
}
} else {
results.push(filedir);
}
}
var digIn = true;
if (dirFilter && typeof dirFilter === 'function') {
if (!dirFilter(filedir)) {
digIn = false;
}
}
if (isDir && digIn) {
collectFiles(filedir, results, fileFilter, dirFilter);
}
}
});
}
}
function randomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function recursiveIssuer(m) {
if (m.issuer) {
return recursiveIssuer(m.issuer);
} else if (m.name) {
return m.name;
}
return false;
}
function isVariableRule(node) {
var valueNode = node.children.find(function (n) {
return n.type === 'value';
});
if (!valueNode) return false;
return valueNode.children.find(function (n) {
return n.type === 'variable';
});
}
function removeUnusedRule(tree) {
var _tree$children;
if (!(tree !== null && tree !== void 0 && (_tree$children = tree.children) !== null && _tree$children !== void 0 && _tree$children.length)) return;
for (var t = 0; t < tree.children.length; t++) {
var node = tree.children[t];
if (node.type === 'ruleset') {
removeUnusedRule(node);
continue;
}
if (node.type === 'block') {
for (var i = 0; i < node.children.length; i++) {
var child = node.children[i];
if (child.type !== 'declaration') continue;
if (!isVariableRule(child)) {
node.children.splice(i - 1, 3);
i -= 2;
}
}
if (!node.children.find(function (n) {
return ['declaration', 'ruleset'].includes(n.type);
})) {
tree.children.splice(t, 1);
t -= 1;
}
node.children.filter(function (n) {
return n.type === 'ruleset';
}).forEach(function (node) {
return removeUnusedRule(node);
});
}
}
}
function clearEmptyBlock(tree) {
var _tree$children2;
if (!(tree !== null && tree !== void 0 && (_tree$children2 = tree.children) !== null && _tree$children2 !== void 0 && _tree$children2.length)) return;
for (var i = 0; i < tree.children.length; i++) {
var node = tree.children[i];
if (node.type === 'ruleset') {
var _node$children;
var blocks = node === null || node === void 0 ? void 0 : (_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.filter(function (n) {
return n.type === 'block';
});
blocks.forEach(function (block) {
var _block$children;
block === null || block === void 0 ? void 0 : (_block$children = block.children) === null || _block$children === void 0 ? void 0 : _block$children.filter(function (n) {
return n.type === 'ruleset';
}).forEach(function (n) {
return clearEmptyBlock(n);
});
});
var isEmpty = blocks.filter(function (block) {
var _block$children2;
return block === null || block === void 0 ? void 0 : (_block$children2 = block.children) === null || _block$children2 === void 0 ? void 0 : _block$children2.find(function (n) {
return ['ruleset', 'declaration'].includes(n.type);
});
}).every(function (n) {
return !n;
});
if (isEmpty) {
tree.children.splice(i - 1, 2);
i -= 1;
}
}
clearEmptyBlock(node);
}
}
function clearRules(astTree) {
removeUnusedRule(astTree);
clearEmptyBlock(astTree);
}
module.exports = {
collectFiles: collectFiles,
randomNum: randomNum,
recursiveIssuer: recursiveIssuer,
clearRules: clearRules
};