import produce from "immer"; import v from "voca"; import { ReactComponentWriter, AutomatedTestsUtilityFunctionsWriter, ReduxStoreWriter, ReactNativeStylesWriter, IndexFileWriter } from "../../file-writers"; import { Creator } from "../base"; import { ICreator, TCreatorArgs } from "../types"; import { TReactComponentType } from "../../commands/types"; import { AppConfig } from "../../../app-config"; class ReactComponentCreator extends Creator implements ICreator { private reactComponentType?: TReactComponentType; constructor(config: TCreatorArgs) { super(config,'react-component'); const { answers: { reactComponentType }, } = config; this.reactComponentType = reactComponentType; } private determineSettingsBasedOnComponentType() { return produce( { addUnitTests: true, addReduxIntegration: true, }, (draft) => { const { reactComponentType } = this; if (reactComponentType === "sub-component") { draft.addReduxIntegration = false; } } ); } execute() { const { name, observers, itemDirectory } = this; const { addReduxIntegration, addUnitTests, } = this.determineSettingsBasedOnComponentType(); if (typeof itemDirectory === "string") { observers.attach( new ReactComponentWriter({ fileURI: `${itemDirectory}/${v.kebabCase(name)}.view.tsx`, strategyName: name, }) ); observers.attach( new ReactNativeStylesWriter({ fileURI: `${itemDirectory}/${v.kebabCase(name)}.styles.tsx`, strategyName: name, }) ); observers.attach( new IndexFileWriter({ fileURI: `${itemDirectory}/index.ts`, strategyName: name }) ); } if (addUnitTests === true) { observers.attach( new AutomatedTestsUtilityFunctionsWriter({ fileURI: AppConfig.AUTOMATED_TEST_UTILITY_FUNCTIONS_FILE, }) ); } if (addReduxIntegration === true) { observers.attach( new ReduxStoreWriter({ fileURI: AppConfig.REDUX_STORE_FILE }) ); } observers.notify(); } } export { ReactComponentCreator };