1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

fix(cli/installer): allow remote import maps (#10499)

This commit is contained in:
Satya Rohith 2021-05-10 06:18:30 +05:30 committed by GitHub
parent d806dc0f16
commit 33b1a6ed61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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));
}
}