feat: update extension install & debug & uninstall

This commit is contained in:
夜鹰 2022-07-19 15:13:40 +08:00
parent 97c94a1946
commit bc864d9dbb
6 changed files with 74 additions and 48 deletions

View File

@ -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",

View File

@ -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');
}

View File

@ -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);

View File

@ -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

View File

@ -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>;

View File

@ -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==