UNPKG

elmer-ui-core

Version:

web app framework

136 lines (135 loc) 5.51 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseBuilder = exports.EnumLogType = void 0; require("colors"); var elmer_common_1 = require("elmer-common"); require("../profil/string.profill"); var EnumLogType; (function (EnumLogType) { EnumLogType["INFO"] = "INFO"; EnumLogType["ERROR"] = "ERROR"; EnumLogType["WARNING"] = "WARNING"; EnumLogType["SUCCESS"] = "SUCCESS"; })(EnumLogType = exports.EnumLogType || (exports.EnumLogType = {})); var BaseBuilder = (function (_super) { __extends(BaseBuilder, _super); function BaseBuilder(_fs) { var _this = _super.call(this) || this; _this.fs = _fs; return _this; } BaseBuilder.prototype.isFile = function (fileName) { var checkFileName = fileName; if (!/^[a-z]\:/i.test(fileName)) { checkFileName = fileName.replace(/\\/g, "/"); } return this.isExists(checkFileName) ? this.fs.statSync(checkFileName).isFile() : false; }; BaseBuilder.prototype.isExists = function (fileName) { return this.fs.existsSync(fileName); }; BaseBuilder.prototype.scanFiles = function (path, fn) { var _this = this; this.fs.readdir(path, function (error, files) { if (error) { var msg = _this.formatLog(error.message, EnumLogType.ERROR); console.error(msg); return; } var tmpPath = path.replace(/\//g, "\\"); tmpPath = /\\$/.test(tmpPath) ? tmpPath : tmpPath + "\\"; for (var _i = 0, files_1 = files; _i < files_1.length; _i++) { var tmpData = files_1[_i]; var tmpFileName = tmpPath + tmpData; tmpFileName = /^[a-z]\:/i.test(tmpFileName) ? tmpFileName : tmpFileName.replace(/\\/g, "/"); if (_this.isFile(tmpFileName)) { _this.isFunction(fn) && fn(tmpFileName, tmpPath); } else { _this.scanFiles(tmpFileName, fn); } } }); }; BaseBuilder.prototype.saveFile = function (fileName, data) { var tmpFile = fileName.replace(/\\/g, "/"); var tIndex = tmpFile.lastIndexOf("/"); if (!/^\//.test(tmpFile)) { if (tIndex > 0) { var tmpPath = tmpFile.substr(0, tIndex); var tmpArr = tmpPath.split("/"); var tmpStrPath = ""; for (var i = 0; i < tmpArr.length; i++) { tmpStrPath += this.isEmpty(tmpStrPath) ? tmpArr[i] : "/" + tmpArr[i]; if (!/^[A-Z]\:$/i.test(tmpStrPath)) { if (!this.isExists(tmpStrPath)) { this.fs.mkdirSync(tmpStrPath); console.log(this.formatLog("创建目录:" + tmpStrPath, EnumLogType.INFO)); } } } this.fs.writeFileSync(fileName, data); } } else { if (tIndex > 0) { var tmpPath = tmpFile.substr(0, tIndex); var tmpArr = tmpPath.split("/"); var tmpStrPath = ""; for (var i = 0; i < tmpArr.length; i++) { tmpStrPath += "/" + tmpArr[i]; if (!this.isExists(tmpStrPath) && !this.isEmpty(tmpStrPath)) { console.log(this.formatLog("创建目录:" + tmpStrPath, EnumLogType.INFO)); this.fs.mkdirSync(tmpStrPath); } } } var saveFileName = fileName.replace(/\\/g, "/"); this.fs.writeFileSync(saveFileName, data); } console.log(this.formatLog("复制文件:" + fileName, EnumLogType.INFO)); }; BaseBuilder.prototype.formatLog = function (msg, type) { var now = (new Date()).format("YYYY-MM-DD HH:ii:ss"); var result = "[" + type.toString() + "][" + now + "] " + msg; switch (type) { case EnumLogType.INFO: { result = result.green; break; } case EnumLogType.ERROR: { result = result.red; break; } case EnumLogType.SUCCESS: { result = result.green; break; } case EnumLogType.WARNING: { result = result.yellow; break; } default: { result = result.magenta; } } return result; }; return BaseBuilder; }(elmer_common_1.Common)); exports.BaseBuilder = BaseBuilder;