修复调试运行时文件路径

This commit is contained in:
whyour 2021-11-23 23:36:43 +08:00
parent 5a6d3739d4
commit 2f5e946979
4 changed files with 55 additions and 39 deletions

View File

@ -9,7 +9,7 @@ import { Logger } from 'winston';
import config from '../config'; import config from '../config';
import * as fs from 'fs'; import * as fs from 'fs';
import { celebrate, Joi } from 'celebrate'; import { celebrate, Joi } from 'celebrate';
import path from 'path'; import path, { join } from 'path';
import ScriptService from '../services/script'; import ScriptService from '../services/script';
const route = Router(); const route = Router();
@ -44,7 +44,7 @@ export default (app: Router) => {
children.push({ children.push({
title: childFile, title: childFile,
value: childFile, value: childFile,
key: `${fileOrDir}-${childFile}`, key: `${fileOrDir}/${childFile}`,
mtime: statObj.mtimeMs, mtime: statObj.mtimeMs,
parent: fileOrDir, parent: fileOrDir,
}); });
@ -84,10 +84,12 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
const path = req.query.path ? `${req.query.path}/` : ''; const filePath = join(
const content = getFileContentByName( config.scriptPath,
`${config.scriptPath}${path}${req.params.file}`, req.query.path as string,
req.params.file,
); );
const content = getFileContentByName(filePath);
res.send({ code: 200, data: content }); res.send({ code: 200, data: content });
} catch (e) { } catch (e) {
logger.error('🔥 error: %o', e); logger.error('🔥 error: %o', e);
@ -101,7 +103,7 @@ export default (app: Router) => {
celebrate({ celebrate({
body: Joi.object({ body: Joi.object({
filename: Joi.string().required(), filename: Joi.string().required(),
path: Joi.string().allow(''), path: Joi.string().optional().allow(''),
content: Joi.string().allow(''), content: Joi.string().allow(''),
originFilename: Joi.string().allow(''), originFilename: Joi.string().allow(''),
}), }),
@ -161,7 +163,7 @@ export default (app: Router) => {
celebrate({ celebrate({
body: Joi.object({ body: Joi.object({
filename: Joi.string().required(), filename: Joi.string().required(),
path: Joi.string().allow(''), path: Joi.string().optional().allow(''),
content: Joi.string().required(), content: Joi.string().required(),
}), }),
}), }),
@ -173,7 +175,7 @@ export default (app: Router) => {
content: string; content: string;
path: string; path: string;
}; };
const filePath = `${config.scriptPath}${path}/${filename}`; const filePath = join(config.scriptPath, path, filename);
fs.writeFileSync(filePath, content); fs.writeFileSync(filePath, content);
return res.send({ code: 200 }); return res.send({ code: 200 });
} catch (e) { } catch (e) {
@ -198,7 +200,7 @@ export default (app: Router) => {
filename: string; filename: string;
path: string; path: string;
}; };
const filePath = `${config.scriptPath}${path}/${filename}`; const filePath = join(config.scriptPath, path, filename);
fs.unlinkSync(filePath); fs.unlinkSync(filePath);
res.send({ code: 200 }); res.send({ code: 200 });
} catch (e) { } catch (e) {
@ -254,7 +256,7 @@ export default (app: Router) => {
filename: string; filename: string;
path: string; path: string;
}; };
const filePath = `${path}/${filename}`; const filePath = join(path, filename);
const scriptService = Container.get(ScriptService); const scriptService = Container.get(ScriptService);
const result = await scriptService.runScript(filePath); const result = await scriptService.runScript(filePath);
res.send(result); res.send(result);

View File

@ -275,14 +275,16 @@ export default function (props: any) {
}} }}
onCollapse={setCollapsed} onCollapse={setCollapsed}
collapsed={collapsed} collapsed={collapsed}
rightContentRender={() => ( rightContentRender={() =>
<Dropdown overlay={menu} trigger={['click']}> ctx.isPhone && (
<span className="side-menu-user-wrapper"> <Dropdown overlay={menu} trigger={['click']}>
<Avatar shape="square" size="small" icon={<UserOutlined />} /> <span className="side-menu-user-wrapper">
<span style={{ marginLeft: 5 }}>admin</span> <Avatar shape="square" size="small" icon={<UserOutlined />} />
</span> <span style={{ marginLeft: 5 }}>admin</span>
</Dropdown> </span>
)} </Dropdown>
)
}
collapsedButtonRender={(collapsed) => ( collapsedButtonRender={(collapsed) => (
<span <span
className="side-menu-container" className="side-menu-container"

View File

@ -24,22 +24,22 @@ const prefixMap: any = {
const EditModal = ({ const EditModal = ({
treeData, treeData,
currentFile, currentNode,
content, content,
handleCancel, handleCancel,
visible, visible,
socketMessage, socketMessage,
}: { }: {
treeData?: any; treeData?: any;
currentFile?: string;
content?: string; content?: string;
visible: boolean; visible: boolean;
socketMessage: any; socketMessage: any;
currentNode: any;
handleCancel: () => void; handleCancel: () => void;
}) => { }) => {
const [value, setValue] = useState(''); const [value, setValue] = useState('');
const [language, setLanguage] = useState<string>('javascript'); const [language, setLanguage] = useState<string>('javascript');
const [fileName, setFileName] = useState<string>(''); const [cNode, setCNode] = useState<any>();
const [selectedKey, setSelectedKey] = useState<string>(''); const [selectedKey, setSelectedKey] = useState<string>('');
const [saveModalVisible, setSaveModalVisible] = useState<boolean>(false); const [saveModalVisible, setSaveModalVisible] = useState<boolean>(false);
const [settingModalVisible, setSettingModalVisible] = const [settingModalVisible, setSettingModalVisible] =
@ -53,28 +53,31 @@ const EditModal = ({
}; };
const onSelect = (value: any, node: any) => { const onSelect = (value: any, node: any) => {
if (node.value === fileName || !value) { if (node.key === selectedKey || !value) {
return; return;
} }
const newMode = LangMap[value.slice(-3)] || ''; const newMode = LangMap[value.slice(-3)] || '';
setFileName(value); setCNode(node);
setLanguage(newMode); setLanguage(newMode);
getDetail(node); getDetail(node);
setSelectedKey(node.key); setSelectedKey(node.key);
}; };
const getDetail = (node: any) => { const getDetail = (node: any) => {
request.get(`${config.apiPrefix}scripts/${node.value}`).then((data) => { request
setValue(data.data); .get(`${config.apiPrefix}scripts/${node.value}?path=${node.parent || ''}`)
}); .then((data) => {
setValue(data.data);
});
}; };
const run = () => { const run = () => {
setLog('');
request request
.put(`${config.apiPrefix}scripts/run`, { .put(`${config.apiPrefix}scripts/run`, {
data: { data: {
filename: fileName, filename: cNode.value,
path: '', path: cNode.parent || '',
}, },
}) })
.then((data) => {}); .then((data) => {});
@ -98,21 +101,21 @@ const EditModal = ({
}, [socketMessage]); }, [socketMessage]);
useEffect(() => { useEffect(() => {
if (currentFile) { if (currentNode) {
setFileName(currentFile); setCNode(currentNode);
setValue(content as string); setValue(content as string);
setSelectedKey(currentFile); setSelectedKey(currentNode.key);
} }
}, [currentFile, content]); }, [content, currentNode]);
return ( return (
<Drawer <Drawer
className="edit-modal" className="edit-modal"
closable={false}
title={ title={
<> <>
<span style={{ marginRight: 8 }}>{fileName}</span>
<TreeSelect <TreeSelect
style={{ marginRight: 8, width: 120 }} style={{ marginRight: 8, width: 150 }}
value={selectedKey} value={selectedKey}
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }} dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
treeData={treeData} treeData={treeData}
@ -122,7 +125,7 @@ const EditModal = ({
/> />
<Select <Select
value={language} value={language}
style={{ width: 120, marginRight: 8 }} style={{ width: 110, marginRight: 8 }}
onChange={(e) => { onChange={(e) => {
setLanguage(e); setLanguage(e);
}} }}
@ -162,6 +165,15 @@ const EditModal = ({
> >
</Button> </Button>
<Button
type="primary"
style={{ marginRight: 8 }}
onClick={() => {
handleCancel();
}}
>
退
</Button>
</> </>
} }
width={'100%'} width={'100%'}
@ -197,7 +209,7 @@ const EditModal = ({
content: content:
editorRef.current && editorRef.current &&
editorRef.current.getValue().replace(/\r\n/g, '\n'), editorRef.current.getValue().replace(/\r\n/g, '\n'),
filename: fileName, filename: cNode?.value,
}} }}
/> />
<SettingModal <SettingModal

View File

@ -116,12 +116,12 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
node: { node: {
title: s, title: s,
value: s, value: s,
key: p ? `${p}-${s}` : s, key: p ? `${p}/${s}` : s,
parent: p, parent: p,
}, },
}; };
setExpandedKeys([p]); setExpandedKeys([p]);
onTreeSelect([`${p}-${s}`], obj); onTreeSelect([`${p}/${s}`], obj);
} }
}; };
@ -525,7 +525,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
<EditModal <EditModal
visible={isLogModalVisible} visible={isLogModalVisible}
treeData={data} treeData={data}
currentFile={select} currentNode={currentNode}
content={value} content={value}
socketMessage={socketMessage} socketMessage={socketMessage}
handleCancel={() => { handleCancel={() => {