From 25b03d43451820ca814795d5d7a75ae646d93a99 Mon Sep 17 00:00:00 2001 From: whyour Date: Fri, 23 Sep 2022 19:12:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E7=AE=A1=E7=90=86=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/script.ts | 11 +++++++++-- back/config/util.ts | 16 +++++++++++++++- src/pages/script/index.tsx | 19 +++++++++++++------ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/back/api/script.ts b/back/api/script.ts index 4273459e..372c78d1 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -4,6 +4,7 @@ import { readDirs, getLastModifyFilePath, readDir, + emptyDir, } from '../config/util'; import { Router, Request, Response, NextFunction } from 'express'; import { Container } from 'typedi'; @@ -169,17 +170,23 @@ export default (app: Router) => { body: Joi.object({ filename: Joi.string().required(), path: Joi.string().allow(''), + type: Joi.string().optional() }), }), async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { - let { filename, path } = req.body as { + let { filename, path, type } = req.body as { filename: string; path: string; + type: string; }; const filePath = join(config.scriptPath, path, filename); - fs.unlinkSync(filePath); + if (type === 'directory') { + emptyDir(filePath); + } else { + fs.unlinkSync(filePath); + } res.send({ code: 200 }); } catch (e) { return next(e); diff --git a/back/config/util.ts b/back/config/util.ts index 363ac1b5..b4ade67b 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -302,7 +302,6 @@ export function readDirs( title: file, key, type: 'directory', - disabled: true, parent: relativePath, children: readDirs(subPath, baseDir).sort( (a: any, b: any) => @@ -345,6 +344,21 @@ export function readDir( return result; } +export function emptyDir(path: string) { + const files = fs.readdirSync(path); + files.forEach(file => { + const filePath = `${path}/${file}`; + const stats = fs.statSync(filePath); + if (stats.isDirectory()) { + emptyDir(filePath); + } else { + fs.unlinkSync(filePath); + } + }); + fs.rmdirSync(path); +} + + export function promiseExec(command: string): Promise { return new Promise((resolve, reject) => { exec( diff --git a/src/pages/script/index.tsx b/src/pages/script/index.tsx index 46f02e24..430c6d08 100644 --- a/src/pages/script/index.tsx +++ b/src/pages/script/index.tsx @@ -139,12 +139,17 @@ const Script = () => { if (node.key === select || !value) { return; } - setValue('加载中...'); - const newMode = value ? LangMap[value.slice(-3)] : ''; - setMode(isPhone && newMode === 'typescript' ? 'javascript' : newMode); setSelect(node.key); setTitle(node.key); setCurrentNode(node); + if (node.type === 'directory') { + setValue('请选择脚本文件'); + return; + } + + const newMode = value ? LangMap[value.slice(-3)] : ''; + setMode(isPhone && newMode === 'typescript' ? 'javascript' : newMode); + setValue('加载中...'); getDetail(node); }; @@ -257,11 +262,11 @@ const Script = () => { title: `确认删除`, content: ( <> - 确认删除文件 + 确认删除 {select} - {' '} - ,删除后不可恢复 + + 文件{currentNode.type === 'directory' ? '夹及其子文件':''},删除后不可恢复 ), onOk() { @@ -270,6 +275,7 @@ const Script = () => { data: { filename: currentNode.title, path: currentNode.parent || '', + type: currentNode.type }, }) .then(({ code }) => { @@ -520,6 +526,7 @@ const Script = () => { >