mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-15 02:38:16 +00:00
style: for demo-server
This commit is contained in:
parent
a5e76c1d0e
commit
35e4575543
@ -5,7 +5,7 @@ import { GenerateProjectDto } from '../dto/generate-project.dto';
|
|||||||
|
|
||||||
@Controller('api')
|
@Controller('api')
|
||||||
export class ApiController {
|
export class ApiController {
|
||||||
constructor(private readonly apiService: ApiService) {}
|
private readonly apiService: ApiService;
|
||||||
|
|
||||||
@Get('generate/test')
|
@Get('generate/test')
|
||||||
generateTest() {
|
generateTest() {
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { AppService } from './app.service';
|
|||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
export class AppController {
|
export class AppController {
|
||||||
constructor(private readonly appService: AppService) {}
|
private readonly appService: AppService;
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
getHello(): string {
|
getHello(): string {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { IResultDir } from '@ali/lowcode-code-generator';
|
import { IResultDir } from '@ali/lowcode-code-generator';
|
||||||
import { isNodeProcess, writeZipToDisk, generateProjectZip } from './utils'
|
import { isNodeProcess, writeZipToDisk, generateProjectZip } from './utils';
|
||||||
|
|
||||||
export type PublisherFactory<T, U> = (configuration?: Partial<T>) => U;
|
export type PublisherFactory<T, U> = (configuration?: Partial<T>) => U;
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ export interface IPublisherResponse<T> {
|
|||||||
payload?: T;
|
payload?: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare type ZipPublisherResponse = string | Buffer | Blob
|
declare type ZipPublisherResponse = string | Buffer | Blob;
|
||||||
|
|
||||||
export interface ZipFactoryParams extends IPublisherFactoryParams {
|
export interface ZipFactoryParams extends IPublisherFactoryParams {
|
||||||
outputPath?: string
|
outputPath?: string
|
||||||
@ -32,39 +32,39 @@ export interface ZipPublisher extends IPublisher<ZipFactoryParams, ZipPublisherR
|
|||||||
export const createZipPublisher: PublisherFactory<ZipFactoryParams, ZipPublisher> = (
|
export const createZipPublisher: PublisherFactory<ZipFactoryParams, ZipPublisher> = (
|
||||||
params: ZipFactoryParams = {}
|
params: ZipFactoryParams = {}
|
||||||
): ZipPublisher => {
|
): ZipPublisher => {
|
||||||
let { project, outputPath } = params
|
let { project, outputPath } = params;
|
||||||
|
|
||||||
const getProject = () => project
|
const getProject = () => project;
|
||||||
const setProject = (projectToSet: IResultDir) => {
|
const setProject = (projectToSet: IResultDir) => {
|
||||||
project = projectToSet
|
project = projectToSet;
|
||||||
}
|
};
|
||||||
|
|
||||||
const getOutputPath = () => outputPath
|
const getOutputPath = () => outputPath;
|
||||||
const setOutputPath = (path: string) => {
|
const setOutputPath = (path: string) => {
|
||||||
outputPath = path
|
outputPath = path;
|
||||||
}
|
};
|
||||||
|
|
||||||
const publish = async (options: ZipFactoryParams = {}) => {
|
const publish = async (options: ZipFactoryParams = {}) => {
|
||||||
const projectToPublish = options.project || project
|
const projectToPublish = options.project || project;
|
||||||
if (!projectToPublish) {
|
if (!projectToPublish) {
|
||||||
throw new Error('MissingProject');
|
throw new Error('MissingProject');
|
||||||
}
|
}
|
||||||
|
|
||||||
const zipName = options.projectSlug || params.projectSlug || projectToPublish.name
|
const zipName = options.projectSlug || params.projectSlug || projectToPublish.name;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const zipContent = await generateProjectZip(projectToPublish)
|
const zipContent = await generateProjectZip(projectToPublish);
|
||||||
|
|
||||||
// If not output path is provided, zip is not written to disk
|
// If not output path is provided, zip is not written to disk
|
||||||
const projectOutputPath = options.outputPath || outputPath
|
const projectOutputPath = options.outputPath || outputPath;
|
||||||
if (projectOutputPath && isNodeProcess()) {
|
if (projectOutputPath && isNodeProcess()) {
|
||||||
await writeZipToDisk(projectOutputPath, zipContent, zipName)
|
await writeZipToDisk(projectOutputPath, zipContent, zipName);
|
||||||
}
|
}
|
||||||
return { success: true, payload: zipContent }
|
return { success: true, payload: zipContent };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error('ZipUnexpected');
|
throw new Error('ZipUnexpected');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
publish,
|
publish,
|
||||||
@ -72,5 +72,5 @@ export const createZipPublisher: PublisherFactory<ZipFactoryParams, ZipPublisher
|
|||||||
setProject,
|
setProject,
|
||||||
getOutputPath,
|
getOutputPath,
|
||||||
setOutputPath,
|
setOutputPath,
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
|
|||||||
@ -6,52 +6,52 @@ export const isNodeProcess = (): boolean => {
|
|||||||
typeof process === 'object' &&
|
typeof process === 'object' &&
|
||||||
typeof process.versions === 'object' &&
|
typeof process.versions === 'object' &&
|
||||||
typeof process.versions.node !== 'undefined'
|
typeof process.versions.node !== 'undefined'
|
||||||
)
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export const writeZipToDisk = (
|
export const writeZipToDisk = (
|
||||||
zipFolderPath: string,
|
zipFolderPath: string,
|
||||||
content: Buffer | Blob,
|
content: Buffer | Blob,
|
||||||
zipName: string
|
zipName: string
|
||||||
): void => {
|
): void => {
|
||||||
const fs = require('fs')
|
const fs = require('fs');
|
||||||
const path = require('path')
|
const path = require('path');
|
||||||
|
|
||||||
if (!fs.existsSync(zipFolderPath)) {
|
if (!fs.existsSync(zipFolderPath)) {
|
||||||
fs.mkdirSync(zipFolderPath, { recursive: true })
|
fs.mkdirSync(zipFolderPath, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
const zipPath = path.join(zipFolderPath, `${zipName}.zip`)
|
const zipPath = path.join(zipFolderPath, `${zipName}.zip`);
|
||||||
|
|
||||||
const writeStream = fs.createWriteStream(zipPath)
|
const writeStream = fs.createWriteStream(zipPath);
|
||||||
writeStream.write(content)
|
writeStream.write(content);
|
||||||
writeStream.end()
|
writeStream.end();
|
||||||
}
|
};
|
||||||
|
|
||||||
export const generateProjectZip = async (project: IResultDir): Promise<Buffer | Blob> => {
|
export const generateProjectZip = async (project: IResultDir): Promise<Buffer | Blob> => {
|
||||||
let zip = new JSZip()
|
let zip = new JSZip();
|
||||||
zip = writeFolderToZip(project, zip, true)
|
zip = writeFolderToZip(project, zip, true);
|
||||||
const zipType = isNodeProcess() ? 'nodebuffer' : 'blob'
|
const zipType = isNodeProcess() ? 'nodebuffer' : 'blob';
|
||||||
return zip.generateAsync({ type: zipType })
|
return zip.generateAsync({ type: zipType });
|
||||||
}
|
};
|
||||||
|
|
||||||
const writeFolderToZip = (
|
const writeFolderToZip = (
|
||||||
folder: IResultDir,
|
folder: IResultDir,
|
||||||
parentFolder: JSZip,
|
parentFolder: JSZip,
|
||||||
ignoreFolder = false
|
ignoreFolder = false
|
||||||
) => {
|
) => {
|
||||||
const zipFolder = ignoreFolder ? parentFolder : parentFolder.folder(folder.name)
|
const zipFolder = ignoreFolder ? parentFolder : parentFolder.folder(folder.name);
|
||||||
|
|
||||||
folder.files.forEach((file: IResultFile) => {
|
folder.files.forEach((file: IResultFile) => {
|
||||||
// const options = file.contentEncoding === 'base64' ? { base64: true } : {}
|
// const options = file.contentEncoding === 'base64' ? { base64: true } : {}
|
||||||
const options = {};
|
const options = {};
|
||||||
const fileName = file.ext ? `${file.name}.${file.ext}` : file.name
|
const fileName = file.ext ? `${file.name}.${file.ext}` : file.name;
|
||||||
zipFolder.file(fileName, file.content, options)
|
zipFolder.file(fileName, file.content, options);
|
||||||
})
|
});
|
||||||
|
|
||||||
folder.dirs.forEach((subFolder: IResultDir) => {
|
folder.dirs.forEach((subFolder: IResultDir) => {
|
||||||
writeFolderToZip(subFolder, zipFolder)
|
writeFolderToZip(subFolder, zipFolder);
|
||||||
})
|
});
|
||||||
|
|
||||||
return parentFolder
|
return parentFolder;
|
||||||
}
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user