Skip to content

Commit

Permalink
tmp: work around a problem with processx on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilzyla committed Oct 17, 2024
1 parent 09b60f1 commit 7716417
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions R/node.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
)
Expand All @@ -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()
}
}

0 comments on commit 7716417

Please sign in to comment.