From e4f5b6e255dd190d6b11a3feed247e13f2e14eb5 Mon Sep 17 00:00:00 2001 From: andreespirela <60560109+andreespirela@users.noreply.github.com> Date: Mon, 15 Apr 2024 00:55:37 -0400 Subject: [PATCH] fix: revert "fix: unbundle command to better download function source code (#314)" (#318) This reverts commit 3a904d89ac46e222116074a66efdeb9d73ee76d0. --- .../npm/constant/some-other-file.ts | 1 - crates/base/test_cases/npm/index.ts | 2 - crates/base/test_cases/relative-path/index.ts | 3 - crates/base/test_cases/shared/index.ts | 13 ---- .../shared/some-other-folder/hello.ts | 1 - crates/sb_core/util/path.rs | 34 --------- crates/sb_graph/lib.rs | 76 +++++++++---------- 7 files changed, 36 insertions(+), 94 deletions(-) delete mode 100644 crates/base/test_cases/npm/constant/some-other-file.ts delete mode 100644 crates/base/test_cases/relative-path/index.ts delete mode 100644 crates/base/test_cases/shared/index.ts delete mode 100644 crates/base/test_cases/shared/some-other-folder/hello.ts diff --git a/crates/base/test_cases/npm/constant/some-other-file.ts b/crates/base/test_cases/npm/constant/some-other-file.ts deleted file mode 100644 index e07cc76c..00000000 --- a/crates/base/test_cases/npm/constant/some-other-file.ts +++ /dev/null @@ -1 +0,0 @@ -export const SomeOtherFile = "The wars between the galactic empire and earth are meant to be won only by those with access to light year power"; \ No newline at end of file diff --git a/crates/base/test_cases/npm/index.ts b/crates/base/test_cases/npm/index.ts index 0a8545f8..5c42b200 100644 --- a/crates/base/test_cases/npm/index.ts +++ b/crates/base/test_cases/npm/index.ts @@ -2,8 +2,6 @@ import isEven from "npm:is-even"; import { serve } from "https://deno.land/std@0.131.0/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( diff --git a/crates/base/test_cases/relative-path/index.ts b/crates/base/test_cases/relative-path/index.ts deleted file mode 100644 index dea01ea3..00000000 --- a/crates/base/test_cases/relative-path/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { handler } from '../shared/index.ts'; -console.log('Relative Path'); -Deno.serve(handler) \ No newline at end of file diff --git a/crates/base/test_cases/shared/index.ts b/crates/base/test_cases/shared/index.ts deleted file mode 100644 index efd33d61..00000000 --- a/crates/base/test_cases/shared/index.ts +++ /dev/null @@ -1,13 +0,0 @@ -import {CONSTANT} from "./some-other-folder/hello.ts"; - -export const handler = async (req) => { - const { name } = await req.json() - const data = { - message: `Hello World ${name}! ${CONSTANT}`, - } - return new Response(JSON.stringify(data), { - headers: { - 'Content-Type': 'application/json', - }, - }) -} \ No newline at end of file diff --git a/crates/base/test_cases/shared/some-other-folder/hello.ts b/crates/base/test_cases/shared/some-other-folder/hello.ts deleted file mode 100644 index da817580..00000000 --- a/crates/base/test_cases/shared/some-other-folder/hello.ts +++ /dev/null @@ -1 +0,0 @@ -export const CONSTANT = 1; \ No newline at end of file diff --git a/crates/sb_core/util/path.rs b/crates/sb_core/util/path.rs index fab94860..505f9aea 100644 --- a/crates/sb_core/util/path.rs +++ b/crates/sb_core/util/path.rs @@ -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> = 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) -> Option { let mut lowest_path: Option<(&str, usize)> = None; diff --git a/crates/sb_graph/lib.rs b/crates/sb_graph/lib.rs index 5cc853a5..6ca9b8be 100644 --- a/crates/sb_graph/lib.rs +++ b/crates/sb_graph/lib.rs @@ -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}; @@ -233,39 +231,46 @@ 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() @@ -273,30 +278,21 @@ async fn extract_modules( .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"); @@ -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(); } }