import path from 'path' ; import { join } from 'path' ; import { existsSync } from 'fs-extra' ; import globby , { GlobbyOptions } from 'globby' ; import { Project } from 'ts-morph' ; import saveSourceFile from '../utils/saveSourceFile' ; import { producedDirname } from '../utils/paths' ; const patterns = [ 'config/**/*.ts' ] ; const cwd = process . cwd ( ) ; export function loadConfigFiles ( ) { let files = [ ] ; for ( const item of patterns ) { let pattern ; const opt = { ignore : [ '**/node_modules/**' ] , onlyFiles : true , cwd , } ; if ( typeof item === 'string' ) pattern = item ; const paths = globby . sync ( pattern , opt ) . map ( i => join ( cwd , i ) ) ; files = [ ... files , ... paths ] ; } return files . map ( i => i . split ( path . sep ) . join ( '/' ) ) ; } export async function genConfig ( ) { const outPath = join ( process . cwd ( ) , producedDirname , 'config.ts' ) ; const project = new Project ( ) ; const sourceFile = project . createSourceFile ( outPath , undefined , { overwrite : true , } ) ; sourceFile . addImportDeclarations ( [ { moduleSpecifier : 'lodash.set' , defaultImport : 'set' , } , ] ) ; const resolversPath = join ( cwd , producedDirname , 'resolvers.ts' ) ; const controllersPath = join ( cwd , producedDirname , 'controllers.ts' ) ; const schedulesPath = join ( cwd , producedDirname , 'schedules.ts' ) ; const eventsPath = join ( cwd , producedDirname , 'events.ts' ) ; const configsPath = join ( cwd , producedDirname , 'configs.ts' ) ; if ( existsSync ( resolversPath ) ) { sourceFile . addStatements ( `import * as resolvers from './resolvers'` ) ; } if ( existsSync ( controllersPath ) ) { sourceFile . addStatements ( `import * as controllers from './controllers'` ) ; } if ( existsSync ( eventsPath ) ) { sourceFile . addStatements ( `import * as events from './events'` ) ; } if ( existsSync ( schedulesPath ) ) { sourceFile . addStatements ( `import * as schedules from './schedules'` ) ; } if ( existsSync ( configsPath ) ) { sourceFile . addStatements ( `import * as configs from './configs'` ) ; } const initialResolvers = existsSync ( resolversPath ) ? 'Object.values(resolvers)' : '[]' ; const initialControllers = existsSync ( controllersPath ) ? 'Object.values(controllers)' : '[]' ; const initialSchedules = existsSync ( schedulesPath ) ? 'Object.values(schedules)' : '[]' ; const initialEvents = existsSync ( eventsPath ) ? 'Object.values(events)' : '[]' ; sourceFile . addStatements ( `
export const config = {
  ...configs
}
set(config, 'controller.controllers', ${ initialControllers })
set(config, 'graphql.resolvers', ${ initialResolvers })
set(config, 'schedule.schedules', ${ initialSchedules })
set(config, 'event.events', ${ initialEvents })
` ) ; await saveSourceFile ( sourceFile ) ; }