diff options
Diffstat (limited to 'cli/args/mod.rs')
-rw-r--r-- | cli/args/mod.rs | 79 |
1 files changed, 74 insertions, 5 deletions
diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 848f50eb4..fb44c0a8f 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -10,6 +10,8 @@ pub mod package_json; pub use self::import_map::resolve_import_map_from_specifier; use self::package_json::PackageJsonDeps; use ::import_map::ImportMap; +use deno_core::resolve_url_or_path; +use deno_graph::npm::NpmPackageReqReference; use indexmap::IndexMap; use crate::npm::NpmRegistryApi; @@ -50,6 +52,7 @@ use deno_runtime::deno_tls::webpki_roots; use deno_runtime::inspector_server::InspectorServer; use deno_runtime::permissions::PermissionsOptions; use once_cell::sync::Lazy; +use std::collections::HashMap; use std::env; use std::io::BufReader; use std::io::Cursor; @@ -139,7 +142,6 @@ impl BenchOptions { pub struct FmtOptions { pub is_stdin: bool, pub check: bool, - pub ext: String, pub options: FmtOptionsConfig, pub files: FilesConfig, } @@ -166,10 +168,6 @@ impl FmtOptions { Ok(Self { is_stdin, check: maybe_fmt_flags.as_ref().map(|f| f.check).unwrap_or(false), - ext: maybe_fmt_flags - .as_ref() - .map(|f| f.ext.to_string()) - .unwrap_or_else(|| "ts".to_string()), options: resolve_fmt_options( maybe_fmt_flags.as_ref(), maybe_config_options, @@ -675,6 +673,73 @@ impl CliOptions { .map(Some) } + pub fn resolve_main_module(&self) -> Result<ModuleSpecifier, AnyError> { + match &self.flags.subcommand { + DenoSubcommand::Bundle(bundle_flags) => { + resolve_url_or_path(&bundle_flags.source_file, self.initial_cwd()) + .map_err(AnyError::from) + } + DenoSubcommand::Compile(compile_flags) => { + resolve_url_or_path(&compile_flags.source_file, self.initial_cwd()) + .map_err(AnyError::from) + } + DenoSubcommand::Eval(_) => { + resolve_url_or_path("./$deno$eval", self.initial_cwd()) + .map_err(AnyError::from) + } + DenoSubcommand::Repl(_) => { + resolve_url_or_path("./$deno$repl.ts", self.initial_cwd()) + .map_err(AnyError::from) + } + DenoSubcommand::Run(run_flags) => { + if run_flags.is_stdin() { + std::env::current_dir() + .context("Unable to get CWD") + .and_then(|cwd| { + resolve_url_or_path("./$deno$stdin", &cwd).map_err(AnyError::from) + }) + } else if self.flags.watch.is_some() { + resolve_url_or_path(&run_flags.script, self.initial_cwd()) + .map_err(AnyError::from) + } else if NpmPackageReqReference::from_str(&run_flags.script).is_ok() { + ModuleSpecifier::parse(&run_flags.script).map_err(AnyError::from) + } else { + resolve_url_or_path(&run_flags.script, self.initial_cwd()) + .map_err(AnyError::from) + } + } + _ => { + bail!("No main module.") + } + } + } + + pub fn resolve_file_header_overrides( + &self, + ) -> HashMap<ModuleSpecifier, HashMap<String, String>> { + let maybe_main_specifier = self.resolve_main_module().ok(); + // TODO(Cre3per): This mapping moved to deno_ast with https://github.com/denoland/deno_ast/issues/133 and should be available in deno_ast >= 0.25.0 via `MediaType::from_path(...).as_media_type()` + let maybe_content_type = + self.flags.ext.as_ref().and_then(|el| match el.as_str() { + "ts" => Some("text/typescript"), + "tsx" => Some("text/tsx"), + "js" => Some("text/javascript"), + "jsx" => Some("text/jsx"), + _ => None, + }); + + if let (Some(main_specifier), Some(content_type)) = + (maybe_main_specifier, maybe_content_type) + { + HashMap::from([( + main_specifier, + HashMap::from([("content-type".to_string(), content_type.to_string())]), + )]) + } else { + HashMap::default() + } + } + pub async fn resolve_npm_resolution_snapshot( &self, api: &NpmRegistryApi, @@ -936,6 +1001,10 @@ impl CliOptions { self.flags.enable_testing_features } + pub fn ext_flag(&self) -> &Option<String> { + &self.flags.ext + } + /// If the --inspect or --inspect-brk flags are used. pub fn is_inspecting(&self) -> bool { self.flags.inspect.is_some() |