use super::lockfile_dir::write_lockfile_dir_remapped;
use super::workspace::write_per_project_lockfiles;
use miette::{Context, IntoDiagnostic};
use std::collections::BTreeSet;
use std::path::PathBuf;
pub(super) struct LockfileWriteInputs {
pub graph: aube_lockfile::LockfileGraph,
pub manifest: aube_manifest::PackageJson,
pub manifests: Vec<(String, aube_manifest::PackageJson)>,
pub lockfile_dir: PathBuf,
pub lockfile_importer_key: String,
pub cwd: PathBuf,
pub write_kind: aube_lockfile::LockfileKind,
pub shared_workspace_lockfile: bool,
pub has_workspace: bool,
pub per_project_write_selection: Option<BTreeSet<String>>,
}
pub(super) type LockfileWriteHandle = tokio::task::JoinHandle<miette::Result<()>>;
pub(super) fn overlap_enabled() -> bool {
aube_util::env::embedder_env("DISABLE_LOCKFILE_WRITE_OVERLAP").is_none()
}
#[allow(clippy::too_many_arguments)]
pub(super) fn write_one(
graph: &aube_lockfile::LockfileGraph,
manifest: &aube_manifest::PackageJson,
manifests: &[(String, aube_manifest::PackageJson)],
lockfile_dir: &std::path::Path,
lockfile_importer_key: &str,
cwd: &std::path::Path,
write_kind: aube_lockfile::LockfileKind,
shared_workspace_lockfile: bool,
has_workspace: bool,
per_project_write_selection: Option<&BTreeSet<String>>,
) -> miette::Result<()> {
if shared_workspace_lockfile || !has_workspace {
let written_path = write_lockfile_dir_remapped(
lockfile_dir,
lockfile_importer_key,
graph,
manifest,
write_kind,
)
.into_diagnostic()
.wrap_err("failed to write lockfile")?;
tracing::debug!(
"Wrote {}",
written_path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| written_path.display().to_string())
);
} else {
write_per_project_lockfiles(
cwd,
graph,
manifests,
write_kind,
per_project_write_selection,
)?;
}
Ok(())
}
pub(super) fn spawn(inputs: LockfileWriteInputs) -> LockfileWriteHandle {
tokio::task::spawn_blocking(move || {
let _diag = aube_util::diag::Span::new(
aube_util::diag::Category::Install,
"lockfile_write_overlapped",
);
write_one(
&inputs.graph,
&inputs.manifest,
&inputs.manifests,
&inputs.lockfile_dir,
&inputs.lockfile_importer_key,
&inputs.cwd,
inputs.write_kind,
inputs.shared_workspace_lockfile,
inputs.has_workspace,
inputs.per_project_write_selection.as_ref(),
)
})
}
pub(super) async fn join(handle: LockfileWriteHandle) -> miette::Result<()> {
match handle.await {
Ok(write_result) => write_result,
Err(join_err) => Result::<(), _>::Err(join_err)
.into_diagnostic()
.wrap_err("lockfile write task panicked"),
}
}