UNPKG

youzanyun-devtool-worker

Version:

- web - ws - proxy

132 lines (131 loc) 5.93 kB
"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, workDir, projectRootDir, projectName, nodeCode, pageName, }) { this.h5CompDemoDir = h5CompDemoDir; this.workDir = workDir; this.projectRootDir = projectRootDir; this.projectName = projectName; this.nodeCode = nodeCode; this.pageName = pageName; } async start() { const h5ExtensionDir = this.h5ExtensionDir = path_1.default.resolve(this.projectRootDir, `${this.projectName}-ui/h5-extension`); const h5SrcDir = path_1.default.resolve(h5ExtensionDir, 'src'); if (this.pageName === '_global') { this.h5CompDir = path_1.default.resolve(h5SrcDir, this.pageName); } else if (this.pageName === '_common') { this.h5CompDir = path_1.default.resolve(h5SrcDir, this.nodeCode, this.pageName); } else { this.h5CompDir = path_1.default.resolve(h5SrcDir, 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); componentProperties.type = componentProperties.type || componentProperties.name; comps.push(componentProperties); } return comps; } async getDistPageComponents() { let result = { localComps: null, localPageFile: '' }; const distPath = path_1.default.resolve(this.h5ExtensionDir, "dist"); const infoPath = path_1.default.resolve(distPath, "info.json"); const isExist = await fs_extra_1.default.pathExists(infoPath); if (!isExist) { return result; } const info = await fs_extra_1.default.readJson(infoPath); if (info) { let comps = []; let localPageName = ''; if (info[this.pageName]) { comps = info[this.pageName].components; localPageName = this.pageName; } else if (info[this.nodeCode]) { comps = info[this.nodeCode].components; localPageName = this.nodeCode; } else if (info["_global"]) { comps = info["_global"].components; localPageName = "_global"; } result.localComps = comps.map((item) => (Object.assign(Object.assign({}, item), { type: item.name, title: item.title }))); let pageFiles = await fs_extra_1.default.readdir(distPath); pageFiles = pageFiles.filter(v => v !== "info.json" && v.indexOf(localPageName) === 0 && !v.endsWith('.map')); const pageFilePath = path_1.default.resolve(distPath, pageFiles[0]); result.localPageFile = `/_yzydev_/local/file?filePath=${pageFilePath}`; } return result; } async getExactDistPageFile() { let result = ''; const distPath = path_1.default.resolve(this.h5ExtensionDir, "dist"); const infoPath = path_1.default.resolve(distPath, "info.json"); const isExist = await fs_extra_1.default.pathExists(infoPath); if (!isExist) { return result; } let localPageName = this.pageName; if (this.pageName === '_common') { localPageName = this.nodeCode; } let pageFiles = await fs_extra_1.default.readdir(distPath); pageFiles = pageFiles.filter(v => v !== "info.json" && v.indexOf(localPageName) === 0 && !v.endsWith('.map')); const pageFilePath = path_1.default.resolve(distPath, pageFiles[0]); result = `/_yzydev_/local/file?filePath=${pageFilePath}`; return result; } } exports.default = PageHandler; ;