From e868e65208b622a61e8dc0872ee8fcc7163e1df0 Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 14 Nov 2022 15:12:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=BB=98=E8=AE=A4=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/config/util.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/back/config/util.ts b/back/config/util.ts index 2ed60963..e3fc0271 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -284,6 +284,20 @@ enum FileType { 'file', } +interface IFile { + title: string; + key: string; + type: 'directory' | 'file', + parent: string; + mtime: number; + children?: IFile[], +} + +export function dirSort(a: IFile, b: IFile) { + 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 +} + export function readDirs( dir: string, baseDir: string = '', @@ -303,10 +317,8 @@ export function readDirs( key, type: 'directory', parent: relativePath, - children: readDirs(subPath, baseDir).sort( - (a: any, b: any) => - (FileType as any)[a.type] - (FileType as any)[b.type], - ), + mtime: stats.mtime.getTime(), + children: readDirs(subPath, baseDir).sort(dirSort), }; } return { @@ -315,11 +327,10 @@ export function readDirs( isLeaf: true, key, parent: relativePath, + mtime: stats.mtime.getTime(), }; }); - return result.sort( - (a: any, b: any) => (FileType as any)[a.type] - (FileType as any)[b.type], - ); + return result.sort(dirSort); } export function readDir(