summaryrefslogtreecommitdiff
path: root/cli/main.rs
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2024-08-06 20:35:30 +0530
committerGitHub <noreply@github.com>2024-08-06 20:35:30 +0530
commitb3f1f3ba04e51554ab59d6255bf34a79a9c42bd0 (patch)
tree6455a67387f69c01803e12bc1652eeb0f7dbf7ca /cli/main.rs
parent897159dc6e1b2319cf2f5f09d8d6cecc0d3175fa (diff)
feat: deno run <task> (#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.
Diffstat (limited to 'cli/main.rs')
-rw-r--r--cli/main.rs32
1 files changed, 29 insertions, 3 deletions
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<Flags>) -> Result<i32, AnyError> {
}
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 {