UNPKG

youzanyun-devtool-worker

Version:

238 lines (237 loc) 11.4 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 lodash_1 = require("lodash"); class PageHandler { constructor({ appHandler, categoryHandler, pageConfigDir, version }) { this.appHandler = appHandler; this.categoryHandler = categoryHandler; this.pageConfigDir = pageConfigDir; this.version = version; } async start() { let pageJson = this.pageJson = await fs_extra_1.default.readJson(path_1.default.resolve(this.pageConfigDir, 'page.json')); let versionJson = this.versionJson = await fs_extra_1.default.readJson(path_1.default.resolve(this.pageConfigDir, this.version, 'version.json')); this.demoDir = this.appHandler.getDemoDir(); this.mpRootDir = this.appHandler.getProjectMpRootDir(); this.showcaseMpRootDir = this.appHandler.showcaseMpRoot; this.pagePackage = pageJson.package; this.pagePath = pageJson.path; if (this.pagePackage !== 'main') { this.pageFullPath = `${this.pagePackage}/${pageJson.path}`; } else { this.pageFullPath = pageJson.path; } this.pageInProjectDir = `src/${path_1.default.dirname(this.pageFullPath)}`; if (versionJson.pageDemoPath) { this.pageDemoPath = `src/${path_1.default.dirname(versionJson.pageDemoPath)}`; } else { this.pageDemoPath = this.pageInProjectDir; } if (versionJson.allowPageModify && versionJson.pageModifyConfig && versionJson.pageModifyConfig.officialComponentDefinitionFile) { this.baseComponents = await fs_extra_1.default.readJson(path_1.default.resolve(this.pageConfigDir, this.version, versionJson.pageModifyConfig.officialComponentDefinitionFile)); } else { this.baseComponents = []; } for (let component of this.baseComponents) { component.type = 'base'; } } async customPageByPageReplace() { await fs_extra_1.default.copy(path_1.default.resolve(this.demoDir, this.pageInProjectDir + '-replace'), path_1.default.resolve(this.mpRootDir, this.pageInProjectDir)); await this._addPageInMpAppJson(); let config = { version: this.version, label: this.pageJson.label, customType: 'page-replace' }; let extension = await this.appHandler.getExtensionConfig(); extension.pageCustom[this.pageFullPath] = config; await this.appHandler.saveExtensionConfig(extension); } async customPageByPageModify() { await fs_extra_1.default.copy(path_1.default.resolve(this.demoDir, this.pageDemoPath), path_1.default.resolve(this.mpRootDir, this.pageInProjectDir)); await fs_extra_1.default.remove(path_1.default.resolve(this.mpRootDir, this.pageInProjectDir, 'custom-components')); await this._addPageInMpAppJson(); await this.saveCustomConfig({ components: this.baseComponents }); } async customPageByPageRemove() { } async cancelPageCustom() { await fs_extra_1.default.remove(path_1.default.resolve(this.mpRootDir, this.pageInProjectDir)); await this._removePageInMpAppJson(); let extension = await this.appHandler.getExtensionConfig(); delete extension.pageCustom[this.pageFullPath]; await this.appHandler.saveExtensionConfig(extension); } async saveCustomConfig(config, isShowcase = false) { const mpDir = isShowcase ? this.showcaseMpRootDir : this.mpRootDir; let pageModifyConfig = this.versionJson.pageModifyConfig; if (pageModifyConfig.allowUiCustom && pageModifyConfig.wxmlTemplate) { let tpl = await fs_extra_1.default.readFile(path_1.default.resolve(this.pageConfigDir, this.version, pageModifyConfig.wxmlTemplate), 'utf8'); let env = new nunjucks_1.default.Environment(null, { tags: { blockStart: '{%', blockEnd: '%}', variableStart: '{$', variableEnd: '$}', commentStart: '{#', commentEnd: '#}' } }); let template = nunjucks_1.default.compile(tpl, env); let groupedComponents = {}; if (pageModifyConfig.componentGroup) { let groupKey = pageModifyConfig.componentGroup.key; for (let component of config.components) { let key = `components_${groupKey}_${component[groupKey] || pageModifyConfig.componentGroup.defaultValue}`; let currentGroup = groupedComponents[key] || []; currentGroup.push(component); groupedComponents[key] = currentGroup; } } let wxml = template.render(Object.assign({ components: config.components }, groupedComponents)); await fs_extra_1.default.writeFile(path_1.default.resolve(mpDir, this.pageInProjectDir, 'index.wxml'), wxml); let pageJson = await fs_extra_1.default.readJson(path_1.default.resolve(mpDir, this.pageInProjectDir, 'index.json')); pageJson.usingComponents = {}; const usingComponents = []; const getCustomComps = (data = []) => { for (let item of data) { if (item.type === 'custom') { usingComponents.push(item.name); continue; } if (item.children) { getCustomComps(item.children); } } }; getCustomComps(config.components); for (let compName of (0, lodash_1.uniq)(usingComponents)) { pageJson.usingComponents[compName] = `./custom-components/${compName}/index`; } await fs_extra_1.default.writeJson(path_1.default.resolve(mpDir, this.pageInProjectDir, 'index.json'), pageJson, { spaces: 2 }); } let extension = await this.appHandler.getExtensionConfig(isShowcase); config.customType = 'component-replace'; config.label = this.pageJson.label; config.version = this.version; extension.pageCustom[this.pageFullPath] = config; await this.appHandler.saveExtensionConfig(extension); } async addNewComponent(component, isShowcase = false) { const mpRootDir = isShowcase ? this.showcaseMpRootDir : this.mpRootDir; console.log(this.mpRootDir, this.pageInProjectDir, this.demoDir, this.pageDemoPath); await fs_extra_1.default.copy(path_1.default.resolve(this.demoDir, this.pageDemoPath, 'custom-components/test'), path_1.default.resolve(mpRootDir, this.pageInProjectDir, `custom-components/${component.name}`)); } async deleteComponent(component) { console.log(this.mpRootDir, this.pageInProjectDir, this.demoDir, this.pageDemoPath); await fs_extra_1.default.remove(path_1.default.resolve(this.mpRootDir, this.pageInProjectDir, `custom-components/${component.name}`), function (err) { if (err) return console.error(err); console.log("success!"); }); } async removeComponent({ name }) { let componentPath = path_1.default.resolve(this.mpRootDir, this.pageInProjectDir, 'custom-components', name); await fs_extra_1.default.remove(componentPath); } async getPageJson() { return this.pageJson; } async getVersionJson() { return this.versionJson; } async getPageDir() { return this.pageInProjectDir; } async getCustomComponentDir() { return path_1.default.join(this.pageInProjectDir, 'custom-components'); } async getBaseComponents() { return this.baseComponents; } async getCustomComponents(isShowcase = false) { const dirPath = isShowcase ? this.showcaseMpRootDir : this.mpRootDir; let indexFileList = await (0, fast_glob_1.default)(['*/index.json'], { cwd: path_1.default.resolve(dirPath, this.pageInProjectDir, 'custom-components') }); let components = indexFileList.map(indexJson => { const name = path_1.default.dirname(indexJson); return { name, path: path_1.default.resolve(this.pageInProjectDir, 'custom-components', name), label: path_1.default.dirname(indexJson), type: 'custom' }; }); return components; } async getCustomConfig() { let extension = await this.appHandler.getExtensionConfig(); let pageCustom = extension.pageCustom; return pageCustom[this.pageFullPath] || { components: this.baseComponents, profile: {} }; } async _addPageInMpAppJson() { let appJson = await fs_extra_1.default.readJson(path_1.default.resolve(this.mpRootDir, 'src/app.json')); if (this.pagePackage !== 'main') { if (!appJson.subPackages) { appJson.subPackages = []; } let subPackage = (0, lodash_1.find)(appJson.subPackages, el => el.root == this.pagePackage); if (!subPackage) { subPackage = { "root": this.pagePackage, "name": this.pagePackage, "pages": [] }; appJson.subPackages.push(subPackage); } if ((0, lodash_1.findIndex)(subPackage.pages, el => el == this.pagePath) == -1) { subPackage.pages.push(this.pagePath); } } else { if ((0, lodash_1.findIndex)(appJson.pages, el => el == this.pagePath) == -1) { appJson.pages.push(this.pagePath); } } await fs_extra_1.default.writeJson(path_1.default.resolve(this.mpRootDir, 'src/app.json'), appJson, { spaces: 2 }); } async _removePageInMpAppJson() { let appJsonPath = path_1.default.resolve(this.mpRootDir, 'src/app.json'); let appJson = await fs_extra_1.default.readJson(appJsonPath); if (this.pagePackage !== 'main') { let subPackageIndex = (0, lodash_1.findIndex)(appJson.subPackages, (el) => el.root == this.pagePackage); if (subPackageIndex > -1) { let subPackage = appJson.subPackages[subPackageIndex]; appJson.subPackages.splice(subPackageIndex, 1); subPackage.pages = (0, lodash_1.filter)(subPackage.pages, el => el != this.pagePath); if (subPackage.pages.length > 0) { appJson.subPackages.push(subPackage); } } } else { appJson.pages = (0, lodash_1.filter)(appJson.pages, el => el != this.pagePath); } await fs_extra_1.default.writeJson(appJsonPath, appJson, { spaces: 2 }); } } exports.default = PageHandler; ;