-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add --open to deno serve to open server in browser #25340
base: main
Are you sure you want to change the base?
Changes from 10 commits
690f6e9
fb4d2af
e8de382
38c0d9d
e1b52c4
1ffac2f
3ab2371
a2e8370
fa17baf
8e63dea
14ff348
4f005ca
5f2862b
2ca1a7d
bb1a0b8
47c0635
be1b6f6
cc734a8
7dcb560
449f1a7
d787db9
c64de5b
a45fdeb
655a2a0
5361049
69be52c
3ff08ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,10 +44,33 @@ pub async fn serve( | |
maybe_npm_install(&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")); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if it worth adding these logs here, does Vite or other tools do it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked Vite, it doesn't log on success, but logs "e.g. (Permission denied)" on failure. |
||
} | ||
|
||
let hmr = serve_flags | ||
.watch | ||
.map(|watch_flags| watch_flags.hmr) | ||
.unwrap_or(false); | ||
|
||
do_serve( | ||
worker_factory, | ||
main_module.clone(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,6 +171,43 @@ impl ServeClient { | |
|
||
return self.endpoint.borrow().clone().unwrap(); | ||
} | ||
|
||
// Check if a new browser tab has been opened by checking | ||
// stderr includes the target output expected | ||
fn check_tab_opened(&self) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this test makes sense - on CI there will be no browser to open, while locally people will suddenly get some new tabs. I'd suggest to not add it at all. |
||
let mut buffer = self.output_buf.borrow_mut(); | ||
let mut temp_buf = [0u8; 64]; | ||
let mut child = self.child.borrow_mut(); | ||
let stderr = child.stderr.as_mut().unwrap(); | ||
|
||
let start = std::time::Instant::now(); | ||
|
||
// Loop to check for "hello" in the output | ||
loop { | ||
if start.elapsed() > Duration::from_secs(5) { | ||
panic!("timed out waiting for the browser tab to be opened."); | ||
} | ||
|
||
let read = stderr.read(&mut temp_buf).unwrap(); | ||
buffer.extend_from_slice(&temp_buf[..read]); | ||
|
||
let target_output = | ||
"browser on the address that the server is running on".as_bytes(); | ||
let target_index = buffer | ||
.windows(target_output.len()) | ||
.position(|window| window == target_output); | ||
match target_index { | ||
Some(_) => { | ||
break; | ||
} | ||
None => | ||
// Sleep for a short duration to avoid busy-waiting | ||
{ | ||
std::thread::sleep(Duration::from_millis(5)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
|
@@ -198,6 +235,35 @@ async fn deno_serve_no_args() { | |
assert_eq!(body, "deno serve with no args in fetch() works!"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn deno_serve_open_browser() { | ||
let client = ServeClientBuilder( | ||
util::deno_cmd() | ||
.current_dir(util::testdata_path()) | ||
.arg("serve") | ||
.arg("--open") | ||
.arg("-L") | ||
.arg("debug") | ||
.arg("-A") | ||
.arg("--port") | ||
.arg("0") | ||
.arg("./serve/no_args.ts") | ||
.stdout_piped() | ||
.stderr_piped(), | ||
None, | ||
); | ||
|
||
let serve_client = client.entry_point("./serve/no_args.ts").build(); | ||
serve_client.check_tab_opened(); | ||
let result_unwrapped = serve_client.get().send().await; | ||
let res = result_unwrapped.unwrap(); | ||
assert_eq!(200, res.status()); | ||
|
||
let body = res.text().await.unwrap(); | ||
assert_eq!(body, "deno serve with no args in fetch() works!"); | ||
serve_client.kill(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn deno_serve_parallel() { | ||
let client = ServeClient::builder() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you run
tools/format.js
before pushing? This seems not formattedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes I did, and did it again now, still the same.