Skip to content

Commit

Permalink
feat: add lockfile resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
nokazn committed Jan 27, 2024
1 parent 54655b3 commit 56d54b1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ path-clean = "1.0.1"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
sha2 = "0.10.8"
strum = "0.25.0"
strum_macros = "0.25.3"
62 changes: 60 additions & 2 deletions src/lockfile.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
pub enum Lockfile {
use base64ct::{Base64, Encoding};
use sha2::{Digest, Sha256};
use std::{
fs, io,
path::{Path, PathBuf},
};
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

use crate::utils::path::to_absolute_path;

#[derive(EnumIter, Debug)]
enum LockfileKind {
PackageLock,
YarnLock,
PnpmLock,
BunLockb,
}

impl LockfileKind {
pub fn file_names(&self) -> Vec<&str> {
match self {
LockfileKind::PackageLock => vec!["package-lock.json"],
LockfileKind::YarnLock => vec!["yarn.lock"],
LockfileKind::PnpmLock => vec!["pnpm-lock.yaml", "pnpm-lock.yml"],
LockfileKind::BunLockb => vec!["bun.lockb"],
}
}
}

#[derive(Debug)]
pub struct Lockfile {
kind: LockfileKind,
path: PathBuf,
}

impl Lockfile {
pub fn new() {}
pub fn new<T: AsRef<Path>>(dir_path: T) -> Result<Self, String> {
match Lockfile::try_to_read_lockfile(&dir_path) {
Some((kind, path)) => Ok(Lockfile { kind, path }),
None => Err(format!(
"No lockfile at `{}`",
to_absolute_path(&dir_path).to_string_lossy()
)),
}
}

fn try_to_read_lockfile<T: AsRef<Path>>(dir_path: T) -> Option<(LockfileKind, PathBuf)> {
for kind in LockfileKind::iter() {
for lockfile in kind.file_names() {
let file_path = dir_path.as_ref().join(lockfile);
if file_path.exists() {
return Some((kind, file_path));
}
}
}
None
}

pub fn generate_hash(&self) -> Result<String, Option<io::Error>> {
let mut file = fs::File::open(&self.path)?;
let mut hasher = Sha256::new();
io::copy(&mut file, &mut hasher)?;
let raw_hash = hasher.finalize();
let hash = Base64::encode_string(&raw_hash);
Ok(hash)
}
}
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
mod lockfile;
mod package_json;
mod utils;

use crate::utils::hash::Hashable;
use lockfile::Lockfile;
use package_json::PackageJson;

fn main() {
// TODO: 後で消す
let result = PackageJson::new("./examples/package.json").map(|p| p.resolve().generate_hash());
dbg!(result.unwrap());
let package_json = PackageJson::new("./examples/package.json").unwrap();
let result = package_json.resolve().generate_hash();
dbg!(&result.unwrap());
let lockfile = Lockfile::new("./examples/").unwrap();
let result = lockfile.generate_hash();
dbg!(lockfile, &result.unwrap());
}

0 comments on commit 56d54b1

Please sign in to comment.