-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.ts
More file actions
91 lines (81 loc) · 2.95 KB
/
runtime.ts
File metadata and controls
91 lines (81 loc) · 2.95 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
88
89
90
91
// Use a safer check that works with dnt's WASM transformer
// The 'in' operator checks for property existence without triggering Reflect.get issues
export const isDeno = typeof globalThis !== "undefined" && "Deno" in globalThis;
export interface FSLike {
readFile(p: string): Promise<string>;
writeFile(p: string, data: string): Promise<void>;
writeBinaryFile(p: string, data: Uint8Array): Promise<void>;
mkdirp(p: string): Promise<void>;
exists(p: string): Promise<boolean>;
}
export interface GlobLike {
glob(cwd: string, patterns: string[]): Promise<string[]>;
}
export interface MermaidRenderer {
toPng(mermaid: string): Promise<Uint8Array | null>;
}
export interface Adapters {
fs: FSLike;
glob: GlobLike;
mermaid: MermaidRenderer;
sep: string;
join: (...parts: string[]) => string;
dirname: (p: string) => string;
relative: (from: string, to: string) => string;
}
export async function loadAdapters(): Promise<Adapters> {
if (isDeno) {
const path = await import("@std/path");
const { ensureDir, walk } = await import("@std/fs");
const walkGlob = async (cwd: string, patterns: string[]) => {
const regs = patterns.map(p => path.globToRegExp(p, { extended: true, globstar: true }));
const out: string[] = [];
for await (const entry of walk(cwd, { includeDirs: false })) {
const rel = path.relative(cwd, entry.path);
if (regs.some(r => r.test(rel))) out.push(entry.path);
}
return out;
};
const mermaid: MermaidRenderer = { toPng(_mmd) { return Promise.resolve(null); } };
const DenoNS = (globalThis as any).Deno;
const fs: FSLike = {
readFile: p => DenoNS.readTextFile(p),
writeFile: (p, d) => DenoNS.writeTextFile(p, d),
writeBinaryFile: (p, d) => DenoNS.writeFile(p, d),
mkdirp: p => ensureDir(p),
exists: p => DenoNS.stat(p).then(()=>true).catch(()=>false),
};
return {
fs,
glob: { glob: walkGlob },
mermaid,
sep: path.SEPARATOR,
join: path.join,
dirname: path.dirname,
relative: path.relative
};
} else {
const fsP = await import("node:fs/promises");
const nodePath = await import("node:path");
const fg = (await import("fast-glob")).default as unknown as (p: string[], o: any)=>Promise<string[]>;
const mermaid: MermaidRenderer = {
toPng(_mmd) { return Promise.resolve(null); }
};
const fs: FSLike = {
readFile: p => fsP.readFile(p, "utf8"),
writeFile: (p, d) => fsP.writeFile(p, d, "utf8").then(()=>{}),
writeBinaryFile: (p, d) => fsP.writeFile(p, d).then(()=>{}),
mkdirp: p => fsP.mkdir(p, { recursive: true }).then(() => {}),
exists: p => fsP.stat(p).then(() => true).catch(() => false),
};
return {
fs,
glob: { glob: (cwd, patterns) => fg(patterns, { cwd, absolute: true }) },
mermaid,
sep: nodePath.sep,
join: nodePath.join,
dirname: nodePath.dirname,
relative: nodePath.relative
};
}
}