|
1 | | -// 获取路径信息 |
2 | | -import {fileURLToPath} from "url"; |
3 | | -import path from "path"; |
4 | | -import fs from "fs"; |
5 | | -import * as v3 from "./v3.js"; |
6 | | -import * as v2 from "./v2.js"; |
7 | | -const srcInfo = getSrcInfo(); |
8 | | -const __filename=srcInfo.filename; |
9 | | -const __dirname=srcInfo.dirname; |
10 | | - |
11 | | -/** |
12 | | - * 设置配置 |
13 | | - * @param options |
14 | | - */ |
15 | | -export function setConfig(options){ |
16 | | - let config = getConfig(); |
17 | | - options.version = Number(options.version); |
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 | 1 |
|
28 | | -/** |
29 | | - * 获取配置 |
30 | | - * @return {JSON} |
31 | | - */ |
32 | | -export function getConfig() { |
33 | | - return getJsonFile(path.join(__dirname,"bin/config.json")); |
34 | | -} |
35 | 2 |
|
36 | | -/** |
37 | | - * 从Json文件中获取JSON数据 |
38 | | - * @param dir 路径 |
39 | | - * @return {JSON} |
40 | | - */ |
41 | | -export function getJsonFile(dir) { |
42 | | - try { |
43 | | - let res = fs.readFileSync(dir,{encoding:'utf-8'}); |
44 | | - return JSON.parse(res); |
45 | | - }catch (e){ |
46 | | - return null; |
47 | | - } |
48 | | -} |
49 | | -/** |
50 | | - * 获取基础路径信息 |
51 | | - * @return {{filename: string, dir: string, dirname: string}} |
52 | | - */ |
53 | | -export function getSrcInfo(){ |
54 | | - let __filename = fileURLToPath(import.meta.url); |
55 | | - return { |
56 | | - filename: __filename, // 当前文件路径 |
57 | | - dirname: path.dirname(__filename), // 当前文件所处的文件夹路径 |
58 | | - dir: path.resolve() // 执行命令时的路径 |
59 | | - } |
60 | | -} |
61 | 3 |
|
62 | 4 |
|
63 | | -/** |
64 | | - * @desc 生成GUID |
65 | | - * @param format 格式 |
66 | | - * @return {string} |
67 | | - */ |
68 | | -export function GUID(format = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") { |
69 | | - let char = ['A', 'B', 'C', 'D', 'E', 'F', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; |
70 | | - let len = 0; |
71 | | - for (let i in format) |
72 | | - if (format[i] === 'x') len += 1; |
73 | | - let random = () => { |
74 | | - let i = parseInt(Math.random() * 15); |
75 | | - |
76 | | - return char[i]; |
77 | | - }; |
78 | | - return format.replace(/x/g, res => random()) |
79 | | -} |
80 | | - |
81 | | -const config = getConfig(); |
82 | | -/** |
83 | | - * 遍历文件夹 |
84 | | - * @param dir 文件夹路径 |
85 | | - * @param callback 回调 , 包含三个参数 status:(-1 ,错误 , 0 文件夹 , 1 文件 2.遍历完成) , info(-1 ,错误信息 , 0,1 文件 , 2 完成信息) , fullPath:当状态为0,1时有此参数 , result 上一级递归的回调值(当状态为0,1时有此参数) |
86 | | - * @param result 回调结果 |
87 | | - */ |
88 | | -export function traverseFolder(dir,callback,result) { |
89 | | - fs.readdir(dir, { withFileTypes: true },(err,dirs)=>{ |
90 | | - if (err) return callback(-1,err); |
91 | | - dirs.forEach(dirent => { |
92 | | - const fullPath = path.join(dir, dirent.name); |
93 | | - |
94 | | - if (dirent.isDirectory()) { |
95 | | - if (config.excludePath && config.excludePath.filter(e => fullPath.includes(path.join(e))).length){ |
96 | | - return callback(2,"Traversal complete. The src <" + fullPath + ">was excluded.") |
97 | | - } |
98 | | - if (config.excludeReg && (new RegExp(config.excludeReg)).test(dirent.name)){ |
99 | | - return callback(2,"Traversal complete. The src <" + fullPath + ">was excluded.") |
100 | | - }else if(config.excludeDir && config.excludeDir.includes(dirent.name)){ |
101 | | - return callback(2,"Traversal complete. The src <" + fullPath + ">was excluded.") |
102 | | - } |
103 | | - let res = callback(0,dirent,fullPath,result) |
104 | | - traverseFolder(fullPath, callback,res); |
105 | | - |
106 | | - } else { |
107 | | - let extname = path.extname(fullPath); |
108 | | - if (extname === ".vue") |
109 | | - return callback(1,dirent,fullPath,result) |
110 | | - else return callback(2,"Traversal complete. The src <" + fullPath + ">is not a vue file.") |
111 | | - } |
112 | | - }); |
113 | | - // 可以在此处添加一个回调来通知整个目录遍历完成 |
114 | | - callback(2, 'Traversal complete'); |
115 | | - }); |
116 | | -} |
117 | | - |
118 | 5 | // vite插件 |
| 6 | +import * as v3 from "./v3.js"; |
| 7 | +import * as v2 from "./v2.js"; |
| 8 | +import {getConfig, setConfig} from "./comm.js"; |
| 9 | + |
119 | 10 | export function vitePluginVueAutoRouter(options){ |
120 | 11 | let config,command; |
121 | 12 | let model = v3; |
|
0 commit comments