mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 11:24:35 +00:00
feat: add api endpoint for sasjs drive
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * from './sas'
|
||||
export * from './deploy'
|
||||
export * from './sasjsExecutor'
|
||||
export * from './sasjsDrive'
|
||||
|
||||
24
src/controllers/sasjsDrive.ts
Normal file
24
src/controllers/sasjsDrive.ts
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user