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:
49
src/controllers/sas.ts
Normal file
49
src/controllers/sas.ts
Normal 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 })
|
||||
}
|
||||
)
|
||||
})
|
||||
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user