UNPKG

youzanyun-devtool-worker

Version:

- web - ws - proxy

207 lines (206 loc) 11.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const download_1 = tslib_1.__importDefault(require("download")); const fast_glob_1 = tslib_1.__importDefault(require("fast-glob")); const path_1 = tslib_1.__importDefault(require("path")); const nunjucks_1 = tslib_1.__importDefault(require("nunjucks")); const spring4js_nodejs_1 = require("spring4js-nodejs"); let CustomCompsService = class CustomCompsService { async start() { this.cacheDir = this.configService.getDpCacheDir(); this.tplPath = path_1.default.resolve(this.cacheDir, 'design-platform-tpl'); this.tplVersionFileName = 'ecloud-tpl-version.json'; await fs_extra_1.default.ensureDir(this.tplPath); } async createDevelopProject({ appId }) { const projectInfo = await this.projectService.getProjectById(appId); const latestVersion = await this._checkAndDownloadTpl(); let appVersionPath = path_1.default.resolve(projectInfo.proPath, this.tplVersionFileName); let isAppVersionExist = await fs_extra_1.default.pathExists(appVersionPath); if (!isAppVersionExist) { await fs_extra_1.default.copy(this.tplPath, projectInfo.proPath); return { latestVersion, currentVersion: latestVersion }; } else { const currentVersionInfo = await fs_extra_1.default.readJSON(appVersionPath); if (currentVersionInfo.designPlatformFrameworkVersion === '0.0.0') { await fs_extra_1.default.copy(this.tplPath, projectInfo.proPath); return { latestVersion, currentVersion: latestVersion }; } return { latestVersion, currentVersion: currentVersionInfo.designPlatformFrameworkVersion }; } } async _checkAndDownloadTpl() { let remoteToolConfig = {}; try { remoteToolConfig = await this.customApiService.getRemoteToolConfig(); } catch (err) { throw new Error('初始化工程失败'); } const remoteVersionInfo = JSON.parse(remoteToolConfig.templateVersion || '{}'); let needDownload = false; let cacheVersionInfo = {}; try { cacheVersionInfo = await fs_extra_1.default.readJson(path_1.default.resolve(this.tplPath, this.tplVersionFileName)); if (remoteVersionInfo.designPlatformFrameworkVersion != cacheVersionInfo.designPlatformFrameworkVersion) { needDownload = true; } } catch (e) { needDownload = true; } if (needDownload) { await fs_extra_1.default.remove(this.tplPath); await download_1.default(remoteVersionInfo.designPlatformUrl, this.tplPath, { extract: true, strip: 1, headers: { accept: 'application/zip' } }); fs_extra_1.default.ensureDir(path_1.default.resolve(this.tplPath, 'showcase-components/src')); fs_extra_1.default.ensureDir(path_1.default.resolve(this.tplPath, 'showcase-editor/src/editors')); } return remoteVersionInfo.designPlatformFrameworkVersion; } async updateProject({ appId }) { const latestVersion = await this._checkAndDownloadTpl(); const projectInfo = await this.projectService.getProjectById(appId); const projectRoot = projectInfo.proPath; let appVersionPath = path_1.default.resolve(projectRoot, this.tplVersionFileName); let isAppVersionExist = await fs_extra_1.default.pathExists(appVersionPath); if (!isAppVersionExist) { await fs_extra_1.default.copy(this.tplPath, projectRoot); } else { await fs_extra_1.default.remove(path_1.default.resolve(projectRoot, 'ecloud-tpl-version.json')); await fs_extra_1.default.copy(path_1.default.resolve(this.tplPath, 'ecloud-tpl-version.json'), path_1.default.resolve(projectRoot, 'ecloud-tpl-version.json')); await fs_extra_1.default.remove(path_1.default.resolve(projectRoot, 'showcase-components/webpack')); await fs_extra_1.default.copy(path_1.default.resolve(this.tplPath, 'showcase-components/webpack'), path_1.default.resolve(projectRoot, 'showcase-components/webpack')); await fs_extra_1.default.remove(path_1.default.resolve(projectRoot, 'showcase-components/package.json')); await fs_extra_1.default.copy(path_1.default.resolve(this.tplPath, 'showcase-components/package.json'), path_1.default.resolve(projectRoot, 'showcase-components/package.json')); await fs_extra_1.default.copy(path_1.default.resolve(this.tplPath, 'showcase-components/README.md'), path_1.default.resolve(projectRoot, 'showcase-components/README.md')); await fs_extra_1.default.remove(path_1.default.resolve(projectRoot, 'showcase-editor/webpack')); await fs_extra_1.default.copy(path_1.default.resolve(this.tplPath, 'showcase-editor/webpack'), path_1.default.resolve(projectRoot, 'showcase-editor/webpack')); await fs_extra_1.default.ensureDir(path_1.default.resolve(projectRoot, 'showcase-editor/src/common')); await fs_extra_1.default.copy(path_1.default.resolve(this.tplPath, 'showcase-editor/src/common'), path_1.default.resolve(projectRoot, 'showcase-editor/src/common')); await fs_extra_1.default.ensureDir(path_1.default.resolve(projectRoot, 'showcase-editor/src/utils')); await fs_extra_1.default.copy(path_1.default.resolve(this.tplPath, 'showcase-editor/src/utils'), path_1.default.resolve(projectRoot, 'showcase-editor/src/utils')); await fs_extra_1.default.remove(path_1.default.resolve(projectRoot, 'showcase-editor/package.json')); await fs_extra_1.default.copy(path_1.default.resolve(this.tplPath, 'showcase-editor/package.json'), path_1.default.resolve(projectRoot, 'showcase-editor/package.json')); await fs_extra_1.default.copy(path_1.default.resolve(this.tplPath, 'showcase-editor/README.md'), path_1.default.resolve(projectRoot, 'showcase-editor/README.md')); } const currentVersionInfo = await fs_extra_1.default.readJSON(appVersionPath); return { latestVersion, currentVersion: currentVersionInfo.designPlatformFrameworkVersion }; } async getCompsByAppName(params) { const appList = await this.projectService.getProjectsByProName({ proName: params.appName, }); const projectRoot = appList[0].proPath; const editorFileList = await fast_glob_1.default(path_1.default.resolve(projectRoot, 'showcase-editor/dist/*.js')); const editors = editorFileList.map(editorFile => /showcase-editor\/dist\/([\s\S]+).js/.exec(editorFile)[1]); const compFileList = await fast_glob_1.default(path_1.default.resolve(projectRoot, 'showcase-components/dist/*.js')); const comps = compFileList.map(compFile => /showcase-components\/dist\/([\s\S]+).js/.exec(compFile)[1]); const result = await this.dpRequestService.getDpCompList(params); if (+result.code !== 0 && +result.code !== 200) { return result; } const missingCompNames = []; const componentList = result.data.componentList || []; const finalCompList = componentList.filter((item) => { const isExist = editors.indexOf(item.code) > -1 && comps.indexOf(item.code) > -1; if (!isExist) { missingCompNames.push(item.code); } return isExist; }); return { compList: finalCompList, missingCompNames, }; } async saveComp(comp) { const { proPath } = comp, otherParams = tslib_1.__rest(comp, ["proPath"]); const result = await this.dpRequestService.saveDpComponent(otherParams); if (+result.code !== 0 && +result.code !== 200) { return result; } const compPath = path_1.default.resolve(proPath, `showcase-components/src/${comp.code}`); const isCompExist = await fs_extra_1.default.pathExists(compPath); if (!isCompExist) { 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(__dirname, '../../../design-platform-tpl/App.vue.njk'), 'utf8'); const compNun = nunjucks_1.default.compile(compTpl); const compView = compNun.render({ comp }); await fs_extra_1.default.writeFile(path_1.default.resolve(compPath, 'App.vue'), compView); } const editorPath = path_1.default.resolve(proPath, `showcase-editor/src/editors/${comp.code}`); const isEditorExist = await fs_extra_1.default.pathExists(editorPath); if (!isEditorExist) { await fs_extra_1.default.ensureDir(editorPath); await fs_extra_1.default.copy(path_1.default.resolve(__dirname, '../../../design-platform-tpl/editor-tpl'), editorPath); await fs_extra_1.default.remove(path_1.default.resolve(editorPath, 'editor.js.njk')); const editorTpl = await fs_extra_1.default.readFile(path_1.default.resolve(__dirname, '../../../design-platform-tpl/editor-tpl/editor.js.njk'), 'utf8'); const editorNun = nunjucks_1.default.compile(editorTpl); const editorView = editorNun.render({ comp }); await fs_extra_1.default.writeFile(path_1.default.resolve(editorPath, 'editor.js'), editorView); } return result.data; } async updateComp(params) { const result = await this.dpRequestService.updateDpComponent(params); return result; } async deleteComp(params) { const { proPath } = params, otherParams = tslib_1.__rest(params, ["proPath"]); const result = await this.dpRequestService.removeDpComponent(otherParams); if (+result.code !== 0 && +result.code !== 200) { return result; } const compPath = path_1.default.resolve(proPath, `showcase-components/src/${params.componentCode}`); const editorPath = path_1.default.resolve(proPath, `showcase-editor/src/editors/${params.componentCode}`); const isCompExist = await fs_extra_1.default.pathExists(compPath); const isEditorExist = await fs_extra_1.default.pathExists(editorPath); if (isCompExist) { await fs_extra_1.default.remove(compPath); } if (isEditorExist) { await fs_extra_1.default.remove(editorPath); } return {}; } }; tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], CustomCompsService.prototype, "projectService", void 0); tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], CustomCompsService.prototype, "configService", void 0); tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], CustomCompsService.prototype, "customApiService", void 0); tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], CustomCompsService.prototype, "dpRequestService", void 0); CustomCompsService = tslib_1.__decorate([ spring4js_nodejs_1.Service() ], CustomCompsService); exports.default = CustomCompsService; ;