From 39e486b8cb5efbadc86eb7029b60c7073744eb2b Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Mon, 9 Aug 2021 11:18:49 +0300 Subject: [PATCH] feat(execute): add macroVars to job execution --- src/controllers/sas.ts | 36 ++++++++++++++++++++++++++++++++---- src/routes/index.ts | 2 ++ src/types/request.ts | 2 ++ src/utils/file.ts | 5 ++++- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/controllers/sas.ts b/src/controllers/sas.ts index b2c784a..487f40f 100644 --- a/src/controllers/sas.ts +++ b/src/controllers/sas.ts @@ -3,14 +3,15 @@ import { readFile, generateTimestamp, deleteFile, - fileExists + fileExists, + createFile } from '@sasjs/utils' import path from 'path' import { ExecutionResult, ExecutionQuery } from '../types' import { - getTmpFolderPath, getTmpFilesFolderPath, - getTmpLogFolderPath + getTmpLogFolderPath, + getTmpWeboutFolderPath } from '../utils' import { configuration } from '../../package.json' @@ -27,11 +28,36 @@ export const processSas = async ( const sasFile: string = sasCodePath.split(path.sep).pop() || 'default' + const sasWeboutPath = path.join( + getTmpWeboutFolderPath(), + [sasFile.replace(/\.sas/g, ''), '-', generateTimestamp(), '.json'].join( + '' + ) + ) + + let sasCode = await readFile(sasCodePath) + const originalSasCode = sasCode + + if (query.macroVars) { + const macroVars = query.macroVars.macroVars + + Object.keys(macroVars).forEach( + (key: string) => + (sasCode = `%let ${key}=${macroVars[key]};\n${sasCode}`) + ) + } + + sasCode = `filename _webout "${sasWeboutPath}";\n${sasCode}` + + await createFile(sasCodePath, sasCode) + const sasLogPath = path.join( getTmpLogFolderPath(), [sasFile.replace(/\.sas/g, ''), '-', generateTimestamp(), '.log'].join('') ) + await createFile(sasLogPath, '') + execFile( configuration.sasPath, ['-SYSIN', sasCodePath, '-log', sasLogPath, '-nosplash'], @@ -41,7 +67,9 @@ export const processSas = async ( const log = await readFile(sasLogPath) - // deleteFile(sasLogPath) + deleteFile(sasLogPath) + + await createFile(sasCodePath, originalSasCode) resolve({ log: log, logPath: sasLogPath }) } diff --git a/src/routes/index.ts b/src/routes/index.ts index dd325ed..8fc6b10 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -39,6 +39,8 @@ router.post('/deploy', async (req, res) => { router.post('/execute', async (req, res) => { if (req.body?._program) { const result: ExecutionResult = await processSas(req.body) + + res.status(200).send(result) } else { res.status(400).send(`Please provide the location of SAS code`) } diff --git a/src/types/request.ts b/src/types/request.ts index 10886b8..876dc89 100644 --- a/src/types/request.ts +++ b/src/types/request.ts @@ -1,5 +1,7 @@ +import { MacroVars } from '@sasjs/utils' export interface ExecutionQuery { _program: string + macroVars?: MacroVars } export const isRequestQuery = (arg: any): arg is ExecutionQuery => diff --git a/src/utils/file.ts b/src/utils/file.ts index fa459f5..5a6a88d 100644 --- a/src/utils/file.ts +++ b/src/utils/file.ts @@ -7,4 +7,7 @@ export const getTmpFolderPath = () => export const getTmpFilesFolderPath = () => path.join(getTmpFolderPath(), 'files') -export const getTmpLogFolderPath = () => path.join(getTmpFolderPath(), 'log') +export const getTmpLogFolderPath = () => path.join(getTmpFolderPath(), 'logs') + +export const getTmpWeboutFolderPath = () => + path.join(getTmpFolderPath(), 'webouts')