-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |