Skip to content

Commit

Permalink
yarn bugfix
Browse files Browse the repository at this point in the history
.
├── 1.22.0
│   └── yarn-v1.22.0
├── 1.22.1
│   └── package
├── 1.22.10
│   └── yarn-v1.22.10
├── 1.22.21
│   └── package
├── 1.22.22
│   └── package
├── 2.4.1
│   └── package
├── 2.4.2
│   └── package
├── 4.0.0
│   └── package
  • Loading branch information
ityuany committed Jul 2, 2024
1 parent ce0aed7 commit 01675ed
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions crates/snm_tarball/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use flate2::read::GzDecoder;
use snm_utils::snm_error::SnmError;
use std::{fs::File, path::PathBuf};
use std::{
fs::File,
path::{Path, PathBuf},
};
use tar::Archive;
use xz2::read::XzDecoder;

Expand All @@ -20,9 +23,44 @@ pub fn decompress(

match tarball_type {
TarballType::Tgz => {
let new_dir_name = "package";
let decoder = GzDecoder::new(input_file);
let mut archive = Archive::new(decoder);
archive.unpack(output_path)?;

// yarn decompress dir different versions are different.
// .
// ├── 1.22.0
// │ └── yarn-v1.22.0 👈
// ├── 1.22.1
// │ └── package
// ├── 1.22.10
// │ └── yarn-v1.22.10 👈
// ├── 1.22.21
// │ └── package
// ├── 1.22.22
// │ └── package
// ├── 2.4.1
// │ └── package
// ├── 2.4.2
// │ └── package
// ├── 4.0.0
// │ └── package

for entry in archive.entries()? {
let mut entry = entry?;
let path = entry.path()?.to_owned();
let new_path = if let Some(first_component) = path.components().next() {
let stripped = path.strip_prefix(first_component.as_os_str()).unwrap();
Path::new(new_dir_name).join(stripped)
} else {
path.to_path_buf()
};
let final_path = Path::new(&output_path).join(new_path);
if let Some(parent) = final_path.parent() {
std::fs::create_dir_all(parent)?;
}
entry.unpack(final_path)?;
}
}
TarballType::Xz => {
let decoder = XzDecoder::new(input_file);
Expand Down

0 comments on commit 01675ed

Please sign in to comment.