diff --git a/cli/lockfile.rs b/cli/lockfile.rs index afbd8d2879..067a0adf9d 100644 --- a/cli/lockfile.rs +++ b/cli/lockfile.rs @@ -8,7 +8,6 @@ use deno_core::parking_lot::Mutex; use deno_core::serde::Deserialize; use deno_core::serde::Serialize; use deno_core::serde_json; -use deno_core::serde_json::json; use deno_core::ModuleSpecifier; use log::debug; use std::cell::RefCell; @@ -21,21 +20,12 @@ use std::sync::Arc; use crate::tools::fmt::format_json; #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct LockfileV2Content { +pub struct LockfileContent { version: String, // Mapping between URLs and their checksums remote: BTreeMap, } -#[derive(Debug, Clone, Deserialize)] -pub struct LockfileV1Content(BTreeMap); - -#[derive(Debug, Clone)] -enum LockfileContent { - V1(LockfileV1Content), - V2(LockfileV2Content), -} - #[derive(Debug, Clone)] pub struct Lockfile { write: bool, @@ -47,10 +37,10 @@ impl Lockfile { pub fn new(filename: PathBuf, write: bool) -> Result { // Writing a lock file always uses the new format. let content = if write { - LockfileContent::V2(LockfileV2Content { + LockfileContent { version: "2".to_string(), remote: BTreeMap::new(), - }) + } } else { let s = std::fs::read_to_string(&filename).with_context(|| { format!("Unable to read lockfile: {}", filename.display()) @@ -59,13 +49,18 @@ impl Lockfile { .context("Unable to parse contents of the lockfile")?; let version = value.get("version").and_then(|v| v.as_str()); if version == Some("2") { - let content: LockfileV2Content = - serde_json::from_value(value).context("Unable to parse lockfile")?; - LockfileContent::V2(content) + serde_json::from_value::(value) + .context("Unable to parse lockfile")? } else { - let content: LockfileV1Content = + // If there's no version field, we assume that user is using the old + // version of the lockfile. We'll migrate it in-place into v2 and it + // will be writte in v2 if user uses `--lock-write` flag. + let remote: BTreeMap = serde_json::from_value(value).context("Unable to parse lockfile")?; - LockfileContent::V1(content) + LockfileContent { + version: "2".to_string(), + remote, + } } }; @@ -82,14 +77,7 @@ impl Lockfile { return Ok(()); } - let json_string = match &self.content { - LockfileContent::V1(c) => { - let j = json!(&c.0); - serde_json::to_string(&j).unwrap() - } - LockfileContent::V2(c) => serde_json::to_string(&c).unwrap(), - }; - + let json_string = serde_json::to_string(&self.content).unwrap(); let format_s = format_json(&json_string, &Default::default()) .ok() .flatten() @@ -120,23 +108,11 @@ impl Lockfile { if specifier.starts_with("file:") { return true; } - match &self.content { - LockfileContent::V1(c) => { - if let Some(lockfile_checksum) = c.0.get(specifier) { - let compiled_checksum = crate::checksum::gen(&[code.as_bytes()]); - lockfile_checksum == &compiled_checksum - } else { - false - } - } - LockfileContent::V2(c) => { - if let Some(lockfile_checksum) = c.remote.get(specifier) { - let compiled_checksum = crate::checksum::gen(&[code.as_bytes()]); - lockfile_checksum == &compiled_checksum - } else { - false - } - } + if let Some(lockfile_checksum) = self.content.remote.get(specifier) { + let compiled_checksum = crate::checksum::gen(&[code.as_bytes()]); + lockfile_checksum == &compiled_checksum + } else { + false } } @@ -145,14 +121,7 @@ impl Lockfile { return; } let checksum = crate::checksum::gen(&[code.as_bytes()]); - match &mut self.content { - LockfileContent::V1(c) => { - c.0.insert(specifier.to_string(), checksum); - } - LockfileContent::V2(c) => { - c.remote.insert(specifier.to_string(), checksum); - } - }; + self.content.remote.insert(specifier.to_string(), checksum); } } @@ -233,10 +202,7 @@ mod tests { let result = Lockfile::new(file_path, false).unwrap(); - let remote = match result.content { - LockfileContent::V2(c) => c.remote, - _ => unreachable!(), - }; + let remote = result.content.remote; let keys: Vec = remote.keys().cloned().collect(); let expected_keys = vec![ String::from("https://deno.land/std@0.71.0/async/delay.ts"), @@ -259,10 +225,7 @@ mod tests { "Here is some source code", ); - let remote = match lockfile.content { - LockfileContent::V2(c) => c.remote, - _ => unreachable!(), - }; + let remote = lockfile.content.remote; let keys: Vec = remote.keys().cloned().collect(); let expected_keys = vec![ String::from("https://deno.land/std@0.71.0/async/delay.ts"),