youzanyun-devtool-worker
Version:
- web - ws - proxy
148 lines (147 loc) • 6.18 kB
JavaScript
"use strict";
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"));
class PageHandler {
constructor({ h5CompDemoDir, h5ReplaceDemoDir, workDir, customType, projectRootDir, projectName, nodeCode, pageName, }) {
this.h5CompDemoDir = h5CompDemoDir;
this.h5ReplaceDemoDir = h5ReplaceDemoDir;
this.workDir = workDir;
this.customType = customType;
this.projectRootDir = projectRootDir;
this.projectName = projectName;
this.nodeCode = nodeCode;
this.pageName = pageName;
}
async start() {
if (this.customType === 'h5-script-replace') {
this.h5ReplaceDir = path_1.default.resolve(this.projectRootDir, `${this.projectName}-ui/h5-script-replace/src`);
}
else {
const h5ExtensionDir = path_1.default.resolve(this.projectRootDir, `${this.projectName}-ui/h5-extension/src`);
if (this.pageName === '_global') {
this.h5CompDir = path_1.default.resolve(h5ExtensionDir, this.pageName);
}
else if (this.pageName === '_common') {
this.h5CompDir = path_1.default.resolve(h5ExtensionDir, this.nodeCode, this.pageName);
}
else {
this.h5CompDir = path_1.default.resolve(h5ExtensionDir, this.nodeCode, this.pageName, 'components');
}
}
}
async savePageCompCustom() {
await fs_extra_1.default.ensureDir(this.h5CompDir);
}
async removePageCompCustom() {
await fs_extra_1.default.remove(this.h5CompDir);
}
async addComponent(info) {
const compPath = path_1.default.resolve(this.h5CompDir, info.name);
const isCompExist = await fs_extra_1.default.pathExists(compPath);
if (isCompExist) {
throw new Error('组件已存在');
}
await fs_extra_1.default.ensureDir(compPath);
await fs_extra_1.default.ensureFile(path_1.default.resolve(compPath, 'App.vue'));
const compTpl = await fs_extra_1.default.readFile(path_1.default.resolve(this.workDir, 'h5-extension-tpl/App.vue.njk'), 'utf8');
const compNun = nunjucks_1.default.compile(compTpl);
const compView = compNun.render({ info });
await fs_extra_1.default.writeFile(path_1.default.resolve(compPath, 'App.vue'), compView);
}
async removeComponent(info) {
const compPath = path_1.default.resolve(this.h5CompDir, info.name);
const isCompExist = await fs_extra_1.default.pathExists(compPath);
if (isCompExist) {
await fs_extra_1.default.remove(compPath);
}
}
async isPageCompCustom() {
const isExist = await fs_extra_1.default.pathExists(this.h5CompDir);
return isExist;
}
async getComponents() {
const comps = [];
const compFiles = await fast_glob_1.default('*/App.vue', {
cwd: this.h5CompDir
});
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.h5CompDir, compPath));
const source = buffer.toString('utf8');
const componentProperties = compCompile_1.default(source);
comps.push(componentProperties);
}
return comps;
}
async savePageReplaceCustom() {
await fs_extra_1.default.ensureDir(this.h5ReplaceDir);
}
async removePageReplaceCustom() {
await fs_extra_1.default.remove(this.h5ReplaceDir);
}
async isPageReplaceCustom() {
const isExist = await fs_extra_1.default.pathExists(this.h5ReplaceDir);
if (!isExist) {
return {
isCustomed: false,
expandedList: []
};
}
const replacePages = await this.getReplacePages(this.pageName);
const isCustomed = !!replacePages && replacePages.length > 0;
return {
isCustomed,
expandedList: replacePages
};
}
async getReplacePages(pageName) {
const configFiles = await fast_glob_1.default('**/config.json', {
cwd: this.h5ReplaceDir
});
const configInfo = configFiles.map((file) => {
const config = fs_extra_1.default.readJsonSync(path_1.default.resolve(this.h5ReplaceDir, file));
return {
pageName: config.pageName,
planName: config.planName,
name: config.name,
};
});
let result = configInfo;
if (pageName) {
result = configInfo.filter(item => {
return item.pageName === pageName;
});
}
return result;
}
async addReplacePage(info) {
await fs_extra_1.default.ensureDir(this.h5ReplaceDir);
const pagePath = path_1.default.resolve(this.h5ReplaceDir, info.name);
const isPageExist = await fs_extra_1.default.pathExists(pagePath);
if (isPageExist) {
throw new Error('页面已存在');
}
await fs_extra_1.default.ensureDir(pagePath);
await fs_extra_1.default.copy(path_1.default.resolve(this.h5ReplaceDemoDir, 'src'), pagePath);
const config = {
"pageName": this.pageName,
"planName": info.title,
"name": info.name
};
await fs_extra_1.default.writeFile(path_1.default.resolve(pagePath, 'config.json'), JSON.stringify(config));
}
async removeReplacePage(info) {
const pagePath = path_1.default.resolve(this.h5ReplaceDir, info.name);
const isPageExist = await fs_extra_1.default.pathExists(pagePath);
if (isPageExist) {
await fs_extra_1.default.remove(pagePath);
}
}
}
exports.default = PageHandler;
;