import { Project } from 'ts-morph' ; import path from 'path' ; import { join } from 'path' ; import fs from 'fs-extra' ; import saveSourceFile from '../utils/saveSourceFile' ; import globby , { GlobbyOptions } from 'globby' ; import { producedDirname } from '../utils/paths' ; const resolvers = [ '**/*.resolver.ts' ] ; export async function loadResolverFiles ( ) { let files = [ ] ; let cwd = process . cwd ( ) ; for ( const item of resolvers ) { 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 genResolvers ( ) { const project = new Project ( ) ; const filePath = path . resolve ( process . cwd ( ) , producedDirname , 'resolvers.ts' ) ; if ( fs . existsSync ( filePath ) ) { fs . removeSync ( filePath ) ; } const sourceFile = project . createSourceFile ( filePath , undefined , { overwrite : true , } ) ; const files = await loadResolverFiles ( ) ; const cwd = process . cwd ( ) ; for ( const file of files ) { sourceFile . addExportDeclaration ( { moduleSpecifier : '..' + file . replace ( cwd , '' ) . replace ( /\.ts$/ , '' ) , } ) ; } if ( files . length ) { await saveSourceFile ( sourceFile ) ; } }