Skip to content

Commit a08068d

Browse files
author
Roland Peelen
committed
Basic naive watcher
1 parent b903723 commit a08068d

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/helpers.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ffi::OsString;
12
use std::fs;
23
use std::path::Component;
34
use std::path::Path;
@@ -119,3 +120,13 @@ pub fn create_build_path(build_path: &str) {
119120
pub fn get_bsc(root_path: &str) -> String {
120121
get_node_modules_path(root_path) + "/rescript/darwinarm64/bsc.exe"
121122
}
123+
124+
pub fn string_ends_with_any(s: &PathBuf, suffixes: &[&str]) -> bool {
125+
suffixes.iter().any(|&suffix| {
126+
s.extension()
127+
.unwrap_or(&OsString::new())
128+
.to_str()
129+
.unwrap_or("")
130+
== suffix
131+
})
132+
}

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ fn main() {
1717
"build" => {
1818
build::build(&folder);
1919
}
20+
"watch" => {
21+
let _modules = build::build(&folder);
22+
watcher::start(&folder);
2023
}
2124
_ => println!("Not a valid build command"),
2225
}

src/watcher.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use crate::build;
2+
use crate::helpers;
13
use futures::{
24
channel::mpsc::{channel, Receiver},
35
SinkExt, StreamExt,
46
};
57
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
68
use std::path::Path;
9+
pub static FILE_TYPES: &[&str] = &["re", "res", "ml", "rei", "resi", "mli"];
710

811
fn async_watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Result<Event>>)> {
912
let (mut tx, rx) = channel(1);
@@ -22,17 +25,32 @@ fn async_watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Resul
2225
Ok((watcher, rx))
2326
}
2427

25-
async fn async_watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {
28+
async fn async_watch(path: String) -> notify::Result<()> {
2629
let (mut watcher, mut rx) = async_watcher()?;
2730

2831
// Add a path to be watched. All files and directories at that path and
2932
// below will be monitored for changes.
3033
watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
3134

3235
while let Some(res) = rx.next().await {
33-
match res {
34-
Ok(event) => println!("changed: {:?}", event),
35-
Err(e) => println!("watch error: {:?}", e),
36+
let files_to_compile = res
37+
.iter()
38+
.map(|event| {
39+
event
40+
.paths
41+
.iter()
42+
.map(|path| path.to_path_buf())
43+
.filter(|path| helpers::string_ends_with_any(path, FILE_TYPES))
44+
.collect::<Vec<PathBuf>>()
45+
})
46+
.flatten()
47+
.into_iter()
48+
.collect::<Vec<PathBuf>>();
49+
50+
let delay = Duration::from_millis(10);
51+
if files_to_compile.len() > 0 {
52+
thread::sleep(delay);
53+
build::build(&path);
3654
}
3755
}
3856

@@ -41,7 +59,7 @@ async fn async_watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {
4159

4260
pub fn start(folder: &str) {
4361
futures::executor::block_on(async {
44-
if let Err(e) = async_watch(folder).await {
62+
if let Err(e) = async_watch(folder.to_string()).await {
4563
println!("error: {:?}", e)
4664
}
4765
});

0 commit comments

Comments
 (0)