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

feat: add api endpoint for sasjs drive

This commit is contained in:
2021-10-14 18:21:20 +00:00
parent f90dcff7cf
commit 96b5fef302
4 changed files with 44 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
export * from './sas' export * from './sas'
export * from './deploy' export * from './deploy'
export * from './sasjsExecutor' export * from './sasjsExecutor'
export * from './sasjsDrive'

View File

@@ -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
}
}
}

View File

@@ -2,6 +2,6 @@ import dirTree from 'directory-tree'
import path from 'path' import path from 'path'
export const sasjsExecutor = () => { export const sasjsExecutor = () => {
const tree = dirTree(path.join(__dirname, '..')) const tree = dirTree(path.join(__dirname, '..', '..', 'tmp'))
return tree return tree
} }

View File

@@ -1,9 +1,11 @@
import express from 'express' import express from 'express'
import path from 'path'
import { import {
processSas, processSas,
createFileTree, createFileTree,
getTreeExample, getTreeExample,
sasjsExecutor sasjsExecutor,
sasjsDrive
} from '../controllers' } from '../controllers'
import { ExecutionResult, isRequestQuery, isFileTree } from '../types' 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) => { router.get('/SASjsExecutor', async (req, res) => {
const tree = sasjsExecutor() const tree = sasjsExecutor()
// res.status(200).send({ status: 'success', tree }) res.status(200).send({ status: 'success', tree })
console.log(tree)
res.render('index', { tree })
}) })
router.get('/SASjsExecutor/do', async (req, res) => { router.get('/SASjsExecutor/do', async (req, res) => {