11import { promises as fs } from "fs" ;
22import { join } from "path" ;
33
4+ const ORDER = [
5+ "barcelona" ,
6+ "andorra" ,
7+ "cadi" ,
8+ "vallter-2000" ,
9+ "port-del-comte" ,
10+ "sant-llorenc" ,
11+ ] ;
12+
413interface Collection {
5- name : string ,
6- id : string ,
7- date : Date ,
8- files : string [ ] ,
14+ name : string ;
15+ id : string ;
16+ files : string [ ] ;
917}
1018
1119export function toAscii ( input : string ) : string {
1220 return input
1321 . toLowerCase ( )
1422 . normalize ( "NFD" )
15- . replace ( / [ \u0300 - \u036f ] / g, "" )
16- . replace ( / [ ^ \x00 - \x7F ] / g, "" )
17- . replace ( " " , "-" ) ;
23+ . replaceAll ( / [ \u0300 - \u036f ] / g, "" )
24+ . replaceAll ( / [ ^ \x00 - \x7F ] / g, "" )
25+ . replaceAll ( " " , "-" ) ;
1826}
1927
2028export default defineEventHandler ( async ( ) => {
@@ -28,18 +36,25 @@ export default defineEventHandler(async () => {
2836 for ( const file of directory ) {
2937 if ( ! file . isFile ( ) ) continue ;
3038 if ( file . name == ".DS_Store" ) continue ;
31- const parent = file . parentPath . split ( "/" ) . at ( - 1 ) ! ;
3239
40+ const parent = file . parentPath . split ( "/" ) . at ( - 1 ) ! ;
3341 if ( ! collections . has ( parent ) ) {
34- const stats = await fs . stat ( `${ file . parentPath } /${ file . name } ` ) ;
35- collections . set ( parent , { name : parent , id : toAscii ( parent ) , date : stats . birthtime , files : [ ] } ) ;
42+ collections . set ( parent , { name : parent , id : toAscii ( parent ) , files : [ ] } ) ;
3643 }
3744
3845 const collection = collections . get ( parent ) ! ;
3946 collection . files . push ( file . name ) ;
4047 }
4148
42- const sorted = Array . from ( collections . values ( ) ) . sort ( ( a , b ) => b . date . getTime ( ) - a . date . getTime ( ) ) ;
49+ const sorted = Array . from ( collections . values ( ) ) . sort ( ( a , b ) => {
50+ const ai = ORDER . indexOf ( a . id ) ;
51+ const bi = ORDER . indexOf ( b . id ) ;
4352
53+ if ( ai === - 1 && bi === - 1 ) return a . name . localeCompare ( b . name ) ;
54+ if ( ai === - 1 ) return 1 ;
55+ if ( bi === - 1 ) return - 1 ;
56+ return ai - bi ;
57+ } ) ;
58+
4459 return sorted ;
4560} ) ;
0 commit comments