UNPKG

tubao-dir

Version:

可以自由灵活的获取到特定目录的文件名字数组以便于配合webpack进行整体工程打包

110 lines 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tubaoDir = void 0; var path = require("path"); var fs = require("fs"); /** * 兔宝世界webpack构建多文件类库目录配置获取 */ var tubaoDir = /** @class */ (function () { /** * 兔宝世界开源webpack多文件类库打包工具目录获取 * @param {string} path 指定要获取的目录 * @param {string} excludeType 指定排除文件类型 * @param {string} excludeName 指定排除文件名字 */ function tubaoDir(excludeType, excludeName) { if (excludeType === void 0) { excludeType = []; } if (excludeName === void 0) { excludeName = []; } /**结果存储目录 */ this.entryMap = []; /**排除文件名字 */ this.excludeName = []; this.mapDir = this.mapDir.bind(this); this.readDir = this.readDir.bind(this); //重置定位作用域 this.excludeType = excludeType; //排除文件类型 this.excludeName = excludeName; //排除文件名字 } /** * 读取特定目录 * @param {string} path 要查询的目录 * @return {string[]} 返回的目录 */ tubaoDir.prototype.read = function (path) { this.entryMap = []; //目录内容置空 this.readDir(path); //遍历目录 console.log(this.entryMap); //结果 return this.entryMap; //返回目录内容 }; /** * 读取目录 * @param {string} dir 起步目录位置 */ tubaoDir.prototype.readDir = function (dir) { var files = fs.readdirSync(dir); //文件列表 if (!files) { console.log(files); return null; } //读取目录错误 for (var key in files) { //遍历 this.mapDir(files[key], dir); } }; /** * 遍历目录 * @param {string} fileName 文件名字 * @param {string} dir 文件所在目录 */ tubaoDir.prototype.mapDir = function (fileName, dir) { var allDir = path.join(dir, fileName); //全部目录位置 var stats = fs.statSync(allDir); //文件信息 if (!stats) { console.log('获取文件和目录信息stats获取失败'); return; } if (stats.isDirectory()) { //是目录 this.readDir(allDir); //读取目录 } else if (stats.isFile()) { //是文件 this.fileDispose(allDir, dir, fileName); //处理文件 } }; /** * 文件处理 * @param {string} allDir 全部目录 * @param {string} dir 目录 * @param {string} fileName 文件名字 */ tubaoDir.prototype.fileDispose = function (allDir, dir, fileName) { if (this.excludeType.includes(path.extname(allDir))) { // 排除目录下的指定文件类型 console.log("\u6392\u9664\u6587\u4EF6\u7C7B\u578B--\u540D\u5B57\uFF1A".concat(allDir, "--\u76EE\u5F55\uFF1A").concat(allDir)); return; } if (this.excludeName.indexOf(fileName.split('.')[0]) != -1) { // 排除目录下的指定文件 console.log("\u6392\u9664\u6587\u4EF6\u540D\u5B57--\u540D\u5B57\uFF1A".concat(fileName, " \u76EE\u5F55\uFF1A").concat(allDir)); return; } console.log("\u5DF2\u8BB0\u5F55\u6587\u4EF6\uFF1A".concat(fileName, "--\u76EE\u5F55\uFF1A").concat(dir)); this.entryMap.push("./" + allDir); //结果存储目录 }; return tubaoDir; }()); exports.tubaoDir = tubaoDir; //# sourceMappingURL=index.js.map