mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-13 01:21:58 +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')
|
||||
export class ApiController {
|
||||
constructor(private readonly apiService: ApiService) {}
|
||||
private readonly apiService: ApiService;
|
||||
|
||||
@Get('generate/test')
|
||||
generateTest() {
|
||||
|
||||
@ -3,7 +3,7 @@ import { AppService } from './app.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {}
|
||||
private readonly appService: AppService;
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
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;
|
||||
|
||||
@ -17,7 +17,7 @@ export interface IPublisherResponse<T> {
|
||||
payload?: T;
|
||||
}
|
||||
|
||||
declare type ZipPublisherResponse = string | Buffer | Blob
|
||||
declare type ZipPublisherResponse = string | Buffer | Blob;
|
||||
|
||||
export interface ZipFactoryParams extends IPublisherFactoryParams {
|
||||
outputPath?: string
|
||||
@ -32,39 +32,39 @@ export interface ZipPublisher extends IPublisher<ZipFactoryParams, ZipPublisherR
|
||||
export const createZipPublisher: PublisherFactory<ZipFactoryParams, ZipPublisher> = (
|
||||
params: ZipFactoryParams = {}
|
||||
): ZipPublisher => {
|
||||
let { project, outputPath } = params
|
||||
let { project, outputPath } = params;
|
||||
|
||||
const getProject = () => project
|
||||
const getProject = () => project;
|
||||
const setProject = (projectToSet: IResultDir) => {
|
||||
project = projectToSet
|
||||
}
|
||||
project = projectToSet;
|
||||
};
|
||||
|
||||
const getOutputPath = () => outputPath
|
||||
const getOutputPath = () => outputPath;
|
||||
const setOutputPath = (path: string) => {
|
||||
outputPath = path
|
||||
}
|
||||
outputPath = path;
|
||||
};
|
||||
|
||||
const publish = async (options: ZipFactoryParams = {}) => {
|
||||
const projectToPublish = options.project || project
|
||||
const projectToPublish = options.project || project;
|
||||
if (!projectToPublish) {
|
||||
throw new Error('MissingProject');
|
||||
}
|
||||
|
||||
const zipName = options.projectSlug || params.projectSlug || projectToPublish.name
|
||||
const zipName = options.projectSlug || params.projectSlug || projectToPublish.name;
|
||||
|
||||
try {
|
||||
const zipContent = await generateProjectZip(projectToPublish)
|
||||
const zipContent = await generateProjectZip(projectToPublish);
|
||||
|
||||
// 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()) {
|
||||
await writeZipToDisk(projectOutputPath, zipContent, zipName)
|
||||
await writeZipToDisk(projectOutputPath, zipContent, zipName);
|
||||
}
|
||||
return { success: true, payload: zipContent }
|
||||
return { success: true, payload: zipContent };
|
||||
} catch (error) {
|
||||
throw new Error('ZipUnexpected');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
publish,
|
||||
@ -72,5 +72,5 @@ export const createZipPublisher: PublisherFactory<ZipFactoryParams, ZipPublisher
|
||||
setProject,
|
||||
getOutputPath,
|
||||
setOutputPath,
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@ -6,52 +6,52 @@ export const isNodeProcess = (): boolean => {
|
||||
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')
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
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)
|
||||
writeStream.write(content)
|
||||
writeStream.end()
|
||||
}
|
||||
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 })
|
||||
}
|
||||
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)
|
||||
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)
|
||||
})
|
||||
const fileName = file.ext ? `${file.name}.${file.ext}` : file.name;
|
||||
zipFolder.file(fileName, file.content, options);
|
||||
});
|
||||
|
||||
folder.dirs.forEach((subFolder: IResultDir) => {
|
||||
writeFolderToZip(subFolder, zipFolder)
|
||||
})
|
||||
writeFolderToZip(subFolder, zipFolder);
|
||||
});
|
||||
|
||||
return parentFolder
|
||||
}
|
||||
return parentFolder;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user