From 5aeefb7955e5e88f9aab2c9bc320745b00637c19 Mon Sep 17 00:00:00 2001 From: sabhas Date: Fri, 22 Oct 2021 19:01:14 +0000 Subject: [PATCH] chore(refactor-api): remove sasjsExecutor file and move code to ExecutionController --- api/src/controllers/Execution.ts | 45 ++++++++++++++++++++++++++-- api/src/controllers/index.ts | 1 - api/src/controllers/sasjsExecutor.ts | 40 ------------------------- api/src/routes/index.ts | 3 +- 4 files changed, 43 insertions(+), 46 deletions(-) delete mode 100644 api/src/controllers/sasjsExecutor.ts diff --git a/api/src/controllers/Execution.ts b/api/src/controllers/Execution.ts index 0410594..a025dd4 100644 --- a/api/src/controllers/Execution.ts +++ b/api/src/controllers/Execution.ts @@ -1,11 +1,13 @@ +import path from 'path' +import fs from 'fs' import { getSessionController } from './' import { readFile, fileExists, createFile } from '@sasjs/utils' -import path from 'path' import { configuration } from '../../package.json' import { promisify } from 'util' import { execFile } from 'child_process' -import { Session } from '../types' -import { generateFileUploadSasCode } from '../utils' +import { Session, TreeNode } from '../types' +import { generateFileUploadSasCode, getTmpFilesFolderPath } from '../utils' + const execFilePromise = promisify(execFile) export class ExecutionController { @@ -105,4 +107,41 @@ ${webout} return Promise.resolve(webout) } + + buildDirectorytree() { + const root: TreeNode = { + name: 'files', + relativePath: '', + absolutePath: getTmpFilesFolderPath(), + children: [] + } + + const stack = [root] + + while (stack.length) { + const currentNode = stack.pop() + + if (currentNode) { + const children = fs.readdirSync(currentNode.absolutePath) + + for (let child of children) { + const absoluteChildPath = `${currentNode.absolutePath}/${child}` + const relativeChildPath = `${currentNode.relativePath}/${child}` + const childNode: TreeNode = { + name: child, + relativePath: relativeChildPath, + absolutePath: absoluteChildPath, + children: [] + } + currentNode.children.push(childNode) + + if (fs.statSync(childNode.absolutePath).isDirectory()) { + stack.push(childNode) + } + } + } + } + + return root + } } diff --git a/api/src/controllers/index.ts b/api/src/controllers/index.ts index a086b5d..ca5fda4 100644 --- a/api/src/controllers/index.ts +++ b/api/src/controllers/index.ts @@ -1,5 +1,4 @@ export * from './deploy' -export * from './sasjsExecutor' export * from './sasjsDrive' export * from './Session' export * from './Execution' diff --git a/api/src/controllers/sasjsExecutor.ts b/api/src/controllers/sasjsExecutor.ts deleted file mode 100644 index 6f13334..0000000 --- a/api/src/controllers/sasjsExecutor.ts +++ /dev/null @@ -1,40 +0,0 @@ -import fs from 'fs' -import { TreeNode } from '../types' -import { getTmpFilesFolderPath } from '../utils' - -export const sasjsExecutor = () => { - const root: TreeNode = { - name: 'files', - relativePath: '', - absolutePath: getTmpFilesFolderPath(), - children: [] - } - - const stack = [root] - - while (stack.length) { - const currentNode = stack.pop() - - if (currentNode) { - const children = fs.readdirSync(currentNode.absolutePath) - - for (let child of children) { - const absoluteChildPath = `${currentNode.absolutePath}/${child}` - const relativeChildPath = `${currentNode.relativePath}/${child}` - const childNode: TreeNode = { - name: child, - relativePath: relativeChildPath, - absolutePath: absoluteChildPath, - children: [] - } - currentNode.children.push(childNode) - - if (fs.statSync(childNode.absolutePath).isDirectory()) { - stack.push(childNode) - } - } - } - } - - return root -} diff --git a/api/src/routes/index.ts b/api/src/routes/index.ts index 3b0a077..a39e0d7 100644 --- a/api/src/routes/index.ts +++ b/api/src/routes/index.ts @@ -3,7 +3,6 @@ import path from 'path' import { createFileTree, getTreeExample, - sasjsExecutor, SASjsDriveController, ExecutionController, FileUploadController @@ -73,7 +72,7 @@ router.post('/SASjsApi/files', async (req, res) => { }) router.get('/SASjsApi/executor', async (req, res) => { - const tree = sasjsExecutor() + const tree = new ExecutionController().buildDirectorytree() res.status(200).send({ status: 'success', tree }) })