1- import * as fs from 'fs/promises' ;
1+ import * as fs from 'node: fs/promises' ;
22import _ from 'lodash' ;
33import { FileType , type IndexFile } from '~/utilities/types' ;
44
@@ -17,24 +17,27 @@ export default defineEventHandler(async (event) => {
1717 . filter ( filename => filename . endsWith ( '.yaml' ) || filename . endsWith ( '.yml' ) )
1818 . map ( async ( filename ) => {
1919 const path = `${ filesDir } /${ filename } ` ;
20- const content = await fs . readFile ( path , 'utf8' ) ;
21- let name = filename
22- . replace ( '.yaml' , '' )
23- . replace ( '.yml' , '' ) ;
20+ const fileHandle = await fs . open ( path , 'r' ) ;
2421
2522 let type = FileType . Other ;
26-
27- if (
28- content . includes ( 'packages:' )
29- && content . includes ( 'localbytes.localdeck' )
30- ) type = FileType . Import ;
31-
32- if ( content . includes ( 'localdeck-configurator?config=' ) ) type = FileType . LocalDeck ;
33-
34- const matchName = content . match ( / n a m e : ( .* ) / ) ;
35- if ( matchName ) name = matchName [ 1 ] ;
36- const matchFriendly = content . match ( / f r i e n d l y _ n a m e : ( .* ) / ) ;
37- if ( matchFriendly ) name = matchFriendly [ 1 ] ;
23+ let matchConfig : RegExpMatchArray | null = null ;
24+ let matchName : RegExpMatchArray | null = null ;
25+ let matchFriendly : RegExpMatchArray | null = null ;
26+ let matchPackage : RegExpMatchArray | null = null ;
27+
28+ for await ( const line of fileHandle . readLines ( ) ) {
29+ matchConfig ??= line . match ( / l o c a l d e c k - c o n f i g u r a t o r \? c o n f i g = ( .* ) / ) ;
30+ matchName ??= line . match ( / n a m e : ? ( " ? ) ( .* ) \1/ ) ;
31+ matchFriendly ??= line . match ( / f r i e n d l y _ n a m e : ? ( " ? ) ( .* ) \1/ ) ;
32+ matchPackage ??= line . match ( / l o c a l b y t e s .l o c a l d e c k : g i t h u b : \/ \/ / ) ;
33+
34+ // noinspection PointlessBooleanExpressionJS
35+ if ( matchConfig && matchName && matchFriendly && matchPackage ) break ;
36+ }
37+
38+ const name = matchFriendly ?. [ 2 ] ?? matchName ?. [ 2 ] ?? ( filename . replace ( / \. y a ? m l / , '' ) ) ;
39+ if ( matchPackage ) type = FileType . Import ;
40+ if ( matchConfig ) type = FileType . LocalDeck ;
3841
3942 return { path, filename, type, name } satisfies IndexFile ;
4043 } ) ;
0 commit comments