Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to upgrade extension & install while first run app #68

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/platform/node/extension-manager/lib/handler.ts
Original file line number Diff line number Diff line change
@@ -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');
@@ -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}`);
}
@@ -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 });
});
}
});
22 changes: 22 additions & 0 deletions src/platform/node/extension-manager/lib/manager.ts
Original file line number Diff line number Diff line change
@@ -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;

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

/**