Skip to content

Commit 2474137

Browse files
committed
remove empty folders
1 parent 329ef87 commit 2474137

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ NeedsCompilation: no
1414
Suggests:
1515
testthat,
1616
formatR
17-
RoxygenNote: 7.3.2
17+
RoxygenNote: 7.3.3
1818
Repository: CRANs
1919
Language: en-US
2020
Authors@R: c(person("Marcelo", "Araya-Salas", role = c("aut", "cre"), email = "marcelo.araya@ucr.ac.cr", comment = c(ORCID = "0000-0003-3594-619X")), person("Andrea Yure", "Arriaga Madrigal", role = c("aut"), email = "arriagamadrigal@gmail.com"))

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sketchy 1.0.7
33

44
### MINOR IMPROVEMENTS
55

6-
* `spot_unused_files()` now returns a data frame with more details about the files found
6+
* `spot_unused_files()` can remove empty folders
77

88
sketchy 1.0.6
99
=========================

R/spot_unused_files.R

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#' @param script.extensions A character vector with the script extensions to be considered. Default is c("R", "Rmd", "qmd").
77
#' @param archive A logical value indicating whether to archive the unused files. If \code{TRUE} the spotted files will be move into the folder "./unused_files". Default is \code{FALSE}.
88
#' @param ignore.folder A character string with the path or paths to the directory(ies) to be ignored. Default is \code{NULL}.
9+
#' @param remove.empty A logical value indicating whether to remove empty folders after moving the unused files. Default is \code{FALSE}.
910
#' @seealso \code{\link{add_to_gitignore}}, \code{\link{make_compendium}}
1011
#' @export
1112
#' @name spot_unused_files
@@ -36,7 +37,8 @@ spot_unused_files <-
3637
"txt"),
3738
script.extensions = c("R", "Rmd", "qmd"),
3839
archive = FALSE,
39-
ignore.folder = NULL) {
40+
ignore.folder = NULL,
41+
remove.empty = FALSE) {
4042
# List all files in the specified directory with the specified extensions
4143
# List all target format files
4244
all_files <-
@@ -50,11 +52,16 @@ spot_unused_files <-
5052
# Create a data frame with the results
5153
results <- data.frame(
5254
file.name = basename(all_files),
53-
folder = paste0(dirname(all_files), "/"),
55+
folder = if (length(all_files) > 0) paste0(dirname(all_files), "/") else all_files,
5456
used = used_files,
5557
row.names = NULL
5658
)
5759

60+
if (nrow(results) == 0) {
61+
message("No files with the specified extensions were found.")
62+
return(invisible(NULL))
63+
}
64+
5865
results <- results[basename(results$folder) != "unused_files",]
5966

6067
# ignore folders
@@ -90,6 +97,22 @@ spot_unused_files <-
9097
}
9198
}
9299

100+
# remove empty folders
101+
if (remove.empty) {
102+
# find empty folders
103+
all_dirs <- list.dirs(path, recursive = TRUE)
104+
empty_dirs <- all_dirs[sapply(all_dirs, function(x) length(list.files(x)) == 0)]
105+
106+
if (length(empty_dirs) > 0) {
107+
message(paste("Removing", length(empty_dirs), "empty folders:"))
108+
print(empty_dirs)
109+
}
110+
111+
# remove empty folders
112+
out <- sapply(empty_dirs, unlink, recursive = TRUE)
113+
114+
}
115+
93116
if (nrow(results) > 0)
94117
return(results[, c(1, 2)])
95118
}

man/spot_unused_files.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testing/testing-checking sketchy function nov-2020.Rmd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,13 +517,17 @@ data("compendiums")
517517
518518
make_compendium(name = "pruebaX66636", format = "sketchy", force = TRUE, path = tempdir())
519519
520-
spot_unused_files(path = ".")
520+
spot_unused_files(path = file.path(tempdir(), "pruebaX66636"), remove.empty = TRUE)
521521
522-
spot_unused_files(path = ".", ignore.folder = c("./docs", "./testing"))
523522
524-
write.csv(iris, "./inst/iris.jpeg")
525-
write.csv(iris, "./inst/2.csv")
523+
write.csv(iris, file.path(tempdir(),"pruebaX66636/data/iris.jpeg"))
524+
write.csv(iris, file.path(tempdir(),"pruebaX66636/data/iris.jpeg"))
525+
526+
527+
spot_unused_files(path = file.path(tempdir(), "pruebaX66636"))
528+
526529
530+
spot_unused_files(path = file.path(tempdir(), "pruebaX66636"), remove.empty = TRUE)
527531
528532
spot_unused_files(path = ".", ignore.folder = c("./docs", "./testing"))
529533

0 commit comments

Comments
 (0)