Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Add the ability to preview without opening the browser #816

Merged
merged 8 commits into from
Nov 5, 2019
15 changes: 10 additions & 5 deletions src/commands/preview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn preview(
body: Option<String>,
livereload: bool,
verbose: bool,
headless: bool,
) -> Result<(), failure::Error> {
commands::build(&target)?;

Expand All @@ -53,10 +54,12 @@ pub fn preview(

info!("Opened websocket server on port {}", ws_port);

open_browser(&format!(
if !headless {
open_browser(&format!(
"https://cloudflareworkers.com/?wrangler_session_id={0}&wrangler_ws_port={1}&hide_editor#{2}:{3}{4}",
&session.to_string(), ws_port, script_id, https_str, preview_host,
))?;
}

//don't do initial GET + POST with livereload as the expected behavior is unclear.

Expand All @@ -70,10 +73,12 @@ pub fn preview(
verbose,
)?;
} else {
open_browser(&format!(
"https://cloudflareworkers.com/?hide_editor#{0}:{1}{2}",
script_id, https_str, preview_host
))?;
if !headless {
open_browser(&format!(
"https://cloudflareworkers.com/?hide_editor#{0}:{1}{2}",
script_id, https_str, preview_host
))?;
}

let cookie = format!(
"__ew_fiddle_preview={}{}{}{}",
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ fn run() -> Result<(), failure::Error> {
"{} Preview your code temporarily on cloudflareworkers.com",
emoji::MICROSCOPE
))
.arg(
Arg::with_name("headless")
.help("Don't open the browser on preview")
.long("headless")
.takes_value(false)
)
.arg(
Arg::with_name("method")
.help("Type of request to preview your worker with (get, post)")
Expand Down Expand Up @@ -506,8 +512,9 @@ fn run() -> Result<(), failure::Error> {

let watch = matches.is_present("watch");
let verbose = matches.is_present("verbose");
let headless = matches.is_present("headless");

commands::preview(target, user, method, body, watch, verbose)?;
commands::preview(target, user, method, body, watch, verbose, headless)?;
} else if matches.subcommand_matches("whoami").is_some() {
log::info!("Getting User settings");
let user = settings::global_user::GlobalUser::new()?;
Expand Down
2 changes: 1 addition & 1 deletion tests/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ fn preview(fixture: &str) {

let mut preview = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
preview.current_dir(utils::fixture_path(fixture));
preview.arg("preview").assert().success();
preview.arg("preview").arg("--headless").assert().success();
}