Skip to content

Commit 0ff2adf

Browse files
author
Training
committed
update
1 parent b6656c9 commit 0ff2adf

File tree

12 files changed

+1085
-40
lines changed

12 files changed

+1085
-40
lines changed

dist/comm.js renamed to dist/common/comm.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,15 @@ function setJsonFile(dir, data) {
101101
* @return {{filename: string, dir: string, dirname: string}}
102102
*/
103103
function getSrcInfo() {
104-
try {
105-
return {
106-
filename: __filename,
107-
dirname: __dirname,
108-
dir: _path["default"].resolve()
109-
};
110-
} catch (e) {
111-
var _filename = (0, _url.fileURLToPath)(import.meta.url);
112-
return {
113-
filename: _filename,
114-
// 当前文件路径
115-
dirname: _path["default"].dirname(_filename),
116-
// 当前文件所处的文件夹路径
117-
dir: _path["default"].resolve() // 执行命令时的路径
118-
};
119-
}
104+
105+
106+
107+
return {
108+
filename: __filename,
109+
dirname: __dirname,
110+
dir: _path["default"].resolve()
111+
};
112+
120113
}
121114

122115
/**
File renamed without changes.

dist/v2.js renamed to dist/common/v2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
2020
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2121
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
2222
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
23-
var dirInfo = (0, _comm.getSrcInfo)();
23+
var dirInfo = (0, _comm.getSrcInfo.bind(null,'commJS'))();
2424
var _filename = dirInfo.filename; // 当前文件路径
2525
var _dirname = dirInfo.dirname; // 当前文件所处的文件夹路径
2626
var __dir = dirInfo.dir;

dist/v3.js renamed to dist/common/v3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
2727
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
2828
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
2929
var _config = (0, _comm.getConfig)();
30-
var dirInfo = (0, _comm.getSrcInfo)();
30+
var dirInfo = (0, _comm.getSrcInfo.bind(null,'commJS'))();
3131
var _filename = dirInfo.filename; // 当前文件路径
3232
var _dirname = dirInfo.dirname; // 当前文件所处的文件夹路径
3333
var __dir = dirInfo.dir; // 执行命令时的路径

dist/es6/comm.mjs

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
}

dist/es6/index.mjs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
3+
4+
5+
// vite插件
6+
import * as v3 from "./v3.mjs";
7+
import * as v2 from "./v2.mjs";
8+
import {getConfig, setConfig} from "./comm.mjs";
9+
import {exec, execSync, spawn} from "child_process";
10+
import path from "path";
11+
12+
13+
/**
14+
* 获取插件模块
15+
* @param options
16+
* @return {{model: {renderAll?: function(): void, loopDir?: function(*=, *): void, watchPages?: function(): void, getConfigStr?: function(*, *=): (string|null|undefined), analysisVue?: function(*): (undefined|*|null), vitePluginVueAutoRouter?: function(): {name: string, enforce: string, configResolved(*): void, config(*, *): void}, writeRouter?: function(): void}, config: *}}
17+
*/
18+
function getModel(options){
19+
let model = v3;
20+
let config = getConfig();
21+
if (options) {
22+
config = setConfig(options);
23+
}
24+
// 组件配置
25+
let c = getConfig();
26+
if (c && c.type === "complex") {
27+
model = v2;
28+
}
29+
return {
30+
config,
31+
model
32+
};
33+
}
34+
35+
export function vitePluginVueAutoRouter(options){
36+
let config,command;
37+
let cfg = getModel(options);
38+
let model = cfg.model;
39+
return {
40+
name:"auto-router",
41+
enforce: 'pre',
42+
configResolved(resolvedConfig) {
43+
config = resolvedConfig;
44+
if (command !== "build")
45+
model.watchPages();
46+
else
47+
model.renderAll();
48+
},
49+
config(cfg, arg){
50+
config = cfg;
51+
command = arg.command;
52+
}
53+
}
54+
}
55+
56+
export class WebpackPluginAutoRouter{
57+
constructor(options){
58+
let cfg = getModel(options);
59+
this.model = cfg.model;
60+
this.config = cfg.config;
61+
}
62+
apply(compiler){
63+
let mode = compiler.options.mode; // development
64+
if (mode === "development"){
65+
this.model.watchPages();
66+
}else{
67+
this.model.renderAll();
68+
}
69+
// compiler.hooks.run.taps();
70+
}
71+
}

0 commit comments

Comments
 (0)