import * as glob from 'glob' ; import type { IOptions } from 'glob' ; import { DEFAULT_EXTENSIONS } from '@babel/core' ; import { Extensions , ExtensionsFunc , ICompilerOptions , IFinaleCompilerOptions , } from './type' ; import { mergeDefaultOption } from './defaults' ; export const getGlobPattern = ( dir , extensions ) => { if ( extensions . length > 1 ) { return `${ dir }/**/*{${ extensions . join ( ',' ) }}` ; } else if ( extensions . length === 1 ) { return `${ dir }/**/*${ extensions [ 0 ] }` ; } else { return `${ dir }/**/*` ; } } ; export const getFinalExtensions = ( extensions ) => { const isExtensions = ( ext ) : ext is Extensions => Array . isArray ( ext ) ; const isExtensionsFunc = ( ext ) : ext is ExtensionsFunc => typeof ext === 'function' ; if ( isExtensions ( extensions ) ) { return [ ... extensions , ... DEFAULT_EXTENSIONS ] ; } else if ( isExtensionsFunc ( extensions ) ) { return extensions ( DEFAULT_EXTENSIONS ) ; } else { return DEFAULT_EXTENSIONS ; } } ; export const getFilesFromDir = ( { dir , finalExt = [ ] , ignore = [ ] , } ) => { let globFindFilenames = [ ] ; const globPattern = getGlobPattern ( dir , finalExt ) ; globFindFilenames = glob . sync ( globPattern , { ignore } ) ; return globFindFilenames ; } ; export const getFinalCompilerOption = ( option ) : IFinaleCompilerOptions => { const optionWithDefault = mergeDefaultOption ( option ) ; const { sourceDir , ignore , enableWatch = false , watchDir , extensions = DEFAULT_EXTENSIONS , } = option ; let globFindFilenames = [ ] ; const finalExt = getFinalExtensions ( extensions ) ; if ( sourceDir ) { globFindFilenames = getFilesFromDir ( { dir : sourceDir , ignore , finalExt , } ) ; } if ( enableWatch ) { globFindFilenames = getFilesFromDir ( { dir : watchDir as string , ignore , finalExt , } ) ; } return { ... optionWithDefault , filenames : [ ... optionWithDefault . filenames , ... globFindFilenames ] , } ; } ;