mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
feat: add --open
to deno serve
to open server in browser (#25340)
Change: Supported --open flag with deno serve -> (deno serve --open somescript.ts/js). The action that takes place is openning the browser on the address that the server is running on. Signed-off-by: HasanAlrimawi <141642411+HasanAlrimawi@users.noreply.github.com>
This commit is contained in:
parent
ad0ebb57ee
commit
e028e26b9c
2 changed files with 53 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
// Copyright 2018-2025 the Deno authors. MIT license.
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use deno_core::error::AnyError;
|
||||
|
@ -46,6 +47,12 @@ pub async fn serve(
|
|||
|
||||
let worker_factory =
|
||||
Arc::new(factory.create_cli_main_worker_factory().await?);
|
||||
|
||||
if serve_flags.open_site {
|
||||
let url = resolve_serve_url(serve_flags.host, serve_flags.port);
|
||||
let _ = open::that_detached(url);
|
||||
}
|
||||
|
||||
let hmr = serve_flags
|
||||
.watch
|
||||
.map(|watch_flags| watch_flags.hmr)
|
||||
|
@ -180,3 +187,40 @@ async fn serve_with_watch(
|
|||
.await?;
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn resolve_serve_url(host: String, port: u16) -> String {
|
||||
let host = if matches!(host.as_str(), "0.0.0.0" | "::") {
|
||||
"127.0.0.1".to_string()
|
||||
} else if std::net::Ipv6Addr::from_str(&host).is_ok() {
|
||||
format!("[{}]", host)
|
||||
} else {
|
||||
host
|
||||
};
|
||||
if port == 80 {
|
||||
format!("http://{host}/")
|
||||
} else {
|
||||
format!("http://{host}:{port}/")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_resolve_serve_url() {
|
||||
assert_eq!(
|
||||
resolve_serve_url("localhost".to_string(), 80),
|
||||
"http://localhost/"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_serve_url("0.0.0.0".to_string(), 80),
|
||||
"http://127.0.0.1/"
|
||||
);
|
||||
assert_eq!(resolve_serve_url("::".to_string(), 80), "http://127.0.0.1/");
|
||||
assert_eq!(
|
||||
resolve_serve_url("::".to_string(), 90),
|
||||
"http://127.0.0.1:90/"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue