summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/import_map.rs34
-rw-r--r--cli/main.rs1
-rw-r--r--cli/state.rs23
3 files changed, 40 insertions, 18 deletions
diff --git a/cli/import_map.rs b/cli/import_map.rs
index 7bfe7476c..d2916c198 100644
--- a/cli/import_map.rs
+++ b/cli/import_map.rs
@@ -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(
@@ -473,6 +481,12 @@ 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";
diff --git a/cli/main.rs b/cli/main.rs
index 020ace5a3..2d3c70c97 100644
--- a/cli/main.rs
+++ b/cli/main.rs
@@ -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(),
diff --git a/cli/state.rs b/cli/state.rs
index 0b0f3b1ae..777382907 100644
--- a/cli/state.rs
+++ b/cli/state.rs
@@ -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,
+ );
+}