mirror of
https://github.com/denoland/deno.git
synced 2025-02-02 04:38:21 -05:00
refactor: better errors in lockfile and preparation for new version (#16344)
A small cleanup that improves errors in the lockfile as well as prepares for adding a new format of the lock file that will allow to provide backward compatibility with existing format (ie. "Lockfile::content" will be changed into an enum "LockfileContent" that will have "V1" and "V2" variants).
This commit is contained in:
parent
e4940135e6
commit
0750b326be
1 changed files with 22 additions and 13 deletions
|
@ -1,5 +1,7 @@
|
||||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
use deno_core::anyhow::Context;
|
||||||
|
use deno_core::error::AnyError;
|
||||||
use deno_core::parking_lot::Mutex;
|
use deno_core::parking_lot::Mutex;
|
||||||
use deno_core::serde_json;
|
use deno_core::serde_json;
|
||||||
use deno_core::serde_json::json;
|
use deno_core::serde_json::json;
|
||||||
|
@ -7,42 +9,50 @@ use deno_core::ModuleSpecifier;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::io::Result;
|
use std::io::Write;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::tools::fmt::format_json;
|
use crate::tools::fmt::format_json;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct LockfileContent {
|
||||||
|
map: BTreeMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Lockfile {
|
pub struct Lockfile {
|
||||||
write: bool,
|
write: bool,
|
||||||
map: BTreeMap<String, String>,
|
content: LockfileContent,
|
||||||
pub filename: PathBuf,
|
pub filename: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Lockfile {
|
impl Lockfile {
|
||||||
pub fn new(filename: PathBuf, write: bool) -> Result<Lockfile> {
|
pub fn new(filename: PathBuf, write: bool) -> Result<Lockfile, AnyError> {
|
||||||
let map = if write {
|
let map = if write {
|
||||||
BTreeMap::new()
|
BTreeMap::new()
|
||||||
} else {
|
} else {
|
||||||
let s = std::fs::read_to_string(&filename)?;
|
let s = std::fs::read_to_string(&filename).with_context(|| {
|
||||||
serde_json::from_str(&s)?
|
format!("Unable to read lockfile: {}", filename.display())
|
||||||
|
})?;
|
||||||
|
serde_json::from_str(&s)
|
||||||
|
.context("Unable to parse contents of the lockfile")?
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Lockfile {
|
Ok(Lockfile {
|
||||||
write,
|
write,
|
||||||
map,
|
content: LockfileContent { map },
|
||||||
filename,
|
filename,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Synchronize lock file to disk - noop if --lock-write file is not specified.
|
// Synchronize lock file to disk - noop if --lock-write file is not specified.
|
||||||
pub fn write(&self) -> Result<()> {
|
pub fn write(&self) -> Result<(), AnyError> {
|
||||||
if !self.write {
|
if !self.write {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let j = json!(&self.map);
|
let j = json!(&self.content.map);
|
||||||
let s = serde_json::to_string_pretty(&j).unwrap();
|
let s = serde_json::to_string_pretty(&j).unwrap();
|
||||||
|
|
||||||
let format_s = format_json(&s, &Default::default())
|
let format_s = format_json(&s, &Default::default())
|
||||||
|
@ -54,7 +64,6 @@ impl Lockfile {
|
||||||
.create(true)
|
.create(true)
|
||||||
.truncate(true)
|
.truncate(true)
|
||||||
.open(&self.filename)?;
|
.open(&self.filename)?;
|
||||||
use std::io::Write;
|
|
||||||
f.write_all(format_s.as_bytes())?;
|
f.write_all(format_s.as_bytes())?;
|
||||||
debug!("lockfile write {}", self.filename.display());
|
debug!("lockfile write {}", self.filename.display());
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -76,7 +85,7 @@ impl Lockfile {
|
||||||
if specifier.starts_with("file:") {
|
if specifier.starts_with("file:") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if let Some(lockfile_checksum) = self.map.get(specifier) {
|
if let Some(lockfile_checksum) = self.content.map.get(specifier) {
|
||||||
let compiled_checksum = crate::checksum::gen(&[code.as_bytes()]);
|
let compiled_checksum = crate::checksum::gen(&[code.as_bytes()]);
|
||||||
lockfile_checksum == &compiled_checksum
|
lockfile_checksum == &compiled_checksum
|
||||||
} else {
|
} else {
|
||||||
|
@ -89,7 +98,7 @@ impl Lockfile {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let checksum = crate::checksum::gen(&[code.as_bytes()]);
|
let checksum = crate::checksum::gen(&[code.as_bytes()]);
|
||||||
self.map.insert(specifier.to_string(), checksum);
|
self.content.map.insert(specifier.to_string(), checksum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,7 +176,7 @@ mod tests {
|
||||||
|
|
||||||
let result = Lockfile::new(file_path, false).unwrap();
|
let result = Lockfile::new(file_path, false).unwrap();
|
||||||
|
|
||||||
let keys: Vec<String> = result.map.keys().cloned().collect();
|
let keys: Vec<String> = result.content.map.keys().cloned().collect();
|
||||||
let expected_keys = vec![
|
let expected_keys = vec![
|
||||||
String::from("https://deno.land/std@0.71.0/async/delay.ts"),
|
String::from("https://deno.land/std@0.71.0/async/delay.ts"),
|
||||||
String::from("https://deno.land/std@0.71.0/textproto/mod.ts"),
|
String::from("https://deno.land/std@0.71.0/textproto/mod.ts"),
|
||||||
|
@ -189,7 +198,7 @@ mod tests {
|
||||||
"Here is some source code",
|
"Here is some source code",
|
||||||
);
|
);
|
||||||
|
|
||||||
let keys: Vec<String> = lockfile.map.keys().cloned().collect();
|
let keys: Vec<String> = lockfile.content.map.keys().cloned().collect();
|
||||||
let expected_keys = vec![
|
let expected_keys = vec![
|
||||||
String::from("https://deno.land/std@0.71.0/async/delay.ts"),
|
String::from("https://deno.land/std@0.71.0/async/delay.ts"),
|
||||||
String::from("https://deno.land/std@0.71.0/io/util.ts"),
|
String::from("https://deno.land/std@0.71.0/io/util.ts"),
|
||||||
|
|
Loading…
Add table
Reference in a new issue