11# ' Find File
22# '
3+ # ' Finds files in the following order:
4+ # '
5+ # ' 1. The file is fully specified and exists (no message)
6+ # ' 2. Only one file is found with that name (with message)
7+ # ' 3. A file with the default extension exists (with message)
8+ # '
39# ' @param name Name of a file
410# ' @param root Path to the root folder
5- # '
611# ' @return A full name path to the file or a error if multiples files or 0.
712# ' @noRd
813find_file <- function (name , root ) {
14+ file <- file.path(root , name )
15+
16+ if (file.exists(file )) {
17+ return (file )
18+ }
19+
920 files <- list.files(
1021 path = root ,
11- pattern = paste0(" ^" , name , " (\\ .|$) " ),
22+ pattern = paste0(" ^" , name , " (\\ .[[:alnum:]]+|)$ " ),
1223 full.names = TRUE
1324 )
1425
26+ if (! length(files )) {
27+ cli :: cli_abort(
28+ " No file found with name: {.field {name}}"
29+ )
30+ }
31+
1532 if (length(files ) == 1 ) {
1633 zephyr :: msg(
1734 " Found one file: {.file {files}}"
@@ -20,13 +37,13 @@ find_file <- function(name, root) {
2037 }
2138
2239 ext <- zephyr :: get_option(" default_ext" , " connector" )
23- files <- files [tools :: file_ext(files ) == ext ]
40+ file_ext <- files [tools :: file_ext(files ) == ext ]
2441
25- if (length(files ) == 1 ) {
42+ if (length(file_ext ) == 1 ) {
2643 zephyr :: msg(
27- " Found one file with default ({.field {ext}}) extension: {.file {files }}"
44+ " Found one file with default ({.field {ext}}) extension: {.file {file_ext }}"
2845 )
29- return (files )
46+ return (file_ext )
3047 }
3148
3249 cli :: cli_abort(
0 commit comments