From c434f8a8b0c0d1d8e5843af57465a24e900d967c Mon Sep 17 00:00:00 2001 From: Maxime GRIS Date: Sat, 6 May 2017 00:44:59 +0200 Subject: [PATCH] Conditional import of Electron/NodeJS libs - The app can be launch in browser mode --- src/app/app-routing.module.ts | 2 +- src/app/app.component.ts | 19 +++++++++------ src/app/app.module.ts | 8 +++++-- .../{ => components}/home/home.component.html | 0 .../{ => components}/home/home.component.scss | 2 +- .../home/home.component.spec.ts | 2 +- .../{ => components}/home/home.component.ts | 0 src/app/providers/electron.service.spec.ts | 15 ++++++++++++ src/app/providers/electron.service.ts | 23 +++++++++++++++++++ 9 files changed, 59 insertions(+), 12 deletions(-) rename src/app/{ => components}/home/home.component.html (100%) rename src/app/{ => components}/home/home.component.scss (71%) rename src/app/{ => components}/home/home.component.spec.ts (95%) rename src/app/{ => components}/home/home.component.ts (100%) create mode 100644 src/app/providers/electron.service.spec.ts create mode 100644 src/app/providers/electron.service.ts diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index c51f0952d..cc41b5b11 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -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'; diff --git a/src/app/app.component.ts b/src/app/app.component.ts index fd7c2ece4..2769906b5 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,6 +1,5 @@ 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', @@ -8,10 +7,16 @@ import * as childProcess from 'child_process'; 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'); + } } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index ce15844b6..7f8736467 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -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, @@ -19,7 +23,7 @@ import { AppRoutingModule } from './app-routing.module'; HttpModule, AppRoutingModule ], - providers: [], + providers: [ElectronService], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/home/home.component.html b/src/app/components/home/home.component.html similarity index 100% rename from src/app/home/home.component.html rename to src/app/components/home/home.component.html diff --git a/src/app/home/home.component.scss b/src/app/components/home/home.component.scss similarity index 71% rename from src/app/home/home.component.scss rename to src/app/components/home/home.component.scss index 48064e634..b67492c0c 100644 --- a/src/app/home/home.component.scss +++ b/src/app/components/home/home.component.scss @@ -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 */ } diff --git a/src/app/home/home.component.spec.ts b/src/app/components/home/home.component.spec.ts similarity index 95% rename from src/app/home/home.component.spec.ts rename to src/app/components/home/home.component.spec.ts index c8363138d..24d7a1455 100644 --- a/src/app/home/home.component.spec.ts +++ b/src/app/components/home/home.component.spec.ts @@ -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(() => { diff --git a/src/app/home/home.component.ts b/src/app/components/home/home.component.ts similarity index 100% rename from src/app/home/home.component.ts rename to src/app/components/home/home.component.ts diff --git a/src/app/providers/electron.service.spec.ts b/src/app/providers/electron.service.spec.ts new file mode 100644 index 000000000..f692d9a9f --- /dev/null +++ b/src/app/providers/electron.service.spec.ts @@ -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(); + })); +}); diff --git a/src/app/providers/electron.service.ts b/src/app/providers/electron.service.ts new file mode 100644 index 000000000..528c1ec77 --- /dev/null +++ b/src/app/providers/electron.service.ts @@ -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; + } + +}