From 771641700458bc1f91d804af8a7f9c2cc3100295 Mon Sep 17 00:00:00 2001 From: Kamil Zyla Date: Mon, 7 Oct 2024 14:36:27 +0200 Subject: [PATCH] tmp: work around a problem with processx on Windows --- R/node.R | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/R/node.R b/R/node.R index 4ab20c5a..8197a470 100644 --- a/R/node.R +++ b/R/node.R @@ -2,6 +2,10 @@ node_path <- function(...) { fs::path(".rhino", ...) } +npm_command <- function() { + Sys.getenv("RHINO_NPM", "npm") +} + # Run a script defined in `package.json`. npm_run <- function(...) { # Use `--silent` to prevent `npm` from echoing the command it runs. @@ -14,29 +18,27 @@ npm_run <- function(...) { npm <- function(...) { node <- node_check() if (!node$status_ok) { - node_missing(node$npm_command, abort = TRUE) + node_missing(abort = TRUE) } - node_init(node$npm_command) - node_run(node$npm_command, ...) + node_init() + node_run(...) } node_check <- function() { - npm_command <- Sys.getenv("RHINO_NPM", "npm") version <- tryCatch( - processx::run(npm_command, "--version")$stdout, + processx::run("cmd", rlang::chr("/c", npm_command(), "--version"))$stdout, error = function(e) NULL ) list( - npm_command = npm_command, status_ok = !is.null(version), - diagnostic_info = paste(npm_command, ifelse(is.null(version), "failed", trimws(version))) + diagnostic_info = paste(npm_command(), ifelse(is.null(version), "failed", trimws(version))) ) } -node_missing <- function(npm_command, info = NULL, abort = FALSE) { +node_missing <- function(info = NULL, abort = FALSE) { docs_url <- "https://go.appsilon.com/rhino-system-dependencies" # nolint object_usage_linter msg <- c( - "!" = "Failed to run system command {.code {npm_command}}.", + "!" = "Failed to run system command {.code {npm_command()}}.", " " = "Do you have Node.js installed? Read more: {.url {docs_url}}", "i" = info ) @@ -47,31 +49,42 @@ node_missing <- function(npm_command, info = NULL, abort = FALSE) { } } -node_init <- function(npm_command) { +node_init <- function() { if (!fs::dir_exists(node_path())) { cli::cli_alert_info("Initializing Node.js directory...") copy_template("node", node_path()) } if (!fs::dir_exists(node_path("node_modules"))) { cli::cli_alert_info("Installing Node.js packages with {.code {npm_command}}...") - node_run(npm_command, "install", "--no-audit", "--no-fund") + node_run("install", "--no-audit", "--no-fund") } } # Run the specified command in Node.js directory (assume it already exists). -node_run <- function(command, ..., background = FALSE) { - args <- list( - command = command, - args = rlang::chr(...), - wd = node_path(), +node_run <- function(..., background = FALSE) { + if (.Platform$OS.type == "windows") { + # Workaround: {processx} cannot find `npm` on Windows, but it works with a shell. + call_args <- list( + command = "cmd", + args = rlang::chr("/c", npm_command(), ...) + ) + } else { + call_args <- list( + command = npm_command(), + args = rlang::chr(...) + ) + } + call_args <- c( + call_args, + wd = wd, stdin = NULL, stdout = "", stderr = "" ) if (background) { - do.call(processx::process$new, args) + do.call(processx::process$new, call_args) } else { - do.call(processx::run, args) + do.call(processx::run, call_args) invisible() } }