diff options
Diffstat (limited to 'cli/args/flags.rs')
-rw-r--r-- | cli/args/flags.rs | 93 |
1 files changed, 58 insertions, 35 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs index f8577ed1b..800d6ff5a 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -339,6 +339,7 @@ pub struct ServeFlags { pub watch: Option<WatchFlagsWithPaths>, pub port: u16, pub host: String, + pub worker_count: Option<usize>, } impl ServeFlags { @@ -349,6 +350,7 @@ impl ServeFlags { watch: None, port, host: host.to_owned(), + worker_count: None, } } } @@ -2693,6 +2695,9 @@ fn serve_subcommand() -> Command { .help("The TCP address to serve on, defaulting to 0.0.0.0 (all interfaces).") .value_parser(serve_host_validator), ) + .arg( + parallel_arg("multiple server workers", false) + ) .arg(check_arg(false)) .arg(watch_arg(true)) .arg(watch_exclude_arg()) @@ -2854,11 +2859,7 @@ Directory arguments are expanded to all contained files matching the glob .action(ArgAction::SetTrue), ) .arg( - Arg::new("parallel") - .long("parallel") - .help("Run test modules in parallel. Parallelism defaults to the number of available CPUs or the value in the DENO_JOBS environment variable.") - .conflicts_with("jobs") - .action(ArgAction::SetTrue) + parallel_arg("test modules", true) ) .arg( Arg::new("jobs") @@ -2901,6 +2902,18 @@ Directory arguments are expanded to all contained files matching the glob ) } +fn parallel_arg(descr: &str, jobs_fallback: bool) -> Arg { + let arg = Arg::new("parallel") + .long("parallel") + .help(format!("Run {descr} in parallel. Parallelism defaults to the number of available CPUs or the value in the DENO_JOBS environment variable.")) + .action(ArgAction::SetTrue); + if jobs_fallback { + arg.conflicts_with("jobs") + } else { + arg + } +} + fn types_subcommand() -> Command { Command::new("types").about( "Print runtime TypeScript declarations. @@ -4416,6 +4429,8 @@ fn serve_parse( .remove_one::<String>("host") .unwrap_or_else(|| "0.0.0.0".to_owned()); + let worker_count = parallel_arg_parse(matches, false).map(|v| v.get()); + runtime_args_parse(flags, matches, true, true); // If the user didn't pass --allow-net, add this port to the network // allowlist. If the host is 0.0.0.0, we add :{port} and allow the same network perms @@ -4455,6 +4470,7 @@ fn serve_parse( watch: watch_arg_parse_with_paths(matches), port, host, + worker_count, }); Ok(()) @@ -4486,6 +4502,42 @@ fn task_parse(flags: &mut Flags, matches: &mut ArgMatches) { flags.subcommand = DenoSubcommand::Task(task_flags); } +fn parallel_arg_parse( + matches: &mut ArgMatches, + fallback_to_jobs: bool, +) -> Option<NonZeroUsize> { + if matches.get_flag("parallel") { + if let Ok(value) = env::var("DENO_JOBS") { + value.parse::<NonZeroUsize>().ok() + } else { + std::thread::available_parallelism().ok() + } + } else if fallback_to_jobs && matches.contains_id("jobs") { + // We can't change this to use the log crate because its not configured + // yet at this point since the flags haven't been parsed. This flag is + // deprecated though so it's not worth changing the code to use the log + // crate here and this is only done for testing anyway. + #[allow(clippy::print_stderr)] + { + eprintln!( + "⚠️ {}", + crate::colors::yellow(concat!( + "The `--jobs` flag is deprecated and will be removed in Deno 2.0.\n", + "Use the `--parallel` flag with possibly the `DENO_JOBS` environment variable instead.\n", + "Learn more at: https://docs.deno.com/runtime/manual/basics/env_variables" + )), + ); + } + if let Some(value) = matches.remove_one::<NonZeroUsize>("jobs") { + Some(value) + } else { + std::thread::available_parallelism().ok() + } + } else { + None + } +} + fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) { flags.type_check_mode = TypeCheckMode::Local; runtime_args_parse(flags, matches, true, true); @@ -4552,36 +4604,7 @@ fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) { flags.argv.extend(script_arg); } - let concurrent_jobs = if matches.get_flag("parallel") { - if let Ok(value) = env::var("DENO_JOBS") { - value.parse::<NonZeroUsize>().ok() - } else { - std::thread::available_parallelism().ok() - } - } else if matches.contains_id("jobs") { - // We can't change this to use the log crate because its not configured - // yet at this point since the flags haven't been parsed. This flag is - // deprecated though so it's not worth changing the code to use the log - // crate here and this is only done for testing anyway. - #[allow(clippy::print_stderr)] - { - eprintln!( - "⚠️ {}", - crate::colors::yellow(concat!( - "The `--jobs` flag is deprecated and will be removed in Deno 2.0.\n", - "Use the `--parallel` flag with possibly the `DENO_JOBS` environment variable instead.\n", - "Learn more at: https://docs.deno.com/runtime/manual/basics/env_variables" - )), - ); - } - if let Some(value) = matches.remove_one::<NonZeroUsize>("jobs") { - Some(value) - } else { - std::thread::available_parallelism().ok() - } - } else { - None - }; + let concurrent_jobs = parallel_arg_parse(matches, true); let include = if let Some(files) = matches.remove_many::<String>("files") { files.collect() |