youzanyun-devtool-worker
Version:
81 lines (80 loc) • 3.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const nunjucks_1 = tslib_1.__importDefault(require("nunjucks"));
const fast_glob_1 = tslib_1.__importDefault(require("fast-glob"));
const constants_1 = require("./constants");
class PageHandler {
constructor({ beautyPcGuideDir, workDir, projectRootDir, projectName, pagePath, }) {
this.beautyPcGuideDir = beautyPcGuideDir;
this.workDir = workDir;
this.projectRootDir = projectRootDir;
this.projectName = projectName;
this.pagePath = pagePath;
}
async start() {
this.extensionDir = path_1.default.resolve(this.projectRootDir, `${this.projectName}-ui/${constants_1.WORK_DIR_NAME}/src`);
this.pageReplaceDir = path_1.default.resolve(this.extensionDir, this.pagePath);
}
async isPageReplaceCustom() {
const isExist = await fs_extra_1.default.pathExists(this.pageReplaceDir);
return isExist;
}
async getPages() {
const pageFiles = await (0, fast_glob_1.default)('**/main.js', {
cwd: this.pageReplaceDir
});
const customInfo = pageFiles.map((file) => {
const filePath = file.split(path_1.default.sep).slice(0, -1).join(path_1.default.sep);
return {
title: filePath,
path: filePath,
isCustomed: true,
};
});
return customInfo;
}
async savePageReplace(info) {
try {
const { name, title, customConfig, } = info;
const compPath = path_1.default.resolve(this.pageReplaceDir, name);
const isCompExist = await fs_extra_1.default.pathExists(compPath);
if (isCompExist) {
throw new Error('组件已存在');
}
await fs_extra_1.default.ensureDir(compPath);
if (customConfig) {
if (customConfig.autoCreateFiles) {
for (let filePath of customConfig.autoCreateFiles) {
await fs_extra_1.default.copy(path_1.default.resolve(this.beautyPcGuideDir, filePath), path_1.default.resolve(compPath, path_1.default.basename(filePath)));
}
}
if (customConfig.njkFiles) {
for (let njkFile of customConfig.njkFiles) {
const fileName = path_1.default.basename(njkFile).split('.njk')[0];
await fs_extra_1.default.ensureFile(path_1.default.resolve(compPath, fileName));
const fileTpl = await fs_extra_1.default.readFile(path_1.default.resolve(this.beautyPcGuideDir, njkFile), 'utf8');
const fileNun = nunjucks_1.default.compile(fileTpl);
const fileView = fileNun.render({ info });
await fs_extra_1.default.writeFile(path_1.default.resolve(compPath, fileName), fileView);
}
}
}
}
catch (err) {
await this.removePageReplace(info);
throw err;
}
}
async removePageReplace(info) {
const compPath = path_1.default.resolve(this.pageReplaceDir, info.name);
const isCompExist = await fs_extra_1.default.pathExists(compPath);
if (isCompExist) {
await fs_extra_1.default.remove(compPath);
}
}
}
exports.default = PageHandler;
;