feat: git hook config (husky eslint prettier)

This commit is contained in:
Amadou Ada DIENE
2021-07-20 10:51:46 +02:00
parent 1a5a76f912
commit 77b15bdfc4
33 changed files with 52217 additions and 51866 deletions

View File

@@ -1,29 +1,29 @@
{
"development": {
"configId": "development",
"mainLogFile": "dist/dev-main.log",
"mainLogLevel": "debug",
"isIconAvailable": true,
"isNodeIntegration": false,
"isContextIsolation": true,
"isEnableRemoteModule": false,
"isOpenDevTools": true
},
"e2e-test": {
"configId": "e2e-test",
"mainLogFile": "dist/e2e-main.log",
"mainLogLevel": "error",
"isIconAvailable": true,
"isNodeIntegration": true,
"isContextIsolation": false,
"isEnableRemoteModule": true,
"isOpenDevTools": false
},
"production": {
"configId": "production",
"mainLogFile": "main.log",
"mainLogLevel": "error",
"isIconAvailable": false,
"isOpenDevTools": true
}
"development": {
"configId": "development",
"mainLogFile": "dist/dev-main.log",
"mainLogLevel": "debug",
"isIconAvailable": true,
"isNodeIntegration": false,
"isContextIsolation": true,
"isEnableRemoteModule": false,
"isOpenDevTools": true
},
"e2e-test": {
"configId": "e2e-test",
"mainLogFile": "dist/e2e-main.log",
"mainLogLevel": "error",
"isIconAvailable": true,
"isNodeIntegration": true,
"isContextIsolation": false,
"isEnableRemoteModule": true,
"isOpenDevTools": false
},
"production": {
"configId": "production",
"mainLogFile": "main.log",
"mainLogLevel": "error",
"isIconAvailable": false,
"isOpenDevTools": true
}
}

View File

@@ -1,63 +1,63 @@
import { app, BrowserWindow, shell } from "electron";
import { Window } from "./window";
import { app, BrowserWindow, shell } from 'electron';
import { Window } from './window';
export class App {
private static _wrapper: Window;
private static _wrapper: Window;
public static launch(): void {
app.on("window-all-closed", App.quit);
app.on("activate", App.start);
app.on("ready", App.start);
public static launch(): void {
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;
// 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);
}
// Limit navigation and open external links in default browser
app.on('web-contents-created', App.openExternalLinksInDefaultBrowser);
}
public static get electronWindow(): BrowserWindow | undefined {
return this._wrapper ? this._wrapper.electronWindow : undefined;
}
public static get electronWindow(): BrowserWindow | undefined {
return this._wrapper ? this._wrapper.electronWindow : undefined;
}
private static start() {
// On MacOS it is common to re-create a window from app even after all windows have been closed
if (!App.electronWindow) {
App._wrapper = new Window();
}
}
private static start() {
// On MacOS it is common to re-create a window from app even after all windows have been closed
if (!App.electronWindow) {
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 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.setWindowOpenHandler((handler: Electron.HandlerDetails) => {
// Telling the user platform to open this event's url in the default browser
shell.openExternal(handler.url);
private static openExternalLinksInDefaultBrowser = (
event: Electron.Event,
contents: Electron.WebContents
) => {
// Disabling creation of new windows
contents.setWindowOpenHandler((handler: Electron.HandlerDetails) => {
// Telling the user platform to open this event's url in the default browser
shell.openExternal(handler.url);
// Blocking this event from loading in current app
return { action: "deny" };
});
// Blocking this event from loading in current app
return { action: 'deny' };
});
// 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();
}
}
);
};
// 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

@@ -1,120 +1,120 @@
import { app, BrowserWindow, ipcMain, nativeImage } from "electron";
import * as path from "path";
import { AbstractService } from "../services/abstract-service";
import { MultiplesService } from "../services/multiples-service";
import { Logger } from "../utils/logger";
import { app, BrowserWindow, ipcMain, nativeImage } from 'electron';
import * as path from 'path';
import { AbstractService } from '../services/abstract-service';
import { MultiplesService } from '../services/multiples-service';
import { Logger } from '../utils/logger';
declare const global: Global;
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
export class Window {
private _electronWindow: BrowserWindow | undefined;
private _electronWindow: BrowserWindow | undefined;
constructor() {
this.createWindow();
this.loadRenderer();
this.registerService<number, number[]>(new MultiplesService());
}
constructor() {
this.createWindow();
this.loadRenderer();
this.registerService<number, number[]>(new MultiplesService());
}
private createWindow(): void {
this._electronWindow = new BrowserWindow({
width: 1280,
height: 720,
backgroundColor: "#FFFFFF",
// FIXME
// 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: global.appConfig.isNodeIntegration,
// Isolate window context to protect against prototype pollution
// except in e2e test when that access is required by Spectron
contextIsolation: global.appConfig.isContextIsolation,
// Ensure that JS values can't unsafely cross between worlds
// when using contextIsolation
worldSafeExecuteJavaScript: global.appConfig.isContextIsolation,
// Disable the remote module to enhance security
// except in e2e test when that access is required by Spectron
enableRemoteModule: global.appConfig.isEnableRemoteModule,
// Use a preload script to enhance security
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
},
});
}
private createWindow(): void {
this._electronWindow = new BrowserWindow({
width: 1280,
height: 720,
backgroundColor: '#FFFFFF',
// FIXME
// 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: global.appConfig.isNodeIntegration,
// Isolate window context to protect against prototype pollution
// except in e2e test when that access is required by Spectron
contextIsolation: global.appConfig.isContextIsolation,
// Ensure that JS values can't unsafely cross between worlds
// when using contextIsolation
worldSafeExecuteJavaScript: global.appConfig.isContextIsolation,
// Disable the remote module to enhance security
// except in e2e test when that access is required by Spectron
enableRemoteModule: global.appConfig.isEnableRemoteModule,
// Use a preload script to enhance security
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
},
});
}
private loadIcon(): Electron.NativeImage | undefined {
let iconObj = undefined;
if (global.appConfig.isIconAvailable) {
const iconPath = path.join(__dirname, "icons/icon.png");
Logger.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 loadIcon(): Electron.NativeImage | undefined {
let iconObj = undefined;
if (global.appConfig.isIconAvailable) {
const iconPath = path.join(__dirname, 'icons/icon.png');
Logger.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 (global.appConfig.configId === "development") {
// Dev mode, take advantage of the live reload by loading local URL
this.electronWindow.loadURL(`http://localhost:4200`);
} else {
// Else mode, we simply load angular bundle
const indexPath = path.join(
__dirname,
"../renderer/angular_window/index.html"
);
this.electronWindow.loadURL(`file://${indexPath}`);
}
private loadRenderer(): void {
if (global.appConfig.configId === 'development') {
// Dev mode, take advantage of the live reload by loading local URL
this.electronWindow.loadURL(`http://localhost:4200`);
} else {
// Else mode, we simply load angular bundle
const indexPath = path.join(
__dirname,
'../renderer/angular_window/index.html'
);
this.electronWindow.loadURL(`file://${indexPath}`);
}
if (global.appConfig.isOpenDevTools) {
this.openDevTools();
}
if (global.appConfig.isOpenDevTools) {
this.openDevTools();
}
// When the window is closed`
this._electronWindow.on("closed", () => {
// Remove IPC Main listeners
ipcMain.removeAllListeners();
// Delete current reference
delete this._electronWindow;
});
}
// When the window is closed`
this._electronWindow.on('closed', () => {
// Remove IPC Main listeners
ipcMain.removeAllListeners();
// Delete current reference
delete this._electronWindow;
});
}
private openDevTools(): void {
this._electronWindow.webContents.openDevTools();
this._electronWindow.webContents.on("devtools-opened", () => {
this._electronWindow.focus();
setImmediate(() => {
this._electronWindow.focus();
});
});
}
private openDevTools(): void {
this._electronWindow.webContents.openDevTools();
this._electronWindow.webContents.on('devtools-opened', () => {
this._electronWindow.focus();
setImmediate(() => {
this._electronWindow.focus();
});
});
}
private registerService<In, Out>(service: AbstractService<In, Out>) {
ipcMain.on(
service.receptionChannel(),
async (event: Electron.IpcMainEvent, ...args: any[]) => {
// Handling input
const input = args[0];
Logger.debug(`[${service.receptionChannel()}] =====> `, input);
const output: Out = await service.process(input);
private registerService<In, Out>(service: AbstractService<In, Out>) {
ipcMain.on(
service.receptionChannel(),
async (event: Electron.IpcMainEvent, ...args: any[]) => {
// Handling input
const input = args[0];
Logger.debug(`[${service.receptionChannel()}] =====> `, input);
const output: Out = await service.process(input);
// Handling output
if (service.sendingChannel()) {
Logger.debug(`[${service.sendingChannel()}] =====> `, output);
this._electronWindow.webContents.send(
service.sendingChannel(),
output
);
}
}
);
}
// Handling output
if (service.sendingChannel()) {
Logger.debug(`[${service.sendingChannel()}] =====> `, output);
this._electronWindow.webContents.send(
service.sendingChannel(),
output
);
}
}
);
}
public get electronWindow(): BrowserWindow | undefined {
return this._electronWindow;
}
public get electronWindow(): BrowserWindow | undefined {
return this._electronWindow;
}
}

View File

@@ -1,27 +1,27 @@
import * as fs from "fs-extra";
import * as _ from "lodash";
import * as path from "path";
import { AppConfig } from "shared-lib";
import { App } from "./components/app";
import * as fs from 'fs-extra';
import * as _ from 'lodash';
import * as path from 'path';
import { AppConfig } from 'shared-lib';
import { App } from './components/app';
declare const global: Global;
declare global {
// Global augmentation of the `Global` interface
interface Global {
appConfig: AppConfig;
}
// Global augmentation of the `Global` interface
interface Global {
appConfig: AppConfig;
}
}
// Load config
const currentEnv = process.env.X_NODE_ENV || process.env.NODE_ENV;
const appConfigs = fs.readJsonSync(path.join(__dirname, "config.json"));
const appConfigs = fs.readJsonSync(path.join(__dirname, 'config.json'));
const defaultConf = appConfigs.development;
const currentConf = appConfigs[currentEnv];
global.appConfig =
currentEnv === "development"
? defaultConf
: _.merge(defaultConf, currentConf);
currentEnv === 'development'
? defaultConf
: _.merge(defaultConf, currentConf);
// Launch app
App.launch();

View File

@@ -1,13 +1,13 @@
export class AbstractService<In, Out> {
receptionChannel(): string {
throw new Error("Method not implemented yet.");
}
receptionChannel(): string {
throw new Error('Method not implemented yet.');
}
sendingChannel(): string {
throw new Error("Method not implemented yet.");
}
sendingChannel(): string {
throw new Error('Method not implemented yet.');
}
process(_input: In): Out {
throw new Error("Method not implemented yet.");
}
process(_input: In): Out {
throw new Error('Method not implemented yet.');
}
}

View File

@@ -1,21 +1,21 @@
import { WindowApiConst } from "shared-lib";
import { AbstractService } from "./abstract-service";
import { WindowApiConst } from 'shared-lib';
import { AbstractService } from './abstract-service';
export class MultiplesService extends AbstractService<number, number[]> {
receptionChannel(): string {
return WindowApiConst.MULTIPLES_INPUT;
}
receptionChannel(): string {
return WindowApiConst.MULTIPLES_INPUT;
}
sendingChannel(): string {
return WindowApiConst.MULTIPLES_OUTPUT;
}
sendingChannel(): string {
return WindowApiConst.MULTIPLES_OUTPUT;
}
process(input: number): number[] {
// From 1 to 10, return input multiples
const multiples = [];
for (let n = 1; n <= 10; n++) {
multiples.push(n * input);
}
return multiples;
}
process(input: number): number[] {
// From 1 to 10, return input multiples
const multiples = [];
for (let n = 1; n <= 10; n++) {
multiples.push(n * input);
}
return multiples;
}
}

View File

@@ -1,143 +1,143 @@
import { app } from "electron";
import * as os from "os";
import * as path from "path";
import * as winston from "winston";
import { app } from 'electron';
import * as os from 'os';
import * as path from 'path';
import * as winston from 'winston';
declare const global: Global;
export class Logger {
private static singleton: Logger;
private _logger: winston.Logger;
private static singleton: Logger;
private _logger: winston.Logger;
public static error(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.error(message, meta);
}
public static error(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.error(message, meta);
}
public static warn(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.warn(message, meta);
}
public static warn(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.warn(message, meta);
}
public static info(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.info(message, meta);
}
public static info(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.info(message, meta);
}
public static http(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.http(message, meta);
}
public static http(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.http(message, meta);
}
public static verbose(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.verbose(message, meta);
}
public static verbose(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.verbose(message, meta);
}
public static debug(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.debug(message, meta);
}
public static debug(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.debug(message, meta);
}
public static silly(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.silly(message, meta);
}
public static silly(message: string, ...meta: any[]): void {
Logger.initSingleton();
Logger.singleton._logger.silly(message, meta);
}
private static initSingleton(): void {
if (!Logger.singleton) {
Logger.singleton = new Logger();
}
}
private static initSingleton(): void {
if (!Logger.singleton) {
Logger.singleton = new Logger();
}
}
private constructor() {
this._logger = winston.createLogger({
level: "debug",
format: winston.format.json(),
defaultMeta: { service: "user-service" },
transports: [
new winston.transports.File({
filename: this.getLogFilename(),
level: global.appConfig.mainLogLevel,
format: winston.format.combine(
winston.format.timestamp(),
this.fileFormat
),
}),
],
});
private constructor() {
this._logger = winston.createLogger({
level: 'debug',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.File({
filename: this.getLogFilename(),
level: global.appConfig.mainLogLevel,
format: winston.format.combine(
winston.format.timestamp(),
this.fileFormat
),
}),
],
});
// If we're not in production then log also to the `console` with the format:
// `${info.timestamp} ${info.level}: ${info.message} JSON.stringify({ ...rest }) `
if (global.appConfig.configId === "development") {
this._logger.add(
new winston.transports.Console({
stderrLevels: ["error", "warn"],
format: winston.format.combine(
winston.format.timestamp(),
this.consoleFormat
),
})
);
}
}
// If we're not in production then log also to the `console` with the format:
// `${info.timestamp} ${info.level}: ${info.message} JSON.stringify({ ...rest }) `
if (global.appConfig.configId === 'development') {
this._logger.add(
new winston.transports.Console({
stderrLevels: ['error', 'warn'],
format: winston.format.combine(
winston.format.timestamp(),
this.consoleFormat
),
})
);
}
}
/**
* Returns log filename with standard path
* In production, returns absolute standard path depending on platform
*/
private getLogFilename() {
let filename = global.appConfig.mainLogFile;
if (global.appConfig.configId === "production") {
const appName = app.getName();
if (process.platform == "linux") {
filename = `.config/${appName}/${filename}`;
} else if (process.platform == "darwin") {
filename = `Library/Logs/${appName}/${filename}`;
} else if (process.platform == "win32") {
filename = `AppData\\Roaming\\${appName}\\${filename}`;
}
}
return path.join(os.homedir(), filename);
}
/**
* Returns log filename with standard path
* In production, returns absolute standard path depending on platform
*/
private getLogFilename() {
let filename = global.appConfig.mainLogFile;
if (global.appConfig.configId === 'production') {
const appName = app.getName();
if (process.platform == 'linux') {
filename = `.config/${appName}/${filename}`;
} else if (process.platform == 'darwin') {
filename = `Library/Logs/${appName}/${filename}`;
} else if (process.platform == 'win32') {
filename = `AppData\\Roaming\\${appName}\\${filename}`;
}
}
return path.join(os.homedir(), filename);
}
/**
* Custom winston file format
* Write JSON logs with given format :
* `${timestamp} ${level} : ${info.message} : ${meta})`
*/
private fileFormat = winston.format.printf(
(data: winston.Logform.TransformableInfo) => {
return JSON.stringify(this.prepareLogData(data));
}
);
/**
* Custom winston file format
* Write JSON logs with given format :
* `${timestamp} ${level} : ${info.message} : ${meta})`
*/
private fileFormat = winston.format.printf(
(data: winston.Logform.TransformableInfo) => {
return JSON.stringify(this.prepareLogData(data));
}
);
/**
* Custom winston console format
* Write logs with given format :
* `${timestamp} ${level} : ${info.message} : JSON.stringify({ ...meta }) `
*/
private consoleFormat = winston.format.printf(
(data: winston.Logform.TransformableInfo) => {
const preparedData = this.prepareLogData(data);
return (
`${preparedData.timestamp} ${preparedData.level} : ` +
`${preparedData.message} : ${JSON.stringify(preparedData.meta)}`
);
}
);
/**
* Custom winston console format
* Write logs with given format :
* `${timestamp} ${level} : ${info.message} : JSON.stringify({ ...meta }) `
*/
private consoleFormat = winston.format.printf(
(data: winston.Logform.TransformableInfo) => {
const preparedData = this.prepareLogData(data);
return (
`${preparedData.timestamp} ${preparedData.level} : ` +
`${preparedData.message} : ${JSON.stringify(preparedData.meta)}`
);
}
);
private prepareLogData = (data: winston.Logform.TransformableInfo) => {
const additionalData = { ...data };
delete additionalData.timestamp;
delete additionalData.level;
delete additionalData.message;
delete additionalData.service;
return {
timestamp: data.timestamp,
level: data.level,
message: data.message,
meta: additionalData,
};
};
private prepareLogData = (data: winston.Logform.TransformableInfo) => {
const additionalData = { ...data };
delete additionalData.timestamp;
delete additionalData.level;
delete additionalData.message;
delete additionalData.service;
return {
timestamp: data.timestamp,
level: data.level,
message: data.message,
meta: additionalData,
};
};
}