Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #427 from chasemc/update_WIP
Browse files Browse the repository at this point in the history
Fixes webpack-merge and updates to js dependencies
  • Loading branch information
chasemc authored Feb 6, 2021
2 parents 47a4300 + b6b6c70 commit 7a2a2fc
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 254 deletions.
148 changes: 67 additions & 81 deletions R/check_for_nodejs_npm.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,73 @@
#' @return path of nodejs executable if found and functional, otherwise: FALSE
.check_node_works <- function(node_top_dir,
expected_version) {

message("Checking if the provided nodejs path already contains nodejs...")

if (!grepl("v", expected_version)) {
expected_version <- paste0("v",
expected_version)
}


message("Looking for nodejs...")

expected_version <- .check_node_version_format(expected_version)

os <- electricShine::get_os()

node_path <- normalizePath(node_top_dir,
"/",
mustWork = FALSE)

if (os == "win") {

node_path <- file.path(node_path,
"node.exe")

} else {

node_path <- file.path(node_path,
"node")
"node")
}

node_exists <- file.exists(node_path)

if (!node_exists) {
message("nodejs executable not found.")

message("...nodejs executable not found")
node_path <- FALSE
return(node_path)

} else {

message("Found nodejs executable.")


message("...found nodejs executable.")
message("Checking if nodejs executable is functional...")

# Check that the node version is the same as what we expect and nodejs is functional

quoted_node_path <- shQuote(node_path)
command <- paste0(quoted_node_path,

command <- paste0(quoted_node_path,
" -v")

nodejs_response <- tryCatch(system(command,
intern = T),
error = function(e) FALSE,
error = function(e) FALSE,
warning = function(e) FALSE)



if (nodejs_response == FALSE) {

message(glue::glue("nodejs at {node_path} seems not to be functional."))
node_path <- FALSE
return(node_path)


# TODO: Use "base::as.numeric_version()" and "base::compareVersion()"
# so e.g. "10.6.0" == "10.6" is TRUE


message(glue::glue("...nodejs at {node_path} seems not to be functional."))
return(FALSE)

} else if (nodejs_response != expected_version) {
message(glue::glue("Found nodejs {node_path} is version {nodejs_response}, expected {expected_version}."))
node_path <- FALSE
return(node_path)

message(glue::glue("...found nodejs {node_path} \n but it is version {nodejs_response}, expected {expected_version}."))
return(FALSE)

} else if (nodejs_response == expected_version) {
message(glue::glue("Found nodejs {nodejs_response} at: {node_path}."))

message(glue::glue("...found a functional nodejs {nodejs_response} at: {node_path}."))
return(node_path)

}

} else {

stop("Unexpected error when looking for nodejs")

}
}
}

Expand All @@ -94,51 +90,41 @@
#'
#' @return path of npm executable if found and functional, otherwise: FALSE
.check_npm_works <- function(node_top_dir) {
message("Checking if given nodejs path already contains nodejs.")

message("Looking for npm...")

os <- electricShine::get_os()

npm_path <- normalizePath(node_top_dir,
"/",
mustWork = FALSE)

npm_path <- file.path(npm_path,
"npm")

npm_exists <- file.exists(npm_path)


npm_exists <- file.exists(file.path(node_top_dir, "npm"))

if (!npm_exists) {

message(glue::glue("npm seems not be installed."))
npm_path <- FALSE
return(npm_path)

# If nopm isn't found- let other functions stop if desired, only notify here
message(glue::glue("...npm not found"))
return(FALSE)

} else {

# Check that the npm version is the same as what we expect

quoted_npm_path <- shQuote(npm_path)

command <- paste0(quoted_npm_path,
" -v")

npm_path <- file.path(node_top_dir,
"npm",
fsep = "/")
command <- shQuote(npm_path)

command <- paste0(command, " -v")

result <- tryCatch(system(command,
intern = T),
error = function(e) FALSE,
error = function(e) FALSE,
warning = function(e) FALSE)

if(isFALSE(result)) {

message(glue::glue("npm executable was found but seems not to be functional."))
npm_path <- FALSE
return(npm_path)


if(isFALSE(result)) {
message(glue::glue("...npm executable was found but seems not to be functional"))
return(FALSE)
} else {
message(glue::glue("Found npm at: {npm_path}"))
message(glue::glue("Found npm v{result} at: {npm_path}"))
return(npm_path)
}
}

}
}

106 changes: 54 additions & 52 deletions R/install_nodejs.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,50 @@
install_nodejs <- function(node_url = "https://nodejs.org/dist",
nodejs_path = file.path(system.file(package = "electricShine"), "nodejs"),
force_install = FALSE,
nodejs_version = "v12.16.2",
nodejs_version = "v15.5.0",
permission_to_install = FALSE){


if (!grepl("v", nodejs_version)) {
# Node version format is "v10.16.0", not "10.16.0"
# add "v" if needed
nodejs_version <- paste0("v",
nodejs_version)
}


nodejs_version <- .check_node_version_format(nodejs_version)

# Check if node and npm are already installed


node_exists <- .check_node_works(node_top_dir = nodejs_path,
expected_version = nodejs_version)

nodejs_path <- normalizePath(nodejs_path, winslash = "/")

subfolder <- file.path(nodejs_path,
glue::glue("node-{nodejs_version}-{platform}-{arch}"),
fsep = "/")
# check within given folder first
if (dir.exists(subfolder)) {
nodejs_path <- subfolder
node_exists <- .check_node_works(node_top_dir = nodejs_path,
expected_version = nodejs_version)
} else {
node_exists <- .check_node_works(node_top_dir = nodejs_path,
expected_version = nodejs_version)
}

npm_exists <- .check_npm_works(node_top_dir = nodejs_path)


if (!base::isFALSE(node_exists) && !base::isFALSE(npm_exists)) {

message("Skipping install_nodejs(), nodejs alreagy installed.")

} else {

if (permission_to_install == FALSE) {

permission_to_install <- .prompt_install_nodejs(nodejs_path)

}

if (permission_to_install == FALSE) {
message("nodejs is required for electricShine to work. Please point nodejs_path
message("nodejs is required for electricShine to work. Please point nodejs_path
to a valid nodejs path or select 'yes' when prompted to install")
} else {

# Get operating system:
os <- electricShine::get_os()

if (identical(os, "win")) {
platform <- "win"
ext <- "zip"
Expand All @@ -62,8 +66,8 @@ install_nodejs <- function(node_url = "https://nodejs.org/dist",
platform <- "linux"
ext <- "tar.xz"
}


if (base::version$arch[[1]] == "x86_64") {
arch <- "x64"
} else {
Expand All @@ -75,34 +79,32 @@ install_nodejs <- function(node_url = "https://nodejs.org/dist",
node_url <- file.path(node_url,
nodejs_version,
binary_name)

# Download to temporary directory
temp <- base::file.path(tempdir(),
temp <- base::file.path(tempdir(),
base::basename(node_url))

utils::download.file(node_url,
destfile = temp,
mode = 'wb')

# Download sha file and parse
online_sha <- glue::glue("https://nodejs.org/dist/{nodejs_version}/SHASUMS256.txt")



online_sha <- utils::read.delim(online_sha, sep = "", header = FALSE)

online_sha <- as.character(online_sha[grepl(binary_name, online_sha[,2]), 1])

# Calculate SHA of downloaded nodejs
local_sha <- as.character(openssl::sha256(file(temp, raw = TRUE)))
# as.character() leaves class as "hash" "sha256"
# change to character
class(local_sha) <- "character"

if (!identical(online_sha, local_sha)) {
stop("Downloaded sha didn't match online sha")
stop("Downloaded SHA didn't match online sha")
}
message("Downloaded sha matches expected sha")
message("Decompressing node.js files, might take a few minutes...")
message("Downloaded SHA matches expected sha")
message(glue::glue("All node.js files will be installed into: \n {nodejs_path}"))

message("Decompressing node.js files, might take a few minutes...")

if (base::grepl("zip$", base::basename(node_url))) {
base::try(
utils::unzip(zipfile = temp,
Expand All @@ -116,7 +118,7 @@ install_nodejs <- function(node_url = "https://nodejs.org/dist",
)
}


if (identical(os, "win")) {
nodejs_path <- file.path(nodejs_path,
tools::file_path_sans_ext(basename(temp),
Expand All @@ -131,23 +133,23 @@ install_nodejs <- function(node_url = "https://nodejs.org/dist",
ext <- "tar.xz"
}


node_exists <- .check_node_works(node_top_dir = nodejs_path,
expected_version = nodejs_version)

npm_exists <- .check_npm_works(node_top_dir = nodejs_path)

if (base::isFALSE(node_exists) || base::isFALSE(npm_exists)) {

stop("Was unable to successfully install nodejs/npm executable.")

}

}
}
nodejs_path <- normalizePath(nodejs_path,
winslash = "/",

nodejs_path <- normalizePath(nodejs_path,
winslash = "/",
mustWork = TRUE)
return(nodejs_path)
}
Expand Down
Loading

0 comments on commit 7a2a2fc

Please sign in to comment.