From 96b5fef3021f67f66e5e3b854319230618421852 Mon Sep 17 00:00:00 2001 From: sabhas Date: Thu, 14 Oct 2021 18:21:20 +0000 Subject: [PATCH] feat: add api endpoint for sasjs drive --- src/controllers/index.ts | 1 + src/controllers/sasjsDrive.ts | 24 ++++++++++++++++++++++++ src/controllers/sasjsExecutor.ts | 2 +- src/routes/index.ts | 23 ++++++++++++++++++----- 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/controllers/sasjsDrive.ts diff --git a/src/controllers/index.ts b/src/controllers/index.ts index f6cd664..d08995e 100644 --- a/src/controllers/index.ts +++ b/src/controllers/index.ts @@ -1,3 +1,4 @@ export * from './sas' export * from './deploy' export * from './sasjsExecutor' +export * from './sasjsDrive' diff --git a/src/controllers/sasjsDrive.ts b/src/controllers/sasjsDrive.ts new file mode 100644 index 0000000..079335c --- /dev/null +++ b/src/controllers/sasjsDrive.ts @@ -0,0 +1,24 @@ +import { fileExists, readFile, createFile } from '@sasjs/utils' + +export const sasjsDrive = async ( + filePath: string, + action: string, + newFileContent?: string +) => { + let fileContent + const isFileExists = await fileExists(filePath) + if (isFileExists) { + switch (action) { + case 'read': + fileContent = await readFile(filePath) + return fileContent + case 'update': + if (newFileContent) { + await createFile(filePath, newFileContent) + } + break + default: + break + } + } +} diff --git a/src/controllers/sasjsExecutor.ts b/src/controllers/sasjsExecutor.ts index ae0ab47..eb12d24 100644 --- a/src/controllers/sasjsExecutor.ts +++ b/src/controllers/sasjsExecutor.ts @@ -2,6 +2,6 @@ import dirTree from 'directory-tree' import path from 'path' export const sasjsExecutor = () => { - const tree = dirTree(path.join(__dirname, '..')) + const tree = dirTree(path.join(__dirname, '..', '..', 'tmp')) return tree } diff --git a/src/routes/index.ts b/src/routes/index.ts index 6c9eb69..4ed9d9a 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,9 +1,11 @@ import express from 'express' +import path from 'path' import { processSas, createFileTree, getTreeExample, - sasjsExecutor + sasjsExecutor, + sasjsDrive } from '../controllers' import { ExecutionResult, isRequestQuery, isFileTree } from '../types' @@ -53,12 +55,23 @@ router.post('/deploy', async (req, res) => { }) }) -// TODO: respond with HTML page including file tree +router.get('/SASjsDrive', async (req, res) => { + if (req.query.filepath) { + const fileContent = await sasjsDrive(req.query.filepath as string, 'read') + res.status(200).send({ status: 'success', fileContent: fileContent }) + } else { + res.sendFile(path.join(__dirname, '..', '..', 'Web', 'build', 'index.html')) + } +}) + +router.post('/SASjsDrive', async (req, res) => { + await sasjsDrive(req.body.filePath as string, 'update', req.body.fileContent) + res.status(200).send({ status: 'success' }) +}) + router.get('/SASjsExecutor', async (req, res) => { const tree = sasjsExecutor() - // res.status(200).send({ status: 'success', tree }) - console.log(tree) - res.render('index', { tree }) + res.status(200).send({ status: 'success', tree }) }) router.get('/SASjsExecutor/do', async (req, res) => {