|
| 1 | +// 获取路径信息 |
| 2 | +import {fileURLToPath} from "url"; |
| 3 | +import path from "path"; |
| 4 | +import fs from "fs"; |
| 5 | +const srcInfo = getSrcInfo(); |
| 6 | +const filename=srcInfo.filename; |
| 7 | +const dirname=srcInfo.dirname; |
| 8 | + |
| 9 | +/** |
| 10 | + * 设置配置 |
| 11 | + * @param options |
| 12 | + */ |
| 13 | +export function setConfig(options){ |
| 14 | + let config = getConfig(); |
| 15 | + if (options.version){ |
| 16 | + options.version = Number(options.version) |
| 17 | + } |
| 18 | + config = Object.assign(config,options); |
| 19 | + if(options.excludeReg){ |
| 20 | + config.excludeDir = null; |
| 21 | + }else if(options.excludeDir){ |
| 22 | + config.excludeReg = null; |
| 23 | + } |
| 24 | + fs.writeFileSync(path.join(dirname,"../","bin/config.json"),JSON.stringify(config),{encoding:'utf-8'}); |
| 25 | + return config; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * 获取配置 |
| 30 | + * @return {any} |
| 31 | + */ |
| 32 | +export function getConfig() { |
| 33 | + let cfgPath = path.join(dirname,"../","bin/config.json"); |
| 34 | + let cfg = getJsonFile(cfgPath); |
| 35 | + if (cfg == null){ |
| 36 | + let res = {"excludeDir":null,"excludeReg":"((component(s)?)|(utils)|(route(r)?))","excludePath":null,"pagePath":"pages","type":"simple"}; |
| 37 | + setJsonFile(cfgPath,res); |
| 38 | + return res; |
| 39 | + } |
| 40 | + return cfg |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * 从Json文件中获取JSON数据 |
| 46 | + * @param dir 路径 |
| 47 | + * @return {JSON} |
| 48 | + */ |
| 49 | +export function getJsonFile(dir) { |
| 50 | + try { |
| 51 | + let res = fs.readFileSync(dir,{encoding:'utf-8'}); |
| 52 | + return JSON.parse(res); |
| 53 | + }catch (e){ |
| 54 | + return null; |
| 55 | + } |
| 56 | +} |
| 57 | +/** |
| 58 | + * 从Json文件中获取JSON数据 |
| 59 | + * @param dir 路径 |
| 60 | + * @param data 数据 |
| 61 | + * @return {JSON} |
| 62 | + */ |
| 63 | +export function setJsonFile(dir,data) { |
| 64 | + try { |
| 65 | + if (typeof data !== "string"){ |
| 66 | + data = JSON.stringify(data); |
| 67 | + } |
| 68 | + fs.writeFileSync(dir,data,{encoding:'utf-8'}); |
| 69 | + return true; |
| 70 | + }catch (e){ |
| 71 | + return false; |
| 72 | + } |
| 73 | +} |
| 74 | +/** |
| 75 | + * 获取基础路径信息 |
| 76 | + * @return {{filename: string, dir: string, dirname: string}} |
| 77 | + */ |
| 78 | +export function getSrcInfo(){ |
| 79 | + |
| 80 | + let __filename = fileURLToPath(import.meta.url); |
| 81 | + return { |
| 82 | + filename: __filename, // 当前文件路径 |
| 83 | + dirname: path.dirname(__filename), // 当前文件所处的文件夹路径 |
| 84 | + dir: path.resolve() // 执行命令时的路径 |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @desc 生成GUID |
| 93 | + * @param format 格式 |
| 94 | + * @return {string} |
| 95 | + */ |
| 96 | +export function GUID(format = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") { |
| 97 | + let char = ['A', 'B', 'C', 'D', 'E', 'F', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; |
| 98 | + let len = 0; |
| 99 | + for (let i in format) |
| 100 | + if (format[i] === 'x') len += 1; |
| 101 | + let random = () => { |
| 102 | + let i = parseInt(Math.random() * 15); |
| 103 | + |
| 104 | + return char[i]; |
| 105 | + }; |
| 106 | + return format.replace(/x/g, res => random()) |
| 107 | +} |
| 108 | + |
| 109 | +const config = getConfig(); |
| 110 | +/** |
| 111 | + * 遍历文件夹 |
| 112 | + * @param dir 文件夹路径 |
| 113 | + * @param callback 回调 , 包含三个参数 status:(-1 ,错误 , 0 文件夹 , 1 文件 2.遍历完成) , info(-1 ,错误信息 , 0,1 文件 , 2 完成信息) , fullPath:当状态为0,1时有此参数 , result 上一级递归的回调值(当状态为0,1时有此参数) |
| 114 | + * @param result 回调结果 |
| 115 | + */ |
| 116 | +export function traverseFolder(dir,callback,result) { |
| 117 | + fs.readdir(dir, { withFileTypes: true },(err,dirs)=>{ |
| 118 | + if (err) return callback(-1,err); |
| 119 | + dirs.forEach(dirent => { |
| 120 | + const fullPath = path.join(dir, dirent.name); |
| 121 | + |
| 122 | + if (dirent.isDirectory()) { |
| 123 | + if (config.excludePath && config.excludePath.filter(e => fullPath.includes(path.join(e))).length){ |
| 124 | + return callback(2,"Traversal complete. The src <" + fullPath + ">was excluded.") |
| 125 | + } |
| 126 | + if (config.excludeReg && (new RegExp(config.excludeReg)).test(dirent.name)){ |
| 127 | + return callback(2,"Traversal complete. The src <" + fullPath + ">was excluded.") |
| 128 | + }else if(config.excludeDir && config.excludeDir.includes(dirent.name)){ |
| 129 | + return callback(2,"Traversal complete. The src <" + fullPath + ">was excluded.") |
| 130 | + } |
| 131 | + let res = callback(0,dirent,fullPath,result) |
| 132 | + traverseFolder(fullPath, callback,res); |
| 133 | + |
| 134 | + } else { |
| 135 | + let extname = path.extname(fullPath); |
| 136 | + if (extname === ".vue") |
| 137 | + return callback(1,dirent,fullPath,result) |
| 138 | + else return callback(2,"Traversal complete. The src <" + fullPath + ">is not a vue file.") |
| 139 | + } |
| 140 | + }); |
| 141 | + // 可以在此处添加一个回调来通知整个目录遍历完成 |
| 142 | + callback(2, 'Traversal complete'); |
| 143 | + }); |
| 144 | +} |
| 145 | +/** |
| 146 | + * @description 深度优先遍历 |
| 147 | + * @param {Array,JSON} data 数据 |
| 148 | + * @param callback 回调 |
| 149 | + * @param options 配置 |
| 150 | + * @constructor |
| 151 | + */ |
| 152 | +export function DFS(data, callback = ()=>{}, options = {}) { |
| 153 | + let _options = Object.assign({}, { |
| 154 | + child: "data" |
| 155 | + }, options); |
| 156 | + let { |
| 157 | + child |
| 158 | + } = _options; |
| 159 | + if (!!data) { |
| 160 | + let stack = []; |
| 161 | + if (Array.isArray(data)) |
| 162 | + for (let i = data.length - 1; i >= 0; i--) { |
| 163 | + let item = data[i]; |
| 164 | + item._tier = 0; |
| 165 | + stack.push(item); |
| 166 | + } |
| 167 | + else if (typeof data === "object") { |
| 168 | + data._tier = 0; |
| 169 | + stack.push(data); |
| 170 | + |
| 171 | + } |
| 172 | + while (stack.length > 0) { |
| 173 | + let item = stack.pop(); |
| 174 | + let tier = item._tier; |
| 175 | + tier++; |
| 176 | + callback(item); |
| 177 | + let children = item[child] || []; |
| 178 | + for (let i = children.length - 1; i >= 0; i--) { |
| 179 | + children[i]._tier = tier; |
| 180 | + stack.push(children[i]); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments