From f8aba4b1fbb5f0803e2577dd16b3b47ffdbf6c87 Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 21 Dec 2023 09:57:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=92=8C=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6=E5=A4=A7=E5=B0=8F=E5=B1=95?= =?UTF-8?q?=E7=A4=BA=EF=BC=8C=E4=BF=AE=E6=94=B9=E8=84=9A=E6=9C=AC=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=88=97=E8=A1=A8=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 7 ---- .prettierignore | 9 +---- back/api/script.ts | 7 ++++ back/config/util.ts | 76 ++++++++++++++++++++++---------------- src/pages/log/index.tsx | 18 +++++++-- src/pages/script/index.tsx | 15 ++++++-- 6 files changed, 78 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index 9128f999..5190b541 100644 --- a/.gitignore +++ b/.gitignore @@ -22,11 +22,4 @@ .env .history .version.ts - -/config -/log -/db -/manual_log -/scripts -/bak /.tmp \ No newline at end of file diff --git a/.prettierignore b/.prettierignore index ce02f600..5179ceca 100644 --- a/.prettierignore +++ b/.prettierignore @@ -21,12 +21,5 @@ src/.umi-production src/.umi-test .env.local .env -history version.ts -config -log -db -manual_log -scripts -bak -.tmp \ No newline at end of file +/.tmp \ No newline at end of file diff --git a/back/api/script.ts b/back/api/script.ts index ab2c5177..368b3104 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -46,6 +46,13 @@ export default (app: Router) => { config.scriptPath, config.scriptPath, blacklist, + (a, b) => { + if (a.type === b.type) { + return a.title.localeCompare(b.title); + } else { + return a.type === 'directory' ? -1 : 1; + } + }, ); } res.send({ diff --git a/back/config/util.ts b/back/config/util.ts index 14d4a49c..b7621752 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -234,12 +234,12 @@ interface IFile { } export function dirSort(a: IFile, b: IFile): number { - if (a.type !== b.type) { - return FileType[a.type] < FileType[b.type] ? -1 : 1 - } else if (a.mtime !== b.mtime) { - return a.mtime > b.mtime ? -1 : 1 + if (a.type === 'file' && b.type === 'file') { + return b.mtime - a.mtime; + } else if (a.type === 'directory' && b.type === 'directory') { + return a.title.localeCompare(b.title); } else { - return 0; + return a.type === 'directory' ? -1 : 1; } } @@ -247,36 +247,39 @@ export async function readDirs( dir: string, baseDir: string = '', blacklist: string[] = [], + sort: (a: IFile, b: IFile) => number = dirSort, ): Promise { const relativePath = path.relative(baseDir, dir); const files = await fs.readdir(dir); - const result: IFile[] = await Promise.all(files - .filter((x) => !blacklist.includes(x)) - .map(async (file: string) => { - const subPath = path.join(dir, file); - const stats = await fs.stat(subPath); - const key = path.join(relativePath, file); - if (stats.isDirectory()) { + const result: IFile[] = await Promise.all( + files + .filter((x) => !blacklist.includes(x)) + .map(async (file: string) => { + const subPath = path.join(dir, file); + const stats = await fs.stat(subPath); + const key = path.join(relativePath, file); + if (stats.isDirectory()) { + return { + title: file, + key, + type: 'directory', + parent: relativePath, + mtime: stats.mtime.getTime(), + children: (await readDirs(subPath, baseDir)).sort(sort), + }; + } return { title: file, + type: 'file', + isLeaf: true, key, - type: 'directory', parent: relativePath, + size: stats.size, mtime: stats.mtime.getTime(), - children: (await readDirs(subPath, baseDir)).sort(dirSort), }; - } - return { - title: file, - type: 'file', - isLeaf: true, - key, - parent: relativePath, - size: stats.size, - mtime: stats.mtime.getTime(), - }; - })); - return result.sort(dirSort); + }), + ); + return result.sort(sort); } export async function readDir( @@ -304,7 +307,10 @@ export async function readDir( export async function promiseExec(command: string): Promise { try { - const { stderr, stdout } = await promisify(exec)(command, { maxBuffer: 200 * 1024 * 1024, encoding: 'utf8' }); + const { stderr, stdout } = await promisify(exec)(command, { + maxBuffer: 200 * 1024 * 1024, + encoding: 'utf8', + }); return stdout || stderr; } catch (error) { return JSON.stringify(error); @@ -313,7 +319,10 @@ export async function promiseExec(command: string): Promise { export async function promiseExecSuccess(command: string): Promise { try { - const { stdout } = await promisify(exec)(command, { maxBuffer: 200 * 1024 * 1024, encoding: 'utf8' }); + const { stdout } = await promisify(exec)(command, { + maxBuffer: 200 * 1024 * 1024, + encoding: 'utf8', + }); return stdout || ''; } catch (error) { return ''; @@ -413,7 +422,7 @@ export async function killTask(pid: number) { [pid, ...pids].reverse().forEach((x) => { process.kill(x, 15); }); - } catch (error) { } + } catch (error) {} } else { process.kill(pid, 2); } @@ -440,7 +449,10 @@ export async function parseContentVersion(content: string): Promise { return load(content) as IVersion; } -export async function getUniqPath(command: string, id: string): Promise { +export async function getUniqPath( + command: string, + id: string, +): Promise { if (/^\d+$/.test(id)) { id = `_${id}`; } else { @@ -482,7 +494,7 @@ export function safeJSONParse(value?: string) { try { return JSON.parse(value); } catch (error) { - Logger.error('[JSON.parse失败]', error) + Logger.error('[JSON.parse失败]', error); return {}; } } @@ -494,6 +506,6 @@ export async function rmPath(path: string) { await fs.rm(path, { force: true, recursive: true, maxRetries: 5 }); } } catch (error) { - Logger.error('[rmPath失败]', error) + Logger.error('[rmPath失败]', error); } } diff --git a/src/pages/log/index.tsx b/src/pages/log/index.tsx index a9e7dd3e..b71ad925 100644 --- a/src/pages/log/index.tsx +++ b/src/pages/log/index.tsx @@ -1,4 +1,4 @@ -import intl from 'react-intl-universal' +import intl from 'react-intl-universal'; import { useState, useEffect, useCallback, Key, useRef } from 'react'; import { TreeSelect, @@ -31,7 +31,7 @@ const { Text } = Typography; const Log = () => { const { headerStyle, isPhone, theme } = useOutletContext(); const [value, setValue] = useState(intl.get('请选择日志文件')); - const [select, setSelect] = useState(''); + const [select, setSelect] = useState(intl.get('请选择日志文件')); const [data, setData] = useState([]); const [loading, setLoading] = useState(false); const [height, setHeight] = useState(); @@ -117,7 +117,8 @@ const Log = () => { {select} - {intl.get('文件')}{currentNode.type === 'directory' ? intl.get('夹下所以日志') : ''} + {intl.get('文件')} + {currentNode.type === 'directory' ? intl.get('夹下所以日志') : ''} {intl.get(',删除后不可恢复')} ), @@ -181,7 +182,16 @@ const Log = () => { return ( + {select} + {currentNode?.type === 'file' && ( + + {(currentNode.size / 1024).toFixed(3)}KB + + )} + + } loading={loading} extra={ isPhone diff --git a/src/pages/script/index.tsx b/src/pages/script/index.tsx index a459f380..8b6914a1 100644 --- a/src/pages/script/index.tsx +++ b/src/pages/script/index.tsx @@ -50,7 +50,7 @@ const { Text } = Typography; const Script = () => { const { headerStyle, isPhone, theme } = useOutletContext(); const [value, setValue] = useState(intl.get('请选择脚本文件')); - const [select, setSelect] = useState(''); + const [select, setSelect] = useState(intl.get('请选择脚本文件')); const [data, setData] = useState([]); const [loading, setLoading] = useState(false); const [mode, setMode] = useState(''); @@ -363,7 +363,7 @@ const Script = () => { }; const initState = () => { - setSelect(''); + setSelect(intl.get('请选择脚本文件')); setCurrentNode(null); setValue(intl.get('请选择脚本文件')); }; @@ -462,7 +462,16 @@ const Script = () => { return ( + {select} + {currentNode?.type === 'file' && ( + + {(currentNode.size / 1024).toFixed(3)}KB + + )} + + } loading={loading} extra={ isPhone