From bf1db4dd47d2488bac073cd468db920ff9fd533d Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Tue, 3 Aug 2021 08:28:28 +0300 Subject: [PATCH] feat(execute): add sas controller --- src/controllers/sas.ts | 49 ++++++++++++++++++++++++++++++++++++++++++ src/routes/index.ts | 8 +++++++ src/types/request.ts | 4 ++-- 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/controllers/sas.ts diff --git a/src/controllers/sas.ts b/src/controllers/sas.ts new file mode 100644 index 0000000..b2c784a --- /dev/null +++ b/src/controllers/sas.ts @@ -0,0 +1,49 @@ +import { execFile } from 'child_process' +import { + readFile, + generateTimestamp, + deleteFile, + fileExists +} from '@sasjs/utils' +import path from 'path' +import { ExecutionResult, ExecutionQuery } from '../types' +import { + getTmpFolderPath, + getTmpFilesFolderPath, + getTmpLogFolderPath +} from '../utils' +import { configuration } from '../../package.json' + +export const processSas = async ( + query: ExecutionQuery +): Promise => + new Promise(async (resolve, reject) => { + let sasCodePath = path.join(getTmpFilesFolderPath(), query._program) + sasCodePath = sasCodePath.replace(new RegExp('/', 'g'), path.sep) + + if (!(await fileExists(sasCodePath))) { + reject('SAS file does not exist.') + } + + const sasFile: string = sasCodePath.split(path.sep).pop() || 'default' + + const sasLogPath = path.join( + getTmpLogFolderPath(), + [sasFile.replace(/\.sas/g, ''), '-', generateTimestamp(), '.log'].join('') + ) + + execFile( + configuration.sasPath, + ['-SYSIN', sasCodePath, '-log', sasLogPath, '-nosplash'], + async (err, _, stderr) => { + if (err) reject(err) + if (stderr) reject(stderr) + + const log = await readFile(sasLogPath) + + // deleteFile(sasLogPath) + + resolve({ log: log, logPath: sasLogPath }) + } + ) + }) diff --git a/src/routes/index.ts b/src/routes/index.ts index 9191b7a..dd325ed 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -36,4 +36,12 @@ router.post('/deploy', async (req, res) => { }) }) +router.post('/execute', async (req, res) => { + if (req.body?._program) { + const result: ExecutionResult = await processSas(req.body) + } else { + res.status(400).send(`Please provide the location of SAS code`) + } +}) + export default router diff --git a/src/types/request.ts b/src/types/request.ts index 04d1573..10886b8 100644 --- a/src/types/request.ts +++ b/src/types/request.ts @@ -1,6 +1,6 @@ -export interface RequestQuery { +export interface ExecutionQuery { _program: string } -export const isRequestQuery = (arg: any): arg is RequestQuery => +export const isRequestQuery = (arg: any): arg is ExecutionQuery => arg && !Array.isArray(arg) && typeof arg._program === 'string'