Skip to content

Commit

Permalink
feat: Server development mode
Browse files Browse the repository at this point in the history
This adds a variant of the server command that will run the the
frontend's development watcher alongside the server. This accomplishes a
few things, namely:

- There is now a unified command for developing on both the frontend and
  the backend. This is needed as they become more coupled, such as for
  login
- The development server could now be passed environment variables from
  the backend (this is not happening, but could)
  • Loading branch information
justinrubek committed Jun 18, 2023
1 parent ca8bb33 commit d8cb304
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
23 changes: 19 additions & 4 deletions crates/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ impl DevCommand {

/// Watches all given paths for changes.
/// Changes are debounced, and gathered into a list of events
pub async fn async_debounce_watch<P: AsRef<Path>>(paths: Vec<P>) -> notify::Result<()> {
pub async fn async_debounce_watch<P: AsRef<Path>>(
paths: Vec<P>,
) -> Result<(), Box<dyn std::error::Error>> {
// Create an AsyncDebouncer with a timeout of 1 second and a tick rate of 1 second.
// Process it until the debouncer is dropped.
let (tx, mut rx) = tokio::sync::mpsc::channel(100);
Expand All @@ -58,12 +60,25 @@ pub async fn async_debounce_watch<P: AsRef<Path>>(paths: Vec<P>) -> notify::Resu
.unwrap();
});

// when the files change, we want to trigger a `yarn run build` in `./web`
let mut build_command: Option<tokio::process::Child> = None;

while let Some(event) = rx.recv().await {
match event {
Ok(events) => {
for event in events {
println!("event: {:?}", event);
Ok(_events) => {
// assuming that we will trigger on any event, so we are not checking the info

// if there is a running process, we must kill it first
if let Some(ref mut command) = build_command {
command.kill().await?;
}

let process = tokio::process::Command::new("yarn")
.args(["build"])
.current_dir("./web")
.spawn()?;

build_command = Some(process);
}
Err(errors) => {
for error in errors {
Expand Down
13 changes: 13 additions & 0 deletions crates/cli/src/commands/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub(crate) struct ServerCommand {
pub(crate) enum ServerCommands {
/// start the http server
Http,
/// passthrough to `yarn run dev`, while also starting the backend using [ServerCommand](super::server::ServerCommand)
Dev,
}

impl ServerCommand {
Expand All @@ -40,6 +42,17 @@ impl ServerCommand {

match self.command {
ServerCommands::Http => server.run().await?,
ServerCommands::Dev => {
// launch yarn
let _child = tokio::process::Command::new("yarn")
.args(["dev"])
.current_dir("./web")
.kill_on_drop(true)
.spawn()?;

// launch server
server.run().await?;
}
}

Ok(())
Expand Down

0 comments on commit d8cb304

Please sign in to comment.