import fs from "fs-extra"; import v from "voca"; import VerEx from "verbal-expressions"; import _ from "lodash"; import { TFileWriterConfig } from "../types"; import { GlobalHelpers } from "../../helpers"; type TFlag = "w" | "wx" | "a"; interface IFileWriter { write: (flag: TFlag) => void; getAllFileImports: (baseURI: string) => string; } abstract class FileWriter implements IFileWriter { protected baseFileName?: string; protected defaultStateName?: string; protected defaultStateTypeAlias?: string; protected defaultStateFile?: string; protected actionTypeDefinition?: string; protected actionPayloadTypeDefinition?: string; protected actionBaseTypeDefinition?: string; protected reducerName?: string; protected reducerFile?: string; constructor(protected readonly config: TFileWriterConfig) { const { name, strategyName } = config; if (typeof name === "string") { this.baseFileName = v.kebabCase(name); this.defaultStateFile = `${this.baseFileName}.default-state`; this.reducerFile=`${v.kebabCase(name)}.reducer`; } if (typeof strategyName === "string") { this.defaultStateName = `${strategyName}DefaultState`; this.defaultStateTypeAlias = `T${strategyName}DefaultState`; this.actionTypeDefinition = `T${strategyName}ActionType`; this.actionPayloadTypeDefinition = `T${strategyName}Payload`; this.actionBaseTypeDefinition = `T${strategyName}Action`; this.reducerName = strategyName; } } abstract getFileContent(): string; getAllFileImports(baseURI: string): string { //get all the export statements for a given baseURI/index.ts file let fileContent = ""; const rootItems = fs.readdirSync(baseURI); const files = rootItems.filter( (item) => item.includes(".") && item !== "index.ts" ); const folders = rootItems.filter((item) => !item.includes(".")); files.forEach((file) => { if (v.endsWith(baseURI, "types")) { //type defs index file const includeFile = v.replaceAll(file, ".ts", ""); const readFileContent = fs .readFileSync(`${baseURI}/${file}`) .toString(); const regex = VerEx() .startOfLine() .then("export type ") .anythingBut("="); const matches = regex.exec(readFileContent); if (_.isArray(matches) && matches.length > 0) { const typeDefsName = v .replaceAll(matches[0], "export type", "") .trim(); fileContent += `export type {${typeDefsName}} from "./${includeFile}";\n`; } } if (!v.endsWith(baseURI, "types")) { //root index file - root index file const includeFile = v.replaceAll(file, ".ts", ""); const readFileContent = fs .readFileSync(`${baseURI}/${file}`) .toString(); const regex = VerEx() .startOfLine() .then("class") .anything() .then("implements"); const matches = regex.exec(readFileContent); if (_.isArray(matches) && matches.length > 0) { let helperClassName = ""; helperClassName = v.replaceAll(matches[0], "class", ""); helperClassName = v.replaceAll(helperClassName, "implements", ""); helperClassName = helperClassName.trim(); fileContent += `export {${helperClassName}} from "./${includeFile}";\n`; } } }); return fileContent; } write(flag?: TFlag) { const { fileURI } = this.config; const fileContent = this.getFileContent(); if (typeof flag === "string") { fs.outputFileSync(fileURI, fileContent, { flag }); } if (flag === undefined) { fs.outputFileSync(fileURI, fileContent); } GlobalHelpers.showSuccess(fileURI + " created"); } } export { FileWriter };