Skip to content

Commit

Permalink
Merge pull request #8 from kennedymwavu/5-bug-in-ext
Browse files Browse the repository at this point in the history
fix: match only files ending in `ext`
  • Loading branch information
kennedymwavu authored Aug 16, 2024
2 parents cba3f48 + 70a56b9 commit 4f3601a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
20 changes: 7 additions & 13 deletions R/monitor.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#' @param file String, file path. Path to the R script to rerun when changes
#' are detected.
#' @param ext Character vector. File extensions to watch.
#' Defaults to `c(".R", ".r")` files.
#' "*" (the default) watches all files in `dir`.
#' @param monitor_hidden Logical. Should hidden files be monitored for changes?
#' Default is `FALSE`.
#' Default is `TRUE`.
#' Hidden files are those whose names start with a dot eg. `.Renviron`, `.env`,
#' etc.
#' This option is especially helpful when `ext = "*"`.
Expand Down Expand Up @@ -43,8 +43,6 @@
#' rmon::monitor(
#' dir = ".",
#' file = "app.R",
#' ext = "*",
#' monitor_hidden = TRUE,
#' exclude_files = "dev.R",
#' exclude_dirs = "test"
#' )
Expand All @@ -61,18 +59,20 @@
monitor <- function(
dir = getwd(),
file,
ext = c(".R", ".r"),
monitor_hidden = FALSE,
ext = "*",
monitor_hidden = TRUE,
exclude_files = NULL,
exclude_patterns = NULL,
exclude_dirs = NULL,
delay = 1) {
file <- normalizePath(path = file.path(dir[[1]], file))
file <- normalizePath(path = file.path(dir[[1]], file[[1]]))
if (!file.exists(file)) {
msg <- sprintf("File '%s' not found!", file)
stop(msg, call. = FALSE)
}

patterns <- paste0(ext, "$", collapse = "|")

now <- function() {
format(Sys.time(), "%c")
}
Expand All @@ -88,12 +88,6 @@ monitor <- function(
)

get_file_info <- function() {
patterns <- paste0(
"\\.",
gsub(pattern = "\\.", replacement = "", x = ext),
collapse = "|"
)

files <- list.files(
path = dir,
pattern = patterns,
Expand Down
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@ rmon::monitor(

```r
rmon::monitor(
dir = c(".", "path-to-another-dir"),
dir = c("path-to-first-dir", "path-to-another-dir"),
file = "app.R"
)
```

if multiple directories are specified, `file` is assumed to be in the first
directory.

# specify extension watch list

by default, `{rmon}` monitors only `.R` files in `dir` for changes.
by default, `{rmon}` monitors all files in `dir` for changes.

to watch `.R`, `.html`, `.css` and `.js` files:
to watch only `.R`, `.html`, `.css` and `.js` files, set the `ext` parameter:

```r
rmon::monitor(
Expand All @@ -57,10 +60,6 @@ rmon::monitor(
)
```

- `ext`: character vector. extensions to watch.

specifying `ext = "*"` watches all files in `dir` for changes.

# ignoring files

to ignore the file `dev.R`, do:
Expand All @@ -73,31 +72,36 @@ rmon::monitor(
)
```

- `exclude_files`: character vector

to ignore the directory `test/` as well:
to ignore the directory `test/`:

```r
rmon::monitor(
dir = ".",
file = "app.R",
exclude_files = "dev.R"
exclude_dirs = "test"
)
```

to ignore all files whose names match the pattern `test` as well:
to ignore all files whose names match the pattern `test`:

```r
rmon::monitor(
dir = ".",
file = "app.R",
exclude_files = "dev.R"
exclude_dirs = c("test"),
exclude_patterns = "test"
)
```

to ignore changes to hidden files, set `monitor_hidden = FALSE`:

```r
rmon::monitor(
dir = ".",
file = "app.R",
monitor_hidden = FALSE
)
```

# delaying restarting

in some situations, you may want to delay restarting until multiple files have changed.
Expand Down
10 changes: 4 additions & 6 deletions man/monitor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4f3601a

Please sign in to comment.