-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
static_files.ts
51 lines (42 loc) 路 1.24 KB
/
static_files.ts
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
import { join } from "../deps/path.ts";
/**
* Class to handle static files.
*/
export default class StaticFiles {
/** List of files and folders to copy */
paths = new Map<string, string>();
add(from: string, to: string) {
// Ensure the path starts with a slash
this.paths.set(join("/", from), join("/", to));
}
/**
* Check whether a src path is included in the list of static files
* and return a [from, to] tuple
*/
search(file: string): [string, string] | undefined {
file = join("/", file);
for (const entry of this.paths) {
const [from, to] = entry;
if (file === from || file.startsWith(join(from, "/"))) {
return [file, join(to, file.slice(from.length))];
}
}
}
/**
* Check whether a dest path is included in the list of static files
* and return a [from, to] tuple
*/
searchReverse(file: string): [string, string] | undefined {
file = join("/", file);
for (const entry of this.paths) {
const [from, to] = entry;
if (file === to || file.startsWith(join(to, "/"))) {
return [join(from, file.slice(to.length)), file];
}
}
}
/** Return all entries */
entries(): [string, string][] {
return [...this.paths.entries()];
}
}