From d19ce253b4e2d2a7dd912d43a553d4c1bd60ba58 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Wed, 15 Dec 2021 18:24:04 +0500 Subject: [PATCH] fix: use env if provided for desktop mode --- api/.env.example | 5 ++++- api/package.json | 2 +- api/src/types/Process.d.ts | 2 +- api/src/utils/connectDB.ts | 17 +++++++++++------ api/src/utils/file.ts | 4 +--- api/src/utils/getDesktopFields.ts | 6 ++++-- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/api/.env.example b/api/.env.example index 8968cda..b3069f3 100644 --- a/api/.env.example +++ b/api/.env.example @@ -5,4 +5,7 @@ PORT_WEB=[port for sasjs web component(react)] default value is 3000 ACCESS_TOKEN_SECRET= REFRESH_TOKEN_SECRET= AUTH_CODE_SECRET= -DB_CONNECT=mongodb+srv://:@/?retryWrites=true&w=majority \ No newline at end of file +DB_CONNECT=mongodb+srv://:@/?retryWrites=true&w=majority + +SAS_PATH=/opt/sas/sas9/SASHome/SASFoundation/9.4/sas +SAS_DRIVE=./tmp diff --git a/api/package.json b/api/package.json index c30b568..d23e2ab 100644 --- a/api/package.json +++ b/api/package.json @@ -86,6 +86,6 @@ "typescript": "^4.3.2" }, "configuration": { - "sasPath": "/opt/sas/sas9/SASHome/SASFoundation/9.4" + "sasPath": "/opt/sas/sas9/SASHome/SASFoundation/9.4/sas" } } diff --git a/api/src/types/Process.d.ts b/api/src/types/Process.d.ts index dbd7f0f..85f7d45 100644 --- a/api/src/types/Process.d.ts +++ b/api/src/types/Process.d.ts @@ -1,7 +1,7 @@ declare namespace NodeJS { export interface Process { sasLoc: string - driveLoc?: string + driveLoc: string sessionController?: import('../controllers/internal').SessionController } } diff --git a/api/src/utils/connectDB.ts b/api/src/utils/connectDB.ts index 4e28cb2..c976fce 100644 --- a/api/src/utils/connectDB.ts +++ b/api/src/utils/connectDB.ts @@ -3,12 +3,14 @@ import mongoose from 'mongoose' import { configuration } from '../../package.json' import { getDesktopFields } from '.' import { populateClients } from '../routes/api/auth' +import { getRealPath } from '@sasjs/utils' export const connectDB = async () => { // NOTE: when exporting app.js as agent for supertest // we should exlcude connecting to the real database if (process.env.NODE_ENV !== 'test') { const { MODE } = process.env + if (MODE?.trim() !== 'server') { console.log('Running in Destop Mode, no DB to connect.') @@ -16,16 +18,19 @@ export const connectDB = async () => { process.sasLoc = sasLoc process.driveLoc = driveLoc + } else { + const { SAS_PATH, DRIVE_PATH } = process.env - return + process.sasLoc = SAS_PATH ?? configuration.sasPath + process.driveLoc = getRealPath( + path.join(process.cwd(), DRIVE_PATH ?? 'tmp') + ) } - const { SAS_PATH } = process.env - const sasDir = SAS_PATH ?? configuration.sasPath - - process.sasLoc = path.join(sasDir, 'sas') - console.log('sasLoc: ', process.sasLoc) + console.log('sasDrive: ', process.driveLoc) + + if (MODE?.trim() !== 'server') return mongoose.connect(process.env.DB_CONNECT as string, async (err) => { if (err) throw err diff --git a/api/src/utils/file.ts b/api/src/utils/file.ts index e9d7731..c48c633 100644 --- a/api/src/utils/file.ts +++ b/api/src/utils/file.ts @@ -1,5 +1,4 @@ import path from 'path' -import { getRealPath } from '@sasjs/utils' export const apiRoot = path.join(__dirname, '..', '..') export const codebaseRoot = path.join(apiRoot, '..') @@ -12,8 +11,7 @@ export const sysInitCompiledPath = path.join( export const getWebBuildFolderPath = () => path.join(codebaseRoot, 'web', 'build') -export const getTmpFolderPath = () => - process.driveLoc ?? getRealPath(path.join(process.cwd(), 'tmp')) +export const getTmpFolderPath = () => process.driveLoc export const getTmpFilesFolderPath = () => path.join(getTmpFolderPath(), 'files') diff --git a/api/src/utils/getDesktopFields.ts b/api/src/utils/getDesktopFields.ts index dc083c0..2700145 100644 --- a/api/src/utils/getDesktopFields.ts +++ b/api/src/utils/getDesktopFields.ts @@ -5,8 +5,10 @@ import { createFolder, fileExists, folderExists } from '@sasjs/utils' const isWindows = () => process.platform === 'win32' export const getDesktopFields = async () => { - const sasLoc = await getSASLocation() - const driveLoc = await getDriveLocation() + const { SAS_PATH, DRIVE_PATH } = process.env + + const sasLoc = SAS_PATH ?? (await getSASLocation()) + const driveLoc = DRIVE_PATH ?? (await getDriveLocation()) return { sasLoc, driveLoc } }