2023-02-24 19:35:43 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
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;
|
|
|
|
use deno_semver::npm::NpmPackageReq;
|
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-04-12 08:36:11 -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 {
|
|
|
|
pub fn reqs_with_info_futures(
|
|
|
|
&self,
|
|
|
|
) -> FuturesOrdered<
|
|
|
|
impl Future<
|
|
|
|
Output = Result<
|
|
|
|
(&NpmPackageReq, Arc<deno_npm::registry::NpmPackageInfo>),
|
|
|
|
NpmRegistryPackageInfoLoadError,
|
|
|
|
>,
|
|
|
|
>,
|
|
|
|
> {
|
2023-05-10 20:06:59 -04:00
|
|
|
let package_reqs = self.deps_provider.reqs();
|
2023-04-12 08:36:11 -04:00
|
|
|
|
|
|
|
FuturesOrdered::from_iter(package_reqs.into_iter().map(|req| {
|
|
|
|
let api = self.npm_registry_api.clone();
|
|
|
|
async move {
|
|
|
|
let info = api.package_info(&req.name).await?;
|
|
|
|
Ok::<_, NpmRegistryPackageInfoLoadError>((req, info))
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
let mut reqs_with_info_futures = inner.reqs_with_info_futures();
|
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-04-12 08:36:11 -04:00
|
|
|
.resolve_package_req_as_pending_with_info(req, &info);
|
|
|
|
if let Err(err) = result {
|
|
|
|
if inner.npm_registry_api.mark_force_reload() {
|
|
|
|
log::debug!("Failed to resolve package. Retrying. Error: {err:#}");
|
|
|
|
// re-initialize
|
|
|
|
reqs_with_info_futures = inner.reqs_with_info_futures();
|
|
|
|
} else {
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 19:35:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|