修复调试功能

This commit is contained in:
whyour 2021-11-20 00:58:23 +08:00
parent 5e94be21aa
commit 40b0e90c0d
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 { celebrate, Joi } from 'celebrate';
import path from 'path';
import ScriptService from '../services/script';
const route = 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 =
| 'ping'
| '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;
transition: background-color 5000s ease-in-out 0s;
-webkit-text-fill-color: @text-color;
caret-color: #e8e6f3 !important;
}
::placeholder {

View File

@ -28,16 +28,19 @@ const EditModal = ({
content,
handleCancel,
visible,
socketMessage,
}: {
treeData?: any;
currentFile?: string;
content?: string;
visible: boolean;
socketMessage: any;
handleCancel: () => void;
}) => {
const [value, setValue] = useState('');
const [language, setLanguage] = useState<string>('javascript');
const [fileName, setFileName] = useState<string>('');
const [selectedKey, setSelectedKey] = useState<string>('');
const [saveModalVisible, setSaveModalVisible] = useState<boolean>(false);
const [settingModalVisible, setSettingModalVisible] =
useState<boolean>(false);
@ -50,10 +53,14 @@ const EditModal = ({
};
const onSelect = (value: any, node: any) => {
if (node.value === fileName || !value) {
return;
}
const newMode = LangMap[value.slice(-3)] || '';
setFileName(value);
setLanguage(newMode);
getDetail(node);
setSelectedKey(node.key);
};
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(() => {
if (currentFile) {
setFileName(currentFile);
setValue(content as string);
setSelectedKey(currentFile);
}
}, [currentFile, content]);
@ -79,12 +113,11 @@ const EditModal = ({
<span style={{ marginRight: 8 }}>{fileName}</span>
<TreeSelect
style={{ marginRight: 8, width: 120 }}
value={currentFile}
value={selectedKey}
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
treeData={treeData}
placeholder="请选择脚本文件"
showSearch
key="value"
onSelect={onSelect}
/>
<Select

View File

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