From 50dd235e3913ea692efbce5b3d3db742af4feeb3 Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 11 Nov 2021 00:10:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E7=AE=A1=E7=90=86=E6=94=B9?= =?UTF-8?q?=E6=88=90=E6=A0=91=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/script.ts | 40 +++++++++++++++++++++++++++++--------- src/pages/script/index.tsx | 24 ++++++++++++----------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/back/api/script.ts b/back/api/script.ts index f7933454..0e04e545 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -18,14 +18,33 @@ export default (app: Router) => { const fileList = fs.readdirSync(config.scriptPath, 'utf-8'); res.send({ code: 200, - data: fileList - .filter((x) => { - return !fs.lstatSync(config.scriptPath + x).isDirectory(); - }) - .map((x) => { + data: fileList.map((x) => { + if (fs.lstatSync(config.scriptPath + x).isDirectory()) { + const childFileList = fs.readdirSync( + config.scriptPath + x, + 'utf-8', + ); + return { + title: x, + value: x, + key: x, + disabled: true, + children: childFileList.map((y) => { + const statObj = fs.statSync(`${config.scriptPath}${x}/${y}`); + return { + title: y, + value: y, + key: y, + mtime: statObj.mtimeMs, + parent: x, + }; + }), + }; + } else { const statObj = fs.statSync(config.scriptPath + x); return { title: x, value: x, key: x, mtime: statObj.mtimeMs }; - }), + } + }), }); } catch (e) { logger.error('🔥 error: %o', e); @@ -39,8 +58,9 @@ export default (app: Router) => { async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { + const path = req.query.path ? `${req.query.path}/` : ''; const content = getFileContentByName( - `${config.scriptPath}${req.params.file}`, + `${config.scriptPath}${path}${req.params.file}`, ); res.send({ code: 200, data: content }); } catch (e) { @@ -112,17 +132,19 @@ export default (app: Router) => { celebrate({ body: Joi.object({ filename: Joi.string().required(), + path: Joi.string().allow(''), content: Joi.string().required(), }), }), async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { - let { filename, content } = req.body as { + let { filename, content, path } = req.body as { filename: string; content: string; + path: string; }; - const filePath = `${config.scriptPath}${filename}`; + const filePath = `${config.scriptPath}${path}/${filename}`; fs.writeFileSync(filePath, content); return res.send({ code: 200 }); } catch (e) { diff --git a/src/pages/script/index.tsx b/src/pages/script/index.tsx index c52a5567..c028c081 100644 --- a/src/pages/script/index.tsx +++ b/src/pages/script/index.tsx @@ -69,7 +69,7 @@ const Script = ({ headerStyle, isPhone, theme }: any) => { const [isEditing, setIsEditing] = useState(false); const editorRef = useRef(null); const [isAddFileModalVisible, setIsAddFileModalVisible] = useState(false); - const [dropdownIsVisible, setDropdownIsVisible] = useState(false); + const [currentNode, setCurrentNode] = useState(); const getScripts = () => { setLoading(true); @@ -85,9 +85,11 @@ const Script = ({ headerStyle, isPhone, theme }: any) => { }; const getDetail = (node: any) => { - request.get(`${config.apiPrefix}scripts/${node.value}`).then((data) => { - setValue(data.data); - }); + request + .get(`${config.apiPrefix}scripts/${node.value}?path=${node.parent || ''}`) + .then((data) => { + setValue(data.data); + }); }; const onSelect = (value: any, node: any) => { @@ -99,6 +101,7 @@ const Script = ({ headerStyle, isPhone, theme }: any) => { setMode(isPhone && newMode === 'typescript' ? 'javascript' : newMode); setSelect(value); setTitle(node.parent || node.value); + setCurrentNode(node); getDetail(node); }; @@ -165,11 +168,12 @@ const Script = ({ headerStyle, isPhone, theme }: any) => { const content = editorRef.current ? editorRef.current.getValue().replace(/\r\n/g, '\n') : value; - return new Promise((resolve) => { + return new Promise((resolve, reject) => { request .put(`${config.apiPrefix}scripts`, { data: { filename: select, + path: currentNode.parent || '', content, }, }) @@ -182,7 +186,8 @@ const Script = ({ headerStyle, isPhone, theme }: any) => { message.error(_data); } resolve(null); - }); + }) + .catch((e) => reject(e)); }); }, onCancel() { @@ -270,6 +275,7 @@ const Script = ({ headerStyle, isPhone, theme }: any) => { const { tree } = getFilterData(word.toLocaleLowerCase(), data); setFilterData(tree); setSelect(''); + setCurrentNode(null); setTitle('请选择脚本文件'); setValue('请选择脚本文件'); }, [data]); @@ -322,11 +328,7 @@ const Script = ({ headerStyle, isPhone, theme }: any) => { key="value" onSelect={onSelect} />, - setDropdownIsVisible(visible)} - > +