From 690f6e996370860df6edc6e8aa94b4d27d3e455c Mon Sep 17 00:00:00 2001 From: HasanAlrimawi Date: Sun, 1 Sep 2024 14:48:34 +0300 Subject: [PATCH] feat: add --open to deno serve to open server in browser --- cli/args/flags.rs | 7 +++++-- cli/tools/serve.rs | 21 +++++++++++++++++++ .../serve/open_browser_tab/__test__.jsonc | 5 +++++ tests/specs/serve/open_browser_tab/main.out | 2 ++ tests/specs/serve/open_browser_tab/main.ts | 5 +++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/specs/serve/open_browser_tab/__test__.jsonc create mode 100644 tests/specs/serve/open_browser_tab/main.out create mode 100644 tests/specs/serve/open_browser_tab/main.ts diff --git a/cli/args/flags.rs b/cli/args/flags.rs index ca8a0a82fa..9bd0419351 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -343,6 +343,7 @@ pub struct ServeFlags { pub port: u16, pub host: String, pub worker_count: Option, + pub open_site: bool, } impl ServeFlags { @@ -354,6 +355,7 @@ impl ServeFlags { port, host: host.to_owned(), worker_count: None, + open_site: false, } } } @@ -2787,7 +2789,7 @@ fn serve_subcommand() -> Command { .long("host") .help("The TCP address to serve on, defaulting to 0.0.0.0 (all interfaces)") .value_parser(serve_host_validator), - ) + ).arg(Arg::new("open_site").long("open").help("Open the browser on the address that the server is running on.").action(ArgAction::SetTrue)) .arg( parallel_arg("multiple server workers", false) ) @@ -4732,7 +4734,7 @@ fn serve_parse( let host = matches .remove_one::("host") .unwrap_or_else(|| "0.0.0.0".to_owned()); - + let open_site = matches.remove_one::("open_site").unwrap_or(false); let worker_count = parallel_arg_parse(matches, false).map(|v| v.get()); runtime_args_parse(flags, matches, true, true); @@ -4775,6 +4777,7 @@ fn serve_parse( port, host, worker_count, + open_site, }); Ok(()) diff --git a/cli/tools/serve.rs b/cli/tools/serve.rs index 24666b8f64..2e89582bce 100644 --- a/cli/tools/serve.rs +++ b/cli/tools/serve.rs @@ -50,6 +50,27 @@ pub async fn serve( )?); let worker_factory = factory.create_cli_main_worker_factory().await?; + if serve_flags.open_site { + let host: String; + if serve_flags.host == "0.0.0.0" || serve_flags.host == "127.0.0.1" { + host = "http://127.0.0.1".to_string(); + } else if serve_flags.host == "localhost" { + host = "http://localhost".to_string(); + } else { + host = format!("https://{}", serve_flags.host); + } + let port = serve_flags.port; + let browser_tab_open_result = open::that_detached(format!("{host}:{port}")); + if browser_tab_open_result.is_ok() { + log::info!( + "{}: Opened the browser on the address that the server is running on", + crate::colors::green("deno serve") + ); + } else { + log::info!("{}: Couldn't open the browser on the address that the server is running on", crate::colors::red("deno serve")); + } + } + do_serve( worker_factory, main_module, diff --git a/tests/specs/serve/open_browser_tab/__test__.jsonc b/tests/specs/serve/open_browser_tab/__test__.jsonc new file mode 100644 index 0000000000..ddad989bb8 --- /dev/null +++ b/tests/specs/serve/open_browser_tab/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "serve --open main.ts", + "output": "main.out", + "tempDir": true +} diff --git a/tests/specs/serve/open_browser_tab/main.out b/tests/specs/serve/open_browser_tab/main.out new file mode 100644 index 0000000000..f1cd3fa5ad --- /dev/null +++ b/tests/specs/serve/open_browser_tab/main.out @@ -0,0 +1,2 @@ +deno serve: Opened the browser on the address that the server is running on +deno serve: Listening on http://localhost:8000/ diff --git a/tests/specs/serve/open_browser_tab/main.ts b/tests/specs/serve/open_browser_tab/main.ts new file mode 100644 index 0000000000..a0c856a66a --- /dev/null +++ b/tests/specs/serve/open_browser_tab/main.ts @@ -0,0 +1,5 @@ +export default { + async fetch() { + return new Response("Hello world!"); + }, +};