2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-10-18 17:09:31 -04:00
|
|
|
use deno_core::error::AnyError;
|
2023-01-23 17:41:02 -05:00
|
|
|
use std::path::PathBuf;
|
2019-11-03 10:39:27 -05:00
|
|
|
|
2022-12-07 18:13:45 -05:00
|
|
|
use crate::args::config_file::LockConfig;
|
2022-11-02 11:32:30 -04:00
|
|
|
use crate::args::ConfigFile;
|
2022-10-25 12:20:07 -04:00
|
|
|
use crate::npm::NpmResolutionPackage;
|
2022-11-02 11:32:30 -04:00
|
|
|
use crate::Flags;
|
2021-12-07 19:21:04 -05:00
|
|
|
|
2022-12-20 12:00:57 -05:00
|
|
|
use super::DenoSubcommand;
|
|
|
|
|
2023-01-23 17:41:02 -05:00
|
|
|
pub use deno_lockfile::Lockfile;
|
|
|
|
pub use deno_lockfile::LockfileError;
|
|
|
|
use deno_lockfile::NpmPackageDependencyLockfileInfo;
|
|
|
|
use deno_lockfile::NpmPackageLockfileInfo;
|
|
|
|
|
|
|
|
pub fn discover(
|
|
|
|
flags: &Flags,
|
|
|
|
maybe_config_file: Option<&ConfigFile>,
|
|
|
|
) -> Result<Option<Lockfile>, AnyError> {
|
|
|
|
if flags.no_lock
|
|
|
|
|| matches!(
|
|
|
|
flags.subcommand,
|
|
|
|
DenoSubcommand::Install(_) | DenoSubcommand::Uninstall(_)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let filename = match flags.lock {
|
|
|
|
Some(ref lock) => PathBuf::from(lock),
|
|
|
|
None => match maybe_config_file {
|
|
|
|
Some(config_file) => {
|
|
|
|
if config_file.specifier.scheme() == "file" {
|
|
|
|
match config_file.to_lock_config()? {
|
|
|
|
Some(LockConfig::Bool(lock)) if !lock => {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
Some(LockConfig::PathBuf(lock)) => config_file
|
|
|
|
.specifier
|
|
|
|
.to_file_path()
|
|
|
|
.unwrap()
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join(lock),
|
|
|
|
_ => {
|
|
|
|
let mut path = config_file.specifier.to_file_path().unwrap();
|
|
|
|
path.set_file_name("deno.lock");
|
|
|
|
path
|
2022-12-07 18:13:45 -05:00
|
|
|
}
|
2022-11-02 11:32:30 -04:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-23 17:41:02 -05:00
|
|
|
return Ok(None);
|
2022-11-02 11:32:30 -04:00
|
|
|
}
|
|
|
|
}
|
2023-01-23 17:41:02 -05:00
|
|
|
None => return Ok(None),
|
|
|
|
},
|
|
|
|
};
|
2022-10-25 12:20:07 -04:00
|
|
|
|
2023-01-23 17:41:02 -05:00
|
|
|
let lockfile = Lockfile::new(filename, flags.lock_write)?;
|
|
|
|
Ok(Some(lockfile))
|
2019-11-03 10:39:27 -05:00
|
|
|
}
|
2020-10-04 19:32:18 -04:00
|
|
|
|
2023-01-23 17:41:02 -05:00
|
|
|
// NOTE(bartlomieju): we don't want a reverse mapping to be possible.
|
|
|
|
#[allow(clippy::from_over_into)]
|
|
|
|
impl Into<NpmPackageLockfileInfo> for NpmResolutionPackage {
|
|
|
|
fn into(self) -> NpmPackageLockfileInfo {
|
|
|
|
let dependencies = self
|
|
|
|
.dependencies
|
|
|
|
.into_iter()
|
|
|
|
.map(|(name, id)| NpmPackageDependencyLockfileInfo {
|
|
|
|
name,
|
|
|
|
id: id.as_serialized(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
NpmPackageLockfileInfo {
|
2023-02-21 12:03:48 -05:00
|
|
|
display_id: self.pkg_id.nv.to_string(),
|
|
|
|
serialized_id: self.pkg_id.as_serialized(),
|
2023-01-23 17:41:02 -05:00
|
|
|
integrity: self.dist.integrity().to_string(),
|
|
|
|
dependencies,
|
|
|
|
}
|
2020-10-04 19:32:18 -04:00
|
|
|
}
|
|
|
|
}
|