-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdeno_dir.ts
More file actions
55 lines (48 loc) · 1.44 KB
/
deno_dir.ts
File metadata and controls
55 lines (48 loc) · 1.44 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
// Copyright the Deno authors. MIT license.
import { isAbsolute, join, resolve } from "@std/path";
import { DiskCache } from "./disk_cache.ts";
import { HttpCache } from "./http_cache.ts";
import { assert } from "./util.ts";
import { resolve_deno_dir } from "./lib/deno_cache_dir_wasm.js";
export class DenoDir {
readonly root: string;
constructor(root?: string | URL) {
const resolvedRoot = DenoDir.tryResolveRootPath(root);
assert(resolvedRoot, "Could not set the Deno root directory");
assert(
isAbsolute(resolvedRoot),
`The root directory "${resolvedRoot}" is not absolute.`,
);
Deno.permissions.request({ name: "read", path: resolvedRoot });
this.root = resolvedRoot;
}
createGenCache(): DiskCache {
return new DiskCache(join(this.root, "gen"));
}
createHttpCache(
options?: { vendorRoot?: string | URL; readOnly?: boolean },
): HttpCache {
return HttpCache.create({
root: join(this.root, "remote"),
vendorRoot: options?.vendorRoot == null
? undefined
: resolvePathOrUrl(options.vendorRoot),
readOnly: options?.readOnly,
});
}
static tryResolveRootPath(
root: string | URL | undefined,
): string | undefined {
if (root) {
return resolvePathOrUrl(root);
} else {
return resolve_deno_dir();
}
}
}
function resolvePathOrUrl(path: URL | string) {
if (path instanceof URL) {
path = path.toString();
}
return resolve(path);
}