Skip to content

Commit

Permalink
fix: npm install error in windows os
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Mar 3, 2023
1 parent 4001bef commit 8fa1b3d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
7 changes: 4 additions & 3 deletions src/app/electron-main/language.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class LanguageInstance {
public static get Instance(): LanguageInstance {
return this._instance || (this._instance = new this());
}
get(): string {
async get() {
await app.whenReady();
let lang = store.get('language') || (app.getLocale().includes('zh') ? 'zh-Hans' : 'en-US');
return lang as string;
}
getPath() {
const systemLanguage = this.get();
async getPath() {
const systemLanguage = await this.get();
return LANGUAGES.find(val => val.value === systemLanguage).path;
}
set(localeID) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/electron-main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ class EoBrowserWindow {
this.win = null;
});
}
public loadURL() {
public async loadURL() {
const file: string =
processEnv === 'development'
? 'http://localhost:4200'
: `file://${path.join(__dirname, `../../../src/workbench/browser/dist/${LanguageService.getPath()}/index.html`)}`;
: `file://${path.join(__dirname, `../../../src/workbench/browser/dist/${await LanguageService.getPath()}/index.html`)}`;
this.win.loadURL(file);
if (['development'].includes(processEnv)) {
this.win.webContents.openDevTools({
Expand Down
4 changes: 2 additions & 2 deletions src/platform/node/extension-manager/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ export class CoreHandler {
*
* @param {string} name 模块名称
*/
info(name: string): ExtensionInfo {
async info(name: string): Promise<ExtensionInfo> {
let extensionInfo: ExtensionInfo;
try {
const baseDir: string = this.getModuleDir(name);
extensionInfo = readJson(path.join(baseDir, 'package.json')) as ExtensionInfo;
extensionInfo.baseDir = baseDir;
// Get language locale
//!Warn:baseDir must be set before get locale file
const lang = LanguageService.get();
const lang = await LanguageService.get();
if (extensionInfo.features?.i18n) {
const locale = getLocaleData(extensionInfo, lang);
if (locale) {
Expand Down
4 changes: 3 additions & 1 deletion src/platform/node/extension-manager/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ export class ModuleHandler extends CoreHandler {
}
private setRegistry() {
return new Promise(resolve => {
const npm = spawn('npm', ['config', 'set', 'registry', this.registry], { cwd: this.baseDir });
const npm = spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['config', 'set', 'registry', this.registry], {
cwd: this.baseDir
});
npm.on('close', () => {
resolve(true);
});
Expand Down
36 changes: 19 additions & 17 deletions src/platform/node/extension-manager/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ModuleManager {
/**
* 模块管理器
*/
private readonly moduleHandler: ModuleHandler;
private moduleHandler: ModuleHandler;

/**
* extension list
Expand All @@ -37,25 +37,19 @@ export class ModuleManager {
/**
* 模块集合
*/
private readonly modules: Map<string, ExtensionInfo>;
private modules: Map<string, ExtensionInfo>;

/**
* 功能点集合
*/
private readonly features: Map<string, Map<string, FeatureInfo>>;
private features: Map<string, Map<string, FeatureInfo>>;

private lang;

constructor() {
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();
this.updateAll();
}

async getRemoteExtension(): Promise<ModuleManagerInfo[]> {
Expand Down Expand Up @@ -90,7 +84,7 @@ export class ModuleManager {
async install(module: ModuleManagerInfo): Promise<ModuleHandlerResult> {
const result = await this.moduleHandler.install([module], module?.isLocal || false);
if (result.code === 0) {
const moduleInfo: ExtensionInfo = this.moduleHandler.info(module.name);
const moduleInfo: ExtensionInfo = await this.moduleHandler.info(module.name);
this.set(moduleInfo);
}
return result;
Expand All @@ -102,7 +96,7 @@ export class ModuleManager {
* @param module
*/
async uninstall(module: ModuleManagerInfo): Promise<ModuleHandlerResult> {
const moduleInfo: ExtensionInfo = this.moduleHandler.info(module.name);
const moduleInfo: ExtensionInfo = await this.moduleHandler.info(module.name);
const result = await this.moduleHandler.uninstall([{ name: module.name }], module.isLocal || false);
if (result.code === 0) {
this.delete(moduleInfo);
Expand Down Expand Up @@ -143,8 +137,8 @@ export class ModuleManager {
*
* @param module
*/
refresh(module: ModuleManagerInfo): void {
const moduleInfo: ExtensionInfo = this.moduleHandler.info(module.name);
async refresh(module: ModuleManagerInfo) {
const moduleInfo: ExtensionInfo = await this.moduleHandler.info(module.name);
this.set(moduleInfo);
}
/**
Expand Down Expand Up @@ -288,12 +282,20 @@ export class ModuleManager {
/**
* 读取本地package.json文件得到本地安装的模块列表,依次获取模块信息加入模块列表
*/
private init() {
private async init() {
this.moduleHandler = new ModuleHandler({
baseDir: HOME_DIR,
registry: (await this.lang.get()) === 'zh-Hans' ? 'https://registry.npmmirror.com' : 'https://registry.npmjs.org'
});
this.modules = new Map();
this.features = new Map();

const names: string[] = this.moduleHandler.list();
names.forEach((name: string) => {
const moduleInfo: ExtensionInfo = this.moduleHandler.info(name);
names.forEach(async (name: string) => {
const moduleInfo: ExtensionInfo = await this.moduleHandler.info(name);
this.setup(moduleInfo);
});
this.updateAll();
}

getExtFeatures(extName: string): ExtensionInfo['features'] {
Expand Down

0 comments on commit 8fa1b3d

Please sign in to comment.