Skip to content

Commit

Permalink
feat: better handling of permissions (#10)
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
kitsonk authored May 19, 2022
1 parent c5ab9a6 commit 48307aa
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
5 changes: 2 additions & 3 deletions deno_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 6,6 @@ import { cacheDir, homeDir } from "./dirs.ts";
import { HttpCache } from "./http_cache.ts";
import { assert } from "./util.ts";

await Deno.permissions.request({ name: "env", variable: "DENO_DIR" });
await Deno.permissions.request({ name: "read" });

export class DenoDir {
deps: HttpCache;
gen: DiskCache;
Expand All @@ -20,6 17,7 @@ export class DenoDir {
root = normalize(join(Deno.cwd(), root));
}
} else {
Deno.permissions.request({ name: "env", variable: "DENO_DIR" });
const dd = Deno.env.get("DENO_DIR");
if (dd) {
if (!isAbsolute(dd)) {
Expand All @@ -41,6 39,7 @@ export class DenoDir {
}
assert(root, "Could not set the Deno root directory");
assert(isAbsolute(root), `The root directory "${root}" is not absolute.`);
Deno.permissions.request({ name: "read" });
Deno.permissions.request({ name: "write", path: root });
this.root = root;
this.deps = new HttpCache(join(root, "deps"));
Expand Down
14 changes: 4 additions & 10 deletions dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 2,14 @@

import { join } from "./deps.ts";

if (Deno.build.os === "darwin" || Deno.build.os === "linux") {
await Deno.permissions.request({ name: "env", variable: "HOME" });
if (Deno.build.os === "linux") {
await Deno.permissions.request({ name: "env", variable: "XDG_CACHE_HOME" });
}
} else {
await Deno.permissions.request({ name: "env", variable: "USERPROFILE" });
await Deno.permissions.request({ name: "env", variable: "LOCALAPPDATA" });
}

export function cacheDir(): string | undefined {
if (Deno.build.os === "darwin") {
const home = homeDir();
if (home) {
return join(home, "Library/Caches");
}
} else if (Deno.build.os === "linux") {
Deno.permissions.request({ name: "env", variable: "XDG_CACHE_HOME" });
const cacheHome = Deno.env.get("XDG_CACHE_HOME");
if (cacheHome) {
return cacheHome;
Expand All @@ -29,16 20,19 @@ export function cacheDir(): string | undefined {
}
}
} else {
Deno.permissions.request({ name: "env", variable: "LOCALAPPDATA" });
return Deno.env.get("LOCALAPPDATA");
}
}

export function homeDir(): string | undefined {
switch (Deno.build.os) {
case "windows":
Deno.permissions.request({ name: "env", variable: "USERPROFILE" });
return Deno.env.get("USERPROFILE");
case "linux":
case "darwin":
Deno.permissions.request({ name: "env", variable: "HOME" });
return Deno.env.get("HOME");
default:
throw Error("unreachable");
Expand Down
3 changes: 1 addition & 2 deletions file_fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 20,6 @@ import type { HttpCache } from "./http_cache.ts";
*/
export type CacheSetting = "only" | "reloadAll" | "use" | string[];

await Deno.permissions.request({ name: "env", variable: "DENO_AUTH_TOKENS" });

function shouldUseCache(cacheSetting: CacheSetting, specifier: URL): boolean {
switch (cacheSetting) {
case "only":
Expand Down Expand Up @@ -102,6 100,7 @@ export class FileFetcher {
cacheSetting: CacheSetting = "use",
allowRemote = true,
) {
Deno.permissions.request({ name: "env", variable: "DENO_AUTH_TOKENS" });
this.#authTokens = new AuthTokens(Deno.env.get("DENO_AUTH_TOKENS"));
this.#allowRemote = allowRemote;
this.#cacheSetting = cacheSetting;
Expand Down

0 comments on commit 48307aa

Please sign in to comment.