1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-11 00:10:06 +00:00

feat(api): set up endpoint for sas code execution

This commit is contained in:
Yury Shkoda
2021-06-10 20:12:23 +03:00
parent cb841ddcac
commit f6046b15ae
6 changed files with 84 additions and 9 deletions

46
src/controllers/index.ts Normal file
View File

@@ -0,0 +1,46 @@
import { execFile } from 'child_process'
import { readFile, generateTimestamp, deleteFile } from '@sasjs/utils'
import path from 'path'
import { ExecutionResult, RequestQuery } from '../types'
// FIXME
const sasExePath = `C:\\Program Files\\SASHome\\SASFoundation\\9.4\\sas.exe`
const baseSasLogPath = 'C:\\Users\\YuryShkoda\\projects\\server\\sas\\logs'
const baseSasCodePath = `sas`
// TODO: create utils isSasFile
export const processSas = async (
query: RequestQuery
): Promise<ExecutionResult> =>
new Promise((resolve, reject) => {
let sasCodePath = query._program
sasCodePath = path.join(baseSasCodePath, `${sasCodePath}.sas`)
sasCodePath = sasCodePath.replace(new RegExp('/', 'g'), path.sep)
const sasFile: string = sasCodePath.split(path.sep).pop() || 'default'
const sasLogPath = [
baseSasLogPath,
path.sep,
sasFile.replace(/\.sas/g, ''),
'-',
generateTimestamp(),
'.log'
].join('')
execFile(
sasExePath,
['-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 })
}
)
})