import { stripIndent } from "common-tags"; import { FileWriter } from "../../base"; import { IFileWriter, TFileWriterConfig } from "../../types"; import { GlobalHelpers } from "../../../helpers"; import { AppConfig } from "../../../../app-config"; class HelperContextFilesWriter extends FileWriter implements IFileWriter { constructor(protected config: TFileWriterConfig) { super(config); } private getSyncFileContents() { return stripIndent` import { ${AppConfig.SYNC_INTERFACE_NAME} } from '../${AppConfig.SYNC_INTERFACE_IMPORT_BASE_URI}'; interface IContext { execute:()=>void; } class ${AppConfig.SYNC_CONTEXT_CLASS_NAME} implements IContext { constructor(private strategy: ${AppConfig.SYNC_INTERFACE_NAME}) {} execute(){ this.strategy.execute(); } } export { ${AppConfig.SYNC_CONTEXT_CLASS_NAME} }; `; } private getAsyncFileContents() { return stripIndent` import { ${AppConfig.ASYNC_INTERFACE_NAME} } from '../${AppConfig.ASYNC_INTERFACE_IMPORT_BASE_URI}'; interface IContext { execute:()=>Promise; } class ${AppConfig.ASYNC_CONTEXT_CLASS_NAME} implements IContext { constructor(private strategy: ${AppConfig.ASYNC_INTERFACE_NAME}) {} async execute(){ return this.strategy.execute(); } } export { ${AppConfig.ASYNC_CONTEXT_CLASS_NAME} }; `; } getFileContent() { const { config: { isAsync }, getAsyncFileContents, getSyncFileContents, } = this; let fileContent = ""; if (isAsync === true) { fileContent = getAsyncFileContents(); } if (isAsync === false) { fileContent = getSyncFileContents(); } return fileContent; } execute() { try { this.write('wx'); } catch (error) { GlobalHelpers.showError(error); } } } export { HelperContextFilesWriter };