Skip to content

Commit

Permalink
Feat: Add more dir APIs for Deno (denoland#3491)
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy authored and ry committed Dec 15, 2019
1 parent 22a2afe commit de94698
Show file tree
Hide file tree
Showing 6 changed files with 581 additions and 17 deletions.
22 changes: 21 additions & 1 deletion cli/js/deno.ts
@@ -1,7 +1,27 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

// Public deno module.
export { env, exit, isTTY, execPath, homeDir, hostname } from "./os.ts";
export {
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 {
File,
Expand Down
3 changes: 2 additions & 1 deletion cli/js/dispatch.ts
Expand Up @@ -13,7 +13,7 @@ export let OP_EXEC_PATH: number;
export let OP_UTIME: number;
export let OP_SET_ENV: number;
export let OP_GET_ENV: number;
export let OP_HOME_DIR: number;
export let OP_GET_DIR: number;
export let OP_START: number;
export let OP_APPLY_SOURCE_MAP: number;
export let OP_FORMAT_ERROR: number;
Expand Down Expand Up @@ -85,6 +85,7 @@ export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
case OP_READ:
minimal.asyncMsgFromRust(opId, ui8);
break;
case OP_GET_DIR:
case OP_EXIT:
case OP_IS_TTY:
case OP_ENV:
Expand Down
144 changes: 144 additions & 0 deletions cli/js/lib.deno_runtime.d.ts
Expand Up @@ -56,9 +56,153 @@ declare namespace Deno {
export function env(key: string): string | undefined;
/**
* 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
* Requires the `--allow-env` flag.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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;
/**
* Returns the path to the current deno executable.
* Requires the `--allow-env` flag.
Expand Down
188 changes: 183 additions & 5 deletions cli/js/os.ts
Expand Up @@ -133,11 +133,189 @@ export function start(preserveDenoNamespace = true, source?: string): Start {
* Requires the `--allow-env` flag.
*/
export function homeDir(): string {
const path = sendSync(dispatch.OP_HOME_DIR);
if (!path) {
throw new Error("Could not get home directory.");
}
return path;
return sendSync(dispatch.OP_GET_DIR, { name: "home" });
}

/**
* Returns the current user's cache directory.
* If the directory does not exist, an exception is thrown
* Requires the `--allow-env` flag.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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.
* |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" });
}

/**
Expand Down

0 comments on commit de94698

Please sign in to comment.