const fs = require ( 'fs' ) ; const path = require ( 'path' ) ; const getFileSuffix = ( filename ) => { if ( filename && filename . includes ( '.' ) ) { return filename . split ( '.' ) . pop ( ) . toLocaleLowerCase ( ) ; } return '' ; } ; param dirPath param existFiles export const getFilesFromDirectory = ( dirPaths , supportedSuffix , existFiles ? ) => { const dirFiles = existFiles || [ ] ; const dirPath = Array . isArray ( dirPaths ) ? dirPaths [ 0 ] : dirPaths ; const stat = fs . lstatSync ( dirPath ) ; if ( stat . isDirectory ( ) ) { const files = fs . readdirSync ( dirPath ) ; files . forEach ( ( file ) => { const filePath = path . resolve ( dirPath . toString ( ) , file ) ; const fileStat = fs . statSync ( filePath ) ; if ( fileStat . isFile ( ) ) { const suffix = getFileSuffix ( file ) ; if ( ! supportedSuffix || supportedSuffix . includes ( suffix ) ) { dirFiles . push ( filePath ) ; } } if ( fileStat . isDirectory ( ) ) { getFilesFromDirectory ( filePath , supportedSuffix , dirFiles ) ; } } ) ; } return dirFiles ; } ; param path param supportedSuffix returns export const getResultFromImg = ( path , supportedSuffix ) => { const fileName = path . split ( '.' ) ; const suffix = fileName . slice ( - 1 ) [ 0 ] ?. toLowerCase ( ) ?? '' ; if ( supportedSuffix . includes ( suffix ) ) { fileName . push ( 'json' ) ; return fileName . join ( '.' ) ; } return '' ; } ; param files param supportedSuffix param path param resultPath returns export const getResultFromFiles = ( files , supportedSuffix , path , resultPath ) => { return files . map ( ( url , i ) => { const fileName = getImgRelativePath ( url , path , resultPath ) ; try { const result = fs . readFileSync ( getResultFromImg ( getResultPathFromImgPath ( url , path , resultPath ) , supportedSuffix ) ) ; return { id : i + 1 , result : result . toString ( ) , url , fileName } ; } catch { return { id : i + 1 , result : '{}' , url , fileName } ; } } ) ; } ; param url param path param resultPath returns export const getResultPathFromImgPath = ( url , path , resultPath ) => { const urls = url . split ( path ) ; if ( urls . length === 1 ) { return url ; } urls . shift ( ) ; urls . unshift ( resultPath ) ; return urls . join ( '' ) ; } ; param url param path param resultPath returns export const getImgRelativePath = ( url , path , resultPath ) => { const urls = url . split ( path ) ; if ( urls . length === 1 ) { return url ; } urls . shift ( ) ; return urls . join ( '' ) ; } ;