修复调试功能

This commit is contained in:
whyour 2021-11-20 00:58:23 +08:00
parent 8079e4e7b8
commit 274a6ce52e
6 changed files with 113 additions and 9 deletions

View File

@ -10,6 +10,7 @@ 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 from 'path';
import ScriptService from '../services/script';
const route = Router(); const route = Router();
export default (app: Router) => { export default (app: Router) => {
@ -237,4 +238,30 @@ export default (app: Router) => {
} }
}, },
); );
route.put(
'/scripts/run',
celebrate({
body: Joi.object({
filename: Joi.string().required(),
path: Joi.string().optional().allow(''),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
let { filename, path } = req.body as {
filename: string;
path: string;
};
const filePath = `${path}/${filename}`;
const scriptService = Container.get(ScriptService);
const result = await scriptService.runScript(filePath);
res.send(result);
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
}; };

View File

@ -13,4 +13,5 @@ export class SockMessage {
export type SockMessageType = export type SockMessageType =
| 'ping' | 'ping'
| 'installDependence' | 'installDependence'
| 'updateSystemVersion'; | 'updateSystemVersion'
| 'manuallyRunScript';

43
back/services/script.ts Normal file
View File

@ -0,0 +1,43 @@
import { Service, Inject } from 'typedi';
import winston from 'winston';
import { spawn } from 'child_process';
import SockService from './sock';
@Service()
export default class ScriptService {
constructor(
@Inject('logger') private logger: winston.Logger,
private sockService: SockService,
) {}
public async runScript(path: string) {
const cp = spawn(`task -l ${path} now`, { shell: '/bin/bash' });
this.sockService.sendMessage({
type: 'manuallyRunScript',
message: `开始执行脚本`,
});
cp.stdout.on('data', (data) => {
this.sockService.sendMessage({
type: 'manuallyRunScript',
message: data.toString(),
});
});
cp.stderr.on('data', (data) => {
this.sockService.sendMessage({
type: 'manuallyRunScript',
message: data.toString(),
});
});
cp.on('error', (err) => {
this.sockService.sendMessage({
type: 'manuallyRunScript',
message: JSON.stringify(err),
});
});
return { code: 200 };
}
}

View File

@ -235,6 +235,7 @@
box-shadow: none; box-shadow: none;
transition: background-color 5000s ease-in-out 0s; transition: background-color 5000s ease-in-out 0s;
-webkit-text-fill-color: @text-color; -webkit-text-fill-color: @text-color;
caret-color: #e8e6f3 !important;
} }
::placeholder { ::placeholder {

View File

@ -28,16 +28,19 @@ const EditModal = ({
content, content,
handleCancel, handleCancel,
visible, visible,
socketMessage,
}: { }: {
treeData?: any; treeData?: any;
currentFile?: string; currentFile?: string;
content?: string; content?: string;
visible: boolean; visible: boolean;
socketMessage: 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 [fileName, setFileName] = 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] =
useState<boolean>(false); useState<boolean>(false);
@ -50,10 +53,14 @@ const EditModal = ({
}; };
const onSelect = (value: any, node: any) => { const onSelect = (value: any, node: any) => {
if (node.value === fileName || !value) {
return;
}
const newMode = LangMap[value.slice(-3)] || ''; const newMode = LangMap[value.slice(-3)] || '';
setFileName(value); setFileName(value);
setLanguage(newMode); setLanguage(newMode);
getDetail(node); getDetail(node);
setSelectedKey(node.key);
}; };
const getDetail = (node: any) => { const getDetail = (node: any) => {
@ -62,12 +69,39 @@ const EditModal = ({
}); });
}; };
const run = () => {}; const run = () => {
request
.put(`${config.apiPrefix}scripts/run`, {
data: {
filename: fileName,
path: '',
},
})
.then((data) => {});
};
useEffect(() => {
if (!socketMessage) {
return;
}
let { type, message: _message, references } = socketMessage;
if (type !== 'manuallyRunScript') {
return;
}
if (log) {
_message = `\n${_message}`;
}
setLog(`${log}${_message}`);
}, [socketMessage]);
useEffect(() => { useEffect(() => {
if (currentFile) { if (currentFile) {
setFileName(currentFile); setFileName(currentFile);
setValue(content as string); setValue(content as string);
setSelectedKey(currentFile);
} }
}, [currentFile, content]); }, [currentFile, content]);
@ -79,12 +113,11 @@ const EditModal = ({
<span style={{ marginRight: 8 }}>{fileName}</span> <span style={{ marginRight: 8 }}>{fileName}</span>
<TreeSelect <TreeSelect
style={{ marginRight: 8, width: 120 }} style={{ marginRight: 8, width: 120 }}
value={currentFile} value={selectedKey}
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }} dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
treeData={treeData} treeData={treeData}
placeholder="请选择脚本文件" placeholder="请选择脚本文件"
showSearch showSearch
key="value"
onSelect={onSelect} onSelect={onSelect}
/> />
<Select <Select

View File

@ -71,7 +71,7 @@ const LangMap: any = {
'.ts': 'typescript', '.ts': 'typescript',
}; };
const Script = ({ headerStyle, isPhone, theme }: any) => { const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
const [title, setTitle] = useState('请选择脚本文件'); const [title, setTitle] = useState('请选择脚本文件');
const [value, setValue] = useState('请选择脚本文件'); const [value, setValue] = useState('请选择脚本文件');
const [select, setSelect] = useState<any>(); const [select, setSelect] = useState<any>();
@ -96,11 +96,9 @@ const Script = ({ headerStyle, isPhone, theme }: any) => {
.then((data) => { .then((data) => {
setData(data.data); setData(data.data);
setFilterData(data.data); setFilterData(data.data);
})
.finally(() => {
setLoading(false);
initGetScript(); initGetScript();
}); })
.finally(() => setLoading(false));
}; };
const getDetail = (node: any) => { const getDetail = (node: any) => {
@ -529,6 +527,7 @@ const Script = ({ headerStyle, isPhone, theme }: any) => {
treeData={data} treeData={data}
currentFile={select} currentFile={select}
content={value} content={value}
socketMessage={socketMessage}
handleCancel={() => { handleCancel={() => {
setIsLogModalVisible(false); setIsLogModalVisible(false);
}} }}