Skip to content

Commit

Permalink
feat: add redirects to the lockfile (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Aug 24, 2023
1 parent 4e6a66b commit 7b9799f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:

jobs:
rust:
name: deno_semver-ubuntu-latest-release
name: ubuntu-latest-release
runs-on: ubuntu-latest
timeout-minutes: 30

Expand All @@ -29,7 +29,9 @@ jobs:
- uses: dsherret/rust-toolchain-file@v1

- name: Cache
uses: Swatinem/rust-cache@v1
uses: Swatinem/rust-cache@v2
with:
save-if: ${{ github.ref == 'refs/heads/main' }}

- name: Format
run: cargo fmt --all -- --check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
token: ${{ secrets.DENOBOT_PAT }}

- uses: denoland/setup-deno@v1
- uses: dtolnay/rust-toolchain@stable
- uses: dsherret/rust-toolchain-file@v1

- name: Tag and release
env:
Expand Down
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"
54 changes: 48 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 Down Expand Up @@ -89,14 +89,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 +181,7 @@ impl Lockfile {
version: "2".to_string(),
remote,
npm: NpmContent::default(),
redirects: Default::default(),
}
};

Expand All @@ -188,20 +193,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 @@ -321,6 +330,7 @@ Use \"--lock-write\" flag to regenerate the lockfile at \"{}\".",
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use std::fs::File;
use std::io::prelude::*;
use std::io::Write;
Expand Down Expand Up @@ -557,4 +567,36 @@ 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 7b9799f

Please sign in to comment.