UNPKG

youzanyun-devtool-worker

Version:

- web - ws - proxy

368 lines (367 loc) 13.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const spring4js_nodejs_1 = require("spring4js-nodejs"); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const path_1 = tslib_1.__importDefault(require("path")); const TIME = 4000; let DeployPollService = class DeployPollService { constructor() { this._deploy = new spring4js_nodejs_1.Emitter(); this.onDeploy = this._deploy.registerEvent; this._pollError = new spring4js_nodejs_1.Emitter(); this.onPollError = this._pollError.registerEvent; this.isPolling = false; this.isGeneralDeploy = false; this.generalReleaseId = null; this.isMpDeploy = false; this.mpReleaseId = null; } get ecloudAuthHeader() { return { cookie: `app_env=dev; sid=${this.sid}; appid=${this.appDetail.appId}; apptype=${this.appDetail.appType}; user_name=${encodeURI(this.userName)}`, }; } async start() { } async initData(projectId) { const { proName } = await this.projectService.getProjectById(projectId); const { appDetail } = await this.applicationService.getAppDetail(proName); const { userName } = await this.userService.getUserInfo(); this.sid = await this.userService.getSid(); this.appDetail = appDetail; this.userName = userName; } async startLoop(projectId) { await this.initData(projectId); if (this.isPolling) { return { success: false, message: '轮询中,请勿重复操作' }; } this.isPolling = true; this.isMpDeploy = false; this.isGeneralDeploy = false; this.loop.call(this); return { success: true }; } async stopLoop() { if (this.isPolling) { this.isPolling = false; this.isMpDeploy = false; this.isGeneralDeploy = false; this.generalReleaseId = null; this.mpReleaseId = null; return { success: true }; } else { return { success: false, message: '轮询未开始' }; } } loop() { if (!this.isPolling) { return; } if (this.isGeneralDeploy && this.isMpDeploy) { setTimeout(this.loop.bind(this), TIME); return; } this.requestService.get('https://diy.youzanyun.com/api/apps/deploy/get-deploy-records', { params: { region: this.appDetail.appRegion, zone: 'dev', appId: this.appDetail.appId, pageSize: 30, pageIndex: 1, }, headers: this.ecloudAuthHeader, }) .then(res => { if (res.code !== 200) { throw new Error(res.message); } const deployList = res.data.items || []; const deployingList = deployList.filter((item) => item.releaseStatus === 'releasing'); const releaseingMp = deployingList.filter(item => item.type === 'mp')[0]; const releaseingGeneral = deployingList.filter(item => item.type === 'general')[0]; const recentMpReleaseId = (deployList.find((item) => item.type === 'mp') || {}).releaseId; const localReleaseList = deployList.filter((item) => (item.isLocalPublish === 1 && item.releaseStatus === 'success')); const recentSuccessGeneral = localReleaseList.find((item) => item.type === 'general'); const recentSuccessMp = localReleaseList.find((item) => item.type === 'mp'); const fireData = {}; if (recentSuccessGeneral) { fireData.isLocalSuccessGeneral = true; fireData.recentSuccessGeneral = recentSuccessGeneral; } if (recentSuccessMp) { fireData.isLocalSuccessMp = true; fireData.recentSuccessMp = recentSuccessMp; } if (recentMpReleaseId) { fireData.isRecentMpReleaseId = true; fireData.recentMpReleaseId = recentMpReleaseId; } if (fireData.isRecentMpReleaseId || fireData.isLocalSuccessGeneral || fireData.isLocalSuccessMp) { this._deploy.fire({ type: 'merge-message', code: 200, data: fireData, }); } if (releaseingMp && !this.isMpDeploy) { this.loopMp.call(this, releaseingMp); } if (releaseingGeneral && !this.isGeneralDeploy) { this.loopGeneral.call(this, releaseingGeneral); } }) .catch(err => { this._pollError.fire({ message: err.message || err, type: 'loop-catch-error', data: err.message, code: 500, }); }) .finally(() => { setTimeout(this.loop.bind(this), TIME); }); } async getMpLog() { const { data: log } = await this.requestService.get('https://diy.youzanyun.com/api/apps/business/deploy-mp/log', { params: { releaseId: this.mpReleaseId, }, headers: this.ecloudAuthHeader, }); return log || ''; } loopMp(releaseingMp) { if (releaseingMp) { this.isMpDeploy = true; this.mpReleaseId = releaseingMp.releaseId; } if (!this.mpReleaseId) { return; } this.requestService.get('https://diy.youzanyun.com/api/apps/deploy/get-deploy-status', { params: { zone: 'dev', appId: this.appDetail.appId, releaseId: this.mpReleaseId, }, headers: this.ecloudAuthHeader, }) .then(async (res) => { if (+res.code === 200) { const log = await this.getMpLog(); if (res.data === 'releasing') { setTimeout(this.loopMp.bind(this, releaseingMp), TIME); this._deploy.fire({ type: 'releaseing-mp', data: { releaseingMp, log }, }); } else if (res.data === 'success') { this._deploy.fire({ type: 'success-mp', data: { releaseingMp, log }, }); } else if (res.data === 'failed') { this._deploy.fire({ type: 'failed-mp', data: { releaseingMp, log }, }); } else { this.isMpDeploy = false; this.mpReleaseId = null; } } else { throw new Error(res.message); } }) .catch(err => { this._pollError.fire({ type: 'loop-mp-error', message: err.message || err, data: err, code: 500, }); }); } async getGeneralLog() { const { data: log } = await this.requestService.get('https://diy.youzanyun.com/api/apps/deploy/fetch-deploy-error-log', { params: { releaseId: this.generalReleaseId, appId: this.appDetail.appId, zone: 'dev', }, headers: this.ecloudAuthHeader, }); return log || ''; } loopGeneral(releaseingGeneral) { if (releaseingGeneral) { this.isGeneralDeploy = true; this.generalReleaseId = releaseingGeneral.releaseId; } if (!this.generalReleaseId) { return; } this.requestService.get('https://diy.youzanyun.com/api/apps/deploy/get-deploy-status', { params: { zone: 'dev', appId: this.appDetail.appId, releaseId: this.generalReleaseId, }, headers: this.ecloudAuthHeader, }) .then(async (res) => { if (+res.code === 200) { let log = await this.getGeneralLog(); if (res.data === 'releasing') { setTimeout(this.loopGeneral.bind(this, releaseingGeneral), TIME); this._deploy.fire({ type: 'releaseing-general', data: { releaseingGeneral, log }, }); } else if (res.data === 'success') { this._deploy.fire({ type: 'success-general', data: { releaseingGeneral, log }, }); } else if (res.data === 'failed') { this._deploy.fire({ type: 'failed-general', data: { releaseingGeneral, log }, }); } else { this.isGeneralDeploy = false; this.generalReleaseId = null; } } else { throw new Error(res.message); } }) .catch(err => { this._pollError.fire({ message: err.message || err, type: 'loop-general-error', data: err, code: 500, }); }); } async openGeneralConfig(param) { const { extPointId, uuid } = param; const result = await this.requestService.post('https://diy.youzanyun.com/api/apps/business/impl/router/add', { appId: this.appDetail.appId, extPointId, env: "dev", customRuleMap: {}, isDefaultConfig: true, ruleMap: {}, createBy: '', uuid, }, { headers: this.ecloudAuthHeader, }); return result; } async closeGeneralConfig(param) { const { uuid } = param; const result = await this.requestService.del(`https://diy.youzanyun.com/api/apps/business/impl/router/delete?env=dev&uuid=${uuid}`, { headers: this.ecloudAuthHeader, }); return result; } async getShopList(shopType) { const params = { appType: 0, pageSize: 100, page: 1, authStatus: 1, shopType, }; const result = await this.requestService.get('https://diy.youzanyun.com/api/apps/shop/multi', { params, headers: this.ecloudAuthHeader, }).catch(err => { console.error('err', err); }); return result; } async saveMpConfig(config) { let dataDir = this.configService.getDataDir(); let mpConfigFile = path_1.default.resolve(dataDir, `mp-deploy-config.${this.appDetail.appId}.json`); await fs_extra_1.default.writeJson(mpConfigFile, config, { spaces: 2 }); return { code: 200 }; } async getMpConfig() { let dataDir = this.configService.getDataDir(); let mpConfigFIle = path_1.default.resolve(dataDir, `mp-deploy-config.${this.appDetail.appId}.json`); let exists = await fs_extra_1.default.pathExists(mpConfigFIle); if (exists) { const config = await fs_extra_1.default.readJson(mpConfigFIle); return { code: 200, data: config }; } return { code: 404, message: '暂无配置' }; } async deploy(type) { console.log(type); let context = null; if (type === 'mp') { const mpConfig = await this.getMpConfig(); console.log(mpConfig); if (mpConfig.code === 200) { context = JSON.stringify(mpConfig.data); } else { return mpConfig; } } else if (type === 'general') { context = "{}"; } else { return { code: 400, message: '未知的发布类型' }; } const result = await this.requestService.post('https://diy.youzanyun.com/api/apps/deploy/deploy-new-app', { appId: this.appDetail.appId, comment: "开发者工具一键发布", region: this.appDetail.region, requestType: "normal", isLocalPublish: 1, zone: "dev", context, type }, { headers: this.ecloudAuthHeader, }); return result; } }; tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], DeployPollService.prototype, "configService", void 0); tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], DeployPollService.prototype, "userService", void 0); tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], DeployPollService.prototype, "requestService", void 0); tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], DeployPollService.prototype, "projectService", void 0); tslib_1.__decorate([ spring4js_nodejs_1.Resource() ], DeployPollService.prototype, "applicationService", void 0); DeployPollService = tslib_1.__decorate([ spring4js_nodejs_1.Service() ], DeployPollService); exports.default = DeployPollService;