diff --git a/back/api/script.ts b/back/api/script.ts index a03758ca..e86891c9 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -1,6 +1,7 @@ import { fileExist, getFileContentByName, + readDirs, getLastModifyFilePath, } from '../config/util'; import { Router, Request, Response, NextFunction } from 'express'; @@ -21,53 +22,7 @@ export default (app: Router) => { async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { - const fileList = fs.readdirSync(config.scriptPath, 'utf-8'); - - let result = []; - for (let i = 0; i < fileList.length; i++) { - const fileOrDir = fileList[i]; - const fPath = path.join(config.scriptPath, fileOrDir); - const dirStat = fs.statSync(fPath); - if (['node_modules', 'pnpm-lock.yaml'].includes(fileOrDir)) { - continue; - } - - if (dirStat.isDirectory()) { - const childFileList = fs.readdirSync(fPath, 'utf-8'); - let children = []; - for (let j = 0; j < childFileList.length; j++) { - const childFile = childFileList[j]; - const sPath = path.join(config.scriptPath, fileOrDir, childFile); - const _fileExist = await fileExist(sPath); - if (_fileExist && fs.statSync(sPath).isFile()) { - const statObj = fs.statSync(sPath); - children.push({ - title: childFile, - value: childFile, - key: `${fileOrDir}/${childFile}`, - mtime: statObj.mtimeMs, - parent: fileOrDir, - }); - } - } - result.push({ - title: fileOrDir, - value: fileOrDir, - key: fileOrDir, - mtime: dirStat.mtimeMs, - disabled: true, - children: children.sort((a, b) => b.mtime - a.mtime), - }); - } else { - result.push({ - title: fileOrDir, - value: fileOrDir, - key: fileOrDir, - mtime: dirStat.mtimeMs, - }); - } - } - + const result = readDirs(config.scriptPath, config.scriptPath); res.send({ code: 200, data: result, diff --git a/back/config/util.ts b/back/config/util.ts index 3be63946..f1db23ed 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -276,3 +276,32 @@ export async function concurrentRun( return replyList; } + +export function readDirs(dir: string, baseDir: string = '') { + const relativePath = path.relative(baseDir, dir); + const files = fs.readdirSync(dir); + const result: any = files.map((file: string) => { + const subPath = path.join(dir, file); + const stats = fs.statSync(subPath); + const key = path.join(relativePath, file); + if (stats.isDirectory()) { + return { + title: file, + value: file, + key, + type: 'directory', + disabled: true, + parent: relativePath, + children: readDirs(subPath, baseDir), + }; + } + return { + title: file, + value: file, + type: 'file', + key, + parent: relativePath, + }; + }); + return result; +}