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
|
|
|
|
|
|
|
use crate::args::package_json::PackageJsonDeps;
|
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-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-03-03 17:27:05 -05:00
|
|
|
package_deps: PackageJsonDeps,
|
2023-02-24 19:35:43 -05:00
|
|
|
}
|
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
impl PackageJsonDepsInstallerInner {
|
|
|
|
pub fn reqs(&self) -> Vec<&NpmPackageReq> {
|
|
|
|
let mut package_reqs = self
|
|
|
|
.package_deps
|
|
|
|
.values()
|
|
|
|
.filter_map(|r| r.as_ref().ok())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
package_reqs.sort(); // deterministic resolution
|
|
|
|
package_reqs
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reqs_with_info_futures(
|
|
|
|
&self,
|
|
|
|
) -> FuturesOrdered<
|
|
|
|
impl Future<
|
|
|
|
Output = Result<
|
|
|
|
(&NpmPackageReq, Arc<deno_npm::registry::NpmPackageInfo>),
|
|
|
|
NpmRegistryPackageInfoLoadError,
|
|
|
|
>,
|
|
|
|
>,
|
|
|
|
> {
|
|
|
|
let package_reqs = self.reqs();
|
|
|
|
|
|
|
|
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-04-14 16:22:33 -04:00
|
|
|
npm_registry_api: Arc<CliNpmRegistryApi>,
|
|
|
|
npm_resolution: Arc<NpmResolution>,
|
2023-03-03 17:27:05 -05:00
|
|
|
deps: Option<PackageJsonDeps>,
|
2023-02-24 19:35:43 -05:00
|
|
|
) -> Self {
|
2023-04-14 16:22:33 -04:00
|
|
|
Self(deps.map(|package_deps| PackageJsonDepsInstallerInner {
|
|
|
|
has_installed_flag: Default::default(),
|
|
|
|
npm_registry_api,
|
|
|
|
npm_resolution,
|
|
|
|
package_deps,
|
2023-02-24 19:35:43 -05:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-03-03 17:27:05 -05:00
|
|
|
pub fn package_deps(&self) -> Option<&PackageJsonDeps> {
|
2023-02-24 19:35:43 -05:00
|
|
|
self.0.as_ref().map(|inner| &inner.package_deps)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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(())
|
|
|
|
}
|
|
|
|
}
|