summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2021-05-10 06:18:30 +0530
committerGitHub <noreply@github.com>2021-05-10 09:48:30 +0900
commit33b1a6ed617a9e3f9448d6699f6fca8af7330c80 (patch)
tree462ada2f663b5e93a503a6c93de070eee25a9819
parentd806dc0f16cc1d0bd99aad903cbe3d543cd1cb1d (diff)
fix(cli/installer): allow remote import maps (#10499)
-rw-r--r--cli/tools/installer.rs85
1 files changed, 68 insertions, 17 deletions
diff --git a/cli/tools/installer.rs b/cli/tools/installer.rs
index 48e3b8fdd..3693b2fb3 100644
--- a/cli/tools/installer.rs
+++ b/cli/tools/installer.rs
@@ -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,
@@ -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));
@@ -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 {
@@ -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));
+ }
}