From 73d965daf54b16c0921e4b18d11a1e6f8650884d Mon Sep 17 00:00:00 2001 From: Yury Date: Mon, 15 Jan 2024 15:14:06 +0300 Subject: [PATCH] fix(api): fixed env issue in MacOS executable --- api/package-lock.json | 16 +++++++------- api/package.json | 2 +- api/src/utils/setProcessVariables.ts | 32 ++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/api/package-lock.json b/api/package-lock.json index 6f27e06f..36cbb54d 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -51,7 +51,7 @@ "adm-zip": "^0.5.9", "axios": "0.27.2", "csrf": "^3.1.0", - "dotenv": "^10.0.0", + "dotenv": "^16.0.1", "http-headers-validation": "^0.0.1", "jest": "^27.0.6", "mongodb-memory-server": "8.11.4", @@ -4788,12 +4788,12 @@ } }, "node_modules/dotenv": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", - "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "version": "16.0.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz", + "integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==", "dev": true, "engines": { - "node": ">=10" + "node": ">=12" } }, "node_modules/duplexer2": { @@ -15217,9 +15217,9 @@ } }, "dotenv": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", - "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "version": "16.0.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz", + "integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==", "dev": true }, "duplexer2": { diff --git a/api/package.json b/api/package.json index dc85ac71..ead29f53 100644 --- a/api/package.json +++ b/api/package.json @@ -88,7 +88,7 @@ "adm-zip": "^0.5.9", "axios": "0.27.2", "csrf": "^3.1.0", - "dotenv": "^10.0.0", + "dotenv": "^16.0.1", "http-headers-validation": "^0.0.1", "jest": "^27.0.6", "mongodb-memory-server": "8.11.4", diff --git a/api/src/utils/setProcessVariables.ts b/api/src/utils/setProcessVariables.ts index c1a6cd44..170ad6e4 100644 --- a/api/src/utils/setProcessVariables.ts +++ b/api/src/utils/setProcessVariables.ts @@ -1,9 +1,31 @@ import path from 'path' -import { createFolder, getAbsolutePath, getRealPath } from '@sasjs/utils' - +import { + createFolder, + getAbsolutePath, + getRealPath, + fileExists +} from '@sasjs/utils' +import dotenv from 'dotenv' import { connectDB, getDesktopFields, ModeType, RunTimeType, SECRETS } from '.' export const setProcessVariables = async () => { + const { execPath } = process + + // Check if execPath ends with 'api-macos' to determine executable for MacOS. + // This is needed to fix picking .env file issue in MacOS executable. + if (execPath) { + const envPathSplitted = execPath.split(path.sep) + + if (envPathSplitted.pop() === 'api-macos') { + const envPath = path.join(envPathSplitted.join(path.sep), '.env') + + // Override environment variables from envPath if file exists + if (await fileExists(envPath)) { + dotenv.config({ path: envPath, override: true }) + } + } + } + const { MODE, RUN_TIMES } = process.env if (MODE === ModeType.Server) { @@ -21,6 +43,7 @@ export const setProcessVariables = async () => { if (process.env.NODE_ENV === 'test') { process.sasjsRoot = path.join(process.cwd(), 'sasjs_root') process.driveLoc = path.join(process.cwd(), 'sasjs_root', 'drive') + return } @@ -41,7 +64,9 @@ export const setProcessVariables = async () => { const { SASJS_ROOT } = process.env const absPath = getAbsolutePath(SASJS_ROOT ?? 'sasjs_root', process.cwd()) + await createFolder(absPath) + process.sasjsRoot = getRealPath(absPath) const { DRIVE_LOCATION } = process.env @@ -49,6 +74,7 @@ export const setProcessVariables = async () => { DRIVE_LOCATION ?? path.join(process.sasjsRoot, 'drive'), process.cwd() ) + await createFolder(absDrivePath) process.driveLoc = getRealPath(absDrivePath) @@ -57,7 +83,9 @@ export const setProcessVariables = async () => { LOG_LOCATION ?? path.join(process.sasjsRoot, 'logs'), process.cwd() ) + await createFolder(absLogsPath) + process.logsLoc = getRealPath(absLogsPath) process.logsUUID = 'SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784'