import v from "voca"; import { IObserve } from "../../observe/types"; import { TCreatorArgs, TProjectFolder } from "../types"; import { TCreateCommandUserInput, TCreateCommandAnswers, TCreationType, } from "../../commands/types"; import { Observe } from "../../observe"; import { AppConfig } from "../../../app-config"; const { REACT_COMPONENTS_BASE_DIR, SAGAS_BASE_DIR, HELPER_CLASSES_BASE_DIR, GLOBAL_ACTION_CREATORS_BASE_DIR, GLOBAL_REDUX_REDUCERS_BASE_DIR, } = AppConfig; class Creator { protected observers: IObserve; protected writerConfiguration: TCreateCommandUserInput; protected name: string; protected answers: TCreateCommandAnswers; protected strategyName: string; protected folderName: string; protected projectFolder: TProjectFolder; protected itemDirectory?: string; protected itemBaseDirectory?: string; protected defaultStateFile?: string; protected reducerFile?: string; constructor(config: TCreatorArgs, private type: TCreationType) { const { name, answers } = config; this.writerConfiguration = { ...answers, name }; this.name = name; this.answers = answers; this.strategyName = v.capitalize(name); this.folderName = v.kebabCase(name); this.projectFolder = this.getProjectFolderName(); this.itemBaseDirectory = this.getBaseDirOfNewItem(); this.itemDirectory = this.getItemCompleteDirectory(); this.defaultStateFile=`${v.kebabCase(name)}.default-state.ts`; this.reducerFile=`${v.kebabCase(name)}.reducer.ts`; this.observers = new Observe(); } private getBaseDirOfNewItem(): string | undefined { const { type } = this; if (type === "helper-class") { return HELPER_CLASSES_BASE_DIR; } if (type === "react-component") { return REACT_COMPONENTS_BASE_DIR; } if (type === "redux-saga") { return SAGAS_BASE_DIR; } if (type === "global-action-creator") { return GLOBAL_ACTION_CREATORS_BASE_DIR; } if (type === 'global-redux-reducer') { return GLOBAL_REDUX_REDUCERS_BASE_DIR; } } private getItemCompleteDirectory(): string | undefined { const { type, folderName, itemBaseDirectory, projectFolder, writerConfiguration: { collectionNameFromList,reactComponentType }, } = this; if (typeof itemBaseDirectory === "string") { const baseCompleteDirectory = `${itemBaseDirectory}/${projectFolder}`; if (type === "helper-class"){ const collectionFolderName = v.kebabCase(collectionNameFromList); return `${baseCompleteDirectory}/${collectionFolderName}/${folderName}`; } if (type === "react-component" && typeof reactComponentType==='string') { return `${baseCompleteDirectory}/${reactComponentType}s/${folderName}`; } return baseCompleteDirectory; } } private getProjectFolderName(): TProjectFolder { const { isCustom } = this.writerConfiguration; if (isCustom === true) { return "app-specific"; } return "app-agnostic"; } } export { Creator };