mirror of
https://gitee.com/eolink_admin/postcat.git
synced 2024-11-30 10:47:41 +08:00
feat: update extension install & debug & uninstall
This commit is contained in:
parent
97c94a1946
commit
bc864d9dbb
@ -33,6 +33,7 @@
|
||||
"dependencies": {
|
||||
"@bqy/node-module-alias": "^1.0.1",
|
||||
"@electron/remote": "2.0.8",
|
||||
"axios": "0.27.2",
|
||||
"content-disposition": "^0.5.4",
|
||||
"copyfiles": "2.4.1",
|
||||
"crypto-js": "^4.1.1",
|
||||
|
@ -204,7 +204,7 @@ try {
|
||||
} else if (arg.action === 'getAppModuleList') {
|
||||
returnValue = moduleManager.getAppModuleList();
|
||||
} else if (arg.action === 'installModule') {
|
||||
const data = await moduleManager.install(arg.data);
|
||||
const data = await moduleManager.installExt(arg.data);
|
||||
if (data.code === 0) {
|
||||
//subView.mainView.view.webContents.send('moduleUpdate');
|
||||
}
|
||||
|
@ -59,15 +59,15 @@ export class ModuleHandler extends CoreHandler {
|
||||
* @param modules 模块名称数组
|
||||
* @param isLocal 本地安装用link
|
||||
*/
|
||||
async install(modules: string[], isLocal: boolean): Promise<ModuleHandlerResult> {
|
||||
async install(modules: any[], isLocal: boolean): Promise<ModuleHandlerResult> {
|
||||
return await this.execCommand(isLocal ? 'link' : 'install', modules);
|
||||
}
|
||||
/**
|
||||
* 更新模块
|
||||
* @param {...string[]} modules 模块名称数组
|
||||
* @param [{ name:string, version:string }]
|
||||
*/
|
||||
async update(...modules: string[]): Promise<ModuleHandlerResult> {
|
||||
return await this.execCommand('update', modules);
|
||||
async update(modules: any[]): Promise<ModuleHandlerResult> {
|
||||
return await this.execCommand('install', modules);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -75,7 +75,7 @@ export class ModuleHandler extends CoreHandler {
|
||||
* @param {string[]} modules 模块名称数组
|
||||
* @param isLocal 本地卸载用unlink
|
||||
*/
|
||||
async uninstall(modules: string[], isLocal: boolean): Promise<ModuleHandlerResult> {
|
||||
async uninstall(modules: any[], isLocal: boolean): Promise<ModuleHandlerResult> {
|
||||
return await this.execCommand(isLocal ? 'unlink' : 'uninstall', modules);
|
||||
}
|
||||
|
||||
@ -106,14 +106,16 @@ export class ModuleHandler extends CoreHandler {
|
||||
});
|
||||
}
|
||||
}
|
||||
private executeByAppNpm(command: string, modules: string[], resolve, reject) {
|
||||
private executeByAppNpm(command: string, modules: any[], resolve, reject) {
|
||||
// https://www.npmjs.com/package/bin-links
|
||||
npmCli.load({ 'bin-links': false, verbose: true, prefix: this.baseDir }, (loaderr) => {
|
||||
const moduleList = modules.map((it) => it + '@latest');
|
||||
console.log('moduleList', command, moduleList);
|
||||
const moduleList = modules.map(({ name, version }) => (version ? `${name}@${version}` : name));
|
||||
let executeCommand = ['update', 'install', 'uninstall'];
|
||||
if (!executeCommand.includes(command)) return;
|
||||
if (!executeCommand.includes(command)) {
|
||||
return;
|
||||
}
|
||||
npmCli.commands[command](moduleList, (err, data) => {
|
||||
console.log('command', command);
|
||||
process.chdir(this.baseDir);
|
||||
if (err) {
|
||||
return reject(err);
|
||||
@ -160,7 +162,7 @@ export class ModuleHandler extends CoreHandler {
|
||||
* @param command
|
||||
* @param modules
|
||||
*/
|
||||
private async execCommand(command: string, modules: string[]): Promise<ModuleHandlerResult> {
|
||||
private async execCommand(command: string, modules: any[]): Promise<ModuleHandlerResult> {
|
||||
return await new Promise((resolve: any, reject: any): void => {
|
||||
// this.executeBySystemNpm(command, modules, resolve)
|
||||
this.executeByAppNpm(command, modules, resolve, reject);
|
||||
|
@ -3,9 +3,17 @@ import { ModuleHandler } from './handler';
|
||||
import { ModuleHandlerResult, ModuleInfo, ModuleManagerInfo, ModuleManagerInterface, ModuleType } from '../types';
|
||||
import { isNotEmpty } from 'eo/shared/common/common';
|
||||
import { processEnv } from '../../constant';
|
||||
import http from 'axios';
|
||||
import { DATA_DIR } from '../../../../shared/electron-main/constant';
|
||||
import { promises, readFileSync } from 'fs';
|
||||
|
||||
// * npm pkg name
|
||||
const installExtension = [{ name: 'eoapi-export-openapi' }, { name: 'eoapi-import-openapi' }];
|
||||
const defaultExtension = [{ name: 'eoapi-export-openapi' }, { name: 'eoapi-import-openapi' }];
|
||||
const isExists = async (filePath) =>
|
||||
await promises
|
||||
.access(filePath)
|
||||
.then(() => true)
|
||||
.catch((_) => false);
|
||||
export class ModuleManager implements ModuleManagerInterface {
|
||||
/**
|
||||
* 模块管理器
|
||||
@ -15,7 +23,7 @@ export class ModuleManager implements ModuleManagerInterface {
|
||||
/**
|
||||
* extension list
|
||||
*/
|
||||
private readonly installExtension = installExtension;
|
||||
private installExtension = [];
|
||||
|
||||
/**
|
||||
* 模块集合
|
||||
@ -35,12 +43,22 @@ export class ModuleManager implements ModuleManagerInterface {
|
||||
this.updateAll();
|
||||
}
|
||||
|
||||
async getRemoteExtension() {
|
||||
const { data } = await http.get(process.env.EXTENSION_URL + '/list');
|
||||
return data.data.map(({ name, version }) => ({ name, version }));
|
||||
}
|
||||
|
||||
async installExt({ name }) {
|
||||
const remoteExtension = await this.getRemoteExtension();
|
||||
return await this.install(remoteExtension.find((it) => it.name === name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装模块,调用npm install | link
|
||||
* @param module
|
||||
*/
|
||||
async install(module: ModuleManagerInfo): Promise<ModuleHandlerResult> {
|
||||
const result = await this.moduleHandler.install([module.name], module.isLocal || false);
|
||||
const result = await this.moduleHandler.install([{ name: module.name }], module.isLocal || false);
|
||||
if (result.code === 0) {
|
||||
const moduleInfo: ModuleInfo = this.moduleHandler.info(module.name);
|
||||
this.set(moduleInfo);
|
||||
@ -48,46 +66,42 @@ export class ModuleManager implements ModuleManagerInterface {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新模块,调用npm update
|
||||
* @param module
|
||||
*/
|
||||
async update(module: ModuleManagerInfo): Promise<ModuleHandlerResult> {
|
||||
const result = await this.moduleHandler.update(module.name);
|
||||
if (result.code === 0) {
|
||||
this.refresh(module);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
updateAll() {
|
||||
// * ModuleManager will be new only one while app run start, so it should be here upgrade & install extension
|
||||
// * Upgrade
|
||||
const list = Array.from(this.getModules().values()).map((val) => val.name);
|
||||
list.forEach((item) => {
|
||||
//!Warn this is bug,it did't work @kungfuboy
|
||||
this.update({ name: item });
|
||||
});
|
||||
// * Install default extension
|
||||
this.installExtension.forEach((item) => {
|
||||
// * If the extension already in local extension list, then do not repeat installation
|
||||
if (list.includes(item.name)) return;
|
||||
//!TODO this will reinstall all package, npm link(debug) package will be remove after npm install command,
|
||||
this.install(item);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 删除模块,调用npm uninstall | unlink
|
||||
* @param module
|
||||
*/
|
||||
async uninstall(module: ModuleManagerInfo): Promise<ModuleHandlerResult> {
|
||||
const moduleInfo: ModuleInfo = this.moduleHandler.info(module.name);
|
||||
const result = await this.moduleHandler.uninstall([module.name], module.isLocal || false);
|
||||
const result = await this.moduleHandler.uninstall([{ name: module.name }], module.isLocal || false);
|
||||
if (result.code === 0) {
|
||||
this.delete(moduleInfo);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async updateAll() {
|
||||
// * ModuleManager will be new only one while app run start, so it should be here upgrade & install extension
|
||||
// * Upgrade
|
||||
const list = Array.from(this.getModules().values()).map((val) => ({ name: val.name, version: val.version }));
|
||||
// * get version in remote
|
||||
const remoteExtension = await this.getRemoteExtension();
|
||||
const isOK = await isExists(`${DATA_DIR}/debugger.json`);
|
||||
let debugExtension = [];
|
||||
if (isOK) {
|
||||
const debuggerExtension = readFileSync(`${DATA_DIR}/debugger.json`, 'utf-8');
|
||||
const { extensions } = JSON.parse(debuggerExtension);
|
||||
debugExtension = extensions;
|
||||
}
|
||||
const localExtensionName = [...new Set(list.map((it) => it.name).concat(defaultExtension.map((it) => it.name)))];
|
||||
this.installExtension = remoteExtension
|
||||
.filter((it) => localExtensionName.includes(it.name))
|
||||
.filter((it) => !debugExtension.includes(it.name));
|
||||
this.moduleHandler.update(this.installExtension);
|
||||
this.installExtension.forEach((it) => {
|
||||
this.install(it);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取本地package.json更新模块信息
|
||||
* @param module
|
||||
|
@ -13,8 +13,9 @@ export enum ModuleType {
|
||||
app = 'app',
|
||||
feature = 'feature',
|
||||
}
|
||||
export interface I18nLocale{
|
||||
locale: string; package: any
|
||||
export interface I18nLocale {
|
||||
locale: string;
|
||||
package: any;
|
||||
}
|
||||
/**
|
||||
* 模块信息接口
|
||||
@ -68,7 +69,7 @@ export interface ModuleInfo {
|
||||
features?: {
|
||||
[index: string]: any;
|
||||
};
|
||||
i18n?:I18nLocale[]
|
||||
i18n?: I18nLocale[];
|
||||
}
|
||||
/**
|
||||
* 贡献点
|
||||
@ -117,11 +118,11 @@ export interface ModuleManagerInfo {
|
||||
* getModules 获取所有模块列表,或返回有模块关联子模块的信息
|
||||
*/
|
||||
export interface ModuleManagerInterface {
|
||||
installExt: any;
|
||||
install: (module: ModuleManagerInfo) => Promise<ModuleHandlerResult>;
|
||||
update: (module: ModuleManagerInfo) => Promise<ModuleHandlerResult>;
|
||||
uninstall: (module: ModuleManagerInfo) => Promise<ModuleHandlerResult>;
|
||||
refresh: (module: ModuleManagerInfo) => void;
|
||||
refreshAll:()=>void;
|
||||
refreshAll: () => void;
|
||||
getModule: (moduleID: string, belongs?: boolean) => ModuleInfo;
|
||||
getModules: (belongs?: boolean) => Map<string, ModuleInfo>;
|
||||
getAppModuleList: () => Array<ModuleInfo>;
|
||||
|
10
yarn.lock
10
yarn.lock
@ -3092,6 +3092,14 @@ aws4@^1.8.0:
|
||||
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
|
||||
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==
|
||||
|
||||
axios@0.27.2:
|
||||
version "0.27.2"
|
||||
resolved "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
|
||||
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
|
||||
dependencies:
|
||||
follow-redirects "^1.14.9"
|
||||
form-data "^4.0.0"
|
||||
|
||||
axios@^0.25.0:
|
||||
version "0.25.0"
|
||||
resolved "https://registry.yarnpkg.com/axios/-/axios-0.25.0.tgz#349cfbb31331a9b4453190791760a8d35b093e0a"
|
||||
@ -6120,7 +6128,7 @@ flush-write-stream@^1.0.0:
|
||||
inherits "^2.0.3"
|
||||
readable-stream "^2.3.6"
|
||||
|
||||
follow-redirects@^1.0.0, follow-redirects@^1.14.7:
|
||||
follow-redirects@^1.0.0, follow-redirects@^1.14.7, follow-redirects@^1.14.9:
|
||||
version "1.15.1"
|
||||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5"
|
||||
integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==
|
||||
|
Loading…
Reference in New Issue
Block a user