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

feat(execute): add sas controller

This commit is contained in:
Yury Shkoda
2021-08-03 08:28:28 +03:00
parent 8b1e79497f
commit bf1db4dd47
3 changed files with 59 additions and 2 deletions

49
src/controllers/sas.ts Normal file
View File

@@ -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<ExecutionResult> =>
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 })
}
)
})

View File

@@ -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

View File

@@ -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'