feat: env config setup
This commit is contained in:
@@ -53,6 +53,7 @@
|
|||||||
"electron": "^8.2.3",
|
"electron": "^8.2.3",
|
||||||
"electron-builder": "^22.5.1",
|
"electron-builder": "^22.5.1",
|
||||||
"electron-webpack": "^2.8.2",
|
"electron-webpack": "^2.8.2",
|
||||||
|
"fs-extra": "^9.0.0",
|
||||||
"jasmine-core": "~3.5.0",
|
"jasmine-core": "~3.5.0",
|
||||||
"jasmine-spec-reporter": "~4.2.1",
|
"jasmine-spec-reporter": "~4.2.1",
|
||||||
"karma": "~4.4.1",
|
"karma": "~4.4.1",
|
||||||
|
|||||||
@@ -4,17 +4,13 @@ import * as url from 'url';
|
|||||||
import { AbstractService } from '../services/abstract-service';
|
import { AbstractService } from '../services/abstract-service';
|
||||||
import { MultiplesService } from '../services/multiples-service';
|
import { MultiplesService } from '../services/multiples-service';
|
||||||
|
|
||||||
|
declare const global: any;
|
||||||
declare const __static: string;
|
declare const __static: string;
|
||||||
|
|
||||||
export class Window {
|
export class Window {
|
||||||
private _window: BrowserWindow | any;
|
private _window: BrowserWindow | any;
|
||||||
private _dev: boolean;
|
|
||||||
private _e2e: boolean;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this._dev = process.env.NODE_ENV === 'development';
|
|
||||||
this._e2e = process.env.X_NODE_ENV === 'e2e-test';
|
|
||||||
|
|
||||||
this.createWindow();
|
this.createWindow();
|
||||||
this.loadRenderer();
|
this.loadRenderer();
|
||||||
this.registerService(MultiplesService);
|
this.registerService(MultiplesService);
|
||||||
@@ -25,18 +21,18 @@ export class Window {
|
|||||||
width: 800,
|
width: 800,
|
||||||
height: 600,
|
height: 600,
|
||||||
backgroundColor: '#FFFFFF',
|
backgroundColor: '#FFFFFF',
|
||||||
icon: this._dev ? this.loadIcon() : null,
|
icon: this.loadIcon(),
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
// Default behavior in Electron since 5, that
|
// Default behavior in Electron since 5, that
|
||||||
// limits the powers granted to remote content
|
// limits the powers granted to remote content
|
||||||
// except in e2e test when those powers are required by Spectron
|
// 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
|
// Isolate window context to protect against prototype pollution
|
||||||
// except in e2e test when that access is required by Spectron
|
// 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
|
// Disable the remote module to enhance security
|
||||||
// except in e2e test when that access is required by Spectron
|
// 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
|
// Use a preload script to enhance security
|
||||||
preload: path.join(app.getAppPath(), 'preload.js'),
|
preload: path.join(app.getAppPath(), 'preload.js'),
|
||||||
},
|
},
|
||||||
@@ -44,21 +40,23 @@ export class Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private loadIcon(): Electron.NativeImage {
|
private loadIcon(): Electron.NativeImage {
|
||||||
const iconPath = path.join(__static, 'icons/icon.png');
|
let iconObj = null;
|
||||||
console.debug('Icon Path ', iconPath);
|
if (global.gConfig.isIconAvailable) {
|
||||||
const iconObj = nativeImage.createFromPath(iconPath);
|
const iconPath = path.join(__static, 'icons/icon.png');
|
||||||
// Change dock icon on MacOS
|
console.debug('Icon Path ', iconPath);
|
||||||
if (iconObj && process.platform === 'darwin') {
|
iconObj = nativeImage.createFromPath(iconPath);
|
||||||
app.dock.setIcon(iconObj);
|
// Change dock icon on MacOS
|
||||||
|
if (iconObj && process.platform === 'darwin') {
|
||||||
|
app.dock.setIcon(iconObj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return iconObj;
|
return iconObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadRenderer(): void {
|
private loadRenderer(): void {
|
||||||
if (this._dev) {
|
if (global.gConfig.config_id === 'development') {
|
||||||
// Dev mode, take advantage of the live reload by loading local URL
|
// Dev mode, take advantage of the live reload by loading local URL
|
||||||
this.window.loadURL(`http://localhost:4200`);
|
this.window.loadURL(`http://localhost:4200`);
|
||||||
this.openDevTools();
|
|
||||||
} else {
|
} else {
|
||||||
// Else mode, we simply load angular bundle
|
// Else mode, we simply load angular bundle
|
||||||
const indexPath = url.format({
|
const indexPath = url.format({
|
||||||
@@ -69,6 +67,10 @@ export class Window {
|
|||||||
this.window.loadURL(indexPath);
|
this.window.loadURL(indexPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (global.gConfig.isOpenDevTools) {
|
||||||
|
this.openDevTools();
|
||||||
|
}
|
||||||
|
|
||||||
// Delete current reference when the window is closed`
|
// Delete current reference when the window is closed`
|
||||||
this._window.on('closed', () => {
|
this._window.on('closed', () => {
|
||||||
delete this._window;
|
delete this._window;
|
||||||
|
|||||||
@@ -1,3 +1,20 @@
|
|||||||
|
import * as fs from 'fs-extra';
|
||||||
|
import * as _ from 'lodash';
|
||||||
|
import * as path from 'path';
|
||||||
import { App } from './components/app';
|
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();
|
App.launch();
|
||||||
|
|||||||
23
src/static/config.json
Normal file
23
src/static/config.json
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user