From af74afd10d2db6adc1dd21cc243523312cff3cfa Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 23 May 2022 19:22:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/script.ts | 49 ++------------------------------------------- back/config/util.ts | 29 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 47 deletions(-) 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; +}