mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-13 09:41:57 +00:00
feat: 🎸 出码模块的 DiskPublisher 改成支持传入自定义 FS
This commit is contained in:
parent
8ecc002f65
commit
46c896eacf
@ -1,16 +1,14 @@
|
||||
import {
|
||||
IResultDir,
|
||||
PublisherFactory,
|
||||
IPublisher,
|
||||
IPublisherFactoryParams,
|
||||
PublisherError,
|
||||
} from '../../types';
|
||||
import { writeFolder } from './utils';
|
||||
import * as defaultFs from 'fs';
|
||||
import { IResultDir, PublisherFactory, IPublisher, IPublisherFactoryParams, PublisherError } from '../../types';
|
||||
import { writeFolder, IFileSystem } from './utils';
|
||||
|
||||
export type { IFileSystem };
|
||||
|
||||
export interface IDiskFactoryParams extends IPublisherFactoryParams {
|
||||
outputPath?: string;
|
||||
projectSlug?: string;
|
||||
createProjectFolder?: boolean;
|
||||
fs?: IFileSystem;
|
||||
}
|
||||
|
||||
export interface IDiskPublisher extends IPublisher<IDiskFactoryParams, string> {
|
||||
@ -18,11 +16,10 @@ export interface IDiskPublisher extends IPublisher<IDiskFactoryParams, string> {
|
||||
setOutputPath: (path: string) => void;
|
||||
}
|
||||
|
||||
export const createDiskPublisher: PublisherFactory<
|
||||
IDiskFactoryParams,
|
||||
IDiskPublisher
|
||||
> = (params: IDiskFactoryParams = {}): IDiskPublisher => {
|
||||
let { project, outputPath = './' } = params;
|
||||
export const createDiskPublisher: PublisherFactory<IDiskFactoryParams, IDiskPublisher> = (
|
||||
params: IDiskFactoryParams = {},
|
||||
): IDiskPublisher => {
|
||||
let { project, outputPath = './', fs = defaultFs } = params;
|
||||
|
||||
const getProject = (): IResultDir => {
|
||||
if (!project) {
|
||||
@ -49,19 +46,14 @@ export const createDiskPublisher: PublisherFactory<
|
||||
|
||||
const projectOutputPath = options.outputPath || outputPath;
|
||||
const overrideProjectSlug = options.projectSlug || params.projectSlug;
|
||||
const createProjectFolder =
|
||||
options.createProjectFolder || params.createProjectFolder;
|
||||
const createProjectFolder = options.createProjectFolder || params.createProjectFolder;
|
||||
|
||||
if (overrideProjectSlug) {
|
||||
projectToPublish.name = overrideProjectSlug;
|
||||
}
|
||||
|
||||
try {
|
||||
await writeFolder(
|
||||
projectToPublish,
|
||||
projectOutputPath,
|
||||
createProjectFolder,
|
||||
);
|
||||
await writeFolder(projectToPublish, projectOutputPath, createProjectFolder, fs);
|
||||
return { success: true, payload: projectOutputPath };
|
||||
} catch (error) {
|
||||
throw new PublisherError(error);
|
||||
|
||||
@ -1,39 +1,38 @@
|
||||
import { existsSync, mkdir, writeFile } from 'fs';
|
||||
import * as systemFs from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { IResultDir, IResultFile } from '../../types';
|
||||
|
||||
export interface IFileSystem {
|
||||
existsSync: typeof systemFs.existsSync;
|
||||
mkdir: typeof systemFs.mkdir;
|
||||
writeFile: typeof systemFs.writeFile;
|
||||
}
|
||||
|
||||
export const writeFolder = async (
|
||||
folder: IResultDir,
|
||||
currentPath: string,
|
||||
createProjectFolder = true,
|
||||
fs: IFileSystem = systemFs,
|
||||
): Promise<void> => {
|
||||
const { name, files, dirs } = folder;
|
||||
|
||||
const folderPath = createProjectFolder
|
||||
? join(currentPath, name)
|
||||
: currentPath;
|
||||
const folderPath = createProjectFolder ? join(currentPath, name) : currentPath;
|
||||
|
||||
if (!existsSync(folderPath)) {
|
||||
await createDirectory(folderPath);
|
||||
if (!fs.existsSync(folderPath)) {
|
||||
await createDirectory(folderPath, fs);
|
||||
}
|
||||
|
||||
const promises = [
|
||||
writeFilesToFolder(folderPath, files),
|
||||
writeSubFoldersToFolder(folderPath, dirs),
|
||||
];
|
||||
const promises = [writeFilesToFolder(folderPath, files, fs), writeSubFoldersToFolder(folderPath, dirs, fs)];
|
||||
|
||||
await Promise.all(promises);
|
||||
};
|
||||
|
||||
const writeFilesToFolder = async (
|
||||
folderPath: string,
|
||||
files: IResultFile[],
|
||||
): Promise<void> => {
|
||||
const promises = files.map(file => {
|
||||
const writeFilesToFolder = async (folderPath: string, files: IResultFile[], fs: IFileSystem): Promise<void> => {
|
||||
const promises = files.map((file) => {
|
||||
const fileName = file.ext ? `${file.name}.${file.ext}` : file.name;
|
||||
const filePath = join(folderPath, fileName);
|
||||
return writeContentToFile(filePath, file.content);
|
||||
return writeContentToFile(filePath, file.content, 'utf8', fs);
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
@ -42,17 +41,18 @@ const writeFilesToFolder = async (
|
||||
const writeSubFoldersToFolder = async (
|
||||
folderPath: string,
|
||||
subFolders: IResultDir[],
|
||||
fs: IFileSystem,
|
||||
): Promise<void> => {
|
||||
const promises = subFolders.map(subFolder => {
|
||||
const promises = subFolders.map((subFolder) => {
|
||||
return writeFolder(subFolder, folderPath);
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
};
|
||||
|
||||
const createDirectory = (pathToDir: string): Promise<void> => {
|
||||
const createDirectory = (pathToDir: string, fs: IFileSystem): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
mkdir(pathToDir, { recursive: true }, err => {
|
||||
fs.mkdir(pathToDir, { recursive: true }, (err) => {
|
||||
err ? reject(err) : resolve();
|
||||
});
|
||||
});
|
||||
@ -62,9 +62,10 @@ const writeContentToFile = (
|
||||
filePath: string,
|
||||
fileContent: string,
|
||||
encoding: string = 'utf8',
|
||||
fs: IFileSystem,
|
||||
): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
writeFile(filePath, fileContent, encoding, err => {
|
||||
fs.writeFile(filePath, fileContent, encoding, (err) => {
|
||||
err ? reject(err) : resolve();
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user