mirror of
https://github.com/denoland/deno.git
synced 2025-01-08 23:28:18 -05:00
feat: add --open to deno serve to open server in browser
This commit is contained in:
parent
f7556d8962
commit
690f6e9963
5 changed files with 38 additions and 2 deletions
|
@ -343,6 +343,7 @@ pub struct ServeFlags {
|
|||
pub port: u16,
|
||||
pub host: String,
|
||||
pub worker_count: Option<usize>,
|
||||
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::<String>("host")
|
||||
.unwrap_or_else(|| "0.0.0.0".to_owned());
|
||||
|
||||
let open_site = matches.remove_one::<bool>("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(())
|
||||
|
|
|
@ -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,
|
||||
|
|
5
tests/specs/serve/open_browser_tab/__test__.jsonc
Normal file
5
tests/specs/serve/open_browser_tab/__test__.jsonc
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"args": "serve --open main.ts",
|
||||
"output": "main.out",
|
||||
"tempDir": true
|
||||
}
|
2
tests/specs/serve/open_browser_tab/main.out
Normal file
2
tests/specs/serve/open_browser_tab/main.out
Normal file
|
@ -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/
|
5
tests/specs/serve/open_browser_tab/main.ts
Normal file
5
tests/specs/serve/open_browser_tab/main.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
export default {
|
||||
async fetch() {
|
||||
return new Response("Hello world!");
|
||||
},
|
||||
};
|
Loading…
Reference in a new issue