-
Notifications
You must be signed in to change notification settings - Fork 16
/
deno_dir.ts
56 lines (49 loc) · 1.51 KB
/
deno_dir.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
// Copyright 2018-2024 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 { instantiate } from "./lib/deno_cache_dir.generated.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 },
): Promise<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 {
const instance = instantiate();
return instance.resolve_deno_dir();
}
}
}
function resolvePathOrUrl(path: URL | string) {
if (path instanceof URL) {
path = path.toString();
}
return resolve(path);
}