2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-02-20 13:14:06 -05:00
|
|
|
|
2024-09-04 10:00:44 -04:00
|
|
|
use std::collections::HashSet;
|
2023-02-23 10:58:10 -05:00
|
|
|
use std::path::PathBuf;
|
2024-06-26 17:24:10 -04:00
|
|
|
use std::sync::Arc;
|
2023-02-20 13:14:06 -05:00
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_config::workspace::Workspace;
|
2024-09-04 10:00:44 -04:00
|
|
|
use deno_core::serde_json;
|
2024-07-23 17:34:46 -04:00
|
|
|
use deno_package_json::PackageJsonDepValue;
|
2024-09-04 10:00:44 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageReq;
|
2023-03-03 17:27:05 -05:00
|
|
|
|
2024-09-04 10:00:44 -04:00
|
|
|
use crate::util::path::is_banned_path_char;
|
|
|
|
|
2024-07-15 15:08:51 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InstallNpmRemotePkg {
|
|
|
|
pub alias: String,
|
|
|
|
pub base_dir: PathBuf,
|
|
|
|
pub req: PackageReq,
|
|
|
|
}
|
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InstallNpmWorkspacePkg {
|
|
|
|
pub alias: String,
|
2024-07-15 15:08:51 -04:00
|
|
|
pub target_dir: PathBuf,
|
2023-05-10 20:06:59 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
#[derive(Debug, Default)]
|
2024-09-04 10:00:44 -04:00
|
|
|
pub struct NpmInstallDepsProvider {
|
2024-07-15 15:08:51 -04:00
|
|
|
remote_pkgs: Vec<InstallNpmRemotePkg>,
|
2024-07-03 20:54:33 -04:00
|
|
|
workspace_pkgs: Vec<InstallNpmWorkspacePkg>,
|
|
|
|
}
|
2023-02-23 10:58:10 -05:00
|
|
|
|
2024-09-04 10:00:44 -04:00
|
|
|
impl NpmInstallDepsProvider {
|
2024-07-03 20:54:33 -04:00
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
2023-02-23 10:58:10 -05:00
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
pub fn from_workspace(workspace: &Arc<Workspace>) -> Self {
|
2024-09-04 10:00:44 -04:00
|
|
|
// todo(dsherret): estimate capacity?
|
2024-07-03 20:54:33 -04:00
|
|
|
let mut workspace_pkgs = Vec::new();
|
2024-07-15 15:08:51 -04:00
|
|
|
let mut remote_pkgs = Vec::new();
|
2024-07-03 20:54:33 -04:00
|
|
|
let workspace_npm_pkgs = workspace.npm_packages();
|
2024-09-04 10:00:44 -04:00
|
|
|
|
|
|
|
for (_, folder) in workspace.config_folders() {
|
|
|
|
let mut deno_json_aliases = HashSet::new();
|
|
|
|
|
|
|
|
// deal with the deno.json first because it takes precedence during resolution
|
|
|
|
if let Some(deno_json) = &folder.deno_json {
|
|
|
|
// don't bother with externally referenced import maps as users
|
|
|
|
// should inline their import map to get this behaviour
|
|
|
|
if let Some(serde_json::Value::Object(obj)) = &deno_json.json.imports {
|
|
|
|
deno_json_aliases.reserve(obj.len());
|
|
|
|
let mut pkg_pkgs = Vec::with_capacity(obj.len());
|
|
|
|
for (alias, value) in obj {
|
|
|
|
let serde_json::Value::String(specifier) = value else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let Ok(npm_req_ref) = NpmPackageReqReference::from_str(specifier)
|
|
|
|
else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
// skip any aliases with banned characters
|
|
|
|
if alias.chars().any(|c| c == '\\' || is_banned_path_char(c)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
deno_json_aliases.insert(alias.to_lowercase());
|
|
|
|
let pkg_req = npm_req_ref.into_inner().req;
|
|
|
|
let workspace_pkg = workspace_npm_pkgs
|
|
|
|
.iter()
|
|
|
|
.find(|pkg| pkg.matches_req(&pkg_req));
|
2024-07-15 15:08:51 -04:00
|
|
|
|
|
|
|
if let Some(pkg) = workspace_pkg {
|
2024-07-03 20:54:33 -04:00
|
|
|
workspace_pkgs.push(InstallNpmWorkspacePkg {
|
2024-09-04 10:00:44 -04:00
|
|
|
alias: alias.to_string(),
|
2024-07-15 15:08:51 -04:00
|
|
|
target_dir: pkg.pkg_json.dir_path().to_path_buf(),
|
2024-07-03 20:54:33 -04:00
|
|
|
});
|
|
|
|
} else {
|
2024-07-15 15:08:51 -04:00
|
|
|
pkg_pkgs.push(InstallNpmRemotePkg {
|
2024-09-04 10:00:44 -04:00
|
|
|
alias: alias.to_string(),
|
|
|
|
base_dir: deno_json.dir_path(),
|
2024-07-15 15:08:51 -04:00
|
|
|
req: pkg_req,
|
|
|
|
});
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
|
|
|
}
|
2024-09-04 10:00:44 -04:00
|
|
|
|
|
|
|
// sort within each package (more like npm resolution)
|
|
|
|
pkg_pkgs.sort_by(|a, b| a.alias.cmp(&b.alias));
|
|
|
|
remote_pkgs.extend(pkg_pkgs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(pkg_json) = &folder.pkg_json {
|
|
|
|
let deps = pkg_json.resolve_local_package_json_deps();
|
|
|
|
let mut pkg_pkgs = Vec::with_capacity(deps.len());
|
|
|
|
for (alias, dep) in deps {
|
|
|
|
let Ok(dep) = dep else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if deno_json_aliases.contains(&alias.to_lowercase()) {
|
|
|
|
// aliases in deno.json take precedence over package.json, so
|
|
|
|
// since this can't be resolved don't bother installing it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
match dep {
|
|
|
|
PackageJsonDepValue::Req(pkg_req) => {
|
|
|
|
let workspace_pkg = workspace_npm_pkgs.iter().find(|pkg| {
|
|
|
|
pkg.matches_req(&pkg_req)
|
|
|
|
// do not resolve to the current package
|
|
|
|
&& pkg.pkg_json.path != pkg_json.path
|
2024-07-03 20:54:33 -04:00
|
|
|
});
|
2024-09-04 10:00:44 -04:00
|
|
|
|
|
|
|
if let Some(pkg) = workspace_pkg {
|
|
|
|
workspace_pkgs.push(InstallNpmWorkspacePkg {
|
|
|
|
alias,
|
|
|
|
target_dir: pkg.pkg_json.dir_path().to_path_buf(),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
pkg_pkgs.push(InstallNpmRemotePkg {
|
|
|
|
alias,
|
|
|
|
base_dir: pkg_json.dir_path().to_path_buf(),
|
|
|
|
req: pkg_req,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PackageJsonDepValue::Workspace(version_req) => {
|
|
|
|
if let Some(pkg) = workspace_npm_pkgs.iter().find(|pkg| {
|
|
|
|
pkg.matches_name_and_version_req(&alias, &version_req)
|
|
|
|
}) {
|
|
|
|
workspace_pkgs.push(InstallNpmWorkspacePkg {
|
|
|
|
alias,
|
|
|
|
target_dir: pkg.pkg_json.dir_path().to_path_buf(),
|
|
|
|
});
|
|
|
|
}
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
2023-02-23 10:58:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 10:00:44 -04:00
|
|
|
// sort within each package as npm does
|
|
|
|
pkg_pkgs.sort_by(|a, b| a.alias.cmp(&b.alias));
|
|
|
|
remote_pkgs.extend(pkg_pkgs);
|
|
|
|
}
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
2024-09-04 10:00:44 -04:00
|
|
|
|
2024-07-15 15:08:51 -04:00
|
|
|
remote_pkgs.shrink_to_fit();
|
2024-07-03 20:54:33 -04:00
|
|
|
workspace_pkgs.shrink_to_fit();
|
|
|
|
Self {
|
2024-07-15 15:08:51 -04:00
|
|
|
remote_pkgs,
|
2024-07-03 20:54:33 -04:00
|
|
|
workspace_pkgs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-15 15:08:51 -04:00
|
|
|
pub fn remote_pkgs(&self) -> &Vec<InstallNpmRemotePkg> {
|
|
|
|
&self.remote_pkgs
|
2023-02-23 10:58:10 -05:00
|
|
|
}
|
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
pub fn workspace_pkgs(&self) -> &Vec<InstallNpmWorkspacePkg> {
|
|
|
|
&self.workspace_pkgs
|
|
|
|
}
|
2023-02-23 10:58:10 -05:00
|
|
|
}
|