Skip to content

Commit 4dfd44f

Browse files
feat: deno run with no arguments prints list of available tasks (#30592)
Closes #29552 --------- Signed-off-by: David Sherret <[email protected]> Co-authored-by: David Sherret <[email protected]>
1 parent 893ceb3 commit 4dfd44f

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

cli/args/flags.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ pub struct RunFlags {
339339
pub watch: Option<WatchFlagsWithPaths>,
340340
pub bare: bool,
341341
pub coverage_dir: Option<String>,
342+
pub print_task_list: bool,
342343
}
343344

344345
impl RunFlags {
@@ -349,6 +350,7 @@ impl RunFlags {
349350
watch: None,
350351
bare: false,
351352
coverage_dir: None,
353+
print_task_list: false,
352354
}
353355
}
354356

@@ -5763,7 +5765,7 @@ fn repl_parse(
57635765
fn run_parse(
57645766
flags: &mut Flags,
57655767
matches: &mut ArgMatches,
5766-
mut app: Command,
5768+
app: Command,
57675769
bare: bool,
57685770
) -> clap::error::Result<()> {
57695771
runtime_args_parse(flags, matches, true, true, true)?;
@@ -5782,6 +5784,7 @@ fn run_parse(
57825784
watch: watch_arg_parse_with_paths(matches)?,
57835785
bare,
57845786
coverage_dir,
5787+
print_task_list: false,
57855788
});
57865789
}
57875790
_ => {
@@ -5791,10 +5794,14 @@ fn run_parse(
57915794
"[SCRIPT_ARG] may only be omitted with --v8-flags=--help, else to use the repl with arguments, please use the `deno repl` subcommand",
57925795
));
57935796
} else {
5794-
return Err(app.find_subcommand_mut("run").unwrap().error(
5795-
clap::error::ErrorKind::MissingRequiredArgument,
5796-
"[SCRIPT_ARG] may only be omitted with --v8-flags=--help",
5797-
));
5797+
// When no script argument is provided, show available tasks like `deno task`
5798+
flags.subcommand = DenoSubcommand::Run(RunFlags {
5799+
script: "".to_string(),
5800+
watch: None,
5801+
bare: false,
5802+
coverage_dir: None,
5803+
print_task_list: true,
5804+
});
57985805
}
57995806
}
58005807
}
@@ -6764,6 +6771,7 @@ mod tests {
67646771
}),
67656772
bare: false,
67666773
coverage_dir: None,
6774+
print_task_list: false,
67676775
}),
67686776
code_cache_enabled: true,
67696777
..Flags::default()
@@ -6790,6 +6798,7 @@ mod tests {
67906798
}),
67916799
bare: true,
67926800
coverage_dir: None,
6801+
print_task_list: false,
67936802
}),
67946803
code_cache_enabled: true,
67956804
..Flags::default()
@@ -6817,6 +6826,7 @@ mod tests {
68176826
}),
68186827
bare: false,
68196828
coverage_dir: None,
6829+
print_task_list: false,
68206830
}),
68216831
code_cache_enabled: true,
68226832
..Flags::default()
@@ -6844,6 +6854,7 @@ mod tests {
68446854
}),
68456855
bare: false,
68466856
coverage_dir: None,
6857+
print_task_list: false,
68476858
}),
68486859
code_cache_enabled: true,
68496860
..Flags::default()
@@ -6871,6 +6882,7 @@ mod tests {
68716882
}),
68726883
bare: false,
68736884
coverage_dir: None,
6885+
print_task_list: false,
68746886
}),
68756887
code_cache_enabled: true,
68766888
..Flags::default()
@@ -6899,6 +6911,7 @@ mod tests {
68996911
}),
69006912
bare: true,
69016913
coverage_dir: None,
6914+
print_task_list: false,
69026915
}),
69036916
code_cache_enabled: true,
69046917
..Flags::default()
@@ -6930,6 +6943,7 @@ mod tests {
69306943
}),
69316944
bare: false,
69326945
coverage_dir: None,
6946+
print_task_list: false,
69336947
}),
69346948
code_cache_enabled: true,
69356949
..Flags::default()
@@ -6960,6 +6974,7 @@ mod tests {
69606974
}),
69616975
bare: true,
69626976
coverage_dir: None,
6977+
print_task_list: false,
69636978
}),
69646979
code_cache_enabled: true,
69656980
..Flags::default()
@@ -6987,6 +7002,7 @@ mod tests {
69877002
}),
69887003
bare: false,
69897004
coverage_dir: None,
7005+
print_task_list: false,
69907006
}),
69917007
code_cache_enabled: true,
69927008
..Flags::default()
@@ -7015,6 +7031,7 @@ mod tests {
70157031
}),
70167032
bare: false,
70177033
coverage_dir: None,
7034+
print_task_list: false,
70187035
}),
70197036
code_cache_enabled: true,
70207037
..Flags::default()
@@ -7042,6 +7059,7 @@ mod tests {
70427059
}),
70437060
bare: true,
70447061
coverage_dir: None,
7062+
print_task_list: false,
70457063
}),
70467064
code_cache_enabled: true,
70477065
..Flags::default()
@@ -7081,6 +7099,7 @@ mod tests {
70817099
watch: None,
70827100
bare: false,
70837101
coverage_dir: Some("foo".to_string()),
7102+
print_task_list: false,
70847103
}),
70857104
code_cache_enabled: true,
70867105
..Flags::default()
@@ -7120,7 +7139,7 @@ mod tests {
71207139
);
71217140

71227141
let r = flags_from_vec(svec!["deno", "run", "--v8-flags=--expose-gc"]);
7123-
assert!(r.is_err());
7142+
assert!(r.is_ok());
71247143
}
71257144

71267145
#[test]
@@ -7397,6 +7416,7 @@ mod tests {
73977416
watch: None,
73987417
bare: true,
73997418
coverage_dir: None,
7419+
print_task_list: false,
74007420
}),
74017421
permissions: PermissionFlags {
74027422
deny_read: Some(vec![]),
@@ -8687,6 +8707,7 @@ mod tests {
86878707
watch: None,
86888708
bare: true,
86898709
coverage_dir: None,
8710+
print_task_list: false,
86908711
}),
86918712
permissions: PermissionFlags {
86928713
deny_net: Some(svec!["127.0.0.1"]),
@@ -8875,6 +8896,7 @@ mod tests {
88758896
watch: None,
88768897
bare: true,
88778898
coverage_dir: None,
8899+
print_task_list: false,
88788900
}),
88798901
permissions: PermissionFlags {
88808902
deny_sys: Some(svec!["hostname"]),
@@ -9175,6 +9197,7 @@ mod tests {
91759197
watch: None,
91769198
bare: true,
91779199
coverage_dir: None,
9200+
print_task_list: false,
91789201
}),
91799202
..Flags::default()
91809203
}
@@ -9486,6 +9509,7 @@ mod tests {
94869509
watch: None,
94879510
bare: true,
94889511
coverage_dir: None,
9512+
print_task_list: false,
94899513
}),
94909514
log_level: Some(Level::Error),
94919515
code_cache_enabled: true,
@@ -9607,6 +9631,7 @@ mod tests {
96079631
watch: None,
96089632
bare: true,
96099633
coverage_dir: None,
9634+
print_task_list: false,
96109635
}),
96119636
type_check_mode: TypeCheckMode::None,
96129637
code_cache_enabled: true,
@@ -9779,6 +9804,7 @@ mod tests {
97799804
watch: None,
97809805
bare: true,
97819806
coverage_dir: None,
9807+
print_task_list: false,
97829808
}),
97839809
node_modules_dir: Some(NodeModulesDirMode::Auto),
97849810
code_cache_enabled: true,
@@ -11003,6 +11029,7 @@ mod tests {
1100311029
watch: None,
1100411030
bare: true,
1100511031
coverage_dir: None,
11032+
print_task_list: false,
1100611033
}),
1100711034
inspect_wait: Some("127.0.0.1:9229".parse().unwrap()),
1100811035
code_cache_enabled: true,
@@ -11697,6 +11724,7 @@ mod tests {
1169711724
watch: None,
1169811725
bare: true,
1169911726
coverage_dir: None,
11727+
print_task_list: false,
1170011728
}),
1170111729
type_check_mode: TypeCheckMode::None,
1170211730
code_cache_enabled: true,
@@ -12251,6 +12279,7 @@ mod tests {
1225112279
watch: None,
1225212280
bare: true,
1225312281
coverage_dir: None,
12282+
print_task_list: false,
1225412283
}),
1225512284
config_flag: ConfigFlag::Disabled,
1225612285
code_cache_enabled: true,

cli/main.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use std::collections::HashMap;
3333
use std::env;
3434
use std::future::Future;
3535
use std::io::IsTerminal;
36+
use std::io::Write as _;
3637
use std::ops::Deref;
3738
use std::path::PathBuf;
3839
use std::sync::Arc;
@@ -241,7 +242,28 @@ async fn run_subcommand(
241242
spawn_subcommand(async move { tools::repl::run(flags, repl_flags).await })
242243
}
243244
DenoSubcommand::Run(run_flags) => spawn_subcommand(async move {
244-
if run_flags.is_stdin() {
245+
if run_flags.print_task_list {
246+
let task_flags = TaskFlags {
247+
cwd: None,
248+
task: None,
249+
is_run: true,
250+
recursive: false,
251+
filter: None,
252+
eval: false,
253+
};
254+
let mut flags = flags.deref().clone();
255+
flags.subcommand = DenoSubcommand::Task(task_flags.clone());
256+
writeln!(
257+
&mut std::io::stdout(),
258+
"Please specify a {} or a {}.\n",
259+
colors::bold("[SCRIPT_ARG]"),
260+
colors::bold("task name")
261+
)?;
262+
std::io::stdout().flush()?;
263+
tools::task::execute_script(Arc::new(flags), task_flags)
264+
.await
265+
.map(|_| 1)
266+
} else if run_flags.is_stdin() {
245267
// these futures are boxed to prevent stack overflows on Windows
246268
tools::run::run_from_stdin(flags.clone(), unconfigured_runtime, roots)
247269
.boxed_local()

cli/tools/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub async fn execute_script(
6363
let start_dir = &cli_options.start_dir;
6464
if !start_dir.has_deno_or_pkg_json() && !task_flags.eval {
6565
bail!(
66-
"deno task couldn't find deno.json(c). See https://docs.deno.com/go/config"
66+
"deno task couldn't find deno.json(c) or package.json. See https://docs.deno.com/go/config"
6767
)
6868
}
6969
let force_use_pkg_json =

tests/specs/run/run_task/empty.out

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
error: [SCRIPT_ARG] may only be omitted with --v8-flags=--help
2-
3-
Usage: deno run [OPTIONS] [SCRIPT_ARG]...
1+
Please specify a [SCRIPT_ARG] or a task name.
42

3+
Available tasks:
4+
- main
5+
deno run main.ts
6+
- main:foo
7+
deno run main.ts

0 commit comments

Comments
 (0)