import { IResultDir } from '@ali/lowcode-code-generator'; import { isNodeProcess, writeZipToDisk, generateProjectZip } from './utils' export type PublisherFactory = (configuration?: Partial) => U; export interface IPublisher { publish: (options?: T) => Promise>; getProject: () => IResultDir | void; setProject: (project: IResultDir) => void; } export interface IPublisherFactoryParams { project?: IResultDir; } export interface IPublisherResponse { success: boolean; payload?: T; } declare type ZipPublisherResponse = string | Buffer | Blob export interface ZipFactoryParams extends IPublisherFactoryParams { outputPath?: string projectSlug?: string } export interface ZipPublisher extends IPublisher { getOutputPath: () => string setOutputPath: (path: string) => void } export const createZipPublisher: PublisherFactory = ( params: ZipFactoryParams = {} ): ZipPublisher => { let { project, outputPath } = params const getProject = () => project const setProject = (projectToSet: IResultDir) => { project = projectToSet } const getOutputPath = () => outputPath const setOutputPath = (path: string) => { outputPath = path } const publish = async (options: ZipFactoryParams = {}) => { const projectToPublish = options.project || project if (!projectToPublish) { throw new Error('MissingProject'); } const zipName = options.projectSlug || params.projectSlug || projectToPublish.name try { const zipContent = await generateProjectZip(projectToPublish) // If not output path is provided, zip is not written to disk const projectOutputPath = options.outputPath || outputPath if (projectOutputPath && isNodeProcess()) { await writeZipToDisk(projectOutputPath, zipContent, zipName) } return { success: true, payload: zipContent } } catch (error) { throw new Error('ZipUnexpected'); } } return { publish, getProject, setProject, getOutputPath, setOutputPath, } }