修改对比工具数据源,修复编辑器语言

This commit is contained in:
whyour 2024-01-14 16:28:11 +08:00
parent 5c393fef36
commit 1a94a660a5
6 changed files with 71 additions and 33 deletions

View File

@ -98,7 +98,7 @@ ql resetlet
ql resettfa ql resettfa
``` ```
| Parameter | Description | | **Parameter** | **Description** |
|---|---| |---|---|
| file_url | Script address | | file_url | Script address |
| repo_url | Repository address | | repo_url | Repository address |

View File

@ -98,17 +98,17 @@ ql resetlet
ql resettfa ql resettfa
``` ```
| **参数** | **说明** | | **参数** | **说明** |
|------------|---------------------------------------------------| |------------|---------------------------------------------------------------------------------------------|
| file_url | 脚本地址 | | file_url | 脚本地址 |
| repo_url | 仓库地址 | | repo_url | 仓库地址 |
| whitelist | 拉取仓库时的白名单,即就是需要拉取的脚本的路径包含的字符串,多个竖线分割 | | whitelist | 拉取仓库时的白名单,即就是需要拉取的脚本的路径包含的字符串,多个竖线分割 |
| blacklist | 拉取仓库时的黑名单,即就是需要拉取的脚本的路径不包含的字符串,多个竖线分割 | | blacklist | 拉取仓库时的黑名单,即就是需要拉取的脚本的路径不包含的字符串,多个竖线分割 |
| dependence | 拉取仓库需要的依赖文件会直接从仓库拷贝到scripts下的仓库目录不受黑名单影响多个竖线分割 | | dependence | 拉取仓库需要的依赖文件会直接从仓库拷贝到scripts下的仓库目录不受黑名单影响多个竖线分割 |
| extensions | 拉取仓库的文件后缀,多个竖线分割 | | extensions | 拉取仓库的文件后缀,多个竖线分割 |
| branch | 拉取仓库的分支 | | branch | 拉取仓库的分支 |
| days | 需要保留的日志的天数 | | days | 需要保留的日志的天数 |
| file_path | 任务执行时的文件路径 | | file_path | 任务执行时的文件路径 |
## 部署 ## 部署

View File

@ -6,11 +6,27 @@ import config from '../config';
import * as fs from 'fs/promises'; import * as fs from 'fs/promises';
import { celebrate, Joi } from 'celebrate'; import { celebrate, Joi } from 'celebrate';
import { join } from 'path'; import { join } from 'path';
import { SAMPLE_FILES } from '../config/const';
import got from 'got';
const route = Router(); const route = Router();
export default (app: Router) => { export default (app: Router) => {
app.use('/configs', route); app.use('/configs', route);
route.get(
'/sample',
async (req: Request, res: Response, next: NextFunction) => {
try {
res.send({
code: 200,
data: SAMPLE_FILES,
});
} catch (e) {
return next(e);
}
},
);
route.get( route.get(
'/files', '/files',
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
@ -40,9 +56,14 @@ export default (app: Router) => {
if (config.blackFileList.includes(req.params.file)) { if (config.blackFileList.includes(req.params.file)) {
res.send({ code: 403, message: '文件无法访问' }); res.send({ code: 403, message: '文件无法访问' });
} }
if (req.params.file.includes('sample')) { if (req.params.file.startsWith('sample/')) {
const res = await got.get(
`https://gitlab.com/whyour/qinglong/-/raw/master/${req.params.file}`,
);
content = res.body;
} else if (req.params.file.startsWith('data/scripts/')) {
content = await getFileContentByName( content = await getFileContentByName(
join(config.samplePath, req.params.file), join(config.rootPath, req.params.file),
); );
} else { } else {
content = await getFileContentByName( content = await getFileContentByName(

View File

@ -5,3 +5,21 @@ export const QL_COMMAND = 'ql';
export const TASK_PREFIX = `${TASK_COMMAND} `; export const TASK_PREFIX = `${TASK_COMMAND} `;
export const QL_PREFIX = `${QL_COMMAND} `; export const QL_PREFIX = `${QL_COMMAND} `;
export const SAMPLE_FILES = [
{
title: 'config.sample.sh',
value: 'sample/config.sample.sh',
target: 'config.sh',
},
{
title: 'notify.js',
value: 'sample/notify.js',
target: 'data/scripts/sendNotify.js',
},
{
title: 'notify.py',
value: 'sample/notify.py',
target: 'data/scripts/notify.py',
},
];

View File

@ -1,4 +1,4 @@
import intl from 'react-intl-universal' import intl from 'react-intl-universal';
import React, { import React, {
PureComponent, PureComponent,
Fragment, Fragment,
@ -16,6 +16,7 @@ import { useOutletContext } from '@umijs/max';
import { SharedContext } from '@/layouts'; import { SharedContext } from '@/layouts';
import { langs } from '@uiw/codemirror-extensions-langs'; import { langs } from '@uiw/codemirror-extensions-langs';
import { useHotkeys } from 'react-hotkeys-hook'; import { useHotkeys } from 'react-hotkeys-hook';
import { getEditorMode } from '@/utils';
const Config = () => { const Config = () => {
const { headerStyle, isPhone, theme } = useOutletContext<SharedContext>(); const { headerStyle, isPhone, theme } = useOutletContext<SharedContext>();
@ -26,6 +27,7 @@ const Config = () => {
const [data, setData] = useState<any[]>([]); const [data, setData] = useState<any[]>([]);
const editorRef = useRef<any>(null); const editorRef = useRef<any>(null);
const [confirmLoading, setConfirmLoading] = useState(false); const [confirmLoading, setConfirmLoading] = useState(false);
const [language, setLanguage] = useState<string>('shell');
const getConfig = (name: string) => { const getConfig = (name: string) => {
request.get(`${config.apiPrefix}configs/${name}`).then(({ code, data }) => { request.get(`${config.apiPrefix}configs/${name}`).then(({ code, data }) => {
@ -67,6 +69,8 @@ const Config = () => {
setSelect(value); setSelect(value);
setTitle(node.value); setTitle(node.value);
getConfig(node.value); getConfig(node.value);
const newMode = getEditorMode(value);
setLanguage(newMode);
}; };
useHotkeys( useHotkeys(
@ -122,7 +126,7 @@ const Config = () => {
/> />
) : ( ) : (
<Editor <Editor
defaultLanguage="shell" language={language}
value={value} value={value}
theme={theme} theme={theme}
options={{ options={{

View File

@ -9,22 +9,24 @@ import { DiffEditor } from '@monaco-editor/react';
import ReactDiffViewer from 'react-diff-viewer'; import ReactDiffViewer from 'react-diff-viewer';
import { useOutletContext } from '@umijs/max'; import { useOutletContext } from '@umijs/max';
import { SharedContext } from '@/layouts'; import { SharedContext } from '@/layouts';
import { getEditorMode } from '@/utils';
const { Option } = Select; const { Option } = Select;
const Diff = () => { const Diff = () => {
const { headerStyle, isPhone, theme } = useOutletContext<SharedContext>(); const { headerStyle, isPhone, theme } = useOutletContext<SharedContext>();
const [origin, setOrigin] = useState('config.sample.sh'); const [origin, setOrigin] = useState('sample/config.sample.sh');
const [current, setCurrent] = useState('config.sh'); const [current, setCurrent] = useState('config.sh');
const [originValue, setOriginValue] = useState(''); const [originValue, setOriginValue] = useState('');
const [currentValue, setCurrentValue] = useState(''); const [currentValue, setCurrentValue] = useState('');
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [files, setFiles] = useState<any[]>([]); const [files, setFiles] = useState<any[]>([]);
const editorRef = useRef<any>(null); const editorRef = useRef<any>(null);
const [language, setLanguage] = useState<string>('shell');
const getConfig = () => { const getConfig = () => {
request request
.get(`${config.apiPrefix}configs/${current}`) .get(`${config.apiPrefix}configs/${encodeURIComponent(current)}`)
.then(({ code, data }) => { .then(({ code, data }) => {
if (code === 200) { if (code === 200) {
setCurrentValue(data); setCurrentValue(data);
@ -34,7 +36,7 @@ const Diff = () => {
const getSample = () => { const getSample = () => {
request request
.get(`${config.apiPrefix}configs/${origin}`) .get(`${config.apiPrefix}configs/${encodeURIComponent(origin)}`)
.then(({ code, data }) => { .then(({ code, data }) => {
if (code === 200) { if (code === 200) {
setOriginValue(data); setOriginValue(data);
@ -62,7 +64,7 @@ const Diff = () => {
const getFiles = () => { const getFiles = () => {
setLoading(true); setLoading(true);
request request
.get(`${config.apiPrefix}configs/files`) .get(`${config.apiPrefix}configs/sample`)
.then(({ code, data }) => { .then(({ code, data }) => {
if (code === 200) { if (code === 200) {
setFiles(data); setFiles(data);
@ -71,12 +73,11 @@ const Diff = () => {
.finally(() => setLoading(false)); .finally(() => setLoading(false));
}; };
const originFileChange = (value: string) => { const originFileChange = (value: string, op) => {
setCurrent(op.extra.target);
setOrigin(value); setOrigin(value);
}; const newMode = getEditorMode(value);
setLanguage(newMode);
const currentFileChange = (value: string) => {
setCurrent(value);
}; };
useEffect(() => { useEffect(() => {
@ -112,7 +113,7 @@ const Diff = () => {
<Form.Item label={intl.get('源文件')}> <Form.Item label={intl.get('源文件')}>
<Select value={origin} onChange={originFileChange}> <Select value={origin} onChange={originFileChange}>
{files.map((x) => ( {files.map((x) => (
<Option key={x.value} value={x.value}> <Option key={x.value} value={x.value} extra={x}>
{x.title} {x.title}
</Option> </Option>
))} ))}
@ -121,13 +122,7 @@ const Diff = () => {
</Col> </Col>
<Col span={12}> <Col span={12}>
<Form.Item label={intl.get('当前文件')}> <Form.Item label={intl.get('当前文件')}>
<Select value={current} onChange={currentFileChange}> <span className="ant-form-text">{current}</span>
{files.map((x) => (
<Option key={x.value} value={x.value}>
{x.title}
</Option>
))}
</Select>
</Form.Item> </Form.Item>
</Col> </Col>
</Row> </Row>
@ -159,7 +154,7 @@ const Diff = () => {
/> />
) : ( ) : (
<DiffEditor <DiffEditor
language={'shell'} language={language}
original={originValue} original={originValue}
modified={currentValue} modified={currentValue}
options={{ options={{