2025-01-01 04:12:39 +09:00
|
|
|
// Copyright 2018-2025 the Deno authors. MIT license.
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-02-06 16:20:20 +01:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
2022-08-20 11:31:33 -04:00
|
|
|
use std::io::ErrorKind;
|
2024-06-26 17:24:10 -04:00
|
|
|
use std::path::Path;
|
2022-08-20 11:31:33 -04:00
|
|
|
use std::path::PathBuf;
|
2024-12-30 12:38:20 -05:00
|
|
|
|
|
|
|
use deno_package_json::PackageJson;
|
|
|
|
use deno_package_json::PackageJsonRc;
|
|
|
|
use sys_traits::FsRead;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::errors::ClosestPkgJsonError;
|
2024-07-16 18:32:41 -04:00
|
|
|
use crate::errors::PackageJsonLoadError;
|
|
|
|
|
2025-01-27 10:28:33 -05:00
|
|
|
pub trait NodePackageJsonCache:
|
|
|
|
deno_package_json::PackageJsonCache
|
|
|
|
+ std::fmt::Debug
|
|
|
|
+ crate::sync::MaybeSend
|
|
|
|
+ crate::sync::MaybeSync
|
|
|
|
{
|
|
|
|
fn as_deno_package_json_cache(
|
|
|
|
&self,
|
|
|
|
) -> &dyn deno_package_json::PackageJsonCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> NodePackageJsonCache for T
|
|
|
|
where
|
|
|
|
T: deno_package_json::PackageJsonCache
|
|
|
|
+ std::fmt::Debug
|
2025-01-24 09:50:25 -05:00
|
|
|
+ crate::sync::MaybeSend
|
|
|
|
+ crate::sync::MaybeSync,
|
2025-01-27 10:28:33 -05:00
|
|
|
{
|
|
|
|
fn as_deno_package_json_cache(
|
|
|
|
&self,
|
|
|
|
) -> &dyn deno_package_json::PackageJsonCache {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::disallowed_types)]
|
|
|
|
pub type PackageJsonCacheRc = crate::sync::MaybeArc<dyn NodePackageJsonCache>;
|
2025-01-24 09:50:25 -05:00
|
|
|
|
2023-02-06 16:20:20 +01:00
|
|
|
thread_local! {
|
2024-06-26 17:24:10 -04:00
|
|
|
static CACHE: RefCell<HashMap<PathBuf, PackageJsonRc>> = RefCell::new(HashMap::new());
|
2023-02-06 16:20:20 +01:00
|
|
|
}
|
|
|
|
|
2025-01-24 09:50:25 -05:00
|
|
|
#[derive(Debug)]
|
2024-06-26 17:24:10 -04:00
|
|
|
pub struct PackageJsonThreadLocalCache;
|
2023-02-23 10:58:10 -05:00
|
|
|
|
2024-06-26 17:24:10 -04:00
|
|
|
impl PackageJsonThreadLocalCache {
|
|
|
|
pub fn clear() {
|
2025-01-27 15:23:20 -05:00
|
|
|
CACHE.with_borrow_mut(|cache| cache.clear());
|
2023-02-23 10:58:10 -05:00
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 17:34:46 -04:00
|
|
|
impl deno_package_json::PackageJsonCache for PackageJsonThreadLocalCache {
|
2024-06-26 17:24:10 -04:00
|
|
|
fn get(&self, path: &Path) -> Option<PackageJsonRc> {
|
2025-01-27 15:23:20 -05:00
|
|
|
CACHE.with_borrow(|cache| cache.get(path).cloned())
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2024-06-26 17:24:10 -04:00
|
|
|
fn set(&self, path: PathBuf, package_json: PackageJsonRc) {
|
2025-01-27 15:23:20 -05:00
|
|
|
CACHE.with_borrow_mut(|cache| cache.insert(path, package_json));
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
}
|
2023-09-22 11:21:38 +02:00
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
#[allow(clippy::disallowed_types)]
|
2024-12-30 12:38:20 -05:00
|
|
|
pub type PackageJsonResolverRc<TSys> =
|
|
|
|
crate::sync::MaybeArc<PackageJsonResolver<TSys>>;
|
2024-11-01 12:27:00 -04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2024-12-30 12:38:20 -05:00
|
|
|
pub struct PackageJsonResolver<TSys: FsRead> {
|
|
|
|
sys: TSys,
|
2025-01-24 09:50:25 -05:00
|
|
|
loader_cache: Option<PackageJsonCacheRc>,
|
2024-11-01 12:27:00 -04:00
|
|
|
}
|
|
|
|
|
2024-12-30 12:38:20 -05:00
|
|
|
impl<TSys: FsRead> PackageJsonResolver<TSys> {
|
2025-01-24 09:50:25 -05:00
|
|
|
pub fn new(sys: TSys, loader_cache: Option<PackageJsonCacheRc>) -> Self {
|
|
|
|
Self { sys, loader_cache }
|
2024-11-01 12:27:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_closest_package_json(
|
|
|
|
&self,
|
2025-01-27 15:23:20 -05:00
|
|
|
file_path: &Path,
|
2024-11-01 12:27:00 -04:00
|
|
|
) -> Result<Option<PackageJsonRc>, ClosestPkgJsonError> {
|
2025-01-27 15:23:20 -05:00
|
|
|
let Some(parent_dir) = file_path.parent() else {
|
2024-11-01 12:27:00 -04:00
|
|
|
return Ok(None);
|
|
|
|
};
|
2024-12-20 17:35:02 -05:00
|
|
|
for current_dir in parent_dir.ancestors() {
|
2024-11-01 12:27:00 -04:00
|
|
|
let package_json_path = current_dir.join("package.json");
|
|
|
|
if let Some(pkg_json) = self.load_package_json(&package_json_path)? {
|
|
|
|
return Ok(Some(pkg_json));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_package_json(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
) -> Result<Option<PackageJsonRc>, PackageJsonLoadError> {
|
|
|
|
let result = PackageJson::load_from_path(
|
2024-12-30 12:38:20 -05:00
|
|
|
&self.sys,
|
2025-01-24 09:50:25 -05:00
|
|
|
self
|
|
|
|
.loader_cache
|
|
|
|
.as_deref()
|
2025-01-27 10:28:33 -05:00
|
|
|
.map(|cache| cache.as_deno_package_json_cache()),
|
2024-12-30 12:38:20 -05:00
|
|
|
path,
|
2024-11-01 12:27:00 -04:00
|
|
|
);
|
|
|
|
match result {
|
|
|
|
Ok(pkg_json) => Ok(Some(pkg_json)),
|
|
|
|
Err(deno_package_json::PackageJsonLoadError::Io { source, .. })
|
|
|
|
if source.kind() == ErrorKind::NotFound =>
|
|
|
|
{
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
Err(err) => Err(PackageJsonLoadError(err)),
|
|
|
|
}
|
2023-09-22 11:21:38 +02:00
|
|
|
}
|
|
|
|
}
|