Skip to content

Commit

Permalink
feat: support to upgrade extension & install while first run app (#68)
Browse files Browse the repository at this point in the history
* feat: support to upgrade extension & install while first run app

* feat: fix return reject & resolve
  • Loading branch information
kungfuboy authored Jun 14, 2022
1 parent 488755c commit bbd8803
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/platform/node/extension-manager/lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class ModuleHandler extends CoreHandler {
* @param result npm install安装成功回调的结果
* @param moduleList 所有的模块列表
*/
private operatePackage(result: any[], moduleList: string[], action: 'uninstall' | 'install') {
private operatePackage(result: any[], moduleList: string[], action: 'uninstall' | 'install' | 'update') {
if (Array.isArray(result)) {
const moduleNames = moduleList.map((n) => n.split('@')[0]);
const packagePath = path.join(this.baseDir, 'package.json');
Expand Down Expand Up @@ -114,7 +114,7 @@ export class ModuleHandler extends CoreHandler {
private async execCommand(command: string, modules: string[]): Promise<ModuleHandlerResult> {
return await new Promise((resolve: any, reject: any): void => {
let args = [command].concat(modules).concat('--color=always', '--save');
if (!['link', 'unlink', 'uninstall'].includes(command)) {
if (!['link', 'unlink', 'uninstall', 'update'].includes(command)) {
if (this.registry) {
args = args.concat(`--registry=${this.registry}`);
}
Expand All @@ -126,24 +126,34 @@ export class ModuleHandler extends CoreHandler {
// console.log('command', [command].concat(modules), this.baseDir);
npmCli.load({ 'bin-links': false, verbose: true, prefix: this.baseDir }, (loaderr) => {
const moduleList = modules.map((it) => it + '@latest');
if (command === 'update') {
npmCli.commands.update(moduleList, (err, data) => {
process.chdir(this.baseDir);
if (err) {
return reject(err);
}
this.operatePackage(data, moduleList, 'update');
return resolve({ code: 0, data });
});
}
if (command === 'install') {
npmCli.commands.install(moduleList, (err, data) => {
process.chdir(this.baseDir);
if (err) {
reject(err);
return reject(err);
}
this.operatePackage(data, moduleList, 'install');
resolve({ code: 0, data });
return resolve({ code: 0, data });
});
}
if (command === 'uninstall') {
npmCli.commands.uninstall(moduleList, (err, data) => {
process.chdir(this.baseDir);
if (err) {
reject(err);
return reject(err);
}
this.operatePackage(data, moduleList, 'uninstall');
resolve({ code: 0, data });
return resolve({ code: 0, data });
});
}
});
Expand Down
22 changes: 22 additions & 0 deletions src/platform/node/extension-manager/lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import { ModuleHandlerResult, ModuleInfo, ModuleManagerInfo, ModuleManagerInterf
import { isNotEmpty } from 'eo/shared/common/common';
import { processEnv } from '../../constant';

// * npm pkg name
const installExtension = [{ name: 'eoapi-export-openapi' }, { name: 'eoapi-import-openapi' }];

export class ModuleManager implements ModuleManagerInterface {
/**
* 模块管理器
*/
private readonly moduleHandler: ModuleHandler;

/**
* extension list
*/
private readonly installExtension = installExtension;

/**
* 模块集合
*/
Expand All @@ -25,6 +33,20 @@ export class ModuleManager implements ModuleManagerInterface {
this.modules = new Map();
this.features = new Map();
this.init();
// * 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().keys());
list.forEach((item) => {
this.update({ name: item });
});
// * install
// console.log('install');
this.installExtension.forEach((item) => {
// * If the extension already in local extension list, then do not repeat installation
if (!list.includes(item.name)) {
this.install(item);
}
});
}

/**
Expand Down

0 comments on commit bbd8803

Please sign in to comment.