Skip to content

Commit

Permalink
fix: revert "fix: unbundle command to better download function source…
Browse files Browse the repository at this point in the history
… code (#314)" (#318)

This reverts commit 3a904d8.
  • Loading branch information
andreespirela authored Apr 15, 2024
1 parent 892ce19 commit e4f5b6e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 94 deletions.
1 change: 0 additions & 1 deletion crates/base/test_cases/npm/constant/some-other-file.ts

This file was deleted.

2 changes: 0 additions & 2 deletions crates/base/test_cases/npm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import isEven from "npm:is-even";
import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { hello } from "./hello.js";
import { numbers } from "./folder1/folder2/numbers.ts"
import {SomeOtherFile} from "./constant/some-other-file.ts";
console.log('SomeOtherFile', SomeOtherFile);

serve(async (req: Request) => {
return new Response(
Expand Down
3 changes: 0 additions & 3 deletions crates/base/test_cases/relative-path/index.ts

This file was deleted.

13 changes: 0 additions & 13 deletions crates/base/test_cases/shared/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion crates/base/test_cases/shared/some-other-folder/hello.ts

This file was deleted.

34 changes: 0 additions & 34 deletions crates/sb_core/util/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,6 @@ pub fn root_url_to_safe_local_dirname(root: &ModuleSpecifier) -> PathBuf {
result
}

pub fn closest_common_directory(paths: &[String]) -> String {
if paths.is_empty() {
return String::new();
}

// Parse the URLs and collect the path components
let paths: Vec<Vec<_>> = paths
.iter()
.map(|url| {
let path = url.trim_start_matches("file://");
Path::new(path)
.components()
.map(|comp| comp.as_os_str().to_str().unwrap_or(""))
.collect()
})
.collect();

// Find the shortest path to limit the comparison
let min_length = paths.iter().map(|p| p.len()).min().unwrap_or(0);

let mut common_path = Vec::new();

for i in 0..min_length {
let component = paths[0][i];
if paths.iter().all(|path| path[i] == component) {
common_path.push(component);
} else {
break;
}
}

common_path.join("/")
}

pub fn find_lowest_path(paths: &Vec<String>) -> Option<String> {
let mut lowest_path: Option<(&str, usize)> = None;

Expand Down
76 changes: 36 additions & 40 deletions crates/sb_graph/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use deno_ast::MediaType;
use deno_core::error::AnyError;
use deno_core::futures::io::{AllowStdIo, BufReader};
use deno_core::url::Url;
use deno_core::{normalize_path, serde_json, FastString, JsBuffer, ModuleSpecifier};
use deno_core::{serde_json, FastString, JsBuffer, ModuleSpecifier};
use deno_fs::{FileSystem, RealFs};
use deno_npm::NpmSystemInfo;
use eszip::{EszipV2, ModuleKind};
use glob::glob;
use log::error;
use sb_core::util::fs::specifier_to_file_path;
use sb_core::util::path::{closest_common_directory, find_lowest_path};
use sb_fs::{build_vfs, VfsOpts};
use sb_npm::InnerCliNpmResolverRef;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -233,70 +231,68 @@ pub struct ExtractEszipPayload {
pub folder: PathBuf,
}

fn ensure_unix_relative_path(path: &Path) -> &Path {
assert!(path.is_relative());
assert!(!path.to_string_lossy().starts_with('\\'));
path
}

fn create_module_path(global_specifier: &str, entry_path: &Path, output_folder: &Path) -> PathBuf {
let cleaned_specifier = global_specifier.replace(entry_path.to_str().unwrap(), "");
let module_path = PathBuf::from(cleaned_specifier);

if let Some(parent) = module_path.parent() {
if parent.parent().is_some() {
let output_folder_and_mod_folder = output_folder.join(
parent
.strip_prefix("/")
.unwrap_or_else(|_| ensure_unix_relative_path(parent)),
);
if !output_folder_and_mod_folder.exists() {
create_dir_all(&output_folder_and_mod_folder).unwrap();
}
}
}

output_folder.join(
module_path
.strip_prefix("/")
.unwrap_or_else(|_| ensure_unix_relative_path(&module_path)),
)
}

async fn extract_modules(
eszip: &EszipV2,
specifiers: &[String],
lowest_path: &str,
output_folder: &Path,
) {
let closest_common_dir = closest_common_directory(specifiers);
let closest_common_dir_buf = normalize_path(PathBuf::from(closest_common_dir));
let main_path = PathBuf::from(lowest_path);
let entry_path = main_path.parent().unwrap();

for global_specifier in specifiers {
let full_path = PathBuf::from(global_specifier);
let is_insider_path = full_path.starts_with(entry_path);

let target_folder = if is_insider_path {
output_folder.join("src")
} else {
output_folder.to_path_buf()
};

let new_file_path = if is_insider_path {
target_folder.join(full_path.strip_prefix(entry_path).unwrap())
} else {
let mod_specifier = ModuleSpecifier::parse(full_path.to_str().unwrap()).unwrap();
let file_path = specifier_to_file_path(&mod_specifier).unwrap();
target_folder.join(
file_path
.strip_prefix(closest_common_dir_buf.clone())
.unwrap(),
)
};

let module_path = create_module_path(global_specifier, entry_path, output_folder);
let module_content = eszip
.get_module(global_specifier)
.unwrap()
.take_source()
.await
.unwrap();

if let Some(parent) = new_file_path.parent() {
create_dir_all(parent).unwrap();
}

let mut file = File::create(&new_file_path).unwrap();
file.write_all(&module_content).unwrap();
let mut file = File::create(&module_path).unwrap();
file.write_all(module_content.as_ref()).unwrap();
}
}

pub async fn extract_eszip(payload: ExtractEszipPayload) {
let eszip = payload_to_eszip(payload.data).await;
let output_folder = payload.folder;
let output_folder_src = &output_folder.join("src");

if !output_folder.exists() {
create_dir_all(&output_folder).unwrap();
}

if !output_folder_src.exists() {
create_dir_all(output_folder_src).unwrap();
}

let file_specifiers = extract_file_specifiers(&eszip);
if let Some(lowest_path) = find_lowest_path(&file_specifiers) {
if let Some(lowest_path) = sb_core::util::path::find_lowest_path(&file_specifiers) {
extract_modules(&eszip, &file_specifiers, &lowest_path, &output_folder).await;
} else {
panic!("Path seems to be invalid");
Expand Down Expand Up @@ -338,7 +334,7 @@ mod test {
})
.await;

assert!(PathBuf::from("../base/test_cases/extracted-npm/src/hello.js").exists());
assert!(PathBuf::from("../base/test_cases/extracted-npm/hello.js").exists());
remove_dir_all(PathBuf::from("../base/test_cases/extracted-npm/")).unwrap();
}
}

0 comments on commit e4f5b6e

Please sign in to comment.