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

Fix import map panics, use import map's location as its base URL (#2770)

This commit is contained in:
Nayeem Rahman 2019-08-15 18:58:04 +01:00 committed by Ryan Dahl
parent d2d3afaf2d
commit 52a66c2796
3 changed files with 40 additions and 18 deletions

View file

@ -1,3 +1,4 @@
use deno::ErrBox;
use deno::ModuleSpecifier;
use indexmap::IndexMap;
use serde_json::Map;
@ -6,6 +7,7 @@ use std::cmp::Ordering;
use std::error::Error;
use std::fmt;
use std::fs;
use std::io;
use url::Url;
#[derive(Debug)]
@ -44,22 +46,28 @@ pub struct ImportMap {
}
impl ImportMap {
pub fn load(base_url: &str, file_name: &str) -> Result<Self, ImportMapError> {
let cwd = std::env::current_dir().unwrap();
let resolved_path = cwd.join(file_name);
pub fn load(file_path: &str) -> Result<Self, ErrBox> {
let file_url = ModuleSpecifier::resolve_url_or_path(file_path)?.to_string();
let resolved_path = std::env::current_dir().unwrap().join(file_path);
debug!(
"Attempt to load import map: {}",
resolved_path.to_str().unwrap()
);
// Load the contents of import map
match fs::read_to_string(&resolved_path) {
Ok(json_string) => ImportMap::from_json(base_url, &json_string),
_ => panic!(
"Error retrieving import map file at \"{}\"",
resolved_path.to_str().unwrap()
),
}
let json_string = fs::read_to_string(&resolved_path).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidInput,
format!(
"Error retrieving import map file at \"{}\": {}",
resolved_path.to_str().unwrap(),
err.to_string()
)
.as_str(),
)
})?;
// The URL of the import map is the base URL for its values.
ImportMap::from_json(&file_url, &json_string).map_err(ErrBox::from)
}
pub fn from_json(
@ -472,6 +480,12 @@ impl ImportMap {
mod tests {
use super::*;
#[test]
fn load_nonexistent() {
let file_path = "nonexistent_import_map.json";
assert!(ImportMap::load(file_path).is_err());
}
#[test]
fn from_json_1() {
let base_url = "https://deno.land";

View file

@ -115,6 +115,7 @@ fn create_worker_and_state(
// TODO(kevinkassimo): maybe make include_deno_namespace also configurable?
let state =
ThreadSafeState::new(flags, argv, ops::op_selector_std, progress, true)
.map_err(print_err_and_exit)
.unwrap();
let worker = Worker::new(
"main".to_string(),

View file

@ -200,14 +200,7 @@ impl ThreadSafeState {
let import_map: Option<ImportMap> = match &flags.import_map_path {
None => None,
Some(file_name) => {
let base_url = match &main_module {
Some(module_specifier) => module_specifier.clone(),
None => unreachable!(),
};
let import_map = ImportMap::load(&base_url.to_string(), file_name)?;
Some(import_map)
}
Some(file_path) => Some(ImportMap::load(file_path)?),
};
let mut seeded_rng = None;
@ -380,3 +373,17 @@ fn thread_safe() {
String::from("hello.js"),
]));
}
#[test]
fn import_map_given_for_repl() {
let _result = ThreadSafeState::new(
flags::DenoFlags {
import_map_path: Some("import_map.json".to_string()),
..flags::DenoFlags::default()
},
vec![String::from("./deno")],
ops::op_selector_std,
Progress::new(),
true,
);
}