mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-02-28 12:50:38 +00:00
fix: update demo-server deps
This commit is contained in:
parent
180da7ce1c
commit
23ded02055
@ -30,7 +30,8 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ali/lowcode-code-generator": "0.8.8",
|
"@ali/lowcode-code-generator": "*",
|
||||||
|
"@ali/lowcode-types": "^1.0.11",
|
||||||
"@nestjs/common": "^7.0.0",
|
"@nestjs/common": "^7.0.0",
|
||||||
"@nestjs/core": "^7.0.0",
|
"@nestjs/core": "^7.0.0",
|
||||||
"@nestjs/platform-express": "^7.0.0",
|
"@nestjs/platform-express": "^7.0.0",
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import * as path from 'path';
|
|||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import CodeGenerator from '@ali/lowcode-code-generator';
|
import CodeGenerator from '@ali/lowcode-code-generator';
|
||||||
import { createZipPublisher } from '../publisher';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ApiService {
|
export class ApiService {
|
||||||
@ -11,7 +10,7 @@ export class ApiService {
|
|||||||
const tmpDir = os.tmpdir();
|
const tmpDir = os.tmpdir();
|
||||||
const createIceJsProjectBuilder = CodeGenerator.solutions.icejs;
|
const createIceJsProjectBuilder = CodeGenerator.solutions.icejs;
|
||||||
const builder = createIceJsProjectBuilder();
|
const builder = createIceJsProjectBuilder();
|
||||||
const publisher = createZipPublisher({
|
const publisher = CodeGenerator.publishers.zip({
|
||||||
outputPath: tmpDir,
|
outputPath: tmpDir,
|
||||||
projectSlug: 'demo-project',
|
projectSlug: 'demo-project',
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,76 +0,0 @@
|
|||||||
import { IResultDir } from '@ali/lowcode-code-generator';
|
|
||||||
import { isNodeProcess, writeZipToDisk, generateProjectZip } from './utils';
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
declare type ZipPublisherResponse = string | Buffer | Blob;
|
|
||||||
|
|
||||||
export interface ZipFactoryParams extends IPublisherFactoryParams {
|
|
||||||
outputPath?: string
|
|
||||||
projectSlug?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ZipPublisher extends IPublisher<ZipFactoryParams, ZipPublisherResponse> {
|
|
||||||
getOutputPath: () => string
|
|
||||||
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 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,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@ -1,57 +0,0 @@
|
|||||||
import * as JSZip from 'jszip';
|
|
||||||
import { IResultFile, IResultDir } from '@ali/lowcode-code-generator';
|
|
||||||
|
|
||||||
export const isNodeProcess = (): boolean => {
|
|
||||||
return (
|
|
||||||
typeof process === 'object' &&
|
|
||||||
typeof process.versions === 'object' &&
|
|
||||||
typeof process.versions.node !== 'undefined'
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const writeZipToDisk = (
|
|
||||||
zipFolderPath: string,
|
|
||||||
content: Buffer | Blob,
|
|
||||||
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<Buffer | Blob> => {
|
|
||||||
let zip = new JSZip();
|
|
||||||
zip = writeFolderToZip(project, zip, true);
|
|
||||||
const zipType = isNodeProcess() ? 'nodebuffer' : 'blob';
|
|
||||||
return zip.generateAsync({ type: zipType });
|
|
||||||
};
|
|
||||||
|
|
||||||
const writeFolderToZip = (
|
|
||||||
folder: IResultDir,
|
|
||||||
parentFolder: JSZip,
|
|
||||||
ignoreFolder = false,
|
|
||||||
) => {
|
|
||||||
const zipFolder = ignoreFolder ? parentFolder : parentFolder.folder(folder.name);
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
Loading…
x
Reference in New Issue
Block a user