mirror of
https://github.com/denoland/deno.git
synced 2025-01-09 07:39:15 -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 port: u16,
|
||||||
pub host: String,
|
pub host: String,
|
||||||
pub worker_count: Option<usize>,
|
pub worker_count: Option<usize>,
|
||||||
|
pub open_site: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServeFlags {
|
impl ServeFlags {
|
||||||
|
@ -354,6 +355,7 @@ impl ServeFlags {
|
||||||
port,
|
port,
|
||||||
host: host.to_owned(),
|
host: host.to_owned(),
|
||||||
worker_count: None,
|
worker_count: None,
|
||||||
|
open_site: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2787,7 +2789,7 @@ fn serve_subcommand() -> Command {
|
||||||
.long("host")
|
.long("host")
|
||||||
.help("The TCP address to serve on, defaulting to 0.0.0.0 (all interfaces)")
|
.help("The TCP address to serve on, defaulting to 0.0.0.0 (all interfaces)")
|
||||||
.value_parser(serve_host_validator),
|
.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(
|
.arg(
|
||||||
parallel_arg("multiple server workers", false)
|
parallel_arg("multiple server workers", false)
|
||||||
)
|
)
|
||||||
|
@ -4732,7 +4734,7 @@ fn serve_parse(
|
||||||
let host = matches
|
let host = matches
|
||||||
.remove_one::<String>("host")
|
.remove_one::<String>("host")
|
||||||
.unwrap_or_else(|| "0.0.0.0".to_owned());
|
.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());
|
let worker_count = parallel_arg_parse(matches, false).map(|v| v.get());
|
||||||
|
|
||||||
runtime_args_parse(flags, matches, true, true);
|
runtime_args_parse(flags, matches, true, true);
|
||||||
|
@ -4775,6 +4777,7 @@ fn serve_parse(
|
||||||
port,
|
port,
|
||||||
host,
|
host,
|
||||||
worker_count,
|
worker_count,
|
||||||
|
open_site,
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -50,6 +50,27 @@ pub async fn serve(
|
||||||
)?);
|
)?);
|
||||||
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
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(
|
do_serve(
|
||||||
worker_factory,
|
worker_factory,
|
||||||
main_module,
|
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