1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat(flags): improve help output and make deno run list tasks (#25108)

- rewrite flag help
- use gray for indentation
- reorganize permission flags and split them up
- make help subcommand act like help flag
- `deno run` outputs list of tasks
- Fixes #25120

error handling for `deno run` in case of no config file being found
needs to be improved

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Leo Kettmeir 2024-08-21 06:54:59 -07:00 committed by GitHub
parent e920835417
commit 7139337083
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 300 additions and 222 deletions

12
Cargo.lock generated
View file

@ -152,15 +152,16 @@ dependencies = [
[[package]] [[package]]
name = "anstream" name = "anstream"
version = "0.6.13" version = "0.6.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
dependencies = [ dependencies = [
"anstyle", "anstyle",
"anstyle-parse", "anstyle-parse",
"anstyle-query", "anstyle-query",
"anstyle-wincon", "anstyle-wincon",
"colorchoice", "colorchoice",
"is_terminal_polyfill",
"utf8parse", "utf8parse",
] ]
@ -1146,6 +1147,7 @@ dependencies = [
name = "deno" name = "deno"
version = "1.46.0-rc.3" version = "1.46.0-rc.3"
dependencies = [ dependencies = [
"anstream",
"async-trait", "async-trait",
"base32", "base32",
"base64 0.21.7", "base64 0.21.7",
@ -3871,6 +3873,12 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.10.5" version = "0.10.5"

View file

@ -83,6 +83,7 @@ libsui = "0.3.0"
napi_sym.workspace = true napi_sym.workspace = true
node_resolver.workspace = true node_resolver.workspace = true
anstream = "0.6.14"
async-trait.workspace = true async-trait.workspace = true
base32.workspace = true base32.workspace = true
base64.workspace = true base64.workspace = true

File diff suppressed because it is too large Load diff

View file

@ -218,9 +218,10 @@ async fn run_subcommand(flags: Arc<Flags>) -> Result<i32, AnyError> {
let task_flags = TaskFlags { let task_flags = TaskFlags {
cwd: None, cwd: None,
task: Some(run_flags.script.clone()), task: Some(run_flags.script.clone()),
is_run: true,
}; };
new_flags.subcommand = DenoSubcommand::Task(task_flags.clone()); new_flags.subcommand = DenoSubcommand::Task(task_flags.clone());
let result = tools::task::execute_script(Arc::new(new_flags), task_flags.clone(), true).await; let result = tools::task::execute_script(Arc::new(new_flags), task_flags.clone()).await;
match result { match result {
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(_) => { Err(_) => {
@ -240,7 +241,7 @@ async fn run_subcommand(flags: Arc<Flags>) -> Result<i32, AnyError> {
tools::serve::serve(flags, serve_flags).await tools::serve::serve(flags, serve_flags).await
}), }),
DenoSubcommand::Task(task_flags) => spawn_subcommand(async { DenoSubcommand::Task(task_flags) => spawn_subcommand(async {
tools::task::execute_script(flags, task_flags, false).await tools::task::execute_script(flags, task_flags).await
}), }),
DenoSubcommand::Test(test_flags) => { DenoSubcommand::Test(test_flags) => {
spawn_subcommand(async { spawn_subcommand(async {
@ -290,7 +291,21 @@ async fn run_subcommand(flags: Arc<Flags>) -> Result<i32, AnyError> {
tools::registry::publish(flags, publish_flags).await tools::registry::publish(flags, publish_flags).await
}), }),
DenoSubcommand::Help(help_flags) => spawn_subcommand(async move { DenoSubcommand::Help(help_flags) => spawn_subcommand(async move {
display::write_to_stdout_ignore_sigpipe(help_flags.help.ansi().to_string().as_bytes()) use std::io::Write;
let mut stream = anstream::AutoStream::new(std::io::stdout(), if colors::use_color() {
anstream::ColorChoice::Auto
} else {
anstream::ColorChoice::Never
});
match stream.write_all(help_flags.help.ansi().to_string().as_bytes()) {
Ok(()) => Ok(()),
Err(e) => match e.kind() {
std::io::ErrorKind::BrokenPipe => Ok(()),
_ => Err(e),
},
}
}), }),
}; };

View file

@ -29,13 +29,24 @@ use std::sync::Arc;
pub async fn execute_script( pub async fn execute_script(
flags: Arc<Flags>, flags: Arc<Flags>,
task_flags: TaskFlags, task_flags: TaskFlags,
using_run: bool,
) -> Result<i32, AnyError> { ) -> Result<i32, AnyError> {
let factory = CliFactory::from_flags(flags); let factory = CliFactory::from_flags(flags);
let cli_options = factory.cli_options()?; let cli_options = factory.cli_options()?;
let start_dir = &cli_options.start_dir; let start_dir = &cli_options.start_dir;
if !start_dir.has_deno_or_pkg_json() { if !start_dir.has_deno_or_pkg_json() {
bail!("deno task couldn't find deno.json(c). See https://docs.deno.com/go/config") if task_flags.is_run {
bail!(
r#"deno run couldn't find deno.json(c).
If you meant to run a script, specify it, e.g., `deno run ./script.ts`.
To run a task, ensure the config file exists.
Examples:
- Script: `deno run ./script.ts`
- Task: `deno run dev`
See https://docs.deno.com/go/config"#
)
} else {
bail!("deno task couldn't find deno.json(c). See https://docs.deno.com/go/config")
}
} }
let force_use_pkg_json = let force_use_pkg_json =
std::env::var_os(crate::task_runner::USE_PKG_JSON_HIDDEN_ENV_VAR_NAME) std::env::var_os(crate::task_runner::USE_PKG_JSON_HIDDEN_ENV_VAR_NAME)
@ -142,7 +153,7 @@ pub async fn execute_script(
} }
}, },
None => { None => {
if using_run { if task_flags.is_run {
return Err(anyhow!("Task not found: {}", task_name)); return Err(anyhow!("Task not found: {}", task_name));
} }
log::error!("Task not found: {}", task_name); log::error!("Task not found: {}", task_name);