mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-02-01 00:58:12 +00:00
feat: add zip publisher
This commit is contained in:
parent
2e332e3323
commit
31156edd9b
@ -22,6 +22,7 @@
|
|||||||
"@babel/types": "^7.9.5",
|
"@babel/types": "^7.9.5",
|
||||||
"@types/prettier": "^1.19.1",
|
"@types/prettier": "^1.19.1",
|
||||||
"change-case": "^3.1.0",
|
"change-case": "^3.1.0",
|
||||||
|
"jszip": "^3.5.0",
|
||||||
"prettier": "^2.0.2",
|
"prettier": "^2.0.2",
|
||||||
"short-uuid": "^3.1.1"
|
"short-uuid": "^3.1.1"
|
||||||
},
|
},
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
import { createProjectBuilder } from './generator/ProjectBuilder';
|
import { createProjectBuilder } from './generator/ProjectBuilder';
|
||||||
import { createModuleBuilder } from './generator/ModuleBuilder';
|
import { createModuleBuilder } from './generator/ModuleBuilder';
|
||||||
import { createDiskPublisher } from './publisher/disk';
|
import { createDiskPublisher } from './publisher/disk';
|
||||||
|
import { createZipPublisher } from './publisher/zip';
|
||||||
import createIceJsProjectBuilder from './solutions/icejs';
|
import createIceJsProjectBuilder from './solutions/icejs';
|
||||||
import createRecoreProjectBuilder from './solutions/recore';
|
import createRecoreProjectBuilder from './solutions/recore';
|
||||||
|
|
||||||
@ -57,6 +58,7 @@ export default {
|
|||||||
},
|
},
|
||||||
publishers: {
|
publishers: {
|
||||||
disk: createDiskPublisher,
|
disk: createDiskPublisher,
|
||||||
|
zip: createZipPublisher,
|
||||||
},
|
},
|
||||||
plugins: {
|
plugins: {
|
||||||
common: {
|
common: {
|
||||||
|
|||||||
@ -1,21 +1,10 @@
|
|||||||
import { CodeGeneratorError, IResultDir } from '../../types';
|
import {
|
||||||
|
IResultDir,
|
||||||
export type PublisherFactory<T, U> = (configuration?: Partial<T>) => U;
|
PublisherFactory,
|
||||||
|
IPublisher,
|
||||||
export interface IPublisher<T, U> {
|
IPublisherFactoryParams,
|
||||||
publish: (options?: T) => Promise<IPublisherResponse<U>>;
|
PublisherError,
|
||||||
getProject: () => IResultDir | void;
|
} from '../../types';
|
||||||
setProject: (project: IResultDir) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IPublisherFactoryParams {
|
|
||||||
project?: IResultDir;
|
|
||||||
}
|
|
||||||
export interface IPublisherResponse<T> {
|
|
||||||
success: boolean;
|
|
||||||
payload?: T;
|
|
||||||
}
|
|
||||||
|
|
||||||
import { writeFolder } from './utils';
|
import { writeFolder } from './utils';
|
||||||
|
|
||||||
export interface IDiskFactoryParams extends IPublisherFactoryParams {
|
export interface IDiskFactoryParams extends IPublisherFactoryParams {
|
||||||
@ -37,7 +26,7 @@ export const createDiskPublisher: PublisherFactory<
|
|||||||
|
|
||||||
const getProject = (): IResultDir => {
|
const getProject = (): IResultDir => {
|
||||||
if (!project) {
|
if (!project) {
|
||||||
throw new CodeGeneratorError('Missing Project');
|
throw new PublisherError('Missing Project');
|
||||||
}
|
}
|
||||||
return project;
|
return project;
|
||||||
};
|
};
|
||||||
@ -55,7 +44,7 @@ export const createDiskPublisher: PublisherFactory<
|
|||||||
const publish = async (options: IDiskFactoryParams = {}) => {
|
const publish = async (options: IDiskFactoryParams = {}) => {
|
||||||
const projectToPublish = options.project || project;
|
const projectToPublish = options.project || project;
|
||||||
if (!projectToPublish) {
|
if (!projectToPublish) {
|
||||||
throw new CodeGeneratorError('Missing Project');
|
throw new PublisherError('Missing Project');
|
||||||
}
|
}
|
||||||
|
|
||||||
const projectOutputPath = options.outputPath || outputPath;
|
const projectOutputPath = options.outputPath || outputPath;
|
||||||
@ -75,7 +64,7 @@ export const createDiskPublisher: PublisherFactory<
|
|||||||
);
|
);
|
||||||
return { success: true, payload: projectOutputPath };
|
return { success: true, payload: projectOutputPath };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new CodeGeneratorError(error);
|
throw new PublisherError(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
69
packages/code-generator/src/publisher/zip/index.ts
Normal file
69
packages/code-generator/src/publisher/zip/index.ts
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
import {
|
||||||
|
IResultDir,
|
||||||
|
PublisherFactory,
|
||||||
|
IPublisher,
|
||||||
|
IPublisherFactoryParams,
|
||||||
|
PublisherError,
|
||||||
|
} from '../../types';
|
||||||
|
import { isNodeProcess, writeZipToDisk, generateProjectZip } from './utils'
|
||||||
|
|
||||||
|
// export type ZipBuffer = Buffer | Blob;
|
||||||
|
export type ZipBuffer = Buffer;
|
||||||
|
|
||||||
|
declare type ZipPublisherResponse = string | ZipBuffer;
|
||||||
|
|
||||||
|
export interface ZipFactoryParams extends IPublisherFactoryParams {
|
||||||
|
outputPath?: string;
|
||||||
|
projectSlug?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ZipPublisher extends IPublisher<ZipFactoryParams, ZipPublisherResponse> {
|
||||||
|
getOutputPath: () => string | undefined;
|
||||||
|
setOutputPath: (path: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createZipPublisher: PublisherFactory<ZipFactoryParams, ZipPublisher> = (
|
||||||
|
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 PublisherError('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 PublisherError(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
publish,
|
||||||
|
getProject,
|
||||||
|
setProject,
|
||||||
|
getOutputPath,
|
||||||
|
setOutputPath,
|
||||||
|
};
|
||||||
|
}
|
||||||
60
packages/code-generator/src/publisher/zip/utils.ts
Normal file
60
packages/code-generator/src/publisher/zip/utils.ts
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import JSZip from 'jszip';
|
||||||
|
import { IResultDir, IResultFile } from '../../types';
|
||||||
|
import { ZipBuffer } from './index';
|
||||||
|
|
||||||
|
export const isNodeProcess = (): boolean => {
|
||||||
|
return (
|
||||||
|
typeof process === 'object' &&
|
||||||
|
typeof process.versions === 'object' &&
|
||||||
|
typeof process.versions.node !== 'undefined'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const writeZipToDisk = (
|
||||||
|
zipFolderPath: string,
|
||||||
|
content: ZipBuffer,
|
||||||
|
zipName: string,
|
||||||
|
): void => {
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
if (!fs.existsSync(zipFolderPath)) {
|
||||||
|
fs.mkdirSync(zipFolderPath, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
const zipPath = path.join(zipFolderPath, `${zipName}.zip`);
|
||||||
|
|
||||||
|
const writeStream = fs.createWriteStream(zipPath);
|
||||||
|
writeStream.write(content);
|
||||||
|
writeStream.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
export const generateProjectZip = async (project: IResultDir): Promise<ZipBuffer> => {
|
||||||
|
let zip = new JSZip();
|
||||||
|
zip = writeFolderToZip(project, zip, true);
|
||||||
|
// const zipType = isNodeProcess() ? 'nodebuffer' : 'blob';
|
||||||
|
const zipType = 'nodebuffer'; // 目前先只支持 node 调用
|
||||||
|
return zip.generateAsync({ type: zipType });
|
||||||
|
}
|
||||||
|
|
||||||
|
const writeFolderToZip = (
|
||||||
|
folder: IResultDir,
|
||||||
|
parentFolder: JSZip,
|
||||||
|
ignoreFolder: boolean = false,
|
||||||
|
) => {
|
||||||
|
const zipFolder = ignoreFolder ? parentFolder : parentFolder.folder(folder.name);
|
||||||
|
if (zipFolder !== null) {
|
||||||
|
folder.files.forEach((file: IResultFile) => {
|
||||||
|
// const options = file.contentEncoding === 'base64' ? { base64: true } : {};
|
||||||
|
const options = {};
|
||||||
|
const fileName = file.ext ? `${file.name}.${file.ext}` : file.name;
|
||||||
|
zipFolder.file(fileName, file.content, options);
|
||||||
|
});
|
||||||
|
|
||||||
|
folder.dirs.forEach((subFolder: IResultDir) => {
|
||||||
|
writeFolderToZip(subFolder, zipFolder);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return parentFolder;
|
||||||
|
}
|
||||||
@ -18,3 +18,10 @@ export class CompatibilityError extends CodeGeneratorError {
|
|||||||
super(errorString);
|
super(errorString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tslint:disable-next-line: max-classes-per-file
|
||||||
|
export class PublisherError extends CodeGeneratorError {
|
||||||
|
constructor(errorString: string) {
|
||||||
|
super(errorString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -4,3 +4,4 @@ export * from './error';
|
|||||||
export * from './result';
|
export * from './result';
|
||||||
export * from './schema';
|
export * from './schema';
|
||||||
export * from './intermediate';
|
export * from './intermediate';
|
||||||
|
export * from './publisher';
|
||||||
|
|||||||
19
packages/code-generator/src/types/publisher.ts
Normal file
19
packages/code-generator/src/types/publisher.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import {
|
||||||
|
IResultDir,
|
||||||
|
} from './index';
|
||||||
|
|
||||||
|
export type PublisherFactory<T, U> = (configuration?: Partial<T>) => U;
|
||||||
|
|
||||||
|
export interface IPublisher<T, U> {
|
||||||
|
publish: (options?: T) => Promise<IPublisherResponse<U>>;
|
||||||
|
getProject: () => IResultDir | void;
|
||||||
|
setProject: (project: IResultDir) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IPublisherFactoryParams {
|
||||||
|
project?: IResultDir;
|
||||||
|
}
|
||||||
|
export interface IPublisherResponse<T> {
|
||||||
|
success: boolean;
|
||||||
|
payload?: T;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user