-
Notifications
You must be signed in to change notification settings - Fork 15
/
deno_dir.ts
74 lines (67 loc) · 1.92 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2018-2024 the Deno authors. MIT license.
import { isAbsolute, join, normalize, resolve } from "@std/path";
import { DiskCache } from "./disk_cache.ts";
import { cacheDir, homeDir } from "./dirs.ts";
import { HttpCache } from "./http_cache.ts";
import { assert } from "./util.ts";
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) {
root = resolvePathOrUrl(root);
} else {
Deno.permissions.request({ name: "env", variable: "DENO_DIR" });
const dd = Deno.env.get("DENO_DIR");
if (dd) {
if (!isAbsolute(dd)) {
root = normalize(join(Deno.cwd(), dd));
} else {
root = dd;
}
} else {
const cd = cacheDir();
if (cd) {
root = join(cd, "deno");
} else {
const hd = homeDir();
if (hd) {
root = join(hd, ".deno");
}
}
}
}
return root;
}
}
function resolvePathOrUrl(path: URL | string) {
if (path instanceof URL) {
path = path.toString();
}
return resolve(path);
}