Skip to content

Commit

Permalink
feat: add replace arg to all use_external_XXX type functions
Browse files Browse the repository at this point in the history
- replace arg added to function interfaces of
    - use_external_js_file
    - use_external_css_file
    - use_external_html_template
    - use_external_file
- for all cases, replace arg is passed down to check_file_exists()
and handled there; return NULL per default thus allows files to
be overwritten in the corresponding cases
  • Loading branch information
ilyaZar committed Apr 5, 2023
1 parent 4087156 commit fc73159
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions R/use_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param url String representation of URL for the file to be downloaded
#' @param path String representation of the local path for the file to be implemented (use_file only)
#' @param dir Path to the dir where the file while be created.
#' @param replace logical; if \code{TRUE}, then external file is overwritten
#'
#' @note See `?htmltools::htmlTemplate` and `https://shiny.rstudio.com/articles/templates.html`
#' for more information about `htmlTemplate`.
Expand All @@ -22,7 +23,8 @@ use_external_js_file <- function(
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
dir_create = TRUE,
replace = FALSE
) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))
Expand All @@ -41,7 +43,7 @@ use_external_js_file <- function(
new_file
)

check_file <- check_file_exists(where)
check_file <- check_file_exists(where, replace)
if (isFALSE(check_file)) return(invisible(FALSE))

check_url <- check_url_valid(
Expand Down Expand Up @@ -71,7 +73,8 @@ use_external_css_file <- function(
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
dir_create = TRUE,
replace = FALSE
) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))
Expand Down Expand Up @@ -109,7 +112,7 @@ use_external_css_file <- function(
)
# no change to previous version

check_file <- check_file_exists(where)
check_file <- check_file_exists(where, replace)
if (isFALSE(check_file)) return(invisible(FALSE))
# previously
# if (fs_file_exists(where)) {
Expand Down Expand Up @@ -151,7 +154,8 @@ use_external_html_template <- function(
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
dir_create = TRUE,
replace = FALSE
) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))
Expand Down Expand Up @@ -190,7 +194,7 @@ use_external_html_template <- function(
)
# no change to previous version

check_file <- check_file_exists(where)
check_file <- check_file_exists(where, replace)
if (isFALSE(check_file)) return(invisible(FALSE))
# previously
# if (fs_file_exists(where)) {
Expand Down Expand Up @@ -233,7 +237,8 @@ use_external_file <- function(
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
dir_create = TRUE,
replace = FALSE
) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))
Expand Down Expand Up @@ -263,7 +268,7 @@ use_external_file <- function(
name
)

check_file <- check_file_exists(where)
check_file <- check_file_exists(where, replace)
if (isFALSE(check_file)) return(invisible(FALSE))
# previously
# if (fs_file_exists(where)) {
Expand Down Expand Up @@ -544,11 +549,12 @@ get_new_dir <- function(

fs_path_abs(dir)
}
check_file_exists <- function(where) {
if (fs_file_exists(where)) {
check_file_exists <- function(where, replace) {
if (fs_file_exists(where) && isFALSE(replace)) {
cat_exists(where)
return(invisible(FALSE))
}
# otherwise: func returns NULL silently which is on purpose!
}
check_url_valid <- function(
url,
Expand Down

0 comments on commit fc73159

Please sign in to comment.