1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 00:59:24 -05:00

Remove Deno.dir and dirs dependency (#6385)

This commit is contained in:
Ryan Dahl 2020-06-20 23:49:27 -04:00 committed by GitHub
parent c0ea9a99c0
commit 0a81ec6b1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 74 additions and 488 deletions

64
Cargo.lock generated
View file

@ -70,18 +70,6 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034"
[[package]]
name = "arrayref"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
[[package]]
name = "arrayvec"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"
[[package]]
name = "ast_node"
version = "0.6.0"
@ -160,17 +148,6 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "blake2b_simd"
version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a"
dependencies = [
"arrayref",
"arrayvec",
"constant_time_eq",
]
[[package]]
name = "block-buffer"
version = "0.7.3"
@ -332,12 +309,6 @@ dependencies = [
"proc-macro-hack",
]
[[package]]
name = "constant_time_eq"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "crc32fast"
version = "1.2.0"
@ -426,7 +397,6 @@ dependencies = [
"deno_core",
"deno_lint",
"deno_typescript",
"dirs",
"dissimilar",
"dlopen",
"dprint-plugin-typescript",
@ -529,17 +499,6 @@ dependencies = [
"generic-array",
]
[[package]]
name = "dirs"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901"
dependencies = [
"libc",
"redox_users",
"winapi 0.3.8",
]
[[package]]
name = "dissimilar"
version = "1.0.2"
@ -1887,17 +1846,6 @@ version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
[[package]]
name = "redox_users"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431"
dependencies = [
"getrandom",
"redox_syscall",
"rust-argon2",
]
[[package]]
name = "regex"
version = "1.3.9"
@ -1976,18 +1924,6 @@ dependencies = [
"winapi 0.3.8",
]
[[package]]
name = "rust-argon2"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017"
dependencies = [
"base64 0.11.0",
"blake2b_simd",
"constant_time_eq",
"crossbeam-utils",
]
[[package]]
name = "rustc_version"
version = "0.2.3"

View file

@ -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]

View file

@ -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)
}
}

View file

@ -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";

View file

@ -16,8 +16,6 @@ const unstableDenoGlobalProperties = [
"link",
"symlinkSync",
"symlink",
"DirKind",
"dir",
"loadavg",
"osRelease",
"openPlugin",

View file

@ -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?)
*

View file

@ -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");
}

View file

@ -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,

View file

@ -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(), "");
});

View file

@ -136,12 +136,12 @@ export function getPriority(pid = 0): number {
/** Returns the string path of the current user's home directory. */
export function homedir(): string | null {
return Deno.dir("home");
notImplemented(SEE_GITHUB_ISSUE);
}
/** Returns the host name of the operating system as a string. */
export function hostname(): string {
return Deno.hostname();
notImplemented(SEE_GITHUB_ISSUE);
}
/** Returns an array containing the 1, 5, and 15 minute load averages */
@ -182,7 +182,7 @@ export function setPriority(pid: number, priority?: number): void {
/** Returns the operating system's default directory for temporary files as a string. */
export function tmpdir(): string | null {
return Deno.dir("tmp");
notImplemented(SEE_GITHUB_ISSUE);
}
/** Not yet implemented */

View file

@ -10,6 +10,7 @@ Deno.test({
Deno.test({
name: "home directory is a string",
ignore: true,
fn() {
assertEquals(typeof os.homedir(), "string");
},
@ -17,6 +18,7 @@ Deno.test({
Deno.test({
name: "tmp directory is a string",
ignore: true,
fn() {
assertEquals(typeof os.tmpdir(), "string");
},
@ -24,6 +26,7 @@ Deno.test({
Deno.test({
name: "hostname is a string",
ignore: true,
fn() {
assertEquals(typeof os.hostname(), "string");
},
@ -192,8 +195,6 @@ Deno.test({
fn() {
assertEquals(`${os.arch}`, os.arch());
assertEquals(`${os.endianness}`, os.endianness());
assertEquals(`${os.homedir}`, os.homedir());
assertEquals(`${os.hostname}`, os.hostname());
assertEquals(`${os.platform}`, os.platform());
},
});