youzanyun-devtool-worker
Version:
82 lines (81 loc) • 3.77 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 compCompile_1 = tslib_1.__importDefault(require("./compCompile"));
const constants_1 = require("./constants");
class PageHandler {
constructor({ beautyH5GuideDir, workDir, projectRootDir, projectName, pagePath, }) {
this.beautyH5GuideDir = beautyH5GuideDir;
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.replaceDir = path_1.default.resolve(this.extensionDir, this.pagePath);
}
async getComponents() {
const isExist = await fs_extra_1.default.pathExists(this.replaceDir);
if (!isExist) {
return [];
}
const comps = [];
const compFiles = await (0, fast_glob_1.default)('*/index.vue', {
cwd: this.replaceDir
});
for (let i = 0; i < compFiles.length; i++) {
const compPath = compFiles[i];
const buffer = await fs_extra_1.default.readFile(path_1.default.resolve(this.replaceDir, compPath));
const source = buffer.toString('utf8');
const componentProperties = (0, compCompile_1.default)(source);
comps.push(componentProperties);
}
return comps;
}
async saveComponentReplace(info) {
try {
const { name, title, customConfig, } = info;
const compPath = path_1.default.resolve(this.replaceDir, 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.beautyH5GuideDir, 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.beautyH5GuideDir, 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.removeComponentReplace(info);
throw err;
}
}
async removeComponentReplace(info) {
const compPath = path_1.default.resolve(this.replaceDir, info.name);
const isCompExist = await fs_extra_1.default.pathExists(compPath);
if (isCompExist) {
await fs_extra_1.default.remove(compPath);
}
}
}
exports.default = PageHandler;
;