2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-02-24 19:35:43 -05:00
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
use std::future::Future;
|
2023-02-24 19:35:43 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_core::futures::stream::FuturesOrdered;
|
|
|
|
use deno_core::futures::StreamExt;
|
|
|
|
use deno_npm::registry::NpmRegistryApi;
|
2023-04-12 08:36:11 -04:00
|
|
|
use deno_npm::registry::NpmRegistryPackageInfoLoadError;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageReq;
|
2023-03-03 17:27:05 -05:00
|
|
|
|
2023-05-10 20:06:59 -04:00
|
|
|
use crate::args::PackageJsonDepsProvider;
|
2023-04-11 18:10:51 -04:00
|
|
|
use crate::util::sync::AtomicFlag;
|
2023-02-24 19:35:43 -05:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
use super::CliNpmRegistryApi;
|
2023-02-24 19:35:43 -05:00
|
|
|
use super::NpmResolution;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct PackageJsonDepsInstallerInner {
|
2023-05-10 20:06:59 -04:00
|
|
|
deps_provider: Arc<PackageJsonDepsProvider>,
|
2023-04-11 18:10:51 -04:00
|
|
|
has_installed_flag: AtomicFlag,
|
2023-04-14 16:22:33 -04:00
|
|
|
npm_registry_api: Arc<CliNpmRegistryApi>,
|
|
|
|
npm_resolution: Arc<NpmResolution>,
|
2023-02-24 19:35:43 -05:00
|
|
|
}
|
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
impl PackageJsonDepsInstallerInner {
|
2023-05-22 16:55:04 -04:00
|
|
|
pub fn reqs_with_info_futures<'a>(
|
2023-04-12 08:36:11 -04:00
|
|
|
&self,
|
2023-08-21 05:53:52 -04:00
|
|
|
reqs: &'a [&'a PackageReq],
|
2023-04-12 08:36:11 -04:00
|
|
|
) -> FuturesOrdered<
|
|
|
|
impl Future<
|
|
|
|
Output = Result<
|
2023-08-21 05:53:52 -04:00
|
|
|
(&'a PackageReq, Arc<deno_npm::registry::NpmPackageInfo>),
|
2023-04-12 08:36:11 -04:00
|
|
|
NpmRegistryPackageInfoLoadError,
|
|
|
|
>,
|
|
|
|
>,
|
|
|
|
> {
|
2023-05-22 16:55:04 -04:00
|
|
|
FuturesOrdered::from_iter(reqs.iter().map(|req| {
|
2023-04-12 08:36:11 -04:00
|
|
|
let api = self.npm_registry_api.clone();
|
|
|
|
async move {
|
|
|
|
let info = api.package_info(&req.name).await?;
|
2023-05-22 16:55:04 -04:00
|
|
|
Ok::<_, NpmRegistryPackageInfoLoadError>((*req, info))
|
2023-04-12 08:36:11 -04:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 19:35:43 -05:00
|
|
|
/// Holds and controls installing dependencies from package.json.
|
2023-04-14 16:22:33 -04:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct PackageJsonDepsInstaller(Option<PackageJsonDepsInstallerInner>);
|
2023-02-24 19:35:43 -05:00
|
|
|
|
|
|
|
impl PackageJsonDepsInstaller {
|
|
|
|
pub fn new(
|
2023-05-10 20:06:59 -04:00
|
|
|
deps_provider: Arc<PackageJsonDepsProvider>,
|
2023-04-14 16:22:33 -04:00
|
|
|
npm_registry_api: Arc<CliNpmRegistryApi>,
|
|
|
|
npm_resolution: Arc<NpmResolution>,
|
2023-02-24 19:35:43 -05:00
|
|
|
) -> Self {
|
2023-05-10 20:06:59 -04:00
|
|
|
Self(Some(PackageJsonDepsInstallerInner {
|
|
|
|
deps_provider,
|
2023-04-14 16:22:33 -04:00
|
|
|
has_installed_flag: Default::default(),
|
|
|
|
npm_registry_api,
|
|
|
|
npm_resolution,
|
2023-02-24 19:35:43 -05:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:28:36 -04:00
|
|
|
/// Creates an installer that never installs local packages during
|
|
|
|
/// resolution. A top level install will be a no-op.
|
|
|
|
pub fn no_op() -> Self {
|
|
|
|
Self(None)
|
|
|
|
}
|
|
|
|
|
2023-02-24 19:35:43 -05:00
|
|
|
/// Installs the top level dependencies in the package.json file
|
|
|
|
/// without going through and resolving the descendant dependencies yet.
|
|
|
|
pub async fn ensure_top_level_install(&self) -> Result<(), AnyError> {
|
|
|
|
let inner = match &self.0 {
|
|
|
|
Some(inner) => inner,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
|
|
|
|
2023-04-11 18:10:51 -04:00
|
|
|
if !inner.has_installed_flag.raise() {
|
2023-02-24 19:35:43 -05:00
|
|
|
return Ok(()); // already installed by something else
|
|
|
|
}
|
|
|
|
|
2024-01-22 16:31:12 -05:00
|
|
|
let package_reqs = inner.deps_provider.reqs().unwrap_or_default();
|
2023-05-22 16:55:04 -04:00
|
|
|
|
|
|
|
// check if something needs resolving before bothering to load all
|
|
|
|
// the package information (which is slow)
|
|
|
|
if package_reqs.iter().all(|req| {
|
|
|
|
inner
|
|
|
|
.npm_resolution
|
|
|
|
.resolve_pkg_id_from_pkg_req(req)
|
|
|
|
.is_ok()
|
|
|
|
}) {
|
|
|
|
log::debug!(
|
|
|
|
"All package.json deps resolvable. Skipping top level install."
|
|
|
|
);
|
|
|
|
return Ok(()); // everything is already resolvable
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut reqs_with_info_futures =
|
|
|
|
inner.reqs_with_info_futures(&package_reqs);
|
2023-02-24 19:35:43 -05:00
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
while let Some(result) = reqs_with_info_futures.next().await {
|
2023-04-06 18:46:44 -04:00
|
|
|
let (req, info) = result?;
|
2023-04-12 08:36:11 -04:00
|
|
|
let result = inner
|
2023-02-24 19:35:43 -05:00
|
|
|
.npm_resolution
|
2023-09-28 16:43:45 -04:00
|
|
|
.resolve_pkg_req_as_pending_with_info(req, &info);
|
2023-04-12 08:36:11 -04:00
|
|
|
if let Err(err) = result {
|
|
|
|
if inner.npm_registry_api.mark_force_reload() {
|
|
|
|
log::debug!("Failed to resolve package. Retrying. Error: {err:#}");
|
|
|
|
// re-initialize
|
2023-05-22 16:55:04 -04:00
|
|
|
reqs_with_info_futures = inner.reqs_with_info_futures(&package_reqs);
|
2023-04-12 08:36:11 -04:00
|
|
|
} else {
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 19:35:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|