From 095fcdbe69c9952c3ad0f8b7df52153451d9997f Mon Sep 17 00:00:00 2001 From: whyour Date: Sat, 17 Dec 2022 12:45:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E5=BF=AB=E6=8D=B7=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/crontab/detail.tsx | 21 +++++---------------- src/pages/crontab/index.tsx | 23 ++++++----------------- src/utils/index.ts | 30 +++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/pages/crontab/detail.tsx b/src/pages/crontab/detail.tsx index 56ec7ea0..1587e891 100644 --- a/src/pages/crontab/detail.tsx +++ b/src/pages/crontab/detail.tsx @@ -29,6 +29,7 @@ import config from '@/utils/config'; import CronLogModal from './logModal'; import Editor from '@monaco-editor/react'; import IconFont from '@/components/iconfont'; +import { getCommandScript } from '@/utils'; const { Text } = Typography; @@ -147,22 +148,10 @@ const CronDetailModal = ({ }; const getScript = () => { - const cmd = cron.command.split(' ') as string[]; - if (cmd[0] === 'task') { + const result = getCommandScript(cron.command); + if (Array.isArray(result)) { setValidTabs(validTabs); - if (cmd[1].startsWith('/ql/data/scripts')) { - cmd[1] = cmd[1].replace('/ql/data/scripts/', ''); - } - - let p: string, s: string; - let index = cmd[1].lastIndexOf('/'); - if (index >= 0) { - s = cmd[1].slice(index + 1); - p = cmd[1].slice(0, index); - } else { - s = cmd[1]; - p = ''; - } + const [s, p] = result; setScriptInfo({ parent: p, filename: s }); request .get(`${config.apiPrefix}scripts/${s}?path=${p || ''}`) @@ -171,7 +160,7 @@ const CronDetailModal = ({ setValue(data); } }); - } else { + } else if (result) { setValidTabs([validTabs[0]]); } }; diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index eb6d9dd5..f5d3176e 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -50,6 +50,7 @@ import ViewManageModal from './viewManageModal'; import { FilterValue, SorterResult } from 'antd/lib/table/interface'; import { SharedContext } from '@/layouts'; import useTableScrollHeight from '@/hooks/useTableScrollHeight'; +import { getCommandScript } from '@/utils'; const { Text, Paragraph } = Typography; const { Search } = Input; @@ -390,24 +391,12 @@ const Crontab = () => { const tableScrollHeight = useTableScrollHeight(tableRef); const goToScriptManager = (record: any) => { - const cmd = record.command.split(' ') as string[]; - if (cmd[0] === 'task') { - if (cmd[1].startsWith('/ql/data/scripts')) { - cmd[1] = cmd[1].replace('/ql/data/scripts/', ''); - } - - let p: string, s: string; - let index = cmd[1].lastIndexOf('/'); - if (index >= 0) { - s = cmd[1].slice(index + 1); - p = cmd[1].slice(0, index); - } else { - s = cmd[1]; - p = ''; - } + const result = getCommandScript(record.command); + if (Array.isArray(result)) { + const [s, p] = result; history.push(`/script?p=${p}&s=${s}`); - } else if (cmd[1] === 'repo') { - location.href = cmd[2]; + } else if (result) { + location.href = result; } }; diff --git a/src/utils/index.ts b/src/utils/index.ts index 244866f1..ce7ac8db 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -198,7 +198,7 @@ export function getTableScroll({ if (tHeader) { mainTop = tHeader.getBoundingClientRect().top; } - + //窗体高度-表格内容顶部的高度-表格内容底部的高度 let height = document.body.clientHeight - mainTop - extraHeight; return height; @@ -276,3 +276,31 @@ export function logEnded(log: string): boolean { const endTips = [LOG_END_SYMBOL, '执行结束']; return endTips.some((x) => log.includes(x)); } + +export function getCommandScript( + command: string, +): [string, string] | string | undefined { + const cmd = command.split(' ') as string[]; + if (cmd[0] === 'task') { + let scriptsPart = cmd.find((x) => + ['.js', '.ts', '.sh', '.py'].some((y) => x.endsWith(y)), + ); + if (!scriptsPart) return; + if (scriptsPart.startsWith('/ql/data/scripts')) { + scriptsPart = scriptsPart.replace('/ql/data/scripts/', ''); + } + + let p: string, s: string; + let index = scriptsPart.lastIndexOf('/'); + if (index >= 0) { + s = scriptsPart.slice(index + 1); + p = scriptsPart.slice(0, index); + } else { + s = scriptsPart; + p = ''; + } + return [s, p]; + } else if (cmd[1] === 'repo') { + return cmd[2]; + } +}