Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 68 additions & 17 deletions cli/tools/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ pub fn infer_name_from_url(url: &Url) -> Option<String> {
Some(stem)
}

/// Get a valid URL from the provided value.
/// When the provided value is a URL with 'http(s)' scheme,
/// it ensures it is valid by parsing it and if not, it will
/// construct a URL of 'file' scheme from the provided value.
fn get_valid_url(module_url: &str) -> Result<Url, AnyError> {
if is_remote_url(module_url) {
Ok(Url::parse(module_url).expect("Should be valid url"))
} else {
let module_path = PathBuf::from(module_url);
let module_path = if module_path.is_absolute() {
module_path
} else {
let cwd = env::current_dir()
.context("Failed to get current working directory")?;
cwd.join(module_path)
};
Ok(Url::from_file_path(module_path).expect("Path should be absolute"))
}
}

pub fn install(
flags: Flags,
module_url: &str,
Expand All @@ -169,19 +189,7 @@ pub fn install(
};

// Check if module_url is remote
let module_url = if is_remote_url(module_url) {
Url::parse(module_url).expect("Should be valid url")
} else {
let module_path = PathBuf::from(module_url);
let module_path = if module_path.is_absolute() {
module_path
} else {
let cwd = env::current_dir()
.context("Failed to get current working directory")?;
cwd.join(module_path)
};
Url::from_file_path(module_path).expect("Path should be absolute")
};
let module_url = get_valid_url(module_url)?;

let name = name.or_else(|| infer_name_from_url(&module_url));

Expand Down Expand Up @@ -271,11 +279,9 @@ pub fn install(
}

if let Some(import_map_path) = flags.import_map_path {
let mut copy_path = file_path.clone();
copy_path.set_extension("import_map.json");
let import_map_url = get_valid_url(&import_map_path)?;
executable_args.push("--import-map".to_string());
executable_args.push(copy_path.to_str().unwrap().to_string());
extra_files.push((copy_path, fs::read_to_string(import_map_path)?));
executable_args.push(import_map_url.to_string());
}

if let Some(config_path) = flags.config_path {
Expand Down Expand Up @@ -872,4 +878,49 @@ mod tests {
let status = Command::new(file_path).spawn().unwrap().wait().unwrap();
assert!(status.success());
}

#[test]
fn install_with_import_map() {
let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
let import_map_path = temp_dir.path().join("import_map.json");
let import_map_url = Url::from_file_path(&import_map_path).unwrap();
let import_map = "{ \"imports\": {} }";
let mut import_map_file = File::create(&import_map_path).unwrap();
let result = import_map_file.write_all(import_map.as_bytes());
assert!(result.is_ok());

let result = install(
Flags {
import_map_path: Some(import_map_path.to_string_lossy().to_string()),
..Flags::default()
},
"http://localhost:4545/cli/tests/cat.ts",
vec![],
Some("echo_test".to_string()),
Some(temp_dir.path().to_path_buf()),
true,
);
assert!(result.is_ok());

let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) {
file_path = file_path.with_extension("cmd");
}
assert!(file_path.exists());

let mut expected_string = format!(
"--import-map '{}' 'http://localhost:4545/cli/tests/cat.ts'",
import_map_url.to_string()
);
if cfg!(windows) {
expected_string = format!(
"\"--import-map\" \"{}\" \"http://localhost:4545/cli/tests/cat.ts\"",
import_map_url.to_string()
);
}

let content = fs::read_to_string(file_path).unwrap();
assert!(content.contains(&expected_string));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}