diff --git a/src/controllers/deploy.ts b/src/controllers/deploy.ts index 9d8c44d..3df7c43 100644 --- a/src/controllers/deploy.ts +++ b/src/controllers/deploy.ts @@ -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!;' + } + ] + } + ] + } + ] }) diff --git a/src/controllers/sas.ts b/src/controllers/sas.ts index b2c784a..d64319f 100644 --- a/src/controllers/sas.ts +++ b/src/controllers/sas.ts @@ -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 => - new Promise(async (resolve, reject) => { - let sasCodePath = path.join(getTmpFilesFolderPath(), query._program) - sasCodePath = sasCodePath.replace(new RegExp('/', 'g'), path.sep) +): Promise => { + 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) +} diff --git a/src/routes/index.ts b/src/routes/index.ts index dd325ed..4f36222 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -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` + }) } })