befly-shared 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +442 -0
- package/dist/addonHelper.js +83 -0
- package/dist/arrayKeysToCamel.js +18 -0
- package/dist/arrayToTree.js +23 -0
- package/dist/calcPerfTime.js +13 -0
- package/dist/configTypes.js +1 -0
- package/dist/defineAddonConfig.js +40 -0
- package/dist/fieldClear.js +57 -0
- package/dist/genShortId.js +12 -0
- package/dist/index.js +25 -0
- package/dist/keysToCamel.js +21 -0
- package/dist/keysToSnake.js +21 -0
- package/dist/layouts.js +59 -0
- package/dist/pickFields.js +16 -0
- package/dist/redisKeys.js +34 -0
- package/dist/regex.js +200 -0
- package/dist/scanConfig.js +82 -0
- package/dist/scanFiles.js +39 -0
- package/dist/scanViews.js +48 -0
- package/dist/types.js +46 -0
- package/package.json +40 -0
- package/src/addonHelper.ts +88 -0
- package/src/arrayKeysToCamel.ts +18 -0
- package/src/arrayToTree.ts +31 -0
- package/src/calcPerfTime.ts +13 -0
- package/src/configTypes.ts +27 -0
- package/src/defineAddonConfig.ts +45 -0
- package/src/fieldClear.ts +75 -0
- package/src/genShortId.ts +12 -0
- package/src/index.ts +28 -0
- package/src/keysToCamel.ts +22 -0
- package/src/keysToSnake.ts +22 -0
- package/src/layouts.ts +90 -0
- package/src/pickFields.ts +19 -0
- package/src/redisKeys.ts +44 -0
- package/src/regex.ts +223 -0
- package/src/scanConfig.ts +104 -0
- package/src/scanFiles.ts +49 -0
- package/src/scanViews.ts +55 -0
- package/src/types.ts +338 -0
- package/tests/addonHelper.test.ts +55 -0
- package/tests/arrayKeysToCamel.test.ts +21 -0
- package/tests/arrayToTree.test.ts +98 -0
- package/tests/calcPerfTime.test.ts +19 -0
- package/tests/fieldClear.test.ts +39 -0
- package/tests/keysToCamel.test.ts +22 -0
- package/tests/keysToSnake.test.ts +22 -0
- package/tests/layouts.test.ts +93 -0
- package/tests/pickFields.test.ts +22 -0
- package/tests/regex.test.ts +308 -0
- package/tests/scanFiles.test.ts +58 -0
- package/tests/types.test.ts +283 -0
- package/types/addonConfigMerge.d.ts +17 -0
- package/types/addonHelper.d.ts +24 -0
- package/types/arrayKeysToCamel.d.ts +13 -0
- package/types/arrayToTree.d.ts +8 -0
- package/types/calcPerfTime.d.ts +4 -0
- package/types/configMerge.d.ts +49 -0
- package/types/configTypes.d.ts +26 -0
- package/types/defineAddonConfig.d.ts +19 -0
- package/types/fieldClear.d.ts +16 -0
- package/types/genShortId.d.ts +10 -0
- package/types/index.d.ts +22 -0
- package/types/keysToCamel.d.ts +10 -0
- package/types/keysToSnake.d.ts +10 -0
- package/types/layouts.d.ts +29 -0
- package/types/loadAndMergeConfig.d.ts +7 -0
- package/types/mergeConfig.d.ts +7 -0
- package/types/pickFields.d.ts +4 -0
- package/types/redisKeys.d.ts +34 -0
- package/types/regex.d.ts +143 -0
- package/types/scanConfig.d.ts +7 -0
- package/types/scanFiles.d.ts +12 -0
- package/types/scanViews.d.ts +11 -0
- package/types/types.d.ts +274 -0
package/src/layouts.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路由配置接口
|
|
3
|
+
*/
|
|
4
|
+
export interface RouteConfig {
|
|
5
|
+
path?: string;
|
|
6
|
+
component?: any;
|
|
7
|
+
children?: RouteConfig[];
|
|
8
|
+
meta?: Record<string, any>;
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 布局配置接口
|
|
14
|
+
*/
|
|
15
|
+
export interface LayoutConfig {
|
|
16
|
+
path: string;
|
|
17
|
+
layoutName: string;
|
|
18
|
+
component: any;
|
|
19
|
+
children?: LayoutConfig[];
|
|
20
|
+
meta?: Record<string, any>;
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 自定义布局处理函数
|
|
26
|
+
* 根据文件名后缀判断使用哪个布局
|
|
27
|
+
* @param routes - 原始路由配置
|
|
28
|
+
* @param inheritLayout - 继承的布局名称(来自父级目录)
|
|
29
|
+
* @returns 处理后的布局配置(不包含实际的布局组件导入)
|
|
30
|
+
*/
|
|
31
|
+
export function Layouts(routes: RouteConfig[], inheritLayout: string = ''): LayoutConfig[] {
|
|
32
|
+
const result: LayoutConfig[] = [];
|
|
33
|
+
|
|
34
|
+
for (const route of routes) {
|
|
35
|
+
const currentPath = route.path || '';
|
|
36
|
+
|
|
37
|
+
// 检查当前路径是否有 _数字 格式
|
|
38
|
+
const pathMatch = currentPath.match(/_(\d+)$/);
|
|
39
|
+
const currentLayout = pathMatch ? pathMatch[1] : inheritLayout;
|
|
40
|
+
|
|
41
|
+
// 如果有子路由,说明这是中间节点(目录),不包裹布局,只递归处理子路由
|
|
42
|
+
if (route.children && route.children.length > 0) {
|
|
43
|
+
// 清理路径:如果是 xxx_数字 格式,去掉 _数字
|
|
44
|
+
const cleanPath = pathMatch ? currentPath.replace(/_\d+$/, '') : currentPath;
|
|
45
|
+
|
|
46
|
+
// 直接递归处理子路由,不添加当前层级到结果
|
|
47
|
+
const childConfigs = Layouts(route.children, currentLayout);
|
|
48
|
+
|
|
49
|
+
// 将子路由的路径前缀加上当前路径
|
|
50
|
+
for (const child of childConfigs) {
|
|
51
|
+
result.push({
|
|
52
|
+
...child,
|
|
53
|
+
path: cleanPath ? `${cleanPath}/${child.path}`.replace(/\/+/g, '/') : child.path
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 没有子路由的叶子节点,需要包裹布局
|
|
60
|
+
const lastPart = currentPath;
|
|
61
|
+
|
|
62
|
+
// 匹配 _数字 格式(如 index_1, news_2)
|
|
63
|
+
const match = lastPart.match(/_(\d+)$/);
|
|
64
|
+
// 优先使用文件自己的布局,其次使用继承的布局,最后使用 default
|
|
65
|
+
const layoutName = match ? match[1] : currentLayout || 'default';
|
|
66
|
+
|
|
67
|
+
// 计算清理后的路径
|
|
68
|
+
let cleanPath;
|
|
69
|
+
if (lastPart === 'index' || (lastPart.startsWith('index_') && match)) {
|
|
70
|
+
// index 或 index_数字 → 改为空路径(由父级路径表示)
|
|
71
|
+
cleanPath = '';
|
|
72
|
+
} else if (match) {
|
|
73
|
+
// xxx_数字 → 去掉 _数字 后缀
|
|
74
|
+
cleanPath = lastPart.replace(/_\d+$/, '');
|
|
75
|
+
} else {
|
|
76
|
+
// 其他 → 保持原样
|
|
77
|
+
cleanPath = lastPart;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 返回布局配置(不执行实际导入)
|
|
81
|
+
result.push({
|
|
82
|
+
path: cleanPath,
|
|
83
|
+
layoutName: layoutName,
|
|
84
|
+
component: route.component,
|
|
85
|
+
meta: route.meta
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isPlainObject } from 'es-toolkit/compat';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 挑选指定字段
|
|
5
|
+
*/
|
|
6
|
+
export const pickFields = <T extends Record<string, any>>(obj: T, keys: string[]): Partial<T> => {
|
|
7
|
+
if (!obj || (!isPlainObject(obj) && !Array.isArray(obj))) {
|
|
8
|
+
return {};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const result: any = {};
|
|
12
|
+
for (const key of keys) {
|
|
13
|
+
if (key in obj) {
|
|
14
|
+
result[key] = obj[key];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return result;
|
|
19
|
+
};
|
package/src/redisKeys.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redis Key 统一管理
|
|
3
|
+
* 所有 Redis 缓存键在此统一定义,避免硬编码分散
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Redis Key 生成函数集合
|
|
8
|
+
*/
|
|
9
|
+
export const RedisKeys = {
|
|
10
|
+
/** 所有接口缓存 */
|
|
11
|
+
apisAll: () => 'befly:apis:all',
|
|
12
|
+
|
|
13
|
+
/** 所有菜单缓存 */
|
|
14
|
+
menusAll: () => 'befly:menus:all',
|
|
15
|
+
|
|
16
|
+
/** 角色信息缓存(完整角色对象) */
|
|
17
|
+
roleInfo: (roleCode: string) => `befly:role:info:${roleCode}`,
|
|
18
|
+
|
|
19
|
+
/** 角色接口权限缓存(Set 集合) */
|
|
20
|
+
roleApis: (roleCode: string) => `befly:role:apis:${roleCode}`,
|
|
21
|
+
|
|
22
|
+
/** 表结构缓存 */
|
|
23
|
+
tableColumns: (table: string) => `befly:table:columns:${table}`
|
|
24
|
+
} as const;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Redis TTL(过期时间)常量配置(单位:秒)
|
|
28
|
+
*/
|
|
29
|
+
export const RedisTTL = {
|
|
30
|
+
/** 表结构缓存 - 1小时 */
|
|
31
|
+
tableColumns: 3600,
|
|
32
|
+
|
|
33
|
+
/** 角色接口权限 - 24小时 */
|
|
34
|
+
roleApis: 86400,
|
|
35
|
+
|
|
36
|
+
/** 角色信息 - 24小时 */
|
|
37
|
+
roleInfo: 86400,
|
|
38
|
+
|
|
39
|
+
/** 接口列表 - 永久(不过期) */
|
|
40
|
+
apisAll: null,
|
|
41
|
+
|
|
42
|
+
/** 菜单列表 - 永久(不过期) */
|
|
43
|
+
menusAll: null
|
|
44
|
+
} as const;
|
package/src/regex.ts
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内置正则表达式别名
|
|
3
|
+
* 用于表单验证和数据校验
|
|
4
|
+
* 命名规范:小驼峰格式
|
|
5
|
+
*/
|
|
6
|
+
export const RegexAliases = {
|
|
7
|
+
// ============================================
|
|
8
|
+
// 数字类型
|
|
9
|
+
// ============================================
|
|
10
|
+
/** 正整数(不含0) */
|
|
11
|
+
number: '^\\d+$',
|
|
12
|
+
/** 整数(含负数) */
|
|
13
|
+
integer: '^-?\\d+$',
|
|
14
|
+
/** 浮点数 */
|
|
15
|
+
float: '^-?\\d+(\\.\\d+)?$',
|
|
16
|
+
/** 正整数(不含0) */
|
|
17
|
+
positive: '^[1-9]\\d*$',
|
|
18
|
+
/** 负整数 */
|
|
19
|
+
negative: '^-\\d+$',
|
|
20
|
+
/** 零 */
|
|
21
|
+
zero: '^0$',
|
|
22
|
+
|
|
23
|
+
// ============================================
|
|
24
|
+
// 字符串类型
|
|
25
|
+
// ============================================
|
|
26
|
+
/** 纯字母 */
|
|
27
|
+
word: '^[a-zA-Z]+$',
|
|
28
|
+
/** 字母和数字 */
|
|
29
|
+
alphanumeric: '^[a-zA-Z0-9]+$',
|
|
30
|
+
/** 字母、数字和下划线 */
|
|
31
|
+
alphanumericUnderscore: '^[a-zA-Z0-9_]+$',
|
|
32
|
+
/** 小写字母 */
|
|
33
|
+
lowercase: '^[a-z]+$',
|
|
34
|
+
/** 大写字母 */
|
|
35
|
+
uppercase: '^[A-Z]+$',
|
|
36
|
+
|
|
37
|
+
// ============================================
|
|
38
|
+
// 中文
|
|
39
|
+
// ============================================
|
|
40
|
+
/** 纯中文 */
|
|
41
|
+
chinese: '^[\\u4e00-\\u9fa5]+$',
|
|
42
|
+
/** 中文和字母 */
|
|
43
|
+
chineseWord: '^[\\u4e00-\\u9fa5a-zA-Z]+$',
|
|
44
|
+
|
|
45
|
+
// ============================================
|
|
46
|
+
// 常用格式
|
|
47
|
+
// ============================================
|
|
48
|
+
/** 邮箱地址 */
|
|
49
|
+
email: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$',
|
|
50
|
+
/** 中国大陆手机号 */
|
|
51
|
+
phone: '^1[3-9]\\d{9}$',
|
|
52
|
+
/** 固定电话(区号-号码) */
|
|
53
|
+
telephone: '^0\\d{2,3}-?\\d{7,8}$',
|
|
54
|
+
/** URL 地址 */
|
|
55
|
+
url: '^https?://',
|
|
56
|
+
/** IPv4 地址 */
|
|
57
|
+
ip: '^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$',
|
|
58
|
+
/** IPv6 地址 */
|
|
59
|
+
ipv6: '^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$',
|
|
60
|
+
/** 域名 */
|
|
61
|
+
domain: '^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,}$',
|
|
62
|
+
|
|
63
|
+
// ============================================
|
|
64
|
+
// 特殊格式
|
|
65
|
+
// ============================================
|
|
66
|
+
/** UUID */
|
|
67
|
+
uuid: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$',
|
|
68
|
+
/** 十六进制字符串 */
|
|
69
|
+
hex: '^[0-9a-fA-F]+$',
|
|
70
|
+
/** Base64 编码 */
|
|
71
|
+
base64: '^[A-Za-z0-9+/=]+$',
|
|
72
|
+
/** MD5 哈希 */
|
|
73
|
+
md5: '^[a-f0-9]{32}$',
|
|
74
|
+
/** SHA1 哈希 */
|
|
75
|
+
sha1: '^[a-f0-9]{40}$',
|
|
76
|
+
/** SHA256 哈希 */
|
|
77
|
+
sha256: '^[a-f0-9]{64}$',
|
|
78
|
+
|
|
79
|
+
// ============================================
|
|
80
|
+
// 日期时间
|
|
81
|
+
// ============================================
|
|
82
|
+
/** 日期 YYYY-MM-DD */
|
|
83
|
+
date: '^\\d{4}-\\d{2}-\\d{2}$',
|
|
84
|
+
/** 时间 HH:MM:SS */
|
|
85
|
+
time: '^\\d{2}:\\d{2}:\\d{2}$',
|
|
86
|
+
/** ISO 日期时间 */
|
|
87
|
+
datetime: '^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}',
|
|
88
|
+
/** 年份 */
|
|
89
|
+
year: '^\\d{4}$',
|
|
90
|
+
/** 月份 01-12 */
|
|
91
|
+
month: '^(0[1-9]|1[0-2])$',
|
|
92
|
+
/** 日期 01-31 */
|
|
93
|
+
day: '^(0[1-9]|[12]\\d|3[01])$',
|
|
94
|
+
|
|
95
|
+
// ============================================
|
|
96
|
+
// 代码相关
|
|
97
|
+
// ============================================
|
|
98
|
+
/** 变量名 */
|
|
99
|
+
variable: '^[a-zA-Z_][a-zA-Z0-9_]*$',
|
|
100
|
+
/** 常量名(全大写) */
|
|
101
|
+
constant: '^[A-Z][A-Z0-9_]*$',
|
|
102
|
+
/** 包名(小写+连字符) */
|
|
103
|
+
package: '^[a-z][a-z0-9-]*$',
|
|
104
|
+
|
|
105
|
+
// ============================================
|
|
106
|
+
// 证件相关
|
|
107
|
+
// ============================================
|
|
108
|
+
/** 中国身份证号(18位) */
|
|
109
|
+
idCard: '^\\d{17}[\\dXx]$',
|
|
110
|
+
/** 护照号 */
|
|
111
|
+
passport: '^[a-zA-Z0-9]{5,17}$',
|
|
112
|
+
|
|
113
|
+
// ============================================
|
|
114
|
+
// 账号相关(国内常用)
|
|
115
|
+
// ============================================
|
|
116
|
+
/** 银行卡号(16-19位数字) */
|
|
117
|
+
bankCard: '^\\d{16,19}$',
|
|
118
|
+
/** 微信号(6-20位,字母开头,可包含字母、数字、下划线、减号) */
|
|
119
|
+
wechat: '^[a-zA-Z][a-zA-Z0-9_-]{5,19}$',
|
|
120
|
+
/** QQ号(5-11位数字,首位非0) */
|
|
121
|
+
qq: '^[1-9]\\d{4,10}$',
|
|
122
|
+
/** 支付宝账号(手机号或邮箱) */
|
|
123
|
+
alipay: '^(1[3-9]\\d{9}|[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,})$',
|
|
124
|
+
/** 用户名(4-20位,字母开头,可包含字母、数字、下划线) */
|
|
125
|
+
username: '^[a-zA-Z][a-zA-Z0-9_]{3,19}$',
|
|
126
|
+
/** 昵称(2-20位,支持中文、字母、数字) */
|
|
127
|
+
nickname: '^[\\u4e00-\\u9fa5a-zA-Z0-9]{2,20}$',
|
|
128
|
+
|
|
129
|
+
// ============================================
|
|
130
|
+
// 密码强度
|
|
131
|
+
// ============================================
|
|
132
|
+
/** 弱密码(至少6位) */
|
|
133
|
+
passwordWeak: '^.{6,}$',
|
|
134
|
+
/** 中等密码(至少8位,包含字母和数字) */
|
|
135
|
+
passwordMedium: '^(?=.*[a-zA-Z])(?=.*\\d).{8,}$',
|
|
136
|
+
/** 强密码(至少8位,包含大小写字母、数字和特殊字符) */
|
|
137
|
+
passwordStrong: '^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*]).{8,}$',
|
|
138
|
+
|
|
139
|
+
// ============================================
|
|
140
|
+
// 其他常用
|
|
141
|
+
// ============================================
|
|
142
|
+
/** 车牌号(新能源+普通) */
|
|
143
|
+
licensePlate: '^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$',
|
|
144
|
+
/** 邮政编码 */
|
|
145
|
+
postalCode: '^\\d{6}$',
|
|
146
|
+
/** 版本号(语义化版本) */
|
|
147
|
+
semver: '^\\d+\\.\\d+\\.\\d+(-[a-zA-Z0-9.]+)?(\\+[a-zA-Z0-9.]+)?$',
|
|
148
|
+
/** 颜色值(十六进制) */
|
|
149
|
+
colorHex: '^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$',
|
|
150
|
+
|
|
151
|
+
// ============================================
|
|
152
|
+
// 空值
|
|
153
|
+
// ============================================
|
|
154
|
+
/** 空字符串 */
|
|
155
|
+
empty: '^$',
|
|
156
|
+
/** 非空 */
|
|
157
|
+
notempty: '.+'
|
|
158
|
+
} as const;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 正则别名类型
|
|
162
|
+
*/
|
|
163
|
+
export type RegexAliasName = keyof typeof RegexAliases;
|
|
164
|
+
|
|
165
|
+
// ============================================
|
|
166
|
+
// 正则表达式缓存(性能优化)
|
|
167
|
+
// ============================================
|
|
168
|
+
const regexCache = new Map<string, RegExp>();
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 获取正则表达式字符串
|
|
172
|
+
* @param name 正则别名(以 @ 开头)或自定义正则字符串
|
|
173
|
+
* @returns 正则表达式字符串
|
|
174
|
+
*/
|
|
175
|
+
export function getRegex(name: string): string {
|
|
176
|
+
if (name.startsWith('@')) {
|
|
177
|
+
const alias = name.slice(1) as RegexAliasName;
|
|
178
|
+
return RegexAliases[alias] || name;
|
|
179
|
+
}
|
|
180
|
+
return name;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 获取编译后的正则表达式对象(带缓存)
|
|
185
|
+
* @param pattern 正则别名或正则字符串
|
|
186
|
+
* @param flags 正则标志(如 'i', 'g')
|
|
187
|
+
* @returns 编译后的 RegExp 对象
|
|
188
|
+
*/
|
|
189
|
+
export function getCompiledRegex(pattern: string, flags?: string): RegExp {
|
|
190
|
+
const regexStr = getRegex(pattern);
|
|
191
|
+
const cacheKey = `${regexStr}:${flags || ''}`;
|
|
192
|
+
|
|
193
|
+
let cached = regexCache.get(cacheKey);
|
|
194
|
+
if (!cached) {
|
|
195
|
+
cached = new RegExp(regexStr, flags);
|
|
196
|
+
regexCache.set(cacheKey, cached);
|
|
197
|
+
}
|
|
198
|
+
return cached;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 验证值是否匹配正则(使用缓存)
|
|
203
|
+
* @param value 要验证的值
|
|
204
|
+
* @param pattern 正则别名或正则字符串
|
|
205
|
+
* @returns 是否匹配
|
|
206
|
+
*/
|
|
207
|
+
export function matchRegex(value: string, pattern: string): boolean {
|
|
208
|
+
return getCompiledRegex(pattern).test(value);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* 清除正则缓存
|
|
213
|
+
*/
|
|
214
|
+
export function clearRegexCache(): void {
|
|
215
|
+
regexCache.clear();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 获取缓存大小
|
|
220
|
+
*/
|
|
221
|
+
export function getRegexCacheSize(): number {
|
|
222
|
+
return regexCache.size;
|
|
223
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { mergeAndConcat } from 'merge-anything';
|
|
5
|
+
import { isPlainObject } from 'es-toolkit';
|
|
6
|
+
import { get, set } from 'es-toolkit/compat';
|
|
7
|
+
|
|
8
|
+
import type { LoadConfigOptions } from './configTypes.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 扫描并合并配置文件(矩阵搜索:dirs × files)
|
|
12
|
+
* @param options - 加载选项
|
|
13
|
+
* @returns 合并后的配置对象(或第一个找到的配置)
|
|
14
|
+
*/
|
|
15
|
+
export async function scanConfig(options: LoadConfigOptions): Promise<Record<string, any>> {
|
|
16
|
+
const {
|
|
17
|
+
//
|
|
18
|
+
cwd = process.cwd(),
|
|
19
|
+
dirs,
|
|
20
|
+
files,
|
|
21
|
+
extensions = ['.js', '.ts', '.json'],
|
|
22
|
+
mode = 'first',
|
|
23
|
+
paths
|
|
24
|
+
} = options;
|
|
25
|
+
|
|
26
|
+
// 参数验证
|
|
27
|
+
if (!Array.isArray(dirs) || dirs.length === 0) {
|
|
28
|
+
throw new Error('dirs 必须是非空数组');
|
|
29
|
+
}
|
|
30
|
+
if (!Array.isArray(files) || files.length === 0) {
|
|
31
|
+
throw new Error('files 必须是非空数组');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const configs: Record<string, any>[] = [];
|
|
35
|
+
|
|
36
|
+
// 矩阵搜索:dirs × files × extensions
|
|
37
|
+
for (const dir of dirs) {
|
|
38
|
+
// 如果是绝对路径则直接使用,否则拼接 cwd
|
|
39
|
+
const fullDir = isAbsolute(dir) ? dir : join(cwd, dir);
|
|
40
|
+
|
|
41
|
+
for (const file of files) {
|
|
42
|
+
for (const ext of extensions) {
|
|
43
|
+
const fileName = file.endsWith(ext) ? file : file + ext;
|
|
44
|
+
const filePath = join(fullDir, fileName);
|
|
45
|
+
|
|
46
|
+
if (existsSync(filePath)) {
|
|
47
|
+
try {
|
|
48
|
+
// 动态导入配置文件(使用 import 断言处理 JSON)
|
|
49
|
+
let data: any;
|
|
50
|
+
|
|
51
|
+
if (ext === '.json') {
|
|
52
|
+
// JSON 文件使用 import 断言
|
|
53
|
+
const module = await import(filePath, { with: { type: 'json' } });
|
|
54
|
+
data = module.default;
|
|
55
|
+
} else {
|
|
56
|
+
// JS/TS 文件使用动态导入
|
|
57
|
+
const module = await import(filePath);
|
|
58
|
+
data = module.default || module;
|
|
59
|
+
|
|
60
|
+
// 处理 async 函数导出(如 defineAddonConfig)
|
|
61
|
+
if (data instanceof Promise) {
|
|
62
|
+
data = await data;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 验证配置数据
|
|
67
|
+
if (!isPlainObject(data)) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
configs.push(data);
|
|
72
|
+
|
|
73
|
+
// 如果模式为 'first',找到第一个配置后立即返回
|
|
74
|
+
if (mode === 'first') {
|
|
75
|
+
return data;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 找到后跳过同名文件的其他扩展名
|
|
79
|
+
break;
|
|
80
|
+
} catch (error: any) {
|
|
81
|
+
console.error(`加载配置文件失败: ${filePath}`, error.message);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 合并配置(使用 mergeAndConcat 深度合并)
|
|
89
|
+
const finalConfig = configs.length > 0 ? mergeAndConcat({}, ...configs) : {};
|
|
90
|
+
|
|
91
|
+
// 如果指定了 paths,则只返回指定路径的字段
|
|
92
|
+
if (paths && paths.length > 0) {
|
|
93
|
+
const result: Record<string, any> = {};
|
|
94
|
+
for (const path of paths) {
|
|
95
|
+
const value = get(finalConfig, path);
|
|
96
|
+
if (value !== undefined) {
|
|
97
|
+
set(result, path, value);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return finalConfig;
|
|
104
|
+
}
|
package/src/scanFiles.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { relative, basename, normalize } from 'pathe';
|
|
3
|
+
|
|
4
|
+
export interface ScanFileResult {
|
|
5
|
+
filePath: string; // 绝对路径
|
|
6
|
+
relativePath: string; // 相对路径(无扩展名)
|
|
7
|
+
fileName: string; // 文件名(无扩展名)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 扫描指定目录下的文件
|
|
12
|
+
* @param dir 目录路径
|
|
13
|
+
* @param pattern Glob 模式
|
|
14
|
+
* @param ignoreUnderline 是否忽略下划线开头的文件/目录
|
|
15
|
+
*/
|
|
16
|
+
export async function scanFiles(dir: string, pattern: string = '**/*.{ts,js}', ignoreUnderline: boolean = true): Promise<ScanFileResult[]> {
|
|
17
|
+
if (!existsSync(dir)) return [];
|
|
18
|
+
|
|
19
|
+
const normalizedDir = normalize(dir);
|
|
20
|
+
const glob = new Bun.Glob(pattern);
|
|
21
|
+
const results: ScanFileResult[] = [];
|
|
22
|
+
|
|
23
|
+
for await (const file of glob.scan({ cwd: dir, onlyFiles: true, absolute: true })) {
|
|
24
|
+
if (file.endsWith('.d.ts')) continue;
|
|
25
|
+
|
|
26
|
+
// 使用 pathe.normalize 统一路径分隔符为 /
|
|
27
|
+
const normalizedFile = normalize(file);
|
|
28
|
+
|
|
29
|
+
// 获取文件名(去除扩展名)
|
|
30
|
+
const fileName = basename(normalizedFile).replace(/\.[^.]+$/, '');
|
|
31
|
+
|
|
32
|
+
// 计算相对路径(pathe.relative 返回的已经是正斜杠路径)
|
|
33
|
+
const relativePath = relative(normalizedDir, normalizedFile).replace(/\.[^/.]+$/, '');
|
|
34
|
+
|
|
35
|
+
if (ignoreUnderline) {
|
|
36
|
+
// 检查文件名是否以下划线开头
|
|
37
|
+
if (fileName.startsWith('_')) continue;
|
|
38
|
+
// 检查路径中是否包含下划线开头的目录
|
|
39
|
+
if (relativePath.split('/').some((part) => part.startsWith('_'))) continue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
results.push({
|
|
43
|
+
filePath: normalizedFile,
|
|
44
|
+
relativePath,
|
|
45
|
+
fileName
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return results;
|
|
49
|
+
}
|
package/src/scanViews.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { readdirSync, existsSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 扫描项目和所有 @befly-addon 包的 views 目录
|
|
6
|
+
* 用于 unplugin-vue-router 的 routesFolder 配置
|
|
7
|
+
* 注意:此函数只能在 vite.config.js 中使用(Node.js 环境),不能在浏览器中使用
|
|
8
|
+
* @returns 路由文件夹配置数组
|
|
9
|
+
*/
|
|
10
|
+
export function scanViews() {
|
|
11
|
+
// 使用绝对路径:基于项目根目录(process.cwd())
|
|
12
|
+
const projectRoot = process.cwd();
|
|
13
|
+
const addonBasePath = join(projectRoot, 'node_modules', '@befly-addon');
|
|
14
|
+
const routesFolders = [];
|
|
15
|
+
|
|
16
|
+
// 1. 先添加项目自己的 views 目录
|
|
17
|
+
const projectViewsPath = join(projectRoot, 'src', 'views');
|
|
18
|
+
if (existsSync(projectViewsPath)) {
|
|
19
|
+
routesFolders.push({
|
|
20
|
+
src: projectViewsPath,
|
|
21
|
+
path: '',
|
|
22
|
+
exclude: ['**/components/**']
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 2. 扫描 @befly-addon 包的 views 目录
|
|
27
|
+
if (!existsSync(addonBasePath)) {
|
|
28
|
+
return routesFolders;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const addonDirs = readdirSync(addonBasePath);
|
|
33
|
+
|
|
34
|
+
for (const addonName of addonDirs) {
|
|
35
|
+
const addonPath = join(addonBasePath, addonName);
|
|
36
|
+
|
|
37
|
+
// 检查是否为目录(包括符号链接)
|
|
38
|
+
if (!existsSync(addonPath)) continue;
|
|
39
|
+
|
|
40
|
+
const viewsPath = join(addonPath, 'views');
|
|
41
|
+
|
|
42
|
+
if (existsSync(viewsPath)) {
|
|
43
|
+
routesFolders.push({
|
|
44
|
+
src: viewsPath,
|
|
45
|
+
path: `addon/${addonName}/`,
|
|
46
|
+
exclude: ['**/components/**']
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error('扫描 @befly-addon 目录失败:', error);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return routesFolders;
|
|
55
|
+
}
|