1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

Reduce all directory functions to Deno.dir() (#3518)

This commit is contained in:
Ry Dahl 2019-12-18 09:29:00 -05:00 committed by GitHub
parent ff6b514a7b
commit 077b6f7672
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 127 additions and 303 deletions

View file

@ -1,27 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// Public deno module. // Public deno module.
export { export { dir, env, exit, isTTY, execPath, hostname } from "./os.ts";
env,
exit,
isTTY,
execPath,
homeDir,
cacheDir,
configDir,
dataDir,
dataLocalDir,
audioDir,
desktopDir,
documentDir,
downloadDir,
fontDir,
pictureDir,
publicDir,
templateDir,
videoDir,
hostname
} from "./os.ts";
export { chdir, cwd } from "./dir.ts"; export { chdir, cwd } from "./dir.ts";
export { export {
File, File,

View file

@ -54,155 +54,124 @@ declare namespace Deno {
* console.log(myEnv.TEST_VAR == newEnv.TEST_VAR); * console.log(myEnv.TEST_VAR == newEnv.TEST_VAR);
*/ */
export function env(key: string): string | undefined; 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. * Returns the user and platform specific directories.
* 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
* Requires the `--allow-env` flag. * 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 | * |Platform | Value | Example |
* | ------- | ----------------------------------- | ---------------------------- | * | ------- | ----------------------------------- | ---------------------------- |
* | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache | * | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache |
* | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches | * | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches |
* | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | * | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
*/ *
export function cacheDir(): string; * "config"
/**
* Returns the current user's config directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ------------------------------------- | -------------------------------- | * | ------- | ------------------------------------- | -------------------------------- |
* | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config | * | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config |
* | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences | * | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences |
* | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | * | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
*/ *
export function configDir(): string; * "data"
/**
* Returns the current user's data directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------------------------- | ---------------------------------------- | * | ------- | ---------------------------------------- | ---------------------------------------- |
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | * | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
* | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | * | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
* | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | * | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
*/ *
export function dataDir(): string; * "data_local"
/**
* Returns the current user's local data directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------------------------- | ---------------------------------------- | * | ------- | ---------------------------------------- | ---------------------------------------- |
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | * | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
* | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | * | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
* | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | * | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
*/ *
export function dataLocalDir(): string; * "audio"
/**
* Returns the current user's audio directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ------------------ | -------------------- | * | ------- | ------------------ | -------------------- |
* | Linux | `XDG_MUSIC_DIR` | /home/alice/Music | * | Linux | `XDG_MUSIC_DIR` | /home/alice/Music |
* | macOS | `$HOME`/Music | /Users/Alice/Music | * | macOS | `$HOME`/Music | /Users/Alice/Music |
* | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music | * | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music |
*/ *
export function audioDir(): string; * "desktop"
/**
* Returns the current user's desktop directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | -------------------- | ---------------------- | * | ------- | -------------------- | ---------------------- |
* | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop | * | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop |
* | macOS | `$HOME`/Desktop | /Users/Alice/Desktop | * | macOS | `$HOME`/Desktop | /Users/Alice/Desktop |
* | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop | * | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop |
*/ *
export function desktopDir(): string; * "document"
/**
* Returns the current user's document directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------- | ------------------------ | * | ------- | ---------------------- | ------------------------ |
* | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents | * | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents |
* | macOS | `$HOME`/Documents | /Users/Alice/Documents | * | macOS | `$HOME`/Documents | /Users/Alice/Documents |
* | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents | * | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents |
*/ *
export function documentDir(): string; * "download"
/**
* Returns the current user's download directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------- | ------------------------ | * | ------- | ---------------------- | ------------------------ |
* | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads | * | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads |
* | macOS | `$HOME`/Downloads | /Users/Alice/Downloads | * | macOS | `$HOME`/Downloads | /Users/Alice/Downloads |
* | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads | * | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads |
*/ *
export function downloadDir(): string; * "font"
/**
* Returns the current user's font directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------------------------------------- | ------------------------------ | * | ------- | ---------------------------------------------------- | ------------------------------ |
* | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts | * | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts |
* | macOS | `$HOME/Library/Fonts` | /Users/Alice/Library/Fonts | * | macOS | `$HOME/Library/Fonts` | /Users/Alice/Library/Fonts |
* | Windows | | | * | Windows | | |
*/ *
export function fontDir(): string; * "picture"
/**
* Returns the current user's picture directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | --------------------- | ----------------------- | * | ------- | --------------------- | ----------------------- |
* | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures | * | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures |
* | macOS | `$HOME`/Pictures | /Users/Alice/Pictures | * | macOS | `$HOME`/Pictures | /Users/Alice/Pictures |
* | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures | * | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures |
*/ *
export function pictureDir(): string; * "public"
/**
* Returns the current user's public directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | --------------------- | ------------------- | * | ------- | --------------------- | ------------------- |
* | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public | * | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public |
* | macOS | `$HOME`/Public | /Users/Alice/Public | * | macOS | `$HOME`/Public | /Users/Alice/Public |
* | Windows | `{FOLDERID_Public}` | C:\Users\Public | * | Windows | `{FOLDERID_Public}` | C:\Users\Public |
*/ *
export function publicDir(): string; * "template"
/**
* Returns the current user's template directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------- | ---------------------------------------------------------- | * | ------- | ---------------------- | ---------------------------------------------------------- |
* | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates | * | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates |
* | macOS | | | * | macOS | | |
* | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates | * | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates |
*/ *
export function templateDir(): string; * "video"
/**
* Returns the current user's video directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ------------------- | --------------------- | * | ------- | ------------------- | --------------------- |
* | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos | * | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos |
* | 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 videoDir(): string; export function dir(kind: DirKind): string;
/** /**
* Returns the path to the current deno executable. * Returns the path to the current deno executable.
* Requires the `--allow-env` flag. * Requires the `--allow-env` flag.

View file

@ -128,194 +128,123 @@ export function start(preserveDenoNamespace = true, source?: string): Start {
return startResponse; return startResponse;
} }
/** type DirKind =
* Returns the current user's home directory. | "home"
* Requires the `--allow-env` flag. | "cache"
*/ | "config"
export function homeDir(): string { | "data"
return sendSync(dispatch.OP_GET_DIR, { name: "home" }); | "data_local"
} | "audio"
| "desktop"
| "document"
| "download"
| "font"
| "picture"
| "public"
| "template"
| "video";
/** /**
* Returns the current user's cache directory. * Returns the user and platform specific directories.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag. * 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 | * |Platform | Value | Example |
* | ------- | ----------------------------------- | ---------------------------- | * | ------- | ----------------------------------- | ---------------------------- |
* | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache | * | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache |
* | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches | * | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches |
* | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | * | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
*/ *
export function cacheDir(): string { * "config"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ------------------------------------- | -------------------------------- | * | ------- | ------------------------------------- | -------------------------------- |
* | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config | * | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config |
* | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences | * | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences |
* | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | * | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
*/ *
export function configDir(): string { * "data"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------------------------- | ---------------------------------------- | * | ------- | ---------------------------------------- | ---------------------------------------- |
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | * | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
* | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | * | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
* | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming | * | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
*/ *
export function dataDir(): string { * "data_local"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------------------------- | ---------------------------------------- | * | ------- | ---------------------------------------- | ---------------------------------------- |
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share | * | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
* | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support | * | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
* | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local | * | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
*/ *
export function dataLocalDir(): string { * "audio"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ------------------ | -------------------- | * | ------- | ------------------ | -------------------- |
* | Linux | `XDG_MUSIC_DIR` | /home/alice/Music | * | Linux | `XDG_MUSIC_DIR` | /home/alice/Music |
* | macOS | `$HOME`/Music | /Users/Alice/Music | * | macOS | `$HOME`/Music | /Users/Alice/Music |
* | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music | * | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music |
*/ *
export function audioDir(): string { * "desktop"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | -------------------- | ---------------------- | * | ------- | -------------------- | ---------------------- |
* | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop | * | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop |
* | macOS | `$HOME`/Desktop | /Users/Alice/Desktop | * | macOS | `$HOME`/Desktop | /Users/Alice/Desktop |
* | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop | * | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop |
*/ *
export function desktopDir(): string { * "document"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------- | ------------------------ | * | ------- | ---------------------- | ------------------------ |
* | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents | * | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents |
* | macOS | `$HOME`/Documents | /Users/Alice/Documents | * | macOS | `$HOME`/Documents | /Users/Alice/Documents |
* | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents | * | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents |
*/ *
export function documentDir(): string { * "download"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------- | ------------------------ | * | ------- | ---------------------- | ------------------------ |
* | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads | * | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads |
* | macOS | `$HOME`/Downloads | /Users/Alice/Downloads | * | macOS | `$HOME`/Downloads | /Users/Alice/Downloads |
* | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads | * | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads |
*/ *
export function downloadDir(): string { * "font"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------------------------------------- | ------------------------------ | * | ------- | ---------------------------------------------------- | ------------------------------ |
* | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts | * | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts |
* | macOS | `$HOME/Library/Fonts` | /Users/Alice/Library/Fonts | * | macOS | `$HOME/Library/Fonts` | /Users/Alice/Library/Fonts |
* | Windows | | | * | Windows | | |
*/ *
export function fontDir(): string { * "picture"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | --------------------- | ----------------------- | * | ------- | --------------------- | ----------------------- |
* | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures | * | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures |
* | macOS | `$HOME`/Pictures | /Users/Alice/Pictures | * | macOS | `$HOME`/Pictures | /Users/Alice/Pictures |
* | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures | * | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures |
*/ *
export function pictureDir(): string { * "public"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | --------------------- | ------------------- | * | ------- | --------------------- | ------------------- |
* | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public | * | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public |
* | macOS | `$HOME`/Public | /Users/Alice/Public | * | macOS | `$HOME`/Public | /Users/Alice/Public |
* | Windows | `{FOLDERID_Public}` | C:\Users\Public | * | Windows | `{FOLDERID_Public}` | C:\Users\Public |
*/ *
export function publicDir(): string { * "template"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ---------------------- | ---------------------------------------------------------- | * | ------- | ---------------------- | ---------------------------------------------------------- |
* | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates | * | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates |
* | macOS | | | * | macOS | | |
* | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates | * | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates |
*/ *
export function templateDir(): string { * "video"
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.
* |Platform | Value | Example | * |Platform | Value | Example |
* | ------- | ------------------- | --------------------- | * | ------- | ------------------- | --------------------- |
* | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos | * | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos |
* | 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 videoDir(): string { export function dir(kind: DirKind): string {
return sendSync(dispatch.OP_GET_DIR, { name: "video" }); return sendSync(dispatch.OP_GET_DIR, { kind });
} }
/** /**

View file

@ -116,23 +116,7 @@ test(function osIsTTYSmoke(): void {
console.log(Deno.isTTY()); console.log(Deno.isTTY());
}); });
testPerm({ env: true }, function homeDir(): void { testPerm({ env: true }, function getDir(): 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 {
type supportOS = "mac" | "win" | "linux"; type supportOS = "mac" | "win" | "linux";
interface Runtime { interface Runtime {
@ -141,15 +125,13 @@ testPerm({ env: true }, function getUserDir(): void {
} }
interface Scenes { interface Scenes {
name: string; kind: Deno.DirKind;
fn: string;
runtime: Runtime[]; runtime: Runtime[];
} }
const scenes: Scenes[] = [ const scenes: Scenes[] = [
{ {
name: "config", kind: "config",
fn: "configDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -157,8 +139,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "cache", kind: "cache",
fn: "cacheDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -166,8 +147,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "data", kind: "data",
fn: "dataDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -175,8 +155,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "data local", kind: "data_local",
fn: "dataLocalDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -184,8 +163,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "audio", kind: "audio",
fn: "audioDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -193,8 +171,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "desktop", kind: "desktop",
fn: "desktopDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -202,8 +179,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "document", kind: "document",
fn: "documentDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -211,8 +187,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "download", kind: "download",
fn: "downloadDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -220,8 +195,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "font", kind: "font",
fn: "fontDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: false }, { os: "win", shouldHaveValue: false },
@ -229,8 +203,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "picture", kind: "picture",
fn: "pictureDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -238,8 +211,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "public", kind: "public",
fn: "publicDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -247,8 +219,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "template", kind: "template",
fn: "templateDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: false }, { os: "mac", shouldHaveValue: false },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -256,8 +227,7 @@ testPerm({ env: true }, function getUserDir(): void {
] ]
}, },
{ {
name: "video", kind: "video",
fn: "videoDir",
runtime: [ runtime: [
{ os: "mac", shouldHaveValue: true }, { os: "mac", shouldHaveValue: true },
{ os: "win", shouldHaveValue: true }, { os: "win", shouldHaveValue: true },
@ -267,52 +237,28 @@ testPerm({ env: true }, function getUserDir(): void {
]; ];
for (const s of scenes) { for (const s of scenes) {
console.log(`test Deno.${s.fn}()`);
const fn = Deno[s.fn];
for (const r of s.runtime) { for (const r of s.runtime) {
if (Deno.build.os !== r.os) continue; if (Deno.build.os !== r.os) continue;
if (r.shouldHaveValue) { if (r.shouldHaveValue) {
assertNotEquals(fn(), ""); assertNotEquals(Deno.dir(s.kind), "");
} else { } else {
// if not support your platform. it should throw an error // if not support your platform. it should throw an error
assertThrows( assertThrows(
() => fn(), () => Deno.dir(s.kind),
Deno.DenoError, Deno.DenoError,
`Could not get user ${s.name} directory.` `Could not get user ${s.kind} directory.`
); );
} }
} }
} }
}); });
testPerm({}, function getUserDirWithoutPermission(): void { testPerm({}, function getDirWithoutPermission(): void {
const funcs: string[] = [ assertThrows(
"configDir", () => Deno.dir("home"),
"cacheDir", Deno.DenoError,
"dataDir", `run again with the --allow-env flag`
"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({ env: true }, function execPath(): void { testPerm({ env: true }, function execPath(): void {

View file

@ -60,7 +60,7 @@ fn op_start(
#[derive(Deserialize)] #[derive(Deserialize)]
struct GetDirArgs { struct GetDirArgs {
name: std::string::String, kind: std::string::String,
} }
fn op_get_dir( fn op_get_dir(
@ -71,7 +71,7 @@ fn op_get_dir(
state.check_env()?; state.check_env()?;
let args: GetDirArgs = serde_json::from_value(args)?; 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(), "home" => dirs::home_dir(),
"config" => dirs::config_dir(), "config" => dirs::config_dir(),
"cache" => dirs::cache_dir(), "cache" => dirs::cache_dir(),
@ -89,7 +89,7 @@ fn op_get_dir(
_ => { _ => {
return Err(ErrBox::from(Error::new( return Err(ErrBox::from(Error::new(
ErrorKind::InvalidInput, 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 { if path == None {
Err(ErrBox::from(Error::new( Err(ErrBox::from(Error::new(
ErrorKind::NotFound, ErrorKind::NotFound,
format!("Could not get user {} directory.", args.name.as_str()), format!("Could not get user {} directory.", args.kind.as_str()),
))) )))
} else { } else {
Ok(JsonOp::Sync(json!(path Ok(JsonOp::Sync(json!(path