1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-08 07:00:04 +00:00

chore: code fixes

This commit is contained in:
Saad Jutt
2022-06-14 16:48:58 +05:00
parent de9ed15286
commit c830f44e29
9 changed files with 343 additions and 310 deletions

View File

@@ -0,0 +1,18 @@
import path from 'path'
import { fileExists } from '@sasjs/utils'
import { getFilesFolder } from './file'
export const getRunTimeAndFilePath = async (programPath: string) => {
for (const runTime of process.runTimes) {
const codePath =
path
.join(getFilesFolder(), programPath)
.replace(new RegExp('/', 'g'), path.sep) +
'.' +
runTime
if (await fileExists(codePath)) return { codePath, runTime }
}
throw `The Program at (${programPath}) does not exist.`
}

View File

@@ -10,6 +10,7 @@ export * from './generateRefreshToken'
export * from './getCertificates'
export * from './getDesktopFields'
export * from './getPreProgramVariables'
export * from './getRunTimeAndFilePath'
export * from './getServerUrl'
export * from './instantiateLogger'
export * from './isDebugOn'

View File

@@ -1,7 +1,7 @@
import path from 'path'
import { createFolder, getAbsolutePath, getRealPath } from '@sasjs/utils'
import { getDesktopFields, ModeType } from '.'
import { getDesktopFields, ModeType, RunTimeType } from '.'
export const setProcessVariables = async () => {
if (process.env.NODE_ENV === 'test') {
@@ -19,18 +19,15 @@ export const setProcessVariables = async () => {
process.sasLoc = sasLoc
}
const { SASJS_RUNTIMES } = process.env
const runTimes = SASJS_RUNTIMES
? SASJS_RUNTIMES.split(',').map((runTime) => runTime.toLowerCase())
: ['sas']
process.runTimes = runTimes
const { SASJS_ROOT } = process.env
const absPath = getAbsolutePath(SASJS_ROOT ?? 'sasjs_root', process.cwd())
await createFolder(absPath)
process.driveLoc = getRealPath(absPath)
const { RUN_TIMES } = process.env
process.runTimes = (RUN_TIMES as string).split(',') as RunTimeType[]
console.log('sasLoc: ', process.sasLoc)
console.log('sasDrive: ', process.driveLoc)
console.log('runTimes: ', process.runTimes)
}

View File

@@ -26,7 +26,7 @@ export enum LOG_FORMAT_MORGANType {
tiny = 'tiny'
}
export enum SASJSRunTimes {
export enum RunTimeType {
SAS = 'sas',
JS = 'js'
}
@@ -51,7 +51,7 @@ export const verifyEnvVariables = (): ReturnCode => {
errors.push(...verifyLOG_FORMAT_MORGAN())
errors.push(...verifySASJSRunTimes())
errors.push(...verifyRUN_TIMES())
if (errors.length) {
process.logger?.error(
@@ -209,26 +209,24 @@ const verifyLOG_FORMAT_MORGAN = (): string[] => {
return errors
}
const verifySASJSRunTimes = (): string[] => {
const verifyRUN_TIMES = (): string[] => {
const errors: string[] = []
const { SASJS_RUNTIMES } = process.env
const { RUN_TIMES } = process.env
if (SASJS_RUNTIMES) {
const runTimes = SASJS_RUNTIMES.split(',').map((runTime) =>
runTime.toLowerCase()
)
if (RUN_TIMES) {
const runTimes = RUN_TIMES.split(',')
const possibleRunTimes = Object.values(SASJSRunTimes)
const runTimeTypes = Object.values(RunTimeType)
runTimes.forEach((runTime) => {
if (!possibleRunTimes.includes(runTime as SASJSRunTimes)) {
if (!runTimeTypes.includes(runTime.toLowerCase() as RunTimeType)) {
errors.push(
`- Invalid '${runTime}' runtime\n - valid options ${possibleRunTimes}`
`- Invalid '${runTime}' runtime\n - valid options ${runTimeTypes}`
)
}
})
} else {
process.env.SASJS_RUNTIMES = DEFAULTS.SASJS_RUNTIMES
process.env.RUN_TIMES = DEFAULTS.RUN_TIMES
}
return errors
}
@@ -239,5 +237,5 @@ const DEFAULTS = {
PORT: '5000',
HELMET_COEP: HelmetCoepType.TRUE,
LOG_FORMAT_MORGAN: LOG_FORMAT_MORGANType.Common,
SASJS_RUNTIMES: SASJSRunTimes.SAS
RUN_TIMES: RunTimeType.SAS
}