import chalk from "chalk" ; import { capitalizeFirstLetter , toUniqueArray } from "../utils" ; import { STYLES , ROUTING , LANGUAGES , REDUX_ADDONS , DEFAULT_ROUTE , STATE_MANAGEMENT , DEFAULT_APP_NAME , EXPORT_PREFERENCE , } from "../constants" ; interface Answers { routes ? : string ; stores ? : string ; appName ? : string ; reducers : string ; useLogger : boolean ; dependencies : string ; devDependencies : string ; isRoutingNeeded : boolean ; additionalFolders : string ; predefinedFolders : string [ ] ; stateManagement ? : keyof typeof STATE_MANAGEMENT ; exportPreference : keyof typeof EXPORT_PREFERENCE ; language : typeof LANGUAGES [ keyof typeof LANGUAGES ] ; stylingPreference : typeof STYLES [ keyof typeof STYLES ] ; middleware : Exclude < keyof typeof REDUX_ADDONS , 'Redux Logger' | 'Redux Toolkit' > ; } const mappedAnswers = ( answers ) => { let dependencies = [ ] ; let devDependencies = [ ] ; let stores = [ ] ; let reducers = [ ] ; let routes = [ ] ; let folders = [ ] ; const warnings = [ ] ; const { appName , language , useLogger , middleware , isRoutingNeeded , stateManagement , exportPreference , predefinedFolders , additionalFolders , stylingPreference , routes : routesInput , stores : storesInput , reducers : reducersInput , dependencies : setupDependencies , devDependencies : setupDevDependencies , } = answers ; const tsUsed = language === 'Typescript' ; const sagaUsed = middleware === 'Redux Saga' ; const scssUsed = stylingPreference === STYLES . scss ; if ( ! appName ) { warnings . push ( `App has been named as '${ DEFAULT_APP_NAME }' since you didn\'t enter one.` ) ; } if ( setupDependencies . length > 0 ) { dependencies = toUniqueArray ( setupDependencies ) ; } if ( setupDevDependencies . length > 0 ) { devDependencies = toUniqueArray ( setupDevDependencies ) ; } if ( language === 'JavaScript' && devDependencies . length > 0 ) { devDependencies = devDependencies . filter ( ( value ) => ! value . startsWith ( '@types/' ) ) ; } scssUsed && dependencies . push ( 'sass' ) ; if ( isRoutingNeeded ) { dependencies . push ( ROUTING . lib ) ; devDependencies . push ( ROUTING . types ) ; if ( routesInput ) { routes = toUniqueArray ( routesInput , true ) ; routes = routes . map ( ( route ) => capitalizeFirstLetter ( route ) ) ; } else { routes = [ DEFAULT_ROUTE ] ; warnings . push ( `A mock route '${ DEFAULT_ROUTE }' has been added since you didn\'t enter any route(s).` ) ; } } if ( stateManagement ) { dependencies = [ ... dependencies , STATE_MANAGEMENT [ stateManagement ] . lib , STATE_MANAGEMENT [ stateManagement ] . binding , ... stateManagement === 'Redux' ? [ REDUX_ADDONS [ 'Redux Toolkit' ] . lib ] : [ ] , ] ; devDependencies = [ ... devDependencies , ... tsUsed && STATE_MANAGEMENT [ stateManagement ] . types ? [ STATE_MANAGEMENT [ stateManagement ] . types ] : [ ] , ] ; stores = toUniqueArray ( storesInput , true ) ; reducers = toUniqueArray ( reducersInput , true ) ; if ( useLogger ) { const { lib , types } = REDUX_ADDONS [ 'Redux Logger' ] ; dependencies = [ ... dependencies , lib ] ; devDependencies = [ ... devDependencies , ... tsUsed ? [ types ] : [ ] ] ; } if ( sagaUsed ) { dependencies . push ( REDUX_ADDONS [ middleware ] . lib ) ; } } if ( predefinedFolders ) { folders = predefinedFolders ; } if ( additionalFolders ) { folders = [ ... new Set ( [ ... folders , ... toUniqueArray ( additionalFolders ) ] ) ] ; } warnings . forEach ( ( warning , i ) => console . log ( `${ ! i ? '\n' : '' }${ chalk . keyword ( 'orange' ) ( warning ) }` ) ) ; return { tsUsed , routes , folders , scssUsed , sagaUsed , useLogger , dependencies , devDependencies , isRoutingNeeded , stateManagement , appName : appName || DEFAULT_APP_NAME , storesOrReducers : stores . length ? stores : reducers , namedExport : exportPreference === EXPORT_PREFERENCE . named , } ; } ; export { mappedAnswers , } ;