feat: env config setup

This commit is contained in:
Amadou Ada DIENE
2020-04-29 21:05:03 +02:00
parent f17719c7e8
commit 8822440f7d
4 changed files with 60 additions and 17 deletions

View File

@@ -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",

View File

@@ -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;

View File

@@ -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();

23
src/static/config.json Normal file
View File

@@ -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
}
}