mirror of
https://github.com/denoland/deno.git
synced 2025-02-01 20:25:12 -05:00
feat: Return null on error in Deno.dir() (#3531)
This commit is contained in:
parent
e8d82a6348
commit
b7b0668c78
3 changed files with 16 additions and 9 deletions
4
cli/js/lib.deno_runtime.d.ts
vendored
4
cli/js/lib.deno_runtime.d.ts
vendored
|
@ -74,6 +74,8 @@ declare namespace Deno {
|
||||||
/**
|
/**
|
||||||
* Returns the user and platform specific directories.
|
* Returns the user and platform specific directories.
|
||||||
* Requires the `--allow-env` flag.
|
* Requires the `--allow-env` flag.
|
||||||
|
* Returns null if there is no applicable directory or if any other error
|
||||||
|
* occurs.
|
||||||
*
|
*
|
||||||
* Argument values: "home", "cache", "config", "data", "data_local", "audio",
|
* Argument values: "home", "cache", "config", "data", "data_local", "audio",
|
||||||
* "desktop", "document", "download", "font", "picture", "public", "template",
|
* "desktop", "document", "download", "font", "picture", "public", "template",
|
||||||
|
@ -170,7 +172,7 @@ declare namespace Deno {
|
||||||
* | macOS | `$HOME`/Movies | /Users/Alice/Movies |
|
* | macOS | `$HOME`/Movies | /Users/Alice/Movies |
|
||||||
* | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos |
|
* | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos |
|
||||||
*/
|
*/
|
||||||
export function dir(kind: DirKind): string;
|
export function dir(kind: DirKind): string | null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the path to the current deno executable.
|
* Returns the path to the current deno executable.
|
||||||
|
|
12
cli/js/os.ts
12
cli/js/os.ts
|
@ -2,6 +2,7 @@
|
||||||
import { core } from "./core.ts";
|
import { core } from "./core.ts";
|
||||||
import * as dispatch from "./dispatch.ts";
|
import * as dispatch from "./dispatch.ts";
|
||||||
import { sendSync } from "./dispatch_json.ts";
|
import { sendSync } from "./dispatch_json.ts";
|
||||||
|
import { ErrorKind } from "./errors.ts";
|
||||||
import { assert } from "./util.ts";
|
import { assert } from "./util.ts";
|
||||||
import * as util from "./util.ts";
|
import * as util from "./util.ts";
|
||||||
import { window } from "./window.ts";
|
import { window } from "./window.ts";
|
||||||
|
@ -147,6 +148,8 @@ type DirKind =
|
||||||
/**
|
/**
|
||||||
* Returns the user and platform specific directories.
|
* Returns the user and platform specific directories.
|
||||||
* Requires the `--allow-env` flag.
|
* Requires the `--allow-env` flag.
|
||||||
|
* Returns null if there is no applicable directory or if any other error
|
||||||
|
* occurs.
|
||||||
*
|
*
|
||||||
* Argument values: "home", "cache", "config", "data", "data_local", "audio",
|
* Argument values: "home", "cache", "config", "data", "data_local", "audio",
|
||||||
* "desktop", "document", "download", "font", "picture", "public", "template",
|
* "desktop", "document", "download", "font", "picture", "public", "template",
|
||||||
|
@ -243,8 +246,15 @@ type DirKind =
|
||||||
* | macOS | `$HOME`/Movies | /Users/Alice/Movies |
|
* | macOS | `$HOME`/Movies | /Users/Alice/Movies |
|
||||||
* | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos |
|
* | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos |
|
||||||
*/
|
*/
|
||||||
export function dir(kind: DirKind): string {
|
export function dir(kind: DirKind): string | null {
|
||||||
|
try {
|
||||||
return sendSync(dispatch.OP_GET_DIR, { kind });
|
return sendSync(dispatch.OP_GET_DIR, { kind });
|
||||||
|
} catch (error) {
|
||||||
|
if (error.kind == ErrorKind.PermissionDenied) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -242,12 +242,7 @@ testPerm({ env: true }, function getDir(): void {
|
||||||
if (r.shouldHaveValue) {
|
if (r.shouldHaveValue) {
|
||||||
assertNotEquals(Deno.dir(s.kind), "");
|
assertNotEquals(Deno.dir(s.kind), "");
|
||||||
} else {
|
} else {
|
||||||
// if not support your platform. it should throw an error
|
assertEquals(Deno.dir(s.kind), null);
|
||||||
assertThrows(
|
|
||||||
() => Deno.dir(s.kind),
|
|
||||||
Deno.DenoError,
|
|
||||||
`Could not get user ${s.kind} directory.`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue