import fs from 'fs' ; import path from 'path' ; import { INTERNAL_DIR_ALAIS , INTERNAL_SRC_ALIAS , normalizeToPosixPath , } from '@modern-js/utils' ; import type { Entrypoint } from '@modern-js/types' ; import type { ImportStatement } from './generateCode' ; import { FILE_SYSTEM_ROUTES_FILE_NAME } from './constants' ; export const walkDirectory = ( dir ) : string [ ] => fs . readdirSync ( dir ) . reduce < string [ ] > ( ( previous , filename ) => { const filePath = path . join ( dir , filename ) ; if ( fs . statSync ( filePath ) . isDirectory ( ) ) { return [ ... previous , ... walkDirectory ( filePath ) ] ; } else { return [ ... previous , filePath ] ; } } , [ ] ) ; export const getDefaultImports = ( { entrypoint , srcDirectory , } ) : ImportStatement [ ] => { const { entryName , fileSystemRoutes , customBootstrap , entry } = entrypoint ; const imports = [ { specifiers : [ { local : 'React' } ] , value : 'react' , } , { specifiers : [ { imported : 'createApp' } , { imported : 'bootstrap' } ] , value : '@modern-js/runtime' , } , customBootstrap && { specifiers : [ { local : 'customBootstrap' } ] , value : normalizeToPosixPath ( customBootstrap . replace ( srcDirectory , INTERNAL_SRC_ALIAS ) ) , } , ] . filter ( Boolean ) as ImportStatement [ ] ; if ( fileSystemRoutes ) { const route = { specifiers : [ { imported : 'routes' } ] , value : normalizeToPosixPath ( `${ INTERNAL_DIR_ALAIS }/${ entryName }/${ FILE_SYSTEM_ROUTES_FILE_NAME }` ) , } ; if ( fileSystemRoutes . globalApp ) { imports . push ( { specifiers : [ { local : 'App' } ] , value : normalizeToPosixPath ( fileSystemRoutes . globalApp . replace ( srcDirectory , INTERNAL_SRC_ALIAS ) ) , } ) ; } else { route . initialize = 'const App = false;' ; } imports . push ( route ) ; } else { imports . push ( { specifiers : [ { local : 'App' } ] , value : normalizeToPosixPath ( entry . replace ( srcDirectory , INTERNAL_SRC_ALIAS ) ) , } ) ; } return imports ; } ;