mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-13 01:21:58 +00:00
Merge branch fix/demo-server into release/1.0.0
Title: fix: 修复 demo-server 构建时的 types 冲突报错 Link: https://code.aone.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/codereview/3984811
This commit is contained in:
commit
c8148e4bc4
@ -30,7 +30,8 @@
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@ali/lowcode-code-generator": "0.8.8",
|
||||
"@ali/lowcode-code-generator": "*",
|
||||
"@ali/lowcode-types": "*",
|
||||
"@nestjs/common": "^7.0.0",
|
||||
"@nestjs/core": "^7.0.0",
|
||||
"@nestjs/platform-express": "^7.0.0",
|
||||
|
||||
@ -3,7 +3,6 @@ import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import CodeGenerator from '@ali/lowcode-code-generator';
|
||||
import { createZipPublisher } from '../publisher';
|
||||
|
||||
@Injectable()
|
||||
export class ApiService {
|
||||
@ -11,7 +10,7 @@ export class ApiService {
|
||||
const tmpDir = os.tmpdir();
|
||||
const createIceJsProjectBuilder = CodeGenerator.solutions.icejs;
|
||||
const builder = createIceJsProjectBuilder();
|
||||
const publisher = createZipPublisher({
|
||||
const publisher = CodeGenerator.publishers.zip({
|
||||
outputPath: tmpDir,
|
||||
projectSlug: 'demo-project',
|
||||
});
|
||||
@ -24,7 +23,5 @@ export class ApiService {
|
||||
throw new Error('generateProject failed');
|
||||
}
|
||||
return fs.createReadStream(filePath);
|
||||
|
||||
// return filePath;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
};
|
||||
@ -10,6 +10,7 @@
|
||||
"sourceMap": true,
|
||||
"outDir": "./dist",
|
||||
"baseUrl": "./",
|
||||
"incremental": true
|
||||
"incremental": true,
|
||||
"types": []
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user