From 8822440f7dfa4452b588520cca5179121b276188 Mon Sep 17 00:00:00 2001 From: Amadou Ada DIENE Date: Wed, 29 Apr 2020 21:05:03 +0200 Subject: [PATCH] feat: env config setup --- package.json | 1 + src/electron/components/window.ts | 36 ++++++++++++++++--------------- src/electron/main.ts | 17 +++++++++++++++ src/static/config.json | 23 ++++++++++++++++++++ 4 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/static/config.json diff --git a/package.json b/package.json index 56dea15..99a0f25 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "electron": "^8.2.3", "electron-builder": "^22.5.1", "electron-webpack": "^2.8.2", + "fs-extra": "^9.0.0", "jasmine-core": "~3.5.0", "jasmine-spec-reporter": "~4.2.1", "karma": "~4.4.1", diff --git a/src/electron/components/window.ts b/src/electron/components/window.ts index 751298f..ad08b86 100644 --- a/src/electron/components/window.ts +++ b/src/electron/components/window.ts @@ -4,17 +4,13 @@ import * as url from 'url'; import { AbstractService } from '../services/abstract-service'; import { MultiplesService } from '../services/multiples-service'; +declare const global: any; declare const __static: string; export class Window { private _window: BrowserWindow | any; - private _dev: boolean; - private _e2e: boolean; constructor() { - this._dev = process.env.NODE_ENV === 'development'; - this._e2e = process.env.X_NODE_ENV === 'e2e-test'; - this.createWindow(); this.loadRenderer(); this.registerService(MultiplesService); @@ -25,18 +21,18 @@ export class Window { width: 800, height: 600, backgroundColor: '#FFFFFF', - icon: this._dev ? this.loadIcon() : null, + icon: this.loadIcon(), webPreferences: { // Default behavior in Electron since 5, that // limits the powers granted to remote content // except in e2e test when those powers are required by Spectron - nodeIntegration: this._e2e, + nodeIntegration: global.gConfig.isNodeIntegration, // Isolate window context to protect against prototype pollution // except in e2e test when that access is required by Spectron - contextIsolation: !this._e2e, + contextIsolation: global.gConfig.isContextIsolation, // Disable the remote module to enhance security // except in e2e test when that access is required by Spectron - enableRemoteModule: this._e2e, + enableRemoteModule: global.gConfig.isEnableRemoteModule, // Use a preload script to enhance security preload: path.join(app.getAppPath(), 'preload.js'), }, @@ -44,21 +40,23 @@ export class Window { } private loadIcon(): Electron.NativeImage { - const iconPath = path.join(__static, 'icons/icon.png'); - console.debug('Icon Path ', iconPath); - const iconObj = nativeImage.createFromPath(iconPath); - // Change dock icon on MacOS - if (iconObj && process.platform === 'darwin') { - app.dock.setIcon(iconObj); + let iconObj = null; + if (global.gConfig.isIconAvailable) { + const iconPath = path.join(__static, 'icons/icon.png'); + console.debug('Icon Path ', iconPath); + iconObj = nativeImage.createFromPath(iconPath); + // Change dock icon on MacOS + if (iconObj && process.platform === 'darwin') { + app.dock.setIcon(iconObj); + } } return iconObj; } private loadRenderer(): void { - if (this._dev) { + if (global.gConfig.config_id === 'development') { // Dev mode, take advantage of the live reload by loading local URL this.window.loadURL(`http://localhost:4200`); - this.openDevTools(); } else { // Else mode, we simply load angular bundle const indexPath = url.format({ @@ -69,6 +67,10 @@ export class Window { this.window.loadURL(indexPath); } + if (global.gConfig.isOpenDevTools) { + this.openDevTools(); + } + // Delete current reference when the window is closed` this._window.on('closed', () => { delete this._window; diff --git a/src/electron/main.ts b/src/electron/main.ts index ae90f32..150a77d 100644 --- a/src/electron/main.ts +++ b/src/electron/main.ts @@ -1,3 +1,20 @@ +import * as fs from 'fs-extra'; +import * as _ from 'lodash'; +import * as path from 'path'; import { App } from './components/app'; +declare const global: any; +declare const __static: string; + +// Load config +const currentEnv = process.env.X_NODE_ENV || process.env.NODE_ENV; +const appConfig = + currentEnv === 'development' + ? fs.readJsonSync(path.join(__static, 'config.json')) + : fs.readJsonSync(path.join(__dirname, 'static/config.json')); +const defaultConf = appConfig.development; +const currentConf = appConfig[currentEnv]; +global.gConfig = _.merge(defaultConf, currentConf); + +// Launch app App.launch(); diff --git a/src/static/config.json b/src/static/config.json new file mode 100644 index 0000000..3cd3fba --- /dev/null +++ b/src/static/config.json @@ -0,0 +1,23 @@ +{ + "development": { + "config_id": "development", + "isIconAvailable": true, + "isNodeIntegration": false, + "isContextIsolation": true, + "isEnableRemoteModule": false, + "isOpenDevTools": true + }, + "e2e-test": { + "config_id": "e2e-test", + "isIconAvailable": true, + "isNodeIntegration": true, + "isContextIsolation": false, + "isEnableRemoteModule": true, + "isOpenDevTools": false + }, + "production": { + "config_id": "production", + "isIconAvailable": false, + "isOpenDevTools": false + } +}