From b3f1f3ba04e51554ab59d6255bf34a79a9c42bd0 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Tue, 6 Aug 2024 20:35:30 +0530 Subject: feat: deno run (#24891) This PR updates `deno run` to fallback to executing tasks when there is no script with the specified name. If there are both script and a task with the same name then `deno run` will prioritise executing the script. --- cli/main.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'cli/main.rs') diff --git a/cli/main.rs b/cli/main.rs index d17f0f260..9cb2b2644 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -37,6 +37,7 @@ use crate::util::display; use crate::util::v8::get_v8_flags_from_env; use crate::util::v8::init_v8_flags; +use args::TaskFlags; use deno_runtime::WorkerExecutionMode; pub use deno_runtime::UNSTABLE_GRANULAR_FLAGS; @@ -50,8 +51,10 @@ use deno_runtime::fmt_errors::format_js_error; use deno_runtime::tokio_util::create_and_run_current_thread_with_maybe_metrics; use deno_terminal::colors; use factory::CliFactory; +use standalone::MODULE_NOT_FOUND; use std::env; use std::future::Future; +use std::ops::Deref; use std::path::PathBuf; use std::sync::Arc; @@ -177,16 +180,39 @@ async fn run_subcommand(flags: Arc) -> Result { } DenoSubcommand::Run(run_flags) => spawn_subcommand(async move { if run_flags.is_stdin() { - tools::run::run_from_stdin(flags).await + tools::run::run_from_stdin(flags.clone()).await } else { - tools::run::run_script(WorkerExecutionMode::Run, flags, run_flags.watch).await + let result = tools::run::run_script(WorkerExecutionMode::Run, flags.clone(), run_flags.watch).await; + match result { + Ok(v) => Ok(v), + Err(script_err) => { + if script_err.to_string().starts_with(MODULE_NOT_FOUND) { + let mut new_flags = flags.deref().clone(); + let task_flags = TaskFlags { + cwd: None, + task: Some(run_flags.script.clone()), + }; + new_flags.subcommand = DenoSubcommand::Task(task_flags.clone()); + let result = tools::task::execute_script(Arc::new(new_flags), task_flags.clone(), true).await; + match result { + Ok(v) => Ok(v), + Err(_) => { + // Return script error for backwards compatibility. + Err(script_err) + } + } + } else { + Err(script_err) + } + }, + } } }), DenoSubcommand::Serve(serve_flags) => spawn_subcommand(async move { tools::run::run_script(WorkerExecutionMode::Serve, flags, serve_flags.watch).await }), DenoSubcommand::Task(task_flags) => spawn_subcommand(async { - tools::task::execute_script(flags, task_flags).await + tools::task::execute_script(flags, task_flags, false).await }), DenoSubcommand::Test(test_flags) => { spawn_subcommand(async { -- cgit v1.2.3