Conditional import of Electron/NodeJS libs - The app can be launch in browser mode

This commit is contained in:
Maxime GRIS
2017-05-06 00:44:59 +02:00
parent 9d43304b67
commit c434f8a8b0
9 changed files with 59 additions and 12 deletions

View File

@@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { ElectronService } from './electron.service';
describe('ElectronService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ElectronService]
});
});
it('should ...', inject([ElectronService], (service: ElectronService) => {
expect(service).toBeTruthy();
}));
});

View File

@@ -0,0 +1,23 @@
import { Injectable } from '@angular/core';
import { ipcRenderer } from 'electron';
import * as childProcess from 'child_process';
@Injectable()
export class ElectronService {
ipcRenderer: typeof ipcRenderer;
childProcess: typeof childProcess;
constructor() {
if (this.isElectron()) {
this.ipcRenderer = window.require('electron').ipcRenderer;
this.childProcess = window.require('child_process');
}
}
isElectron = () => {
return window && window.process && window.process.type;
}
}