Skip to content

Commit

Permalink
feat(cli): added support for watching custom files
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Jul 12, 2022
1 parent dab7e72 commit 723d4ca
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
34 changes: 28 additions & 6 deletions packages/perseus-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -94,10 +94,16 @@ async fn core(dir: PathBuf) -> Result<i32, Error> {
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
Expand Down Expand Up @@ -147,7 +153,12 @@ async fn core(dir: PathBuf) -> Result<i32, Error> {
// 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 {
Expand All @@ -156,6 +167,17 @@ async fn core(dir: PathBuf) -> Result<i32, Error> {
})?;
}
}
// 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
Expand Down
2 changes: 2 additions & 0 deletions packages/perseus-cli/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn deploy_full(dir: PathBuf, output: String) -> Result<i32, Error> {
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(),
Expand Down Expand Up @@ -131,6 +132,7 @@ fn deploy_export(dir: PathBuf, output: String) -> Result<i32, Error> {
host: String::new(),
port: 0,
watch: false,
custom_watch: Vec::new(),
},
)?;
if export_exit_code != 0 {
Expand Down
12 changes: 10 additions & 2 deletions packages/perseus-cli/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}
/// Exports an error page for the given HTTP status code
#[derive(Parser, Clone)]
Expand Down Expand Up @@ -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<String>,
/// Where to host your exported app
#[clap(long, default_value = "127.0.0.1")]
pub host: String,
Expand Down

0 comments on commit 723d4ca

Please sign in to comment.