Skip to content

Commit

Permalink
Conditional import of Electron/NodeJS libs - The app can be launch in…
Browse files Browse the repository at this point in the history
… browser mode
  • Loading branch information
maximegris committed May 5, 2017
1 parent 9d43304 commit c434f8a
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HomeComponent } from './home/home.component';
import { HomeComponent } from './components/home/home.component';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

Expand Down
19 changes: 12 additions & 7 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { Component } from '@angular/core';
import { ipcRenderer } from 'electron';
import * as childProcess from 'child_process';
import { ElectronService } from './providers/electron.service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor() {
// Check if electron is correctly injected (see externals in webpack.config.js)
console.log('c', ipcRenderer);
// Check if nodeJs childProcess is correctly injected (see externals in webpack.config.js)
console.log('c', childProcess);
constructor(public electronService: ElectronService) {

if(electronService.isElectron()) {
console.log('Mode electron');
// Check if electron is correctly injected (see externals in webpack.config.js)
console.log('c', electronService.ipcRenderer);
// Check if nodeJs childProcess is correctly injected (see externals in webpack.config.js)
console.log('c', electronService.childProcess);
} else {
console.log('Mode web');
}
}
}
8 changes: 6 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import 'zone.js';
import 'reflect-metadata';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { HomeComponent } from './components/home/home.component';

import { AppRoutingModule } from './app-routing.module';

import { ElectronService } from './providers/electron.service';

@NgModule({
declarations: [
AppComponent,
Expand All @@ -19,7 +23,7 @@ import { AppRoutingModule } from './app-routing.module';
HttpModule,
AppRoutingModule
],
providers: [],
providers: [ElectronService],
bootstrap: [AppComponent]
})
export class AppModule { }
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
color: black;
margin:0;
padding:50px 20px;
background: url(../../assets/background.jpg) no-repeat center fixed;
background: url(../../../assets/background.jpg) no-repeat center fixed;
-webkit-background-size: cover; /* pour anciens Chrome et Safari */
background-size: cover; /* version standardisée */
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('HomeComponent', () => {
it(`should have as title 'App works !'`, async(() => {
const fixture = TestBed.createComponent(HomeComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('aApp works !');
expect(app.title).toEqual('App works !');
}));

it('should render title in a h1 tag', async(() => {
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions src/app/providers/electron.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
}));
});
23 changes: 23 additions & 0 deletions src/app/providers/electron.service.ts
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit c434f8a

Please sign in to comment.