@kevisual/router 0.0.26 → 0.0.27

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.
@@ -1,83 +0,0 @@
1
- type GlobOptions = {
2
- cwd?: string;
3
- };
4
-
5
- export const glob = async (match: string = './*.ts', { cwd = process.cwd() }: GlobOptions = {}) => {
6
- const fs = await import('node:fs');
7
- const path = await import('node:path');
8
-
9
- // 将 glob 模式转换为正则表达式
10
- const globToRegex = (pattern: string): RegExp => {
11
- const escaped = pattern
12
- .replace(/\./g, '\\.')
13
- .replace(/\*\*/g, '__DOUBLE_STAR__') // 临时替换 **
14
- .replace(/\*/g, '[^/]*') // * 匹配除 / 外的任意字符
15
- .replace(/__DOUBLE_STAR__/g, '.*') // ** 匹配任意字符包括 /
16
- .replace(/\?/g, '[^/]'); // ? 匹配除 / 外的单个字符
17
- return new RegExp(`^${escaped}$`);
18
- };
19
-
20
- // 递归读取目录
21
- const readDirRecursive = async (dir: string): Promise<string[]> => {
22
- const files: string[] = [];
23
-
24
- try {
25
- const entries = await fs.promises.readdir(dir, { withFileTypes: true });
26
-
27
- for (const entry of entries) {
28
- const fullPath = path.join(dir, entry.name);
29
-
30
- if (entry.isFile()) {
31
- files.push(fullPath);
32
- } else if (entry.isDirectory()) {
33
- // 递归搜索子目录
34
- const subFiles = await readDirRecursive(fullPath);
35
- files.push(...subFiles);
36
- }
37
- }
38
- } catch (error) {
39
- // 忽略无法访问的目录
40
- }
41
-
42
- return files;
43
- };
44
-
45
- // 解析模式是否包含递归搜索
46
- const hasRecursive = match.includes('**');
47
-
48
- try {
49
- let allFiles: string[] = [];
50
-
51
- if (hasRecursive) {
52
- // 处理递归模式
53
- const basePath = match.split('**')[0];
54
- const startDir = path.resolve(cwd, basePath || '.');
55
- allFiles = await readDirRecursive(startDir);
56
- } else {
57
- // 处理非递归模式
58
- const dir = path.resolve(cwd, path.dirname(match));
59
- const entries = await fs.promises.readdir(dir, { withFileTypes: true });
60
-
61
- for (const entry of entries) {
62
- if (entry.isFile()) {
63
- allFiles.push(path.join(dir, entry.name));
64
- }
65
- }
66
- }
67
-
68
- // 创建相对于 cwd 的匹配模式
69
- const normalizedMatch = path.resolve(cwd, match);
70
- const regex = globToRegex(normalizedMatch);
71
-
72
- // 过滤匹配的文件
73
- const matchedFiles = allFiles.filter(file => {
74
- const normalizedFile = path.resolve(file);
75
- return regex.test(normalizedFile);
76
- });
77
-
78
- return matchedFiles;
79
- } catch (error) {
80
- console.error(`Error in glob pattern "${match}":`, error);
81
- return [];
82
- }
83
- };