UNPKG

youzanyun-devtool-worker

Version:

360 lines (359 loc) 16.7 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 download_1 = tslib_1.__importDefault(require("download")); const camelCase_1 = tslib_1.__importDefault(require("lodash/camelCase")); const upperFirst_1 = tslib_1.__importDefault(require("lodash/upperFirst")); const merge_1 = tslib_1.__importDefault(require("lodash/merge")); const spring4js_nodejs_1 = require("spring4js-nodejs"); const AppHandler_1 = tslib_1.__importDefault(require("./AppHandler")); const PageHandler_1 = tslib_1.__importDefault(require("./PageHandler")); const constants_1 = require("./constants"); let BeautyH5Service = class BeautyH5Service { constructor() { this.previewCompsMap = {}; this._broadcastPreviewComps = new spring4js_nodejs_1.Emitter(); this.onBroadcastPreviewComps = this._broadcastPreviewComps.registerEvent; this.remoteTemplateVersion = {}; this.appHandlerCache = {}; this.pageHandlerCache = {}; this.versionFileName = 'ecloud-tpl-version.json'; } async start() { this.workDir = this.configService.getWorkerDir(); const h5DemoDir = this.configService.getH5DemoDir(); await fs_extra_1.default.ensureDir(h5DemoDir); const beautyH5GuideDir = this.beautyH5GuideDir = path_1.default.resolve(h5DemoDir, `${constants_1.WORK_DIR_NAME}`); this.beautyH5DemoDir = path_1.default.resolve(beautyH5GuideDir, 'demo'); const remoteToolConfig = await this.customApiService.getRemoteToolConfig(); let remoteTemplateVersion = {}; if (remoteToolConfig.templateVersion) { remoteTemplateVersion = this.remoteTemplateVersion = JSON.parse(remoteToolConfig.templateVersion); } await this.checkAndDownloadTpl({ version: remoteTemplateVersion.beautyH5FrameworkVersion, demoUrl: remoteTemplateVersion.beautyH5Url }, this.beautyH5GuideDir); const beautyH5ConfigFile = path_1.default.resolve(beautyH5GuideDir, 'config.json'); const beautyH5Config = await fs_extra_1.default.readJSON(beautyH5ConfigFile); this.customTypeConfig = beautyH5Config.customTypeConfig; this.beautyH5Pages = beautyH5Config.pages; } async getMenu(workbenchId, projectId) { let menuData = [{ key: 'home', title: '首页', param: { contentType: 'webview', url: `/html/mei-h5-extension/?projectId=${projectId}&workbenchId=${workbenchId}#/home`, docKey: 'h5-home', } }]; this.customTypeConfig.map((item) => { const { type, title } = item; const menuItem = { key: type, title, }; const pages = this.beautyH5Pages.filter(item => item.customType === type); if (pages && pages.length) { menuItem.children = pages.map((cItem) => ({ key: cItem.type, title: cItem.title, param: { contentType: 'webview', url: `/html/mei-h5-extension?projectId=${projectId}&workbenchId=${workbenchId}&customType=${type}` + `&pageName=${cItem.type}#/list`, } })); } else { menuItem.param = { contentType: 'webview', url: `/html/mei-h5-extension?projectId=${projectId}&workbenchId=${workbenchId}&customType=${type}#/list`, }; } menuData.push(menuItem); }); menuData.push({ key: 'upload', title: '上传', param: { contentType: 'webview', url: `/html/mei-h5-extension/?projectId=${projectId}&workbenchId=${workbenchId}#/upload`, } }); return menuData; } async initPageDemo(projectId) { const project = await this.projectService.getProjectById(projectId); let versionInfo = {}; let remoteConfig = { version: this.remoteTemplateVersion.beautyH5FrameworkVersion, demoUrl: this.remoteTemplateVersion.beautyH5Url }; if (remoteConfig.demoUrl) { await this.checkAndDownloadTpl(remoteConfig, this.beautyH5GuideDir); const extensionDir = path_1.default.resolve(project.proPath, `${project.proName}-ui/${constants_1.WORK_DIR_NAME}`); const versionPath = path_1.default.resolve(extensionDir, this.versionFileName); let isVersionExist = await fs_extra_1.default.pathExists(versionPath); if (!isVersionExist) { await fs_extra_1.default.copy(this.beautyH5DemoDir, extensionDir); await fs_extra_1.default.copy(path_1.default.resolve(this.beautyH5GuideDir, this.versionFileName), versionPath); versionInfo = { latestVersion: remoteConfig.version, currentVersion: remoteConfig.version, }; } else { const currentVersionInfo = await fs_extra_1.default.readJSON(versionPath); versionInfo = { latestVersion: remoteConfig.version, currentVersion: currentVersionInfo.frameworkVersion }; } } return versionInfo; } async updateProject(projectId) { const project = await this.projectService.getProjectById(projectId); const extensionDir = path_1.default.resolve(project.proPath, `${project.proName}-ui/${constants_1.WORK_DIR_NAME}`); await fs_extra_1.default.ensureDir(path_1.default.resolve(extensionDir, 'src')); await fs_extra_1.default.remove(path_1.default.resolve(extensionDir, this.versionFileName)); await fs_extra_1.default.copy(path_1.default.resolve(this.beautyH5GuideDir, this.versionFileName), path_1.default.resolve(extensionDir, this.versionFileName)); await fs_extra_1.default.remove(path_1.default.resolve(extensionDir, 'webpack')); await fs_extra_1.default.copy(path_1.default.resolve(this.beautyH5DemoDir, 'webpack'), path_1.default.resolve(extensionDir, 'webpack')); await fs_extra_1.default.remove(path_1.default.resolve(extensionDir, 'package-lock.json')); await fs_extra_1.default.remove(path_1.default.resolve(extensionDir, 'yarn.lock')); const compPckExist = await fs_extra_1.default.pathExists(path_1.default.resolve(extensionDir, 'package.json')); if (compPckExist) { const currentCompPackage = await fs_extra_1.default.readJSON(path_1.default.resolve(extensionDir, 'package.json')); const demoCompPackage = await fs_extra_1.default.readJSON(path_1.default.resolve(this.beautyH5DemoDir, 'package.json')); await fs_extra_1.default.writeJSON(path_1.default.resolve(extensionDir, 'package.json'), (0, merge_1.default)({}, currentCompPackage, demoCompPackage), { spaces: 2 }); } else { await fs_extra_1.default.copy(path_1.default.resolve(this.beautyH5DemoDir, 'package.json'), path_1.default.resolve(extensionDir, 'package.json')); } await fs_extra_1.default.remove(path_1.default.resolve(extensionDir, 'README.md')); await fs_extra_1.default.copy(path_1.default.resolve(this.beautyH5DemoDir, 'README.md'), path_1.default.resolve(extensionDir, 'README.md')); } async getPageCustomList(params) { const { projectId, customType, pageName, customed } = params; let result = []; const customTypes = this.customTypeConfig.filter(item => !customType ? true : item.type === customType); for (let i = 0; i < customTypes.length; i++) { const item = customTypes[i]; const type = (0, upperFirst_1.default)((0, camelCase_1.default)(item.type)); const funName = `get${type}List`; const pageInfo = await this[funName]({ projectId, }); const customInfo = pageInfo.filter((item) => customed ? item.isCustomed : true); if (customInfo.length > 0) { if (customType) { if (pageName) { const pageData = customInfo.find((item) => item.type === pageName); result = Object.assign({ parentInfo: item }, pageData); } else { result = { parentInfo: item, expandedList: customInfo || [] }; } } else { result.push(Object.assign(Object.assign({}, item), { children: customInfo })); } } } return result; } async getPageAddList(params) { const { projectId } = params; const customType = 'page-add'; let pageInfo = this.customTypeConfig.find(item => item.type === customType) || {}; const appHandler = await this.getAppHandler({ projectId, groupPath: pageInfo.path, }); const customInfo = appHandler.getAddPages(); return customInfo; } async getComponentReplaceList(params) { const customType = 'component-replace'; const pageInfos = this.beautyH5Pages.filter(item => item.customType === customType) || {}; let result = []; const { projectId } = params; for (let page of pageInfos) { if (page.components && page.components.length) { const pageHandler = await this.getPageHandler({ projectId, pagePath: page.path, }); const comps = await pageHandler.getComponents(); const customCompNames = comps.map((item) => item.name); const customInfo = page.components.map((item) => { const isCustomed = customCompNames.indexOf(item.type) > -1; return Object.assign(Object.assign({ isCustomed }, item), { name: item.name || item.type }); }); const isPageCustomed = customInfo ? customInfo.find((item) => item.isCustomed) : false; result.push(Object.assign(Object.assign({}, page), { isCustomed: isPageCustomed, expandedList: customInfo })); } } return result; } async getAppHandler({ projectId, groupPath }) { const cacheKey = projectId; let appHandler = this.pageHandlerCache[cacheKey]; if (appHandler) { return appHandler; } const project = await this.projectService.getProjectById(projectId); appHandler = new AppHandler_1.default({ beautyH5GuideDir: this.beautyH5GuideDir, workDir: this.workDir, projectRootDir: project.proPath, projectName: project.proName, groupPath, }); await appHandler.start(); this.appHandlerCache[cacheKey] = appHandler; return appHandler; } async getPageHandler({ projectId, pagePath }) { let cacheKey = `${projectId}-${pagePath}`; let pageHandler = this.pageHandlerCache[cacheKey]; if (pageHandler) { return pageHandler; } const project = await this.projectService.getProjectById(projectId); pageHandler = new PageHandler_1.default({ beautyH5GuideDir: this.beautyH5GuideDir, workDir: this.workDir, projectRootDir: project.proPath, projectName: project.proName, pagePath, }); await pageHandler.start(); this.pageHandlerCache[cacheKey] = pageHandler; return pageHandler; } async checkAndDownloadTpl(remoteConfig, demoDir) { let needDownload = false; let cacheVersionInfo = {}; try { cacheVersionInfo = await fs_extra_1.default.readJson(path_1.default.resolve(demoDir, this.versionFileName)); if (remoteConfig.version != cacheVersionInfo.frameworkVersion) { needDownload = true; } } catch (e) { needDownload = true; } if (needDownload) { await fs_extra_1.default.remove(demoDir); await (0, download_1.default)(remoteConfig.demoUrl, demoDir, { extract: true, strip: 1, }); } } async getTouchJsPath() { const touchJsPath = path_1.default.resolve(this.configService.getWorkerDir(), 'assets/utils/touch-emulator.js'); return touchJsPath; } async uploadInfo(params) { const { projectId } = params; const project = await this.projectService.getProjectById(projectId); const isClean = await this.projectService.checkProjectStatus(project.proPath); if (!isClean) { throw new Error('请先提交本地代码'); } const userInfo = await this.userService.getUserInfo(); const prodBuildDir = path_1.default.resolve(project.proPath, `${project.proName}-ui/${constants_1.WORK_DIR_NAME}/dist`); const infoPath = path_1.default.resolve(prodBuildDir, 'info.json'); const pagesInfo = await fs_extra_1.default.readJSON(infoPath); const pageValues = Object.values(pagesInfo); for (let t = 0; t < pageValues.length; t++) { const pages = pageValues[t]; if (pages && pages.length > 0) { for (let i = 0; i < pages.length; i++) { const page = pages[i]; const uploadParams = { projectName: project.proName, pageName: page.pageName, fileList: [], addHash: false, }; if (page.url) { uploadParams.fileList = page.url; const url = await this.uploadToCdn(uploadParams); page.url = url; } if (page.js) { uploadParams.fileList = page.js; const url = await this.uploadToCdn(uploadParams); page.js = url; } if (page.css) { uploadParams.fileList = page.css; const url = await this.uploadToCdn(uploadParams); page.css = url; } } } } const pages = [{ pageName: 'mei_diy_wap', content: pagesInfo, }]; const customInfo = { type: 'original', appName: project.proName, commitUser: userInfo.userName || userInfo.mobile || 'youzanyun-developer-tool', commitHash: 'unknown', pages, }; const saveRes = await this.customApiService.saveCustomContent(customInfo); return saveRes; } async uploadToCdn(data) { const params = Object.assign({}, data); const fileListType = typeof data.fileList; if (!data.fileList) { return null; } else if (fileListType === 'string') { params.fileList = [data.fileList]; } const uploadCompFileList = await this.cdnService.uploadFileToCdn(params); const uploadUrls = uploadCompFileList.map(item => item.url); if (fileListType === 'string') { return uploadUrls[0]; } return uploadUrls; } }; tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], BeautyH5Service.prototype, "projectService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], BeautyH5Service.prototype, "configService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], BeautyH5Service.prototype, "customApiService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], BeautyH5Service.prototype, "cdnService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], BeautyH5Service.prototype, "userService", void 0); BeautyH5Service = tslib_1.__decorate([ (0, spring4js_nodejs_1.Service)() ], BeautyH5Service); exports.default = BeautyH5Service; ;