rsuite-theme
Version:
The suite theme for pagurian
154 lines (135 loc) • 4.59 kB
JavaScript
;
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
/**
* Created by Godfery on 2016/9/5 0005.
*/
require('colors');
var Q = require('q');
var glob = require('glob');
var Path = require('path');
var fse = require('fs-extra');
var fs = require('fs');
var color = require('./util/color');
var _require = require('./util/common'),
promisesResolve = _require.promisesResolve;
var async = require('async');
var rootPath = Path.join(__dirname, '../dist');
var originColor = '#00bcd4';
/**
* 导入资源
* @param {Object} options
* @param {String []} paths
* @param {String} dist
*/
function importResources() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$paths = _ref.paths,
paths = _ref$paths === undefined ? [] : _ref$paths,
_ref$dist = _ref.dist,
dist = _ref$dist === undefined ? '' : _ref$dist,
_ref$needDirPath = _ref.needDirPath,
needDirPath = _ref$needDirPath === undefined ? true : _ref$needDirPath;
var doneCallback = arguments[1];
try {
if (dist.length === 0) {
throw 'ERROR: [option.dist] is required';
}
} catch (e) {
console.log(e.red);
}
console.log('ImportResources ...');
/**
* 获取全部文件的路径
* @param paths
* @param callback
*/
var getPathsAllFiles = function getPathsAllFiles(paths, callback) {
var allFiles = [];
var absolutePaths = paths.map(function (path) {
return Path.join(rootPath, path);
});
async.eachLimit(absolutePaths, 1, function (path, next) {
glob(path, {}, function (err, files) {
if (err) {
console.log(err);
} else {
allFiles = [].concat(_toConsumableArray(allFiles), _toConsumableArray(files));
}
next();
});
}, function () {
return callback && callback(allFiles);
});
};
/**
* 复制文件
* @param files
* @param callback
*/
var copyFiles = function copyFiles(files, callback) {
async.eachLimit(files, 1, function (filePath, next) {
var outputPath = needDirPath ? Path.relative(rootPath, filePath) : Path.basename(filePath);
console.log('Copy ' + outputPath);
fse.copy(filePath, Path.join(dist, outputPath), function (err) {
if (err) {
console.error(err);
}
next();
});
}, function () {
callback && callback();
doneCallback && doneCallback();
});
};
getPathsAllFiles(paths, function (file) {
return copyFiles(file, function () {
console.log('ImportResources ' + '[SUCCESS]'.green);
});
});
return module.exports;
};
/**
* 画板方法
* @param {Object} options
*/
function palette() {
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref2$baseColor = _ref2.baseColor,
baseColor = _ref2$baseColor === undefined ? originColor : _ref2$baseColor,
_ref2$src = _ref2.src,
src = _ref2$src === undefined ? 'css/rsuite.min.css' : _ref2$src,
dist = _ref2.dist;
var doneCallback = arguments[1];
try {
if (!dist) {
throw 'ERROR: [option.' + key + '] is required';
}
} catch (e) {
console.log(e.red);
return;
}
var originColors = color.calcColors(originColor);
var colors = color.calcColors(baseColor);
var distPath = Path.dirname(dist);
fse.ensureDir(distPath, function (err) {
if (err) console.log(err);
fs.readFile(Path.join(rootPath, src), 'utf-8', function (err, data) {
originColors.forEach(function (color, index) {
data = data.replace(new RegExp(color, 'g'), colors[index]);
});
fs.writeFile(dist, data, function (err) {
if (err) {
console.log("Failed :" + err.red);
} else {
console.log('Palette ' + dist + '[SUCCESS]'.green);
}
doneCallback && doneCallback();
});
});
});
return module.exports;
}
module.exports = {
importResources: importResources,
palette: palette
};