import * as chalk from 'chalk' ; import { Answers } from 'inquirer' ; import { Input } from '../commands' ; import { getValueOrDefault } from '../lib/compiler/helpers/get-value-or-default' ; import { AbstractCollection , Collection , CollectionFactory , SchematicOption , } from '../lib/schematics' ; import { MESSAGES } from '../lib/ui' ; import { loadConfiguration } from '../lib/utils/load-configuration' ; import { askForProjectName , moveDefaultProjectToStart , shouldAskForProject , shouldGenerateSpec , } from '../lib/utils/project-utils' ; import { AbstractAction } from './abstract.action' ; export class GenerateAction extends AbstractAction { public async handle ( inputs , options ) { await generateFiles ( inputs . concat ( options ) ) ; } } const generateFiles = async ( inputs ) => { const configuration = await loadConfiguration ( ) ; const collectionOption = inputs . find ( ( option ) => option . name === 'collection' ) ! . value as string ; const schematic = inputs . find ( ( option ) => option . name === 'schematic' ) ! . value as string ; const appName = inputs . find ( ( option ) => option . name === 'project' ) ! . value as string ; const spec = inputs . find ( ( option ) => option . name === 'spec' ) ; const collection = CollectionFactory . create ( collectionOption || configuration . collection || Collection . NESTJS ) ; const schematicOptions = mapSchematicOptions ( inputs ) ; schematicOptions . push ( new SchematicOption ( 'language' , configuration . language ) ) ; const configurationProjects = configuration . projects ; let sourceRoot = appName ? getValueOrDefault ( configuration , 'sourceRoot' , appName ) : configuration . sourceRoot ; const specValue = spec ! . value as boolean ; const specOptions = spec ! . options as any ; let generateSpec = shouldGenerateSpec ( configuration , schematic , appName , specValue , specOptions . passedAsInput ) ; if ( shouldAskForProject ( schematic , configurationProjects , appName ) ) { const defaultLabel = ' [ Default ]' ; let defaultProjectName = configuration . sourceRoot + defaultLabel ; for ( const property in configurationProjects ) { if ( configurationProjects [ property ] . sourceRoot === configuration . sourceRoot ) { defaultProjectName = property + defaultLabel ; break ; } } const projects = moveDefaultProjectToStart ( configuration , defaultProjectName , defaultLabel ) ; const answers = await askForProjectName ( MESSAGES . PROJECT_SELECTION_QUESTION , projects ) ; const project = answers . appName . replace ( defaultLabel , '' ) ; if ( project !== configuration . sourceRoot ) { sourceRoot = configurationProjects [ project ] . sourceRoot ; } if ( answers . appName !== defaultProjectName ) { generateSpec = shouldGenerateSpec ( configuration , schematic , answers . appName , specValue , specOptions . passedAsInput ) ; } } schematicOptions . push ( new SchematicOption ( 'sourceRoot' , sourceRoot ) ) ; schematicOptions . push ( new SchematicOption ( 'spec' , generateSpec ) ) ; try { const schematicInput = inputs . find ( ( input ) => input . name === 'schematic' ) ; if ( ! schematicInput ) { throw new Error ( 'Unable to find a schematic for this configuration' ) ; } await collection . execute ( schematicInput . value as string , schematicOptions ) ; } catch ( error ) { if ( error && error . message ) { console . error ( chalk . red ( error . message ) ) ; } } } ; const mapSchematicOptions = ( inputs ) : SchematicOption [ ] => { const excludedInputNames = [ 'schematic' , 'spec' ] ; const options = [ ] ; inputs . forEach ( ( input ) => { if ( ! excludedInputNames . includes ( input . name ) && input . value !== undefined ) { options . push ( new SchematicOption ( input . name , input . value ) ) ; } } ) ; return options ; } ;