1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-11 03:34:35 +00:00

chore: verify executable paths

This commit is contained in:
2022-06-17 18:12:03 +05:00
parent 158acf1f97
commit ab222cbaab
7 changed files with 59 additions and 6 deletions

View File

@@ -61,6 +61,9 @@ MODE=
# Path to SAS executable (sas.exe / sas.sh)
SAS_PATH=/path/to/sas/executable.exe
# Path to Node.js executable
NODE_PATH=node
# Path to working directory
# This location is for SAS WORK, staged files, DRIVE, configuration etc
SASJS_ROOT=./sasjs_root
@@ -131,10 +134,10 @@ HELMET_CSP_CONFIG_PATH=./csp.config.json
LOG_FORMAT_MORGAN=
# A comma separated string that defines the available runTimes.
# Priority is given to the runtime that comes first in string.
# Priority is given to the runtime that cSAS_PATHomes first in string.
# Possible options at the moment are sas and js
# options: [sas,js|js,sas|sas|js] default:sas
# options: [sas,js|js,sas|sas|js] default:sas,js
RUN_TIMES=
```

View File

@@ -19,6 +19,7 @@ AUTH_CODE_SECRET=<secret>
SESSION_SECRET=<secret>
DB_CONNECT=mongodb+srv://<DB_USERNAME>:<DB_PASSWORD>@<CLUSTER>/<DB_NAME>?retryWrites=true&w=majority
NODE_PATH=node
SAS_PATH=/opt/sas/sas9/SASHome/SASFoundation/9.4/sas
SASJS_ROOT=./sasjs_root

View File

@@ -349,7 +349,7 @@ const processProgram = async (
// waiting for the open event so that we can have underlying file descriptor
await once(writeStream, 'open')
execFileSync('node', [codePath], {
execFileSync(process.nodeLoc, [codePath], {
stdio: ['ignore', writeStream, writeStream]
})

View File

@@ -1,6 +1,7 @@
declare namespace NodeJS {
export interface Process {
sasLoc: string
nodeLoc: string
driveLoc: string
sasSessionController?: import('../../controllers/internal').SASSessionController
jsSessionController?: import('../../controllers/internal').JSSessionController

View File

@@ -5,12 +5,13 @@ import { createFolder, fileExists, folderExists } from '@sasjs/utils'
const isWindows = () => process.platform === 'win32'
export const getDesktopFields = async () => {
const { SAS_PATH } = process.env
const { SAS_PATH, NODE_PATH } = process.env
const sasLoc = SAS_PATH ?? (await getSASLocation())
const nodeLoc = NODE_PATH ?? (await getNodeLocation())
// const driveLoc = DRIVE_PATH ?? (await getDriveLocation())
return { sasLoc }
return { sasLoc, nodeLoc }
}
const getDriveLocation = async (): Promise<string> => {
@@ -61,3 +62,27 @@ const getSASLocation = async (): Promise<string> => {
return targetName
}
const getNodeLocation = async (): Promise<string> => {
const validator = async (filePath: string) => {
if (!filePath) return 'Path to NodeJS executable is required.'
if (!(await fileExists(filePath))) {
return 'No file found at provided path.'
}
return true
}
const defaultLocation = isWindows()
? 'C:\\Program Files\\nodejs\\'
: '/usr/local/nodejs/bin'
const targetName = await getString(
'Please enter path to nodejs executable (absolute path): ',
validator,
defaultLocation
)
return targetName
}

View File

@@ -13,10 +13,12 @@ export const setProcessVariables = async () => {
if (MODE === ModeType.Server) {
process.sasLoc = process.env.SAS_PATH as string
process.nodeLoc = process.env.NODE_PATH as string
} else {
const { sasLoc } = await getDesktopFields()
const { sasLoc, nodeLoc } = await getDesktopFields()
process.sasLoc = sasLoc
process.nodeLoc = nodeLoc
}
const { SASJS_ROOT } = process.env

View File

@@ -53,6 +53,8 @@ export const verifyEnvVariables = (): ReturnCode => {
errors.push(...verifyRUN_TIMES())
errors.push(...verifyExecutablePaths())
if (errors.length) {
process.logger?.error(
`Invalid environment variable(s) provided: \n${errors.join('\n')}`
@@ -231,6 +233,25 @@ const verifyRUN_TIMES = (): string[] => {
return errors
}
const verifyExecutablePaths = () => {
const errors: string[] = []
const { RUN_TIMES, SAS_PATH, NODE_PATH, MODE } = process.env
if (MODE === ModeType.Server) {
const runTimes = RUN_TIMES?.split(',')
if (runTimes?.includes(RunTimeType.SAS) && !SAS_PATH) {
errors.push(`- SAS_PATH is required for ${RunTimeType.SAS} run time`)
}
if (runTimes?.includes(RunTimeType.JS) && !NODE_PATH) {
errors.push(`- NODE_PATH is required for ${RunTimeType.JS} run time`)
}
}
return errors
}
const DEFAULTS = {
MODE: ModeType.Desktop,
PROTOCOL: ProtocolType.HTTP,