feat: set registry automaic

This commit is contained in:
夜鹰 2023-02-27 17:02:04 +08:00
parent 831b4bce2b
commit 8f641799d6
2 changed files with 25 additions and 30 deletions

View File

@ -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);
}
/**
@ -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<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);
});
}

View File

@ -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<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();