feat: change project structure to meet targeted architecture

This commit is contained in:
Amadou Ada DIENE
2020-04-18 04:57:24 +02:00
parent f506c0c2ab
commit 9c8788fb2e
6 changed files with 152 additions and 0 deletions

9
electron-webpack.json Normal file
View File

@@ -0,0 +1,9 @@
{
"main": {
"extraEntries": ["@/preload.js"],
"sourceDirectory": "dist/electron"
},
"renderer": {
"sourceDirectory": null
}
}

View File

@@ -0,0 +1,65 @@
import { app, BrowserWindow, shell } from 'electron';
import { Window } from './window';
export class App {
private static _wrapper: Window;
public static launch() {
app.on('window-all-closed', App.quit);
app.on('activate', App.start);
app.on('ready', App.start);
// Fix warning by applying electron new default value for this property
// Further details : https://github.com/electron/electron/issues/18397
app.allowRendererProcessReuse = true;
// Limit navigation and open external links in default browser
app.on('web-contents-created', App.openExternalLinksInDefaultBrowser);
}
public static get window(): BrowserWindow | any {
return this._wrapper ? this._wrapper.window : null;
}
private static start() {
// On MacOS it is common to re-create a window from app even after all windows have been closed
if (!App.window) {
App._wrapper = new Window();
}
}
private static quit() {
// On MacOS it is common for applications to stay open until the user explicitly quits
if (process.platform !== 'darwin') {
app.quit();
}
}
private static openExternalLinksInDefaultBrowser = (
event: Electron.Event,
contents: Electron.WebContents
) => {
// Disabling creation of new windows
contents.on(
'new-window',
(event: Electron.Event, navigationUrl: string) => {
// Blocking this event from loading in current app
event.preventDefault();
// Telling the user platform to open this event's url in the default browser
shell.openExternal(navigationUrl);
}
);
// Limiting navigation
contents.on(
'will-navigate',
(event: Electron.Event, navigationUrl: string) => {
const parsedUrl = new URL(navigationUrl);
// Allowing local navigation only
if (parsedUrl.origin !== 'http://localhost:4200') {
event.preventDefault();
}
}
);
};
}

View File

@@ -0,0 +1,67 @@
import { app, BrowserWindow } from 'electron';
import * as path from 'path';
import * as url from 'url';
export class Window {
private _window: BrowserWindow | any;
private _dev: boolean;
constructor() {
const env = process.env.NODE_ENV;
this._dev = env === 'development';
this.createWindow();
this.loadRender();
}
private createWindow(): void {
this._window = new BrowserWindow({
width: 800,
height: 600,
backgroundColor: '#FFFFFF',
webPreferences: {
// Isolate window context to protect against prototype pollution
contextIsolation: true,
// Disable the remote module to enhance security
enableRemoteModule: false,
// Use a preload script to enhance security
preload: path.join(app.getAppPath(), 'preload.js'),
},
});
}
private loadRender(): void {
if (this._dev) {
// 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({
pathname: path.join(__dirname, `index.html`),
protocol: 'file:',
slashes: true,
});
this.window.loadURL(indexPath);
}
// Delete current reference when the window is closed
this._window.on('closed', () => {
delete this._window;
});
}
private openDevTools(): void {
this._window.webContents.openDevTools();
this._window.webContents.on('devtools-opened', () => {
this._window.focus();
setImmediate(() => {
this._window.focus();
});
});
}
public get window(): BrowserWindow | any {
return this._window;
}
}

3
src/electron/main.ts Normal file
View File

@@ -0,0 +1,3 @@
import { App } from './components/app';
App.launch();

1
src/electron/preload.ts Normal file
View File

@@ -0,0 +1 @@
console.log('The preload script has been injected successfully.');

View File

@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/electron"
},
"include": ["./**/*.ts"]
}