Skip to content

Commit

Permalink
feat: set registry automaic
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfuboy committed Feb 27, 2023
1 parent 831b4bc commit 8f64179
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
46 changes: 17 additions & 29 deletions src/platform/node/extension-manager/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ export class ModuleHandler extends CoreHandler {
* @param isLocal 本地安装用link
*/
async install(modules: any[], isLocal: boolean): Promise<ModuleHandlerResult> {
// * Check the registry before install
// const check = spawn('npm', ['config', 'get', 'registry']);
// check.stdout
// .on('data', (data: string) => {
// console.log('===========>>>>>>>>', data.toString());
// })
// .pipe(process.stdout);
return await this.execCommand(isLocal ? 'link' : 'install', modules);
}
/**
Expand Down Expand Up @@ -137,34 +144,12 @@ export class ModuleHandler extends CoreHandler {
});
});
}
private executeBySystemNpm(command: string, modules: string[], resolve) {
let args = [command].concat(modules).concat('--color=always', '--save');
if (!['link', 'unlink', 'uninstall', 'update'].includes(command)) {
if (this.registry) {
args = args.concat(`--registry=${this.registry}`);
}
if (this.proxy) {
args = args.concat(`--proxy=${this.proxy}`);
}
}
const npm = spawn('npm', args, { cwd: this.baseDir });
let output = '';
npm.stdout
.on('data', (data: string) => {
output += data;
})
.pipe(process.stdout);
npm.stderr
.on('data', (data: string) => {
output += data;
})
.pipe(process.stderr);
npm.on('close', (code: number) => {
if (!code) {
resolve({ code: 0, data: output });
} else {
resolve({ code: code, data: output });
}
private setRegistry() {
return new Promise(resolve => {
const npm = spawn('npm', ['config', 'set', 'registry', this.registry], { cwd: this.baseDir });
npm.on('close', () => {
resolve(true);
});
});
}
/**
Expand All @@ -174,8 +159,11 @@ export class ModuleHandler extends CoreHandler {
* @param modules
*/
private async execCommand(command: string, modules: any[]): Promise<ModuleHandlerResult> {
return await new Promise((resolve: any, reject: any): void => {
return await new Promise(async (resolve: any, reject: any): Promise<void> => {
// this.executeBySystemNpm(command, modules, resolve)
// * Set Proxy
// * Set registry
await this.setRegistry();
this.executeByAppNpm(command, modules, resolve, reject);
});
}
Expand Down
9 changes: 8 additions & 1 deletion src/platform/node/extension-manager/manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LanguageService } from 'eo/app/electron-main/language.service';
import { isNotEmpty } from 'eo/shared/common/common';
import { HOME_DIR } from 'eo/shared/electron-main/constant';
import { ExtensionInfo, SidebarView, FeatureInfo } from 'eo/workbench/browser/src/app/shared/models/extension-manager';
Expand Down Expand Up @@ -43,8 +44,14 @@ export class ModuleManager {
*/
private readonly features: Map<string, Map<string, FeatureInfo>>;

private lang;

constructor() {
this.moduleHandler = new ModuleHandler({ baseDir: HOME_DIR });
this.lang = LanguageService;
this.moduleHandler = new ModuleHandler({
baseDir: HOME_DIR,
registry: this.lang.get === 'zh-Hans' ? 'https://registry.npmmirror.com' : 'https://registry.npmjs.org'
});
this.modules = new Map();
this.features = new Map();
this.init();
Expand Down

0 comments on commit 8f64179

Please sign in to comment.