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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ If you would like to be able to publish your code to multiple places, please see

Additionally, you can preview different environments. This is useful if you have different builds for different environments (like staging vs. production), but typically isn't needed. For more information see the [environments documentation](https://github.com/cloudflare/wrangler/blob/master/docs/content/environments.md).

<!-- TODO: Document changes to wrangler preview -->

- ### 🗂️ `kv`

Interact with your Cloudflare Workers KV store. [Check out the docs.](./docs/content/kv_commands.md)
Expand Down
15 changes: 10 additions & 5 deletions src/commands/publish/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,
browser: bool,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the flag is "terminal" but this attribute says browser... might be more maintainable if they are aligned- what do you think?

i sorta think that it might be nice to have the flag be browser instead of terminal... backwards compat has us a bit stuck though. gonna think on this- but would love to hear your thoughts!

Copy link
Contributor Author

@EverlastingBugstopper EverlastingBugstopper Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference would be that the default opens the browser and that the flag is browser. The reason I implemented it this way is because of the way things are today. That being said, I don't think we really need to worry too much about backwards compatibility wrt preview, but I could be wrong. As for the reason the flag is terminal and the preview function takes browser - it makes more sense semantically (to me) to test for browser == true than for terminal == false when trying to open the browser.

Copy link
Contributor

@ashleymichal ashleymichal Nov 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be configurable via another method as well? there's the whole idea that if there's a flag for it, there should be a config setting for it, or that it should accept an environment variable.

Copy link
Contributor

@ashleymichal ashleymichal Nov 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, perhaps it isn't a no arg flag, but a bool value flag, like --browser=true, with a default?

) -> Result<(), failure::Error> {
let sites_preview: bool = target.site.is_some();

Expand All @@ -51,10 +52,12 @@ pub fn preview(

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

open_browser(&format!(
if browser {
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 @@ -68,10 +71,12 @@ pub fn preview(
verbose,
)?;
} else {
open_browser(&format!(
"https://cloudflareworkers.com/?hide_editor#{0}:{1}{2}",
script_id, https_str, preview_host
))?;
if browser {
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
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ fn run() -> Result<(), failure::Error> {
"{} Preview your code temporarily on cloudflareworkers.com",
emoji::MICROSCOPE
))
.arg(
Arg::with_name("terminal")
.help("Preview your worker in the terminal and not the browser")
.short("t")
.long("terminal")
.takes_value(false)
)
.arg(
Arg::with_name("method")
.help("Type of request to preview your worker with (get, post)")
Expand Down Expand Up @@ -471,8 +478,9 @@ fn run() -> Result<(), failure::Error> {

let watch = matches.is_present("watch");
let verbose = matches.is_present("verbose");
let browser = !matches.is_present("terminal");

commands::preview(target, user, method, body, watch, verbose)?;
commands::preview(target, user, method, body, watch, verbose, browser)?;
} 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("-t").assert().success();
}