-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
87 lines (76 loc) · 2.05 KB
/
Copy pathutils.ts
File metadata and controls
87 lines (76 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// File: utils.ts
export function shouldInclude(
path: string,
basePath: string,
includePatterns: string[],
): boolean {
const relPath = path.replace(basePath, "").replace(/^\//, "");
let include = false;
for (const pattern of includePatterns) {
if (matchPattern(relPath, pattern)) {
include = true;
}
}
return include;
}
export function shouldExclude(
path: string,
basePath: string,
ignorePatterns: string[],
): boolean {
const relPath = path.replace(basePath, "").replace(/^\//, "");
for (const pattern of ignorePatterns) {
if (pattern === "") continue;
if (matchPattern(relPath, pattern)) {
return true;
}
}
return false;
}
export function matchPattern(path: string, pattern: string): boolean {
const regexPattern = pattern
.replace(/[.+^${}()|[\]\\]/g, "\\$&") // Escape regex special chars
.replace(/\*/g, ".*") // Convert * to .*
.replace(/\?/g, "."); // Convert ? to .
return new RegExp(`^${regexPattern}$`).test(path);
}
export async function isSafeSymlink(
symlinkPath: string,
basePath: string,
): Promise<boolean> {
try {
const targetPath = await Deno.realPath(symlinkPath);
const realBasePath = await Deno.realPath(basePath);
return targetPath.startsWith(realBasePath);
} catch {
return false;
}
}
export async function isTextFile(filePath: string): Promise<boolean> {
try {
const file = await Deno.open(filePath);
const chunk = new Uint8Array(1024);
const bytesRead = await file.read(chunk);
file.close();
if (bytesRead === null) return false;
// Check if file contains only printable ASCII characters
for (let i = 0; i < bytesRead; i++) {
const byte = chunk[i];
if (byte < 0x20 && ![0x09, 0x0a, 0x0d].includes(byte)) {
return false;
}
}
return true;
} catch {
return false;
}
}
export async function readFileContent(filePath: string): Promise<string> {
try {
const content = await Deno.readTextFile(filePath);
return content;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return `Error reading file: ${errorMessage}`;
}
}