Skip to content

Commit

Permalink
feat: update extension install & debug & uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfuboy committed Jul 19, 2022
1 parent 97c94a1 commit bc864d9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 48 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/app/electron-main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
22 changes: 12 additions & 10 deletions src/platform/node/extension-manager/lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,23 @@ 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);
}

/**
* 卸载模块
* @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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
76 changes: 45 additions & 31 deletions src/platform/node/extension-manager/lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
* 模块管理器
Expand All @@ -15,7 +23,7 @@ export class ModuleManager implements ModuleManagerInterface {
/**
* extension list
*/
private readonly installExtension = installExtension;
private installExtension = [];

/**
* 模块集合
Expand All @@ -35,59 +43,65 @@ 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);
}
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
Expand Down
11 changes: 6 additions & 5 deletions src/platform/node/extension-manager/types/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/**
* 模块信息接口
Expand Down Expand Up @@ -68,7 +69,7 @@ export interface ModuleInfo {
features?: {
[index: string]: any;
};
i18n?:I18nLocale[]
i18n?: I18nLocale[];
}
/**
* 贡献点
Expand Down Expand Up @@ -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>;
Expand Down
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

[email protected]:
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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit bc864d9

Please sign in to comment.