diff options
Diffstat (limited to 'cli/args')
-rw-r--r-- | cli/args/flags.rs | 33 | ||||
-rw-r--r-- | cli/args/mod.rs | 19 |
2 files changed, 52 insertions, 0 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 7922a3c19..513307e92 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -301,6 +301,7 @@ pub struct Flags { pub cached_only: bool, pub type_check_mode: TypeCheckMode, pub config_flag: ConfigFlag, + pub node_modules_dir: bool, pub coverage_dir: Option<String>, pub enable_testing_features: bool, pub ignore: Vec<PathBuf>, @@ -1734,6 +1735,7 @@ fn compile_args(app: Command) -> Command { .arg(import_map_arg()) .arg(no_remote_arg()) .arg(no_npm_arg()) + .arg(local_npm_arg()) .arg(no_config_arg()) .arg(config_arg()) .arg(no_check_arg()) @@ -1749,6 +1751,7 @@ fn compile_args_without_check_args(app: Command) -> Command { .arg(import_map_arg()) .arg(no_remote_arg()) .arg(no_npm_arg()) + .arg(local_npm_arg()) .arg(config_arg()) .arg(no_config_arg()) .arg(reload_arg()) @@ -2153,6 +2156,12 @@ fn no_npm_arg<'a>() -> Arg<'a> { .help("Do not resolve npm modules") } +fn local_npm_arg<'a>() -> Arg<'a> { + Arg::new("node-modules-dir") + .long("node-modules-dir") + .help("Creates a local node_modules folder") +} + fn unsafely_ignore_certificate_errors_arg<'a>() -> Arg<'a> { Arg::new("unsafely-ignore-certificate-errors") .long("unsafely-ignore-certificate-errors") @@ -2799,6 +2808,7 @@ fn compile_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) { import_map_arg_parse(flags, matches); no_remote_arg_parse(flags, matches); no_npm_arg_parse(flags, matches); + local_npm_args_parse(flags, matches); config_args_parse(flags, matches); no_check_arg_parse(flags, matches); check_arg_parse(flags, matches); @@ -2814,6 +2824,7 @@ fn compile_args_without_no_check_parse( import_map_arg_parse(flags, matches); no_remote_arg_parse(flags, matches); no_npm_arg_parse(flags, matches); + local_npm_args_parse(flags, matches); config_args_parse(flags, matches); reload_arg_parse(flags, matches); lock_args_parse(flags, matches); @@ -3061,6 +3072,12 @@ fn no_npm_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { } } +fn local_npm_args_parse(flags: &mut Flags, matches: &ArgMatches) { + if matches.is_present("node-modules-dir") { + flags.node_modules_dir = true; + } +} + fn inspect_arg_validate(val: &str) -> Result<(), String> { match val.parse::<SocketAddr>() { Ok(_) => Ok(()), @@ -5035,6 +5052,22 @@ mod tests { } #[test] + fn local_npm() { + let r = + flags_from_vec(svec!["deno", "run", "--node-modules-dir", "script.ts"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Run(RunFlags { + script: "script.ts".to_string(), + }), + node_modules_dir: true, + ..Flags::default() + } + ); + } + + #[test] fn cached_only() { let r = flags_from_vec(svec!["deno", "run", "--cached-only", "script.ts"]); assert_eq!( diff --git a/cli/args/mod.rs b/cli/args/mod.rs index e4c0d8556..1e0c72b1c 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -42,6 +42,7 @@ use crate::emit::TsConfigWithIgnoredOptions; use crate::emit::TsTypeLib; use crate::file_fetcher::get_root_cert_store; use crate::file_fetcher::CacheSetting; +use crate::fs_util; use crate::lockfile::Lockfile; use crate::version; @@ -146,6 +147,24 @@ impl CliOptions { self.overrides.import_map_specifier = Some(path); } + /// Resolves the path to use for a local node_modules folder. + pub fn resolve_local_node_modules_folder( + &self, + ) -> Result<Option<PathBuf>, AnyError> { + let path = if !self.flags.node_modules_dir { + return Ok(None); + } else if let Some(config_path) = self + .maybe_config_file + .as_ref() + .and_then(|c| c.specifier.to_file_path().ok()) + { + config_path.parent().unwrap().join("node_modules") + } else { + std::env::current_dir()?.join("node_modules") + }; + Ok(Some(fs_util::canonicalize_path_maybe_not_exists(&path)?)) + } + pub fn resolve_root_cert_store(&self) -> Result<RootCertStore, AnyError> { get_root_cert_store( None, |