summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2020-06-20 23:49:27 -0400
committerGitHub <noreply@github.com>2020-06-20 23:49:27 -0400
commit0a81ec6b1e00ef01900393ae0460eaf3a6ec05d6 (patch)
tree020b1d71aabd8102ae85ae24fc89becb035b4854 /cli
parentc0ea9a99c0dc21faf46f73dca481361853e914fa (diff)
Remove Deno.dir and dirs dependency (#6385)
Diffstat (limited to 'cli')
-rw-r--r--cli/Cargo.toml4
-rw-r--r--cli/deno_dir.rs66
-rw-r--r--cli/js/deno_unstable.ts2
-rw-r--r--cli/js/diagnostics_util.ts2
-rw-r--r--cli/js/lib.deno.unstable.d.ts169
-rw-r--r--cli/js/ops/os.ts30
-rw-r--r--cli/ops/os.rs58
-rw-r--r--cli/tests/unit/os_test.ts156
8 files changed, 68 insertions, 419 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index 61dd868c8..55d058cef 100644
--- a/cli/Cargo.toml
+++ b/cli/Cargo.toml
@@ -28,7 +28,6 @@ base64 = "0.12.2"
bytes = "0.5.5"
byteorder = "1.3.4"
clap = "2.33.1"
-dirs = "1"
dissimilar = "1.0.2"
dlopen = "0.1.8"
dprint-plugin-typescript = "0.19.2"
@@ -64,7 +63,8 @@ uuid = { version = "0.8.1", features = ["v4"] }
swc_ecma_visit = "0.5.1"
[target.'cfg(windows)'.dependencies]
-winapi = "0.3.8"
+winapi = { version = "0.3.8", features = ["knownfolders", "objbase", "shlobj",
+"winbase", "winerror"] }
fwdansi = "1.1.0"
[target.'cfg(unix)'.dependencies]
diff --git a/cli/deno_dir.rs b/cli/deno_dir.rs
index 1447682ec..076692621 100644
--- a/cli/deno_dir.rs
+++ b/cli/deno_dir.rs
@@ -6,7 +6,7 @@ use std::path::PathBuf;
/// in single directory that can be controlled with `$DENO_DIR` env variable.
#[derive(Clone)]
pub struct DenoDir {
- // Example: /Users/rld/.deno/
+ /// Example: /Users/rld/.deno/
pub root: PathBuf,
/// Used by TsCompiler to cache compiler output.
pub gen_cache: DiskCache,
@@ -45,3 +45,67 @@ impl DenoDir {
Ok(deno_dir)
}
}
+
+/// To avoid the poorly managed dirs crate
+#[cfg(not(windows))]
+mod dirs {
+ use std::path::PathBuf;
+
+ pub fn cache_dir() -> Option<PathBuf> {
+ if cfg!(target_os = "macos") {
+ home_dir().map(|h| h.join("Library/Caches"))
+ } else {
+ std::env::var_os("XDG_CACHE_HOME")
+ .map(PathBuf::from)
+ .or_else(|| home_dir().map(|h| h.join(".cache")))
+ }
+ }
+
+ pub fn home_dir() -> Option<PathBuf> {
+ std::env::var_os("HOME")
+ .and_then(|h| if h.is_empty() { None } else { Some(h) })
+ .map(PathBuf::from)
+ }
+}
+
+/// To avoid the poorly managed dirs crate
+// Copied from
+// https://github.com/dirs-dev/dirs-sys-rs/blob/ec7cee0b3e8685573d847f0a0f60aae3d9e07fa2/src/lib.rs#L140-L164
+// MIT license. Copyright (c) 2018-2019 dirs-rs contributors
+#[cfg(windows)]
+mod dirs {
+ use std::ffi::OsString;
+ use std::os::windows::ffi::OsStringExt;
+ use std::path::PathBuf;
+ use winapi::shared::winerror;
+ use winapi::um::{combaseapi, knownfolders, shlobj, shtypes, winbase, winnt};
+
+ fn known_folder(folder_id: shtypes::REFKNOWNFOLDERID) -> Option<PathBuf> {
+ unsafe {
+ let mut path_ptr: winnt::PWSTR = std::ptr::null_mut();
+ let result = shlobj::SHGetKnownFolderPath(
+ folder_id,
+ 0,
+ std::ptr::null_mut(),
+ &mut path_ptr,
+ );
+ if result == winerror::S_OK {
+ let len = winbase::lstrlenW(path_ptr) as usize;
+ let path = std::slice::from_raw_parts(path_ptr, len);
+ let ostr: OsString = OsStringExt::from_wide(path);
+ combaseapi::CoTaskMemFree(path_ptr as *mut winapi::ctypes::c_void);
+ Some(PathBuf::from(ostr))
+ } else {
+ None
+ }
+ }
+ }
+
+ pub fn cache_dir() -> Option<PathBuf> {
+ known_folder(&knownfolders::FOLDERID_LocalAppData)
+ }
+
+ pub fn home_dir() -> Option<PathBuf> {
+ known_folder(&knownfolders::FOLDERID_Profile)
+ }
+}
diff --git a/cli/js/deno_unstable.ts b/cli/js/deno_unstable.ts
index 991df9955..d8624c675 100644
--- a/cli/js/deno_unstable.ts
+++ b/cli/js/deno_unstable.ts
@@ -5,7 +5,7 @@
export { umask } from "./ops/fs/umask.ts";
export { linkSync, link } from "./ops/fs/link.ts";
export { symlinkSync, symlink } from "./ops/fs/symlink.ts";
-export { dir, loadavg, osRelease, hostname } from "./ops/os.ts";
+export { loadavg, osRelease, hostname } from "./ops/os.ts";
export { openPlugin } from "./ops/plugins.ts";
export { transpileOnly, compile, bundle } from "./compiler_api.ts";
export { applySourceMap, formatDiagnostics } from "./ops/errors.ts";
diff --git a/cli/js/diagnostics_util.ts b/cli/js/diagnostics_util.ts
index 7b66d72a3..905a4397c 100644
--- a/cli/js/diagnostics_util.ts
+++ b/cli/js/diagnostics_util.ts
@@ -16,8 +16,6 @@ const unstableDenoGlobalProperties = [
"link",
"symlinkSync",
"symlink",
- "DirKind",
- "dir",
"loadavg",
"osRelease",
"openPlugin",
diff --git a/cli/js/lib.deno.unstable.d.ts b/cli/js/lib.deno.unstable.d.ts
index dd8de2eb6..3339bfbac 100644
--- a/cli/js/lib.deno.unstable.d.ts
+++ b/cli/js/lib.deno.unstable.d.ts
@@ -83,175 +83,6 @@ declare namespace Deno {
options?: SymlinkOptions
): Promise<void>;
- /** **UNSTABLE** */
- export type DirKind =
- | "home"
- | "cache"
- | "config"
- | "executable"
- | "data"
- | "data_local"
- | "audio"
- | "desktop"
- | "document"
- | "download"
- | "font"
- | "picture"
- | "public"
- | "template"
- | "tmp"
- | "video";
-
- /**
- * **UNSTABLE**: Currently under evaluation to decide if method name `dir` and
- * parameter type alias name `DirKind` should be renamed.
- *
- * Returns the user and platform specific directories.
- *
- * ```ts
- * const homeDirectory = Deno.dir("home");
- * ```
- *
- * Requires `allow-env` permission.
- *
- * Returns `null` if there is no applicable directory or if any other error
- * occurs.
- *
- * Argument values: `"home"`, `"cache"`, `"config"`, `"executable"`, `"data"`,
- * `"data_local"`, `"audio"`, `"desktop"`, `"document"`, `"download"`,
- * `"font"`, `"picture"`, `"public"`, `"template"`, `"tmp"`, `"video"`
- *
- * `"home"`
- *
- * |Platform | Value | Example |
- * | ------- | -----------------------------------------| -----------------------|
- * | Linux | `$HOME` | /home/alice |
- * | macOS | `$HOME` | /Users/alice |
- * | Windows | `{FOLDERID_Profile}` | C:\Users\Alice |
- *
- * `"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 |
- *
- * `"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 |
- *
- * `"executable"`
- *
- * |Platform | Value | Example |
- * | ------- | --------------------------------------------------------------- | -----------------------|
- * | Linux | `XDG_BIN_HOME` or `$XDG_DATA_HOME`/../bin or `$HOME`/.local/bin | /home/alice/.local/bin |
- * | macOS | - | - |
- * | Windows | - | - |
- *
- * `"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 |
- *
- * `"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 |
- *
- * `"audio"`
- *
- * |Platform | Value | Example |
- * | ------- | ------------------ | -------------------- |
- * | Linux | `XDG_MUSIC_DIR` | /home/alice/Music |
- * | macOS | `$HOME`/Music | /Users/Alice/Music |
- * | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music |
- *
- * `"desktop"`
- *
- * |Platform | Value | Example |
- * | ------- | -------------------- | ---------------------- |
- * | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop |
- * | macOS | `$HOME`/Desktop | /Users/Alice/Desktop |
- * | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop |
- *
- * `"document"`
- *
- * |Platform | Value | Example |
- * | ------- | ---------------------- | ------------------------ |
- * | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents |
- * | macOS | `$HOME`/Documents | /Users/Alice/Documents |
- * | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents |
- *
- * `"download"`
- *
- * |Platform | Value | Example |
- * | ------- | ---------------------- | ------------------------ |
- * | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads |
- * | macOS | `$HOME`/Downloads | /Users/Alice/Downloads |
- * | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads |
- *
- * `"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 | – | – |
- *
- * `"picture"`
- *
- * |Platform | Value | Example |
- * | ------- | --------------------- | ----------------------- |
- * | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures |
- * | macOS | `$HOME`/Pictures | /Users/Alice/Pictures |
- * | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures |
- *
- * `"public"`
- *
- * |Platform | Value | Example |
- * | ------- | --------------------- | ------------------- |
- * | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public |
- * | macOS | `$HOME`/Public | /Users/Alice/Public |
- * | Windows | `{FOLDERID_Public}` | C:\Users\Public |
- *
- * `"template"`
- *
- * |Platform | Value | Example |
- * | ------- | ---------------------- | ---------------------------------------------------------- |
- * | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates |
- * | macOS | – | – |
- * | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates |
- *
- * `"tmp"`
- *
- * |Platform | Value | Example |
- * | ------- | ---------------------- | ---------------------------------------------------------- |
- * | Linux | `TMPDIR` | /tmp |
- * | macOS | `TMPDIR` | /tmp |
- * | Windows | `{TMP}` | C:\Users\Alice\AppData\Local\Temp |
- *
- * `"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 dir(kind: DirKind): string | null;
-
/** **Unstable** There are questions around which permission this needs. And
* maybe should be renamed (loadAverage?)
*
diff --git a/cli/js/ops/os.ts b/cli/js/ops/os.ts
index e63d8b358..696c9f10b 100644
--- a/cli/js/ops/os.ts
+++ b/cli/js/ops/os.ts
@@ -1,6 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { sendSync } from "./dispatch_json.ts";
-import { errors } from "../errors.ts";
export function loadavg(): number[] {
return sendSync("op_loadavg");
@@ -40,35 +39,6 @@ export const env = {
delete: deleteEnv,
};
-type DirKind =
- | "home"
- | "cache"
- | "config"
- | "executable"
- | "data"
- | "data_local"
- | "audio"
- | "desktop"
- | "document"
- | "download"
- | "font"
- | "picture"
- | "public"
- | "template"
- | "tmp"
- | "video";
-
-export function dir(kind: DirKind): string | null {
- try {
- return sendSync("op_get_dir", { kind });
- } catch (error) {
- if (error instanceof errors.PermissionDenied) {
- throw error;
- }
- return null;
- }
-}
-
export function execPath(): string {
return sendSync("op_exec_path");
}
diff --git a/cli/ops/os.rs b/cli/ops/os.rs
index ab7ed2876..f084f213b 100644
--- a/cli/ops/os.rs
+++ b/cli/ops/os.rs
@@ -6,7 +6,6 @@ use deno_core::CoreIsolate;
use deno_core::ZeroCopyBuf;
use std::collections::HashMap;
use std::env;
-use std::io::{Error, ErrorKind};
use url::Url;
pub fn init(i: &mut CoreIsolate, s: &State) {
@@ -16,68 +15,11 @@ pub fn init(i: &mut CoreIsolate, s: &State) {
i.register_op("op_set_env", s.stateful_json_op(op_set_env));
i.register_op("op_get_env", s.stateful_json_op(op_get_env));
i.register_op("op_delete_env", s.stateful_json_op(op_delete_env));
- i.register_op("op_get_dir", s.stateful_json_op(op_get_dir));
i.register_op("op_hostname", s.stateful_json_op(op_hostname));
i.register_op("op_loadavg", s.stateful_json_op(op_loadavg));
i.register_op("op_os_release", s.stateful_json_op(op_os_release));
}
-#[derive(Deserialize)]
-struct GetDirArgs {
- kind: std::string::String,
-}
-
-fn op_get_dir(
- state: &State,
- args: Value,
- _zero_copy: &mut [ZeroCopyBuf],
-) -> Result<JsonOp, OpError> {
- state.check_unstable("Deno.dir");
- state.check_env()?;
- let args: GetDirArgs = serde_json::from_value(args)?;
-
- let path = match args.kind.as_str() {
- "home" => dirs::home_dir(),
- "config" => dirs::config_dir(),
- "cache" => dirs::cache_dir(),
- "executable" => dirs::executable_dir(),
- "data" => dirs::data_dir(),
- "data_local" => dirs::data_local_dir(),
- "audio" => dirs::audio_dir(),
- "desktop" => dirs::desktop_dir(),
- "document" => dirs::document_dir(),
- "download" => dirs::download_dir(),
- "font" => dirs::font_dir(),
- "picture" => dirs::picture_dir(),
- "public" => dirs::public_dir(),
- "template" => dirs::template_dir(),
- "tmp" => Some(std::env::temp_dir()),
- "video" => dirs::video_dir(),
- _ => {
- return Err(
- Error::new(
- ErrorKind::InvalidInput,
- format!("Invalid dir type `{}`", args.kind.as_str()),
- )
- .into(),
- )
- }
- };
-
- if path == None {
- Err(OpError::not_found(format!(
- "Could not get user {} directory.",
- args.kind.as_str()
- )))
- } else {
- Ok(JsonOp::Sync(json!(path
- .unwrap_or_default()
- .into_os_string()
- .into_string()
- .unwrap_or_default())))
- }
-}
-
fn op_exec_path(
state: &State,
_args: Value,
diff --git a/cli/tests/unit/os_test.ts b/cli/tests/unit/os_test.ts
index 9698692c7..e838cb7c3 100644
--- a/cli/tests/unit/os_test.ts
+++ b/cli/tests/unit/os_test.ts
@@ -123,162 +123,6 @@ unitTest(function osPid(): void {
assert(Deno.pid > 0);
});
-unitTest({ perms: { env: true } }, function getDir(): void {
- type supportOS = "darwin" | "windows" | "linux";
-
- interface Runtime {
- os: supportOS;
- shouldHaveValue: boolean;
- }
-
- interface Scenes {
- kind: Deno.DirKind;
- runtime: Runtime[];
- }
-
- const scenes: Scenes[] = [
- {
- kind: "config",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: true },
- ],
- },
- {
- kind: "cache",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: true },
- ],
- },
- {
- kind: "executable",
- runtime: [
- { os: "darwin", shouldHaveValue: false },
- { os: "windows", shouldHaveValue: false },
- { os: "linux", shouldHaveValue: true },
- ],
- },
- {
- kind: "data",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: true },
- ],
- },
- {
- kind: "data_local",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: true },
- ],
- },
- {
- kind: "audio",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: false },
- ],
- },
- {
- kind: "desktop",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: false },
- ],
- },
- {
- kind: "document",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: false },
- ],
- },
- {
- kind: "download",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: false },
- ],
- },
- {
- kind: "font",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: false },
- { os: "linux", shouldHaveValue: true },
- ],
- },
- {
- kind: "picture",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: false },
- ],
- },
- {
- kind: "public",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: false },
- ],
- },
- {
- kind: "template",
- runtime: [
- { os: "darwin", shouldHaveValue: false },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: false },
- ],
- },
- {
- kind: "tmp",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: true },
- ],
- },
- {
- kind: "video",
- runtime: [
- { os: "darwin", shouldHaveValue: true },
- { os: "windows", shouldHaveValue: true },
- { os: "linux", shouldHaveValue: false },
- ],
- },
- ];
-
- for (const s of scenes) {
- for (const r of s.runtime) {
- if (Deno.build.os !== r.os) continue;
- if (r.shouldHaveValue) {
- const d = Deno.dir(s.kind);
- assert(d);
- assert(d.length > 0);
- }
- }
- }
-});
-
-unitTest(function getDirWithoutPermission(): void {
- assertThrows(
- () => Deno.dir("home"),
- Deno.errors.PermissionDenied,
- `run again with the --allow-env flag`
- );
-});
-
unitTest({ perms: { read: true } }, function execPath(): void {
assertNotEquals(Deno.execPath(), "");
});