From 723d4cae0659f4f02e2e7affc957df065b156373 Mon Sep 17 00:00:00 2001 From: arctic_hen7 Date: Tue, 12 Jul 2022 15:17:07 +1000 Subject: [PATCH] feat(cli): added support for watching custom files --- packages/perseus-cli/src/bin/main.rs | 34 +++++++++++++++++++++++----- packages/perseus-cli/src/deploy.rs | 2 ++ packages/perseus-cli/src/parse.rs | 12 ++++++++-- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/packages/perseus-cli/src/bin/main.rs b/packages/perseus-cli/src/bin/main.rs index 4658331d87..b22fdaf9da 100644 --- a/packages/perseus-cli/src/bin/main.rs +++ b/packages/perseus-cli/src/bin/main.rs @@ -14,7 +14,7 @@ use perseus_cli::{ }; use std::env; use std::io::Write; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::Command; use std::sync::mpsc::channel; @@ -94,10 +94,16 @@ async fn core(dir: PathBuf) -> Result { let watch_allowed = env::var("PERSEUS_WATCHING_PROHIBITED").is_err(); // Check if the user wants to watch for changes match &opts.subcmd { - Subcommand::Export(ExportOpts { watch, .. }) - | Subcommand::Serve(ServeOpts { watch, .. }) - if *watch && watch_allowed => - { + Subcommand::Export(ExportOpts { + watch, + custom_watch, + .. + }) + | Subcommand::Serve(ServeOpts { + watch, + custom_watch, + .. + }) if *watch && watch_allowed => { let (tx_term, rx) = channel(); let tx_fs = tx_term.clone(); // Set the handler for termination events (more than just SIGINT) on all @@ -147,7 +153,12 @@ async fn core(dir: PathBuf) -> Result { // We want to exclude `target/` and `dist`, otherwise we should watch everything let entry = entry.map_err(|err| WatchError::ReadDirEntryFailed { source: err })?; let name = entry.file_name(); - if name != "target" && name != "dist" && name != ".git" { + if name != "target" + && name != "dist" + && name != ".git" + && name != "target_engine" + && name != "target_wasm" + { watcher .watch(&entry.path(), RecursiveMode::Recursive) .map_err(|err| WatchError::WatchFileFailed { @@ -156,6 +167,17 @@ async fn core(dir: PathBuf) -> Result { })?; } } + // Watch any other files/directories the user has nominated + for entry in custom_watch.iter() { + watcher + // If it's a directory, we'll watch it recursively + // If it's a file, the second parameter here is usefully ignored + .watch(Path::new(entry), RecursiveMode::Recursive) + .map_err(|err| WatchError::WatchFileFailed { + filename: entry.to_string(), + source: err, + })?; + } // This will store the handle to the child process // This will be updated every time we re-create the process diff --git a/packages/perseus-cli/src/deploy.rs b/packages/perseus-cli/src/deploy.rs index 343d9b1fda..623fe9e660 100644 --- a/packages/perseus-cli/src/deploy.rs +++ b/packages/perseus-cli/src/deploy.rs @@ -36,6 +36,7 @@ fn deploy_full(dir: PathBuf, output: String) -> Result { release: true, standalone: true, watch: false, + custom_watch: Vec::new(), // These have no impact if `no_run` is `true` (which it is), so we can use the defaults // here host: "127.0.0.1".to_string(), @@ -131,6 +132,7 @@ fn deploy_export(dir: PathBuf, output: String) -> Result { host: String::new(), port: 0, watch: false, + custom_watch: Vec::new(), }, )?; if export_exit_code != 0 { diff --git a/packages/perseus-cli/src/parse.rs b/packages/perseus-cli/src/parse.rs index 3fc25d562f..043f992500 100644 --- a/packages/perseus-cli/src/parse.rs +++ b/packages/perseus-cli/src/parse.rs @@ -56,9 +56,13 @@ pub struct ExportOpts { #[clap(long, default_value = "8080")] pub port: u16, /// Watch the files in your working directory for changes (exluding - /// `target/` and `.perseus/`) + /// `target/` and `dist/`) #[clap(short, long)] pub watch: bool, + /// Marks a specific file/directory to be watched (directories will be + /// recursively watched) + #[clap(long)] + pub custom_watch: Vec, } /// Exports an error page for the given HTTP status code #[derive(Parser, Clone)] @@ -87,9 +91,13 @@ pub struct ServeOpts { #[clap(long)] pub standalone: bool, /// Watch the files in your working directory for changes (exluding - /// `target/` and `.perseus/`) + /// `target/` and `dist/`) #[clap(short, long)] pub watch: bool, + /// Marks a specific file/directory to be watched (directories will be + /// recursively watched) + #[clap(long)] + pub custom_watch: Vec, /// Where to host your exported app #[clap(long, default_value = "127.0.0.1")] pub host: String,