mirror of
https://github.com/sasjs/server.git
synced 2025-12-11 03:34:35 +00:00
feat: improved deploy and execute endpoints
This commit is contained in:
@@ -34,26 +34,23 @@ export const createFileTree = async (
|
||||
}
|
||||
|
||||
export const getTreeExample = () => ({
|
||||
message: 'Provided not supported data format.',
|
||||
supportedFormat: {
|
||||
members: [
|
||||
{
|
||||
name: 'jobs',
|
||||
type: 'folder',
|
||||
members: [
|
||||
{
|
||||
name: 'extract',
|
||||
type: 'folder',
|
||||
members: [
|
||||
{
|
||||
name: 'makedata1',
|
||||
type: 'service',
|
||||
code: '%put Hello World!;'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
members: [
|
||||
{
|
||||
name: 'jobs',
|
||||
type: 'folder',
|
||||
members: [
|
||||
{
|
||||
name: 'extract',
|
||||
type: 'folder',
|
||||
members: [
|
||||
{
|
||||
name: 'makedata1',
|
||||
type: 'service',
|
||||
code: '%put Hello World!;'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { execFile } from 'child_process'
|
||||
import {
|
||||
readFile,
|
||||
generateTimestamp,
|
||||
@@ -13,37 +12,45 @@ import {
|
||||
getTmpLogFolderPath
|
||||
} from '../utils'
|
||||
import { configuration } from '../../package.json'
|
||||
import { promisify } from 'util'
|
||||
import { execFile } from 'child_process'
|
||||
const execFilePromise = promisify(execFile)
|
||||
|
||||
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)
|
||||
): Promise<ExecutionResult> => {
|
||||
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.')
|
||||
}
|
||||
if (!(await fileExists(sasCodePath))) {
|
||||
return Promise.reject('SAS file does not exist.')
|
||||
}
|
||||
|
||||
const sasFile: string = sasCodePath.split(path.sep).pop() || 'default'
|
||||
const sasFile: string = sasCodePath.split(path.sep).pop() || 'default'
|
||||
|
||||
const sasLogPath = path.join(
|
||||
getTmpLogFolderPath(),
|
||||
[sasFile.replace(/\.sas/g, ''), '-', generateTimestamp(), '.log'].join('')
|
||||
)
|
||||
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 { stdout, stderr } = await execFilePromise(configuration.sasPath, [
|
||||
'-SYSIN',
|
||||
sasCodePath,
|
||||
'-log',
|
||||
sasLogPath,
|
||||
'-nosplash'
|
||||
])
|
||||
|
||||
const log = await readFile(sasLogPath)
|
||||
if (stderr) return Promise.reject(stderr)
|
||||
|
||||
// deleteFile(sasLogPath)
|
||||
if (await fileExists(sasLogPath)) {
|
||||
return Promise.resolve({
|
||||
log: await readFile(sasLogPath),
|
||||
logPath: sasLogPath
|
||||
})
|
||||
} else {
|
||||
return Promise.reject(`Log file wasn't created.`)
|
||||
}
|
||||
|
||||
resolve({ log: log, logPath: sasLogPath })
|
||||
}
|
||||
)
|
||||
})
|
||||
// deleteFile(sasLogPath)
|
||||
}
|
||||
|
||||
@@ -22,25 +22,51 @@ router.get('/', async (req, res) => {
|
||||
|
||||
router.post('/deploy', async (req, res) => {
|
||||
if (!isFileTree(req.body)) {
|
||||
res.status(400).send(getTreeExample())
|
||||
res.status(400).send({
|
||||
status: 'failure',
|
||||
message: 'Provided not supported data format.',
|
||||
example: getTreeExample()
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
await createFileTree(req.body.members)
|
||||
.then(() => {
|
||||
res.status(200).send('Files deployed successfully to @sasjs/server.')
|
||||
res.status(200).send({
|
||||
status: 'success',
|
||||
message: 'Files deployed successfully to @sasjs/server.'
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(500).send({ message: 'Deployment failed!', ...err })
|
||||
res
|
||||
.status(500)
|
||||
.send({ status: 'failure', message: 'Deployment failed!', ...err })
|
||||
})
|
||||
})
|
||||
|
||||
router.post('/execute', async (req, res) => {
|
||||
if (req.body?._program) {
|
||||
const result: ExecutionResult = await processSas(req.body)
|
||||
await processSas(req.body)
|
||||
.then((result) => {
|
||||
res.status(200).send({
|
||||
status: 'success',
|
||||
message: 'Job has been sent for execution.',
|
||||
...result
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(400).send({
|
||||
status: 'failure',
|
||||
message: 'Job execution failed.',
|
||||
error: err
|
||||
})
|
||||
})
|
||||
} else {
|
||||
res.status(400).send(`Please provide the location of SAS code`)
|
||||
res.status(400).send({
|
||||
status: 'failure',
|
||||
message: `Please provide the location of SAS code`
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user