From 6cbe01960c5804415019a427f609f1a7c3c995a1 Mon Sep 17 00:00:00 2001 From: yZaio <55974395+yZaio@users.noreply.github.com> Date: Tue, 14 Mar 2023 15:20:50 +0800 Subject: [PATCH] fix: uninstall default and import problem (#267) * fix: uninstall default and import --------- Co-authored-by: sunzhouyang --- .../params-import/params-import.component.ts | 3 +- .../src/app/shared/constants/extension.ts | 1 + .../services/extensions/extension.service.ts | 18 +++--- .../browser/src/app/utils/pc-merge.ts | 62 +++++++++++++++++++ 4 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 src/workbench/browser/src/app/shared/constants/extension.ts create mode 100644 src/workbench/browser/src/app/utils/pc-merge.ts diff --git a/src/workbench/browser/src/app/modules/api-shared/params-import/params-import.component.ts b/src/workbench/browser/src/app/modules/api-shared/params-import/params-import.component.ts index 4313a0247..3c080dd7c 100644 --- a/src/workbench/browser/src/app/modules/api-shared/params-import/params-import.component.ts +++ b/src/workbench/browser/src/app/modules/api-shared/params-import/params-import.component.ts @@ -1,5 +1,6 @@ import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core'; import { EoNgFeedbackMessageService } from 'eo-ng-feedback'; +import { pcMerge } from 'eo/workbench/browser/src/app/utils/pc-merge'; import { cloneDeep, toArray, merge, isEmpty } from 'lodash-es'; import { computed, observable, makeObservable, reaction } from 'mobx'; import qs from 'qs'; @@ -182,7 +183,7 @@ export class ParamsImportComponent implements OnInit { const combineFunc = { overwrite: data => data, append: (data, base) => base.concat(data), - mixin: (data, base) => obj2array(merge(array2obj(base), array2obj(data))) + mixin: (data, base) => pcMerge(base, data, 'paramAttr.example', 'childList', false) }; const endParse = (data, type) => { diff --git a/src/workbench/browser/src/app/shared/constants/extension.ts b/src/workbench/browser/src/app/shared/constants/extension.ts new file mode 100644 index 000000000..bcb0c5e11 --- /dev/null +++ b/src/workbench/browser/src/app/shared/constants/extension.ts @@ -0,0 +1 @@ +export const defaultExtensions = ['postcat-export-openapi', 'postcat-import-openapi']; diff --git a/src/workbench/browser/src/app/shared/services/extensions/extension.service.ts b/src/workbench/browser/src/app/shared/services/extensions/extension.service.ts index 41ab2b775..762ebd05e 100644 --- a/src/workbench/browser/src/app/shared/services/extensions/extension.service.ts +++ b/src/workbench/browser/src/app/shared/services/extensions/extension.service.ts @@ -2,9 +2,11 @@ import { HttpClient, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { ElectronService } from 'eo/workbench/browser/src/app/core/services'; import { LanguageService } from 'eo/workbench/browser/src/app/core/services/language/language.service'; +import { defaultExtensions } from 'eo/workbench/browser/src/app/shared/constants/extension'; import { DISABLE_EXTENSION_NAMES } from 'eo/workbench/browser/src/app/shared/constants/storageKeys'; import { FeatureInfo, ExtensionInfo, SidebarView } from 'eo/workbench/browser/src/app/shared/models/extension-manager'; import { MessageService } from 'eo/workbench/browser/src/app/shared/services/message'; +import { StoreService } from 'eo/workbench/browser/src/app/shared/store/state.service'; import StorageUtil from 'eo/workbench/browser/src/app/utils/storage/storage.utils'; import { APP_CONFIG } from 'eo/workbench/browser/src/environments/environment'; import { lastValueFrom, Subscription } from 'rxjs'; @@ -12,7 +14,6 @@ import { lastValueFrom, Subscription } from 'rxjs'; import { ExtensionCommonService } from './extension-store.service'; import { WebExtensionService } from './webExtension.service'; -const defaultExtensions = ['postcat-export-openapi', 'postcat-import-openapi']; @Injectable({ providedIn: 'root' }) @@ -31,7 +32,8 @@ export class ExtensionService { private extensionCommon: ExtensionCommonService, private language: LanguageService, private webExtensionService: WebExtensionService, - private messageService: MessageService + private messageService: MessageService, + public store: StoreService ) {} async init() { await this.requestList('init'); @@ -42,14 +44,16 @@ export class ExtensionService { }); return; } - //* Web Installl const installedName = []; //Get extensions - [...this.webExtensionService.installedList, ...defaultExtensions.map(name => ({ name }))].forEach(val => { - if (this.installedList.some(m => m.name === val.name)) return; - installedName.push(val.name); - }); + [...this.webExtensionService.installedList, ...(!this.store.getAppHasInitial ? defaultExtensions.map(name => ({ name })) : [])].forEach( + val => { + if (this.installedList.some(m => m.name === val.name)) return; + installedName.push(val.name); + } + ); + //* Install Extension by foreach because of async const uniqueNames = [...Array.from(new Set(installedName)), ...this.webExtensionService.debugExtensionNames]; for (let i = 0; i < uniqueNames.length; i++) { diff --git a/src/workbench/browser/src/app/utils/pc-merge.ts b/src/workbench/browser/src/app/utils/pc-merge.ts new file mode 100644 index 000000000..c6521d317 --- /dev/null +++ b/src/workbench/browser/src/app/utils/pc-merge.ts @@ -0,0 +1,62 @@ +import { cloneDeep } from 'lodash-es'; +export interface ParamAttr { + example: string; +} + +export interface ChildList { + name: string; + isRequired: number; + description: string; + paramAttr: ParamAttr; + dataType: number; +} + +export interface RootObject { + name: string; + isRequired: number; + paramAttr: ParamAttr; + dataType: number; + description: string; + childList: ChildList[]; +} + +/** + * @description Merge specified key + * @param {RootObject[]} baseArr baseArr + * @param {RootObject[]} newDataArr newDataArr + * @param {string} mergerKey specified key + * @param {string} childKey childList correspondence key + * @param {boolean} childKey isChild + * @return {RootObject[]} assginArr + */ + +export const pcMerge = ( + baseArr: RootObject[], + newDataArr: RootObject[], + mergerKey: string, + childKey: string, + isChild: boolean +): RootObject[] => { + let handleData = !isChild ? cloneDeep(baseArr) : baseArr; + for (let item of newDataArr) { + //If you can't find this name, just push it + if (handleData.findIndex(ele => ele.name === item.name) === -1) handleData.push(item); + else { + for (let ele of handleData) { + if (item.name === ele.name) { + //Assigns the specified key + ele[mergerKey] = item[mergerKey]; + if (item[childKey] && item[childKey].length !== 0) { + if (ele[childKey]) { + //Subtree recursion + pcMerge(ele[childKey], item[childKey], mergerKey, childKey, true); + } else { + ele[childKey] = item[childKey]; + } + } + } + } + } + } + return handleData; +};