Skip to content

Commit

Permalink
feat: add redirects to the lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Aug 24, 2023
1 parent 4e6a66b commit ae51406
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ serde_json = "1.0.85"
thiserror = "1.0.40"

[dev-dependencies]
pretty_assertions = "1.4.0"
temp-dir = "0.1.11"
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.68.2"
channel = "1.71.1"
components = ["clippy", "rustfmt"]
profile = "minimal"
51 changes: 45 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub struct NpmPackageInfo {
pub struct NpmContent {
/// Mapping between requests for npm packages and resolved packages, eg.
/// {
/// "chalk": "[email protected]"
/// "react@17": "[email protected]"
/// "chalk": "[email protected]",
/// "react@17": "[email protected]",
/// "foo@latest": "[email protected]"
/// }
pub specifiers: BTreeMap<String, String>,
Expand All @@ -81,6 +81,7 @@ impl NpmContent {
}
}


#[derive(Debug, Clone, Serialize, Deserialize, Hash)]
pub struct LockfileContent {
version: String,
Expand All @@ -89,14 +90,18 @@ pub struct LockfileContent {
#[serde(skip_serializing_if = "NpmContent::is_empty")]
#[serde(default)]
pub npm: NpmContent,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
#[serde(default)]
pub redirects: BTreeMap<String, String>,
}

impl LockfileContent {
fn empty() -> Self {
Self {
version: "2".to_string(),
remote: BTreeMap::new(),
npm: NpmContent::default(),
npm: Default::default(),
redirects: Default::default(),
}
}
}
Expand Down Expand Up @@ -177,6 +182,7 @@ impl Lockfile {
version: "2".to_string(),
remote,
npm: NpmContent::default(),
redirects: Default::default(),
}
};

Expand All @@ -188,20 +194,24 @@ impl Lockfile {
})
}

pub fn as_json_string(&self) -> String {
let mut json_string = serde_json::to_string_pretty(&self.content).unwrap();
json_string.push('\n'); // trailing newline in file
json_string
}

// Synchronize lock file to disk - noop if --lock-write file is not specified.
pub fn write(&self) -> Result<(), Error> {
if !self.has_content_changed && !self.overwrite {
return Ok(());
}

let mut json_string = serde_json::to_string_pretty(&self.content).unwrap();
json_string.push('\n'); // trailing newline in file
let mut f = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&self.filename)?;
f.write_all(json_string.as_bytes())?;
f.write_all(self.as_json_string().as_bytes())?;
Ok(())
}

Expand Down Expand Up @@ -325,6 +335,7 @@ mod tests {
use std::io::prelude::*;
use std::io::Write;
use temp_dir::TempDir;
use pretty_assertions::assert_eq;

const LOCKFILE_JSON: &str = r#"
{
Expand Down Expand Up @@ -557,4 +568,32 @@ mod tests {
let check_err = lockfile.check_or_insert_npm_package(npm_package);
assert!(check_err.is_err());
}

#[test]
fn lockfile_with_redirects() {
let mut lockfile = Lockfile::with_lockfile_content(
PathBuf::from("/foo/deno.lock"),
r#"{
"version": "2",
"remote": {},
"redirects": {
"https://deno.land/x/std/mod.ts": "https://deno.land/[email protected]/mod.ts"
}
}"#,
false,
).unwrap();
lockfile.content.redirects.insert("https://deno.land/x/other/mod.ts".to_string(), "https://deno.land/x/[email protected]/mod.ts".to_string());
assert_eq!(
lockfile.as_json_string(),
r#"{
"version": "2",
"remote": {},
"redirects": {
"https://deno.land/x/other/mod.ts": "https://deno.land/x/[email protected]/mod.ts",
"https://deno.land/x/std/mod.ts": "https://deno.land/[email protected]/mod.ts"
}
}
"#,
);
}
}

0 comments on commit ae51406

Please sign in to comment.