forked from denoland/deno_cache_dir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeno_dir.rs
More file actions
62 lines (57 loc) · 1.89 KB
/
deno_dir.rs
File metadata and controls
62 lines (57 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2018-2025 the Deno authors. MIT license.
use std::path::PathBuf;
use sys_traits::EnvCacheDir;
use sys_traits::EnvCurrentDir;
use sys_traits::EnvHomeDir;
use sys_traits::EnvVar;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum DenoDirResolutionError {
#[error(
"Could not resolve global Deno cache directory. Please make sure that either the DENO_DIR environment variable is set or the cache directory is available."
)]
NoCacheOrHomeDir,
#[error(
"Could not resolve global Deno cache directory because the current working directory could not be resolved. Please set the DENO_DIR environment variable and ensure it is pointing at an absolute path."
)]
FailedCwd {
#[source]
source: std::io::Error,
},
}
#[sys_traits::auto_impl]
pub trait ResolveDenoDirSys:
EnvCacheDir + EnvHomeDir + EnvVar + EnvCurrentDir
{
}
pub fn resolve_deno_dir<Sys: ResolveDenoDirSys>(
sys: &Sys,
maybe_custom_root: Option<PathBuf>,
) -> Result<PathBuf, DenoDirResolutionError> {
let maybe_custom_root =
maybe_custom_root.or_else(|| sys.env_var_path("DENO_DIR"));
let root: PathBuf = if let Some(root) = maybe_custom_root {
root
} else if let Some(xdg_cache_dir) = sys.env_var_path("XDG_CACHE_HOME") {
xdg_cache_dir.join("deno")
} else if let Some(cache_dir) = sys.env_cache_dir() {
// We use the OS cache dir because all files deno writes are cache files
// Once that changes we need to start using different roots if DENO_DIR
// is not set, and keep a single one if it is.
cache_dir.join("deno")
} else if let Some(home_dir) = sys.env_home_dir() {
// fallback path
home_dir.join(".deno")
} else {
return Err(DenoDirResolutionError::NoCacheOrHomeDir);
};
let root = if root.is_absolute() {
root
} else {
sys
.env_current_dir()
.map_err(|source| DenoDirResolutionError::FailedCwd { source })?
.join(root)
};
Ok(root)
}