This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Xortive malonso/hotreload + windows ampersand escape fix (#451)
* changed to use old script id instead of sessions watch_paths added to wrangler.toml * added watch option to wranglerjs * hotreload demo some refactoring/code cleanup work added session id to message * add support for running wranglerjs in watch mode, utilizing webpack's watcher * check for debug flag to use localhost for preview server * remove watch module + refs * added deps * please the borrow checker * cleanup * fix temp file * update querystring params, use sessions instead of old_id/new_id * remove wranglerjs console logs * finalize communication with the fiddle, add port + session support * move watcher inside thread closure to have the correct lifetime * s/build_and_watch/watch_and_build, put watch_and_build inside its own module * add cooldown period for fs events * don't swallow tx errors from watch_and_build * fix wrangler build --watch * made messaging use message crate, removed some println! * cargo fmt * Add message when ignoring stale first change * Check origin header and close if invalid * separate out fiddle message server into its own module * fixed debug save_origins, cargo_fmt, added safe_origins log message * updated preview url * fix origins * don't use hotreload host for normal open command * remove allowing connections from msg is is confusing * cargo fmt * fix origin check and add localhost check * better errors * add build_and_watch * webpack watching works -- code is littered with debug statements and leaves threads open, need to fix that * add guardedcommand for running wranglerjs in --watch while having it die with the main thread * cargo fmt * DRY open browser function * please clippy * namespace query params * cargo fmt * use info@ for fiddle messenger logs * better log messages for wranglerjs + use info@ for watching temp file message as to not confuse the user * fix double build bug * cleanup fiddle messenger: use ws::Result instead of alias * Keep trying port+1 so you can have multiple previews open * also watch entry point for rust * better update message * update preview service url * Use strict equality check in wranglerjs/index.js Co-Authored-By: Sven Sauleau <[email protected]> * Hide editor on wrangler preview without watch * Better messaging for preview update message * use NonRecursive watcher for watch paths that always point to files * bind to port 0 to allow the OS to assign us a port, instead of using our own logic for picking a port * cargo fmt * Make open_browser more robust * cargo fmt * disable wrangler build --watch for now * PR feedback: namespacing, command matching, remove unnecessary unwraps * fix(preview): escape & in windows URL * fix(style): appease cargo fmt * fix(clippy): unecessary if let to is_some * fix(watch): recurisve plz k thx * fix(depr): ... -> ..= * fix(preview): one last recursive
- Loading branch information
1 parent
3acc885
commit fee35f2
Showing
15 changed files
with
856 additions
and
253 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
mod watcher; | ||
pub use watcher::wait_for_changes; | ||
|
||
use crate::commands::build::{command, wranglerjs}; | ||
use crate::commands::publish::Package; | ||
use crate::settings::project::{Project, ProjectType}; | ||
use crate::terminal::message; | ||
use crate::{commands, install}; | ||
|
||
use notify::{self, RecursiveMode, Watcher}; | ||
use std::env; | ||
use std::sync::mpsc; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
pub const COOLDOWN_PERIOD: Duration = Duration::from_millis(2000); | ||
|
||
/// watch a project for changes and re-build it when necessary, | ||
/// outputting a build event to tx. | ||
pub fn watch_and_build( | ||
project: &Project, | ||
tx: Option<mpsc::Sender<()>>, | ||
) -> Result<(), failure::Error> { | ||
let project_type = &project.project_type; | ||
match project_type { | ||
ProjectType::JavaScript => { | ||
let package = Package::new("./")?; | ||
let entry = package.main()?; | ||
thread::spawn(move || { | ||
let (watcher_tx, watcher_rx) = mpsc::channel(); | ||
let mut watcher = notify::watcher(watcher_tx, Duration::from_secs(1)).unwrap(); | ||
|
||
watcher.watch(&entry, RecursiveMode::Recursive).unwrap(); | ||
message::info(&format!("watching {:?}", &entry)); | ||
|
||
loop { | ||
match wait_for_changes(&watcher_rx, COOLDOWN_PERIOD) { | ||
Ok(_path) => { | ||
if let Some(tx) = tx.clone() { | ||
tx.send(()).expect("--watch change message failed to send"); | ||
} | ||
} | ||
Err(_) => message::user_error("Something went wrong while watching."), | ||
} | ||
} | ||
}); | ||
} | ||
ProjectType::Rust => { | ||
let tool_name = "wasm-pack"; | ||
let binary_path = install::install(tool_name, "rustwasm")?.binary(tool_name)?; | ||
let args = ["build", "--target", "no-modules"]; | ||
|
||
let package = Package::new("./")?; | ||
let entry = package.main()?; | ||
|
||
thread::spawn(move || { | ||
let (watcher_tx, watcher_rx) = mpsc::channel(); | ||
let mut watcher = notify::watcher(watcher_tx, Duration::from_secs(1)).unwrap(); | ||
|
||
let mut path = env::current_dir().expect("current dir"); | ||
path.push("src"); | ||
|
||
watcher.watch(&path, RecursiveMode::Recursive).unwrap(); | ||
watcher.watch(&entry, RecursiveMode::Recursive).unwrap(); | ||
message::info(&format!("watching {:?} and {:?}", &path, &entry)); | ||
|
||
loop { | ||
match wait_for_changes(&watcher_rx, COOLDOWN_PERIOD) { | ||
Ok(_path) => { | ||
let command = command(&args, &binary_path); | ||
let command_name = format!("{:?}", command); | ||
if commands::run(command, &command_name).is_ok() { | ||
if let Some(tx) = tx.clone() { | ||
tx.send(()).expect("--watch change message failed to send"); | ||
} | ||
} | ||
} | ||
Err(_) => message::user_error("Something went wrong while watching."), | ||
} | ||
} | ||
}); | ||
} | ||
ProjectType::Webpack => { | ||
wranglerjs::run_build_and_watch(project, tx)?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use notify::DebouncedEvent; | ||
use std::path::PathBuf; | ||
use std::sync::mpsc::Receiver; | ||
use std::time::Duration; | ||
|
||
use failure::{format_err, Error}; | ||
|
||
use crate::terminal::message; | ||
use log::info; | ||
|
||
///Add cooldown for all types of events to watching logic | ||
pub fn wait_for_changes( | ||
rx: &Receiver<DebouncedEvent>, | ||
cooldown: Duration, | ||
) -> Result<PathBuf, Error> { | ||
loop { | ||
let event = rx.recv()?; | ||
match get_changed_path_from_event(event) { | ||
Ok(Some(path)) => { | ||
message::working("Detected changes..."); | ||
//wait for cooldown | ||
while let Ok(_) = rx.recv_timeout(cooldown) { | ||
message::working("Detected change during cooldown..."); | ||
} | ||
message::working("Cooldown over, propogating changes..."); | ||
return Ok(path); | ||
} | ||
Ok(None) => { | ||
continue; //was an event type we don't care about, continue | ||
} | ||
Err(error) => { | ||
message::user_error(&format!("WatchError {:?}", error)); | ||
continue; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
fn get_changed_path_from_event(event: DebouncedEvent) -> Result<Option<PathBuf>, Error> { | ||
info!("Detected Event {:?}", event); | ||
match event { | ||
DebouncedEvent::Error(error, _) => Err(format_err!("{:?}", error)), | ||
DebouncedEvent::NoticeWrite(path) => Ok(Some(path)), | ||
DebouncedEvent::Write(path) => Ok(Some(path)), | ||
DebouncedEvent::NoticeRemove(path) => Ok(Some(path)), | ||
DebouncedEvent::Remove(path) => Ok(Some(path)), | ||
DebouncedEvent::Create(path) => Ok(Some(path)), | ||
_ => Ok(None), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.