diff --git a/cli/js/deno.ts b/cli/js/deno.ts index 11f4675808..8cce7bfceb 100644 --- a/cli/js/deno.ts +++ b/cli/js/deno.ts @@ -1,27 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Public deno module. -export { - env, - exit, - isTTY, - execPath, - homeDir, - cacheDir, - configDir, - dataDir, - dataLocalDir, - audioDir, - desktopDir, - documentDir, - downloadDir, - fontDir, - pictureDir, - publicDir, - templateDir, - videoDir, - hostname -} from "./os.ts"; +export { dir, env, exit, isTTY, execPath, hostname } from "./os.ts"; export { chdir, cwd } from "./dir.ts"; export { File, diff --git a/cli/js/lib.deno_runtime.d.ts b/cli/js/lib.deno_runtime.d.ts index 53cf100f4d..a93ce466f8 100644 --- a/cli/js/lib.deno_runtime.d.ts +++ b/cli/js/lib.deno_runtime.d.ts @@ -54,155 +54,124 @@ declare namespace Deno { * console.log(myEnv.TEST_VAR == newEnv.TEST_VAR); */ export function env(key: string): string | undefined; + + export type DirKind = + | "home" + | "cache" + | "config" + | "data" + | "data_local" + | "audio" + | "desktop" + | "document" + | "download" + | "font" + | "picture" + | "public" + | "template" + | "video"; + /** - * Returns the current user's home directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. - */ - export function homeDir(): string; - /** - * Returns the current user's cache directory. - * If the directory does not exist, an exception is thrown + * Returns the user and platform specific directories. * Requires the `--allow-env` flag. + * + * Argument values: "home", "cache", "config", "data", "data_local", "audio", + * "desktop", "document", "download", "font", "picture", "public", "template", + * "video" + * + * "cache" * |Platform | Value | Example | * | ------- | ----------------------------------- | ---------------------------- | * | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache | * | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches | * | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | - */ - export function cacheDir(): string; - /** - * Returns the current user's config directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "config" * |Platform | Value | Example | * | ------- | ------------------------------------- | -------------------------------- | * | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config | * | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences | * | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | - */ - export function configDir(): string; - /** - * Returns the current user's data directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "data" * |Platform | Value | Example | * | ------- | ---------------------------------------- | ---------------------------------------- | * | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | * | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | * | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | - */ - export function dataDir(): string; - /** - * Returns the current user's local data directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "data_local" * |Platform | Value | Example | * | ------- | ---------------------------------------- | ---------------------------------------- | * | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | * | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | * | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | - */ - export function dataLocalDir(): string; - /** - * Returns the current user's audio directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "audio" * |Platform | Value | Example | * | ------- | ------------------ | -------------------- | * | Linux | `XDG_MUSIC_DIR` | /home/alice/Music | * | macOS | `$HOME`/Music | /Users/Alice/Music | * | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music | - */ - export function audioDir(): string; - /** - * Returns the current user's desktop directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "desktop" * |Platform | Value | Example | * | ------- | -------------------- | ---------------------- | * | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop | * | macOS | `$HOME`/Desktop | /Users/Alice/Desktop | * | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop | - */ - export function desktopDir(): string; - /** - * Returns the current user's document directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "document" * |Platform | Value | Example | * | ------- | ---------------------- | ------------------------ | * | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents | * | macOS | `$HOME`/Documents | /Users/Alice/Documents | * | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents | - */ - export function documentDir(): string; - /** - * Returns the current user's download directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "download" * |Platform | Value | Example | * | ------- | ---------------------- | ------------------------ | * | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads | * | macOS | `$HOME`/Downloads | /Users/Alice/Downloads | * | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads | - */ - export function downloadDir(): string; - /** - * Returns the current user's font directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "font" * |Platform | Value | Example | * | ------- | ---------------------------------------------------- | ------------------------------ | * | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts | * | macOS | `$HOME/Library/Fonts` | /Users/Alice/Library/Fonts | * | Windows | – | – | - */ - export function fontDir(): string; - /** - * Returns the current user's picture directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "picture" * |Platform | Value | Example | * | ------- | --------------------- | ----------------------- | * | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures | * | macOS | `$HOME`/Pictures | /Users/Alice/Pictures | * | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures | - */ - export function pictureDir(): string; - /** - * Returns the current user's public directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "public" * |Platform | Value | Example | * | ------- | --------------------- | ------------------- | * | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public | * | macOS | `$HOME`/Public | /Users/Alice/Public | * | Windows | `{FOLDERID_Public}` | C:\Users\Public | - */ - export function publicDir(): string; - /** - * Returns the current user's template directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "template" * |Platform | Value | Example | * | ------- | ---------------------- | ---------------------------------------------------------- | * | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates | * | macOS | – | – | * | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates | - */ - export function templateDir(): string; - /** - * Returns the current user's video directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "video" * |Platform | Value | Example | * | ------- | ------------------- | --------------------- | * | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos | * | macOS | `$HOME`/Movies | /Users/Alice/Movies | * | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos | */ - export function videoDir(): string; + export function dir(kind: DirKind): string; + /** * Returns the path to the current deno executable. * Requires the `--allow-env` flag. diff --git a/cli/js/os.ts b/cli/js/os.ts index ed46268d9a..baa62c2209 100644 --- a/cli/js/os.ts +++ b/cli/js/os.ts @@ -128,194 +128,123 @@ export function start(preserveDenoNamespace = true, source?: string): Start { return startResponse; } -/** - * Returns the current user's home directory. - * Requires the `--allow-env` flag. - */ -export function homeDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "home" }); -} +type DirKind = + | "home" + | "cache" + | "config" + | "data" + | "data_local" + | "audio" + | "desktop" + | "document" + | "download" + | "font" + | "picture" + | "public" + | "template" + | "video"; /** - * Returns the current user's cache directory. - * If the directory does not exist, an exception is thrown + * Returns the user and platform specific directories. * Requires the `--allow-env` flag. + * + * Argument values: "home", "cache", "config", "data", "data_local", "audio", + * "desktop", "document", "download", "font", "picture", "public", "template", + * "video" + * + * "cache" * |Platform | Value | Example | * | ------- | ----------------------------------- | ---------------------------- | * | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache | * | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches | * | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | - */ -export function cacheDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "cache" }); -} - -/** - * Returns the current user's config directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "config" * |Platform | Value | Example | * | ------- | ------------------------------------- | -------------------------------- | * | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config | * | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences | * | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | - */ -export function configDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "config" }); -} - -/** - * Returns the current user's data directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "data" * |Platform | Value | Example | * | ------- | ---------------------------------------- | ---------------------------------------- | * | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | * | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | * | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | - */ -export function dataDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "data" }); -} - -/** - * Returns the current user's local data directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "data_local" * |Platform | Value | Example | * | ------- | ---------------------------------------- | ---------------------------------------- | * | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | * | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | * | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | - */ -export function dataLocalDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "data_local" }); -} - -/** - * Returns the current user's audio directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "audio" * |Platform | Value | Example | * | ------- | ------------------ | -------------------- | * | Linux | `XDG_MUSIC_DIR` | /home/alice/Music | * | macOS | `$HOME`/Music | /Users/Alice/Music | * | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music | - */ -export function audioDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "audio" }); -} - -/** - * Returns the current user's desktop directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "desktop" * |Platform | Value | Example | * | ------- | -------------------- | ---------------------- | * | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop | * | macOS | `$HOME`/Desktop | /Users/Alice/Desktop | * | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop | - */ -export function desktopDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "desktop" }); -} - -/** - * Returns the current user's document directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "document" * |Platform | Value | Example | * | ------- | ---------------------- | ------------------------ | * | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents | * | macOS | `$HOME`/Documents | /Users/Alice/Documents | * | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents | - */ -export function documentDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "document" }); -} - -/** - * Returns the current user's download directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "download" * |Platform | Value | Example | * | ------- | ---------------------- | ------------------------ | * | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads | * | macOS | `$HOME`/Downloads | /Users/Alice/Downloads | * | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads | - */ -export function downloadDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "download" }); -} - -/** - * Returns the current user's font directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "font" * |Platform | Value | Example | * | ------- | ---------------------------------------------------- | ------------------------------ | * | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts | * | macOS | `$HOME/Library/Fonts` | /Users/Alice/Library/Fonts | * | Windows | – | – | - */ -export function fontDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "font" }); -} - -/** - * Returns the current user's picture directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "picture" * |Platform | Value | Example | * | ------- | --------------------- | ----------------------- | * | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures | * | macOS | `$HOME`/Pictures | /Users/Alice/Pictures | * | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures | - */ -export function pictureDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "picture" }); -} - -/** - * Returns the current user's public directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "public" * |Platform | Value | Example | * | ------- | --------------------- | ------------------- | * | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public | * | macOS | `$HOME`/Public | /Users/Alice/Public | * | Windows | `{FOLDERID_Public}` | C:\Users\Public | - */ -export function publicDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "public" }); -} - -/** - * Returns the current user's template directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "template" * |Platform | Value | Example | * | ------- | ---------------------- | ---------------------------------------------------------- | * | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates | * | macOS | – | – | * | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates | - */ -export function templateDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "template" }); -} - -/** - * Returns the current user's video directory. - * If the directory does not exist, an exception is thrown - * Requires the `--allow-env` flag. + * + * "video" * |Platform | Value | Example | * | ------- | ------------------- | --------------------- | * | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos | * | macOS | `$HOME`/Movies | /Users/Alice/Movies | * | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos | */ -export function videoDir(): string { - return sendSync(dispatch.OP_GET_DIR, { name: "video" }); +export function dir(kind: DirKind): string { + return sendSync(dispatch.OP_GET_DIR, { kind }); } /** diff --git a/cli/js/os_test.ts b/cli/js/os_test.ts index 0b859836b9..e77678ec1e 100644 --- a/cli/js/os_test.ts +++ b/cli/js/os_test.ts @@ -116,23 +116,7 @@ test(function osIsTTYSmoke(): void { console.log(Deno.isTTY()); }); -testPerm({ env: true }, function homeDir(): void { - assertNotEquals(Deno.homeDir(), ""); -}); - -testPerm({ env: false }, function homeDirPerm(): void { - let caughtError = false; - try { - Deno.homeDir(); - } catch (err) { - caughtError = true; - assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); - assertEquals(err.name, "PermissionDenied"); - } - assert(caughtError); -}); - -testPerm({ env: true }, function getUserDir(): void { +testPerm({ env: true }, function getDir(): void { type supportOS = "mac" | "win" | "linux"; interface Runtime { @@ -141,15 +125,13 @@ testPerm({ env: true }, function getUserDir(): void { } interface Scenes { - name: string; - fn: string; + kind: Deno.DirKind; runtime: Runtime[]; } const scenes: Scenes[] = [ { - name: "config", - fn: "configDir", + kind: "config", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -157,8 +139,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "cache", - fn: "cacheDir", + kind: "cache", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -166,8 +147,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "data", - fn: "dataDir", + kind: "data", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -175,8 +155,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "data local", - fn: "dataLocalDir", + kind: "data_local", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -184,8 +163,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "audio", - fn: "audioDir", + kind: "audio", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -193,8 +171,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "desktop", - fn: "desktopDir", + kind: "desktop", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -202,8 +179,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "document", - fn: "documentDir", + kind: "document", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -211,8 +187,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "download", - fn: "downloadDir", + kind: "download", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -220,8 +195,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "font", - fn: "fontDir", + kind: "font", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: false }, @@ -229,8 +203,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "picture", - fn: "pictureDir", + kind: "picture", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -238,8 +211,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "public", - fn: "publicDir", + kind: "public", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -247,8 +219,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "template", - fn: "templateDir", + kind: "template", runtime: [ { os: "mac", shouldHaveValue: false }, { os: "win", shouldHaveValue: true }, @@ -256,8 +227,7 @@ testPerm({ env: true }, function getUserDir(): void { ] }, { - name: "video", - fn: "videoDir", + kind: "video", runtime: [ { os: "mac", shouldHaveValue: true }, { os: "win", shouldHaveValue: true }, @@ -267,52 +237,28 @@ testPerm({ env: true }, function getUserDir(): void { ]; for (const s of scenes) { - console.log(`test Deno.${s.fn}()`); - const fn = Deno[s.fn]; - for (const r of s.runtime) { if (Deno.build.os !== r.os) continue; if (r.shouldHaveValue) { - assertNotEquals(fn(), ""); + assertNotEquals(Deno.dir(s.kind), ""); } else { // if not support your platform. it should throw an error assertThrows( - () => fn(), + () => Deno.dir(s.kind), Deno.DenoError, - `Could not get user ${s.name} directory.` + `Could not get user ${s.kind} directory.` ); } } } }); -testPerm({}, function getUserDirWithoutPermission(): void { - const funcs: string[] = [ - "configDir", - "cacheDir", - "dataDir", - "dataLocalDir", - "audioDir", - "desktopDir", - "documentDir", - "downloadDir", - "fontDir", - "pictureDir", - "publicDir", - "templateDir", - "videoDir" - ]; - - for (const fnName of funcs) { - console.log(`test Deno.${fnName}()`); - const fn = Deno[fnName]; - - assertThrows( - () => fn(), - Deno.DenoError, - `run again with the --allow-env flag` - ); - } +testPerm({}, function getDirWithoutPermission(): void { + assertThrows( + () => Deno.dir("home"), + Deno.DenoError, + `run again with the --allow-env flag` + ); }); testPerm({ env: true }, function execPath(): void { diff --git a/cli/ops/os.rs b/cli/ops/os.rs index 13fadbf6d5..03f81838cc 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -60,7 +60,7 @@ fn op_start( #[derive(Deserialize)] struct GetDirArgs { - name: std::string::String, + kind: std::string::String, } fn op_get_dir( @@ -71,7 +71,7 @@ fn op_get_dir( state.check_env()?; let args: GetDirArgs = serde_json::from_value(args)?; - let path = match args.name.as_str() { + let path = match args.kind.as_str() { "home" => dirs::home_dir(), "config" => dirs::config_dir(), "cache" => dirs::cache_dir(), @@ -89,7 +89,7 @@ fn op_get_dir( _ => { return Err(ErrBox::from(Error::new( ErrorKind::InvalidInput, - format!("Invalid dir type `{}`", args.name.as_str()), + format!("Invalid dir type `{}`", args.kind.as_str()), ))) } }; @@ -97,7 +97,7 @@ fn op_get_dir( if path == None { Err(ErrBox::from(Error::new( ErrorKind::NotFound, - format!("Could not get user {} directory.", args.name.as_str()), + format!("Could not get user {} directory.", args.kind.as_str()), ))) } else { Ok(JsonOp::Sync(json!(path