From 8f641799d63f77193cea31c2920d590c09f4aaa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9C=E9=B9=B0?= <17kungfuboy@gmail.com> Date: Mon, 27 Feb 2023 17:02:04 +0800 Subject: [PATCH] feat: set registry automaic --- .../node/extension-manager/handler.ts | 46 +++++++------------ .../node/extension-manager/manager.ts | 9 +++- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/platform/node/extension-manager/handler.ts b/src/platform/node/extension-manager/handler.ts index 8a708fcc..47e230f3 100644 --- a/src/platform/node/extension-manager/handler.ts +++ b/src/platform/node/extension-manager/handler.ts @@ -69,6 +69,13 @@ export class ModuleHandler extends CoreHandler { * @param isLocal 本地安装用link */ async install(modules: any[], isLocal: boolean): Promise { + // * 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); } /** @@ -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); + }); }); } /** @@ -174,8 +159,11 @@ export class ModuleHandler extends CoreHandler { * @param modules */ private async execCommand(command: string, modules: any[]): Promise { - return await new Promise((resolve: any, reject: any): void => { + return await new Promise(async (resolve: any, reject: any): Promise => { // this.executeBySystemNpm(command, modules, resolve) + // * Set Proxy + // * Set registry + await this.setRegistry(); this.executeByAppNpm(command, modules, resolve, reject); }); } diff --git a/src/platform/node/extension-manager/manager.ts b/src/platform/node/extension-manager/manager.ts index bfe24861..ac15ba4b 100644 --- a/src/platform/node/extension-manager/manager.ts +++ b/src/platform/node/extension-manager/manager.ts @@ -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'; @@ -43,8 +44,14 @@ export class ModuleManager { */ private readonly features: Map>; + 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();