2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 02:34:02 +02:00
|
|
|
|
2024-12-30 12:38:20 -05:00
|
|
|
use deno_cache_dir::DenoDirResolutionError;
|
2023-05-25 14:27:45 -04:00
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
|
2022-11-25 19:04:30 -05:00
|
|
|
use super::DiskCache;
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2023-03-16 12:22:24 -04:00
|
|
|
use std::env;
|
2018-07-26 17:54:22 -04:00
|
|
|
use std::path::PathBuf;
|
2019-06-24 18:04:06 +02:00
|
|
|
|
2023-05-25 14:27:45 -04:00
|
|
|
/// Lazily creates the deno dir which might be useful in scenarios
|
|
|
|
/// where functionality wants to continue if the DENO_DIR can't be created.
|
|
|
|
pub struct DenoDirProvider {
|
|
|
|
maybe_custom_root: Option<PathBuf>,
|
2024-12-30 12:38:20 -05:00
|
|
|
deno_dir: OnceCell<Result<DenoDir, DenoDirResolutionError>>,
|
2023-05-25 14:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DenoDirProvider {
|
|
|
|
pub fn new(maybe_custom_root: Option<PathBuf>) -> Self {
|
|
|
|
Self {
|
|
|
|
maybe_custom_root,
|
|
|
|
deno_dir: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-30 12:38:20 -05:00
|
|
|
pub fn get_or_create(&self) -> Result<&DenoDir, DenoDirResolutionError> {
|
2023-05-25 14:27:45 -04:00
|
|
|
self
|
|
|
|
.deno_dir
|
|
|
|
.get_or_init(|| DenoDir::new(self.maybe_custom_root.clone()))
|
|
|
|
.as_ref()
|
2024-12-30 12:38:20 -05:00
|
|
|
.map_err(|err| match err {
|
|
|
|
DenoDirResolutionError::NoCacheOrHomeDir => {
|
|
|
|
DenoDirResolutionError::NoCacheOrHomeDir
|
|
|
|
}
|
|
|
|
DenoDirResolutionError::FailedCwd { source } => {
|
|
|
|
DenoDirResolutionError::FailedCwd {
|
|
|
|
source: std::io::Error::new(source.kind(), source.to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-05-25 14:27:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 00:15:30 +02:00
|
|
|
/// `DenoDir` serves as coordinator for multiple `DiskCache`s containing them
|
|
|
|
/// in single directory that can be controlled with `$DENO_DIR` env variable.
|
2024-05-09 20:22:27 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2018-07-26 17:54:22 -04:00
|
|
|
pub struct DenoDir {
|
2020-06-20 23:49:27 -04:00
|
|
|
/// Example: /Users/rld/.deno/
|
2024-05-09 20:22:27 +01:00
|
|
|
pub root: PathBuf,
|
2019-07-31 13:58:41 +02:00
|
|
|
/// Used by TsCompiler to cache compiler output.
|
|
|
|
pub gen_cache: DiskCache,
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DenoDir {
|
2024-12-30 12:38:20 -05:00
|
|
|
pub fn new(
|
|
|
|
maybe_custom_root: Option<PathBuf>,
|
|
|
|
) -> Result<Self, deno_cache_dir::DenoDirResolutionError> {
|
|
|
|
let root = deno_cache_dir::resolve_deno_dir(
|
|
|
|
&sys_traits::impls::RealSys,
|
|
|
|
maybe_custom_root,
|
|
|
|
)?;
|
2020-05-15 16:32:52 +02:00
|
|
|
assert!(root.is_absolute());
|
2019-07-31 13:58:41 +02:00
|
|
|
let gen_path = root.join("gen");
|
2019-04-29 15:58:31 +01:00
|
|
|
|
2018-11-04 22:21:21 -08:00
|
|
|
let deno_dir = Self {
|
2018-08-14 16:50:53 -04:00
|
|
|
root,
|
2019-07-31 13:58:41 +02:00
|
|
|
gen_cache: DiskCache::new(&gen_path),
|
2018-08-14 16:50:53 -04:00
|
|
|
};
|
2019-01-17 23:39:06 -05:00
|
|
|
|
2018-07-26 17:54:22 -04:00
|
|
|
Ok(deno_dir)
|
|
|
|
}
|
2022-04-19 22:14:00 -04:00
|
|
|
|
2022-11-25 19:04:30 -05:00
|
|
|
/// The root directory of the DENO_DIR for display purposes only.
|
|
|
|
pub fn root_path_for_display(&self) -> std::path::Display {
|
|
|
|
self.root.display()
|
|
|
|
}
|
|
|
|
|
2024-05-29 14:38:18 -04:00
|
|
|
/// Path for the V8 code cache.
|
|
|
|
pub fn code_cache_db_file_path(&self) -> PathBuf {
|
|
|
|
// bump this version name to invalidate the entire cache
|
|
|
|
self.root.join("v8_code_cache_v2")
|
|
|
|
}
|
|
|
|
|
2022-04-19 22:14:00 -04:00
|
|
|
/// Path for the incremental cache used for formatting.
|
|
|
|
pub fn fmt_incremental_cache_db_file_path(&self) -> PathBuf {
|
|
|
|
// bump this version name to invalidate the entire cache
|
2024-05-29 14:38:18 -04:00
|
|
|
self.root.join("fmt_incremental_cache_v2")
|
2022-04-19 22:14:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Path for the incremental cache used for linting.
|
|
|
|
pub fn lint_incremental_cache_db_file_path(&self) -> PathBuf {
|
|
|
|
// bump this version name to invalidate the entire cache
|
2024-05-29 14:38:18 -04:00
|
|
|
self.root.join("lint_incremental_cache_v2")
|
2022-04-19 22:14:00 -04:00
|
|
|
}
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2022-08-22 12:14:59 -04:00
|
|
|
/// Path for caching swc dependency analysis.
|
|
|
|
pub fn dep_analysis_db_file_path(&self) -> PathBuf {
|
|
|
|
// bump this version name to invalidate the entire cache
|
2024-05-29 14:38:18 -04:00
|
|
|
self.root.join("dep_analysis_cache_v2")
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|
|
|
|
|
2024-02-20 16:29:57 -05:00
|
|
|
/// Path for the cache used for fast check.
|
|
|
|
pub fn fast_check_cache_db_file_path(&self) -> PathBuf {
|
|
|
|
// bump this version name to invalidate the entire cache
|
2024-05-29 14:38:18 -04:00
|
|
|
self.root.join("fast_check_cache_v2")
|
2024-02-20 16:29:57 -05:00
|
|
|
}
|
|
|
|
|
2022-10-01 06:15:56 -04:00
|
|
|
/// Path for caching node analysis.
|
|
|
|
pub fn node_analysis_db_file_path(&self) -> PathBuf {
|
|
|
|
// bump this version name to invalidate the entire cache
|
2024-05-29 14:38:18 -04:00
|
|
|
self.root.join("node_analysis_cache_v2")
|
2022-10-01 06:15:56 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
/// Path for the cache used for type checking.
|
2022-07-12 18:58:39 -04:00
|
|
|
pub fn type_checking_cache_db_file_path(&self) -> PathBuf {
|
|
|
|
// bump this version name to invalidate the entire cache
|
2024-05-29 14:38:18 -04:00
|
|
|
self.root.join("check_cache_v2")
|
2022-07-12 18:58:39 -04:00
|
|
|
}
|
2022-11-25 19:04:30 -05:00
|
|
|
|
|
|
|
/// Path to the registries cache, used for the lps.
|
|
|
|
pub fn registries_folder_path(&self) -> PathBuf {
|
|
|
|
self.root.join("registries")
|
|
|
|
}
|
|
|
|
|
2024-10-01 14:05:40 -04:00
|
|
|
/// Path to the remote cache folder.
|
|
|
|
pub fn remote_folder_path(&self) -> PathBuf {
|
|
|
|
self.root.join("remote")
|
2022-11-25 19:04:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Path to the origin data cache folder.
|
|
|
|
pub fn origin_data_folder_path(&self) -> PathBuf {
|
|
|
|
// TODO(@crowlKats): change to origin_data for 2.0
|
|
|
|
self.root.join("location_data")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// File used for the upgrade checker.
|
|
|
|
pub fn upgrade_check_file_path(&self) -> PathBuf {
|
|
|
|
self.root.join("latest.txt")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Folder used for the npm cache.
|
|
|
|
pub fn npm_folder_path(&self) -> PathBuf {
|
|
|
|
self.root.join("npm")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Path used for the REPL history file.
|
2023-03-16 12:22:24 -04:00
|
|
|
/// Can be overridden or disabled by setting `DENO_REPL_HISTORY` environment variable.
|
|
|
|
pub fn repl_history_file_path(&self) -> Option<PathBuf> {
|
|
|
|
if let Some(deno_repl_history) = env::var_os("DENO_REPL_HISTORY") {
|
|
|
|
if deno_repl_history.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(PathBuf::from(deno_repl_history))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some(self.root.join("deno_history.txt"))
|
|
|
|
}
|
2022-11-25 19:04:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Folder path used for downloading new versions of deno.
|
|
|
|
pub fn dl_folder_path(&self) -> PathBuf {
|
|
|
|
self.root.join("dl")
|
|
|
|
}
|
2019-07-18 00:15:30 +02:00
|
|
|
}
|