-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
reader.ts
73 lines (59 loc) 路 1.71 KB
/
reader.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Exception } from "./errors.ts";
import { join } from "../deps/path.ts";
import type { Data } from "../core.ts";
export interface Options {
/** The root directory of the files */
src: string;
}
/**
* Class to read directories, files and store the content in a cache
* It's used to avoid reading the same file multiple times
*/
export default class Reader {
src: string;
cache = new Map<string, Promise<Data>>();
constructor(options: Options) {
this.src = options.src;
}
/** Delete a file from the cache */
deleteCache(path: string) {
const fullPath = this.getFullPath(path);
this.cache.delete(fullPath);
}
/** Delete all the cache */
clearCache() {
this.cache.clear();
}
getFullPath(path: string): string {
return join(this.src, path);
}
/** Returns the file info of a path */
async getInfo(path: string): Promise<Deno.FileInfo | undefined> {
try {
return await Deno.stat(this.getFullPath(path));
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return;
}
throw err;
}
}
/** Reads a directory */
readDir(path: string): AsyncIterable<Deno.DirEntry> {
return Deno.readDir(this.getFullPath(path));
}
/** Read a file using a loader and return the content */
async read(path: string, loader: Loader): Promise<Data> {
path = this.getFullPath(path);
try {
if (!this.cache.has(path)) {
this.cache.set(path, loader(path));
}
return await this.cache.get(path)!;
} catch (cause) {
throw new Exception("Couldn't load this file", { cause, path });
}
}
}
/** A function that loads and returns the file content */
export type Loader = (path: string) => Promise<Data>;