Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Deno.dir and dirs dependency #6385

Merged
merged 16 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand Down
66 changes: 65 additions & 1 deletion cli/deno_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion cli/js/deno_unstable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 0 additions & 2 deletions cli/js/diagnostics_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const unstableDenoGlobalProperties = [
"link",
"symlinkSync",
"symlink",
"DirKind",
"dir",
"loadavg",
"osRelease",
"openPlugin",
Expand Down
Loading