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,37 +1,38 @@
{ {
"env": { "env": {
"browser": true, "browser": true,
"es6": true, "es6": true,
"node": true "node": true
}, },
"extends": [ "extends": [
"eslint:recommended", "eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended", "plugin:@typescript-eslint/recommended",
"plugin:import/errors", "plugin:import/errors",
"plugin:import/warnings", "plugin:import/warnings",
"plugin:import/typescript" "plugin:import/typescript"
], ],
"parser": "@typescript-eslint/parser", "parser": "@typescript-eslint/parser",
"settings": { "settings": {
"import/resolver": { "import/resolver": {
"node": { "node": {
"extensions": [ "extensions": [".js", ".jsx", ".ts", ".tsx"]
".js", }
".jsx", }
".ts", },
".tsx" "ignorePatterns": ["**/dist/**/*", "**/.dist/**/*"],
] "rules": {
} "@typescript-eslint/no-unused-vars": [
} "error",
}, {
"ignorePatterns": ["**/dist/**/*","**/.dist/**/*"], "argsIgnorePattern": "^_"
"rules": { }
"@typescript-eslint/no-unused-vars": ["error", { ],
"argsIgnorePattern": "^_" "@typescript-eslint/no-explicit-any": [
}], "error",
"@typescript-eslint/no-explicit-any": ["error", { {
"ignoreRestArgs": true "ignoreRestArgs": true
}] }
} ]
}
} }

1
.husky/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
_;

30
.husky/_/husky.sh Normal file
View File

@@ -0,0 +1,30 @@
#!/bin/sh
if [ -z "$husky_skip_init" ]; then
debug () {
[ "$HUSKY_DEBUG" = "1" ] && echo "husky (debug) - $1"
}
readonly hook_name="$(basename "$0")"
debug "starting $hook_name..."
if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi
if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi
export readonly husky_skip_init=1
sh -e "$0" "$@"
exitCode="$?"
if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
exit $exitCode
fi
exit 0
fi

4
.husky/commit-msg Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx --no-install commitlint --edit "$1"

5
.husky/pre-commit Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
npx pretty-quick --staged

8
.prettierrc Normal file
View File

@@ -0,0 +1,8 @@
{
"useTabs": true,
"tabSize": 2,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true
}

102121
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,153 +1,157 @@
{ {
"name": "electron-angular-quick-start", "name": "electron-angular-quick-start",
"productName": "electron-angular-quick-start", "productName": "electron-angular-quick-start",
"version": "2.0.0", "version": "2.0.0",
"description": "An Electron and Angular Quick-starter", "description": "An Electron and Angular Quick-starter",
"repository": "https://github.com/sourcygen/electron-angular-quick-start.git", "repository": "https://github.com/sourcygen/electron-angular-quick-start.git",
"author": "Sourcygen", "author": "Sourcygen",
"license": "MIT", "license": "MIT",
"main": ".webpack/main", "main": ".webpack/main",
"keywords": [ "keywords": [
"angular", "angular",
"angular 12", "angular 12",
"electron", "electron",
"electron 13", "electron 13",
"typescript", "typescript",
"typescript 4", "typescript 4",
"spectron", "spectron",
"spectron 15", "spectron 15",
"scss", "scss",
"live reload" "live reload"
], ],
"workspaces": [ "workspaces": [
"workspaces/angular-app", "workspaces/angular-app",
"workspaces/shared-lib" "workspaces/shared-lib"
], ],
"watch": { "watch": {
"restart-electron-app": { "restart-electron-app": {
"patterns": [ "patterns": [
"workspaces/electron-app", "workspaces/electron-app",
"workspaces/shared-lib" "workspaces/shared-lib"
], ],
"extensions": "ts,tsx" "extensions": "ts,tsx"
} }
}, },
"scripts": { "scripts": {
"start": "npm-run-all -p start:shared-lib start:angular-app start:electron-app", "start": "npm-run-all -p start:shared-lib start:angular-app start:electron-app",
"start:shared-lib": "tsc -b workspaces/shared-lib -w", "start:shared-lib": "tsc -b workspaces/shared-lib -w",
"start:angular-app": "cd workspaces/angular-app && npm run start", "start:angular-app": "cd workspaces/angular-app && npm run start",
"start:electron-app": "wait-on http://localhost:4200 && npm-watch restart-electron-app", "start:electron-app": "wait-on http://localhost:4200 && npm-watch restart-electron-app",
"restart-electron-app": "electron-forge start", "restart-electron-app": "electron-forge start",
"package": "npm run package:angular-app && npm run package:electron-app", "package": "npm run package:angular-app && npm run package:electron-app",
"package:angular-app": "cd workspaces/angular-app && npm run package", "package:angular-app": "cd workspaces/angular-app && npm run package",
"package:electron-app": "electron-forge package", "package:electron-app": "electron-forge package",
"make": "npm run package:angular-app && electron-forge make", "make": "npm run package:angular-app && electron-forge make",
"publish": "electron-forge publish", "publish": "electron-forge publish",
"lint": "eslint --ext .ts .", "lint": "eslint --ext .ts .",
"test:electron-e2e": "npm run package && cross-env X_NODE_ENV=e2e-test node workspaces/electron-e2e/jasmine.js", "test:electron-e2e": "npm run package && cross-env X_NODE_ENV=e2e-test node workspaces/electron-e2e/jasmine.js",
"clean": "del -f .webpack out node_modules workspaces/shared-lib/.dist workspaces/angular-app/node_modules workspaces/angular-app/.dist", "clean": "shx rm -rf .webpack out node_modules workspaces/shared-lib/.dist workspaces/angular-app/node_modules workspaces/angular-app/.dist",
"version": "npx conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md" "version": "npx conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md",
}, "prepare": "husky install",
"config": { "postinstall": "husky install && shx rm -rf .git/hooks && shx ln -s ../.husky .git/hooks"
"forge": { },
"packagerConfig": {}, "config": {
"makers": [ "forge": {
{ "packagerConfig": {},
"name": "@electron-forge/maker-dmg", "makers": [
"config": { {
"name": "EAQS" "name": "@electron-forge/maker-dmg",
} "config": {
}, "name": "EAQS"
{ }
"name": "@electron-forge/maker-squirrel", },
"config": { {
"name": "electron_angular_quick_start" "name": "@electron-forge/maker-squirrel",
} "config": {
}, "name": "electron_angular_quick_start"
{ }
"name": "@electron-forge/maker-zip", },
"platforms": [] {
}, "name": "@electron-forge/maker-zip",
{ "platforms": []
"name": "@electron-forge/maker-deb", },
"config": {} {
}, "name": "@electron-forge/maker-deb",
{ "config": {}
"name": "@electron-forge/maker-rpm", },
"config": {} {
} "name": "@electron-forge/maker-rpm",
], "config": {}
"plugins": [ }
[ ],
"@electron-forge/plugin-webpack", "plugins": [
{ [
"mainConfig": "./webpack.main.config.js", "@electron-forge/plugin-webpack",
"renderer": { {
"config": "./webpack.renderer.config.js", "mainConfig": "./webpack.main.config.js",
"entryPoints": [ "renderer": {
{ "config": "./webpack.renderer.config.js",
"html": "./workspaces/electron-app/renderer/index.html", "entryPoints": [
"js": "./workspaces/electron-app/renderer/index.ts", {
"name": "main_window", "html": "./workspaces/electron-app/renderer/index.html",
"preload": { "js": "./workspaces/electron-app/renderer/index.ts",
"js": "./workspaces/electron-app/renderer/preload.ts" "name": "main_window",
} "preload": {
} "js": "./workspaces/electron-app/renderer/preload.ts"
] }
} }
} ]
] }
] }
} ]
}, ]
"devDependencies": { }
"@commitlint/cli": "^12.1.4", },
"@commitlint/config-conventional": "^12.1.4", "devDependencies": {
"@electron-forge/cli": "^6.0.0-beta.57", "@commitlint/cli": "^12.1.4",
"@electron-forge/maker-deb": "^6.0.0-beta.57", "@commitlint/config-conventional": "^12.1.4",
"@electron-forge/maker-dmg": "^6.0.0-beta.58", "@electron-forge/cli": "^6.0.0-beta.57",
"@electron-forge/maker-rpm": "^6.0.0-beta.57", "@electron-forge/maker-deb": "^6.0.0-beta.57",
"@electron-forge/maker-squirrel": "^6.0.0-beta.57", "@electron-forge/maker-dmg": "^6.0.0-beta.58",
"@electron-forge/maker-zip": "^6.0.0-beta.57", "@electron-forge/maker-rpm": "^6.0.0-beta.57",
"@electron-forge/plugin-webpack": "6.0.0-beta.57", "@electron-forge/maker-squirrel": "^6.0.0-beta.57",
"@types/jasmine": "^3.8.1", "@electron-forge/maker-zip": "^6.0.0-beta.57",
"@types/jasminewd2": "^2.0.10", "@electron-forge/plugin-webpack": "6.0.0-beta.57",
"@types/lodash": "^4.14.171", "@types/jasmine": "^3.8.1",
"@types/node": "^14.17.5", "@types/jasminewd2": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.0.1", "@types/lodash": "^4.14.171",
"@typescript-eslint/parser": "^4.0.1", "@types/node": "^14.17.5",
"@vercel/webpack-asset-relocator-loader": "^1.6.0", "@typescript-eslint/eslint-plugin": "^4.0.1",
"conventional-changelog-cli": "^2.1.1", "@typescript-eslint/parser": "^4.0.1",
"copy-webpack-plugin": "^9.0.1", "@vercel/webpack-asset-relocator-loader": "^1.6.0",
"cross-env": "^7.0.3", "conventional-changelog-cli": "^2.1.1",
"css-loader": "^5.0.0", "copy-webpack-plugin": "^9.0.1",
"del-cli": "^4.0.1", "cross-env": "^7.0.3",
"electron": "^13.1.7", "css-loader": "^5.0.0",
"eslint": "^7.6.0", "electron": "^13.1.7",
"eslint-plugin-import": "^2.20.0", "eslint": "^7.6.0",
"fork-ts-checker-webpack-plugin": "^6.0.1", "eslint-config-prettier": "^8.3.0",
"husky": "^7.0.1", "eslint-plugin-import": "^2.20.0",
"jasmine": "^3.8.0", "eslint-plugin-prettier": "^3.4.0",
"jasmine-core": "^3.8.0", "fork-ts-checker-webpack-plugin": "^6.0.1",
"jasmine-spec-reporter": "^7.0.0", "husky": "^7.0.1",
"node-loader": "^2.0.0", "jasmine": "^3.8.0",
"npm-run-all": "^4.1.5", "jasmine-core": "^3.8.0",
"npm-watch": "^0.10.0", "jasmine-spec-reporter": "^7.0.0",
"spectron": "^15.0.0", "lint-staged": "^11.0.1",
"style-loader": "^2.0.0", "node-loader": "^2.0.0",
"ts-loader": "^9.2.2", "npm-run-all": "^4.1.5",
"ts-node": "^10.1.0", "npm-watch": "^0.10.0",
"typescript": "^4.3.5", "prettier": "^2.3.2",
"wait-on": "^6.0.0" "shx": "^0.3.3",
}, "spectron": "^15.0.0",
"dependencies": { "style-loader": "^2.0.0",
"electron-squirrel-startup": "^1.0.0", "ts-loader": "^9.2.2",
"fs-extra": "^10.0.0", "ts-node": "^10.1.0",
"winston": "^3.3.3" "typescript": "^4.3.5",
}, "wait-on": "^6.0.0"
"husky": { },
"hooks": { "dependencies": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" "electron-squirrel-startup": "^1.0.0",
} "fs-extra": "^10.0.0",
} "winston": "^3.3.3"
},
"lint-staged": {
"*.ts": "npm run lint"
}
} }

View File

@@ -1,23 +1,20 @@
{ {
"compilerOptions": { "compilerOptions": {
"allowJs": true, "allowJs": true,
"module": "es2020", "module": "es2020",
"target": "es2015", "target": "es2015",
"skipLibCheck": true, "skipLibCheck": true,
"esModuleInterop": true, "esModuleInterop": true,
"noImplicitAny": true, "noImplicitAny": true,
"noImplicitReturns": true, "noImplicitReturns": true,
"sourceMap": true, "sourceMap": true,
"baseUrl": ".", "baseUrl": ".",
"outDir": ".dist", "outDir": ".dist",
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"paths": { "paths": {
"*": ["node_modules/*"] "*": ["node_modules/*"]
} }
}, },
"include": [ "include": ["workspaces/electron-app/**/*", "workspaces/electron-e2e/**/*"]
"workspaces/electron-app/**/*",
"workspaces/electron-e2e/**/*"
]
} }

View File

@@ -1,30 +1,30 @@
const path = require("path"); const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin'); const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = { module.exports = {
/** /**
* This is the main entry point for your application, it's the first file * This is the main entry point for your application, it's the first file
* that runs in the main process. * that runs in the main process.
*/ */
entry: './workspaces/electron-app/main/index.ts', entry: './workspaces/electron-app/main/index.ts',
// Put your normal webpack config below here // Put your normal webpack config below here
module: { module: {
rules: require('./webpack.rules'), rules: require('./webpack.rules'),
}, },
resolve: { resolve: {
extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json'], extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json'],
modules: [path.resolve(__dirname, "node_modules"), "node_modules"] modules: [path.resolve(__dirname, 'node_modules'), 'node_modules'],
}, },
plugins: [ plugins: [
new CopyWebpackPlugin({ new CopyWebpackPlugin({
patterns: [ patterns: [
{ from: "workspaces/electron-app/main/assets" }, { from: 'workspaces/electron-app/main/assets' },
{ {
from: "workspaces/angular-app/.dist/angular-app", from: 'workspaces/angular-app/.dist/angular-app',
to: "../renderer/angular_window", to: '../renderer/angular_window',
noErrorOnMissing: true noErrorOnMissing: true,
} },
], ],
}), }),
], ],
}; };

View File

@@ -1,16 +0,0 @@
# Editor configuration, see https://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
[*.ts]
quote_type = single
[*.md]
max_line_length = off
trim_trailing_whitespace = false

View File

@@ -1,111 +1,101 @@
{ {
"$schema": "./node_modules/@angular/cli/lib/config/schema.json", "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1, "version": 1,
"newProjectRoot": "projects", "newProjectRoot": "projects",
"projects": { "projects": {
"angular-app": { "angular-app": {
"projectType": "application", "projectType": "application",
"schematics": { "schematics": {
"@schematics/angular:component": { "@schematics/angular:component": {
"style": "scss" "style": "scss"
}, },
"@schematics/angular:application": { "@schematics/angular:application": {
"strict": true "strict": true
} }
}, },
"root": "", "root": "",
"sourceRoot": "src", "sourceRoot": "src",
"prefix": "app", "prefix": "app",
"architect": { "architect": {
"build": { "build": {
"builder": "@angular-devkit/build-angular:browser", "builder": "@angular-devkit/build-angular:browser",
"options": { "options": {
"outputPath": "./.dist/angular-app", "outputPath": "./.dist/angular-app",
"index": "src/index.html", "index": "src/index.html",
"main": "src/main.ts", "main": "src/main.ts",
"polyfills": "src/polyfills.ts", "polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json", "tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss", "inlineStyleLanguage": "scss",
"assets": [ "assets": ["src/favicon.ico", "src/assets"],
"src/favicon.ico", "styles": ["src/styles.scss"],
"src/assets" "scripts": []
], },
"styles": [ "configurations": {
"src/styles.scss" "production": {
], "budgets": [
"scripts": [] {
}, "type": "initial",
"configurations": { "maximumWarning": "500kb",
"production": { "maximumError": "1mb"
"budgets": [ },
{ {
"type": "initial", "type": "anyComponentStyle",
"maximumWarning": "500kb", "maximumWarning": "2kb",
"maximumError": "1mb" "maximumError": "4kb"
}, }
{ ],
"type": "anyComponentStyle", "fileReplacements": [
"maximumWarning": "2kb", {
"maximumError": "4kb" "replace": "src/environments/environment.ts",
} "with": "src/environments/environment.prod.ts"
], }
"fileReplacements": [ ],
{ "outputHashing": "all"
"replace": "src/environments/environment.ts", },
"with": "src/environments/environment.prod.ts" "development": {
} "buildOptimizer": false,
], "optimization": false,
"outputHashing": "all" "vendorChunk": true,
}, "extractLicenses": false,
"development": { "sourceMap": true,
"buildOptimizer": false, "namedChunks": true
"optimization": false, }
"vendorChunk": true, },
"extractLicenses": false, "defaultConfiguration": "production"
"sourceMap": true, },
"namedChunks": true "serve": {
} "builder": "@angular-devkit/build-angular:dev-server",
}, "configurations": {
"defaultConfiguration": "production" "production": {
}, "browserTarget": "angular-app:build:production"
"serve": { },
"builder": "@angular-devkit/build-angular:dev-server", "development": {
"configurations": { "browserTarget": "angular-app:build:development"
"production": { }
"browserTarget": "angular-app:build:production" },
}, "defaultConfiguration": "development"
"development": { },
"browserTarget": "angular-app:build:development" "extract-i18n": {
} "builder": "@angular-devkit/build-angular:extract-i18n",
}, "options": {
"defaultConfiguration": "development" "browserTarget": "angular-app:build"
}, }
"extract-i18n": { },
"builder": "@angular-devkit/build-angular:extract-i18n", "test": {
"options": { "builder": "@angular-devkit/build-angular:karma",
"browserTarget": "angular-app:build" "options": {
} "main": "src/test.ts",
}, "polyfills": "src/polyfills.ts",
"test": { "tsConfig": "tsconfig.spec.json",
"builder": "@angular-devkit/build-angular:karma", "karmaConfig": "karma.conf.js",
"options": { "inlineStyleLanguage": "scss",
"main": "src/test.ts", "assets": ["src/favicon.ico", "src/assets"],
"polyfills": "src/polyfills.ts", "styles": ["src/styles.scss"],
"tsConfig": "tsconfig.spec.json", "scripts": []
"karmaConfig": "karma.conf.js", }
"inlineStyleLanguage": "scss", }
"assets": [ }
"src/favicon.ico", }
"src/assets" },
], "defaultProject": "angular-app"
"styles": [
"src/styles.scss"
],
"scripts": []
}
}
}
}
},
"defaultProject": "angular-app"
} }

View File

@@ -1,37 +1,37 @@
{ {
"name": "angular-app", "name": "angular-app",
"version": "2.0.0", "version": "2.0.0",
"scripts": { "scripts": {
"start": "ng serve", "start": "ng serve",
"package": "ng build --configuration production --base-href ./" "package": "ng build --configuration production --base-href ./"
}, },
"private": true, "private": true,
"dependencies": { "dependencies": {
"@angular/animations": "~12.1.2", "@angular/animations": "~12.1.2",
"@angular/common": "~12.1.2", "@angular/common": "~12.1.2",
"@angular/compiler": "~12.1.2", "@angular/compiler": "~12.1.2",
"@angular/core": "~12.1.2", "@angular/core": "~12.1.2",
"@angular/forms": "~12.1.2", "@angular/forms": "~12.1.2",
"@angular/platform-browser": "~12.1.2", "@angular/platform-browser": "~12.1.2",
"@angular/platform-browser-dynamic": "~12.1.2", "@angular/platform-browser-dynamic": "~12.1.2",
"@angular/router": "~12.1.2", "@angular/router": "~12.1.2",
"@ngx-translate/core": "^13.0.0", "@ngx-translate/core": "^13.0.0",
"@ngx-translate/http-loader": "^6.0.0", "@ngx-translate/http-loader": "^6.0.0",
"electron": "^13.1.7", "electron": "^13.1.7",
"rxjs": "~6.6.0", "rxjs": "~6.6.0",
"tslib": "^2.2.0", "tslib": "^2.2.0",
"zone.js": "~0.11.4" "zone.js": "~0.11.4"
}, },
"devDependencies": { "devDependencies": {
"@angular-devkit/build-angular": "~12.1.2", "@angular-devkit/build-angular": "~12.1.2",
"@angular/cli": "~12.1.2", "@angular/cli": "~12.1.2",
"@angular/compiler-cli": "~12.1.2", "@angular/compiler-cli": "~12.1.2",
"@types/node": "^12.11.1", "@types/node": "^12.11.1",
"karma": "~6.3.0", "karma": "~6.3.0",
"karma-chrome-launcher": "~3.1.0", "karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3", "karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0", "karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "~1.7.0", "karma-jasmine-html-reporter": "~1.7.0",
"typescript": "^4.3.5" "typescript": "^4.3.5"
} }
} }

View File

@@ -10,26 +10,26 @@ import { MutiplesComponent } from './components/mutiples/mutiples.component';
// AoT requires an exported function for factories // AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader { export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader {
return new TranslateHttpLoader(http, './assets/i18n/', '.json'); return new TranslateHttpLoader(http, './assets/i18n/', '.json');
} }
@NgModule({ @NgModule({
declarations: [AppComponent, MutiplesComponent], declarations: [AppComponent, MutiplesComponent],
imports: [ imports: [
BrowserModule, BrowserModule,
AppRoutingModule, AppRoutingModule,
HttpClientModule, HttpClientModule,
ReactiveFormsModule, ReactiveFormsModule,
TranslateModule.forRoot({ TranslateModule.forRoot({
defaultLanguage: 'en', defaultLanguage: 'en',
loader: { loader: {
provide: TranslateLoader, provide: TranslateLoader,
useFactory: HttpLoaderFactory, useFactory: HttpLoaderFactory,
deps: [HttpClient], deps: [HttpClient],
}, },
}), }),
], ],
providers: [], providers: [],
bootstrap: [AppComponent], bootstrap: [AppComponent],
}) })
export class AppModule {} export class AppModule {}

View File

@@ -5,47 +5,47 @@ import { WindowApiConst } from 'shared-lib';
import { ElectronIpcService } from '../../services/electron-ipc.service'; import { ElectronIpcService } from '../../services/electron-ipc.service';
@Component({ @Component({
selector: 'app-mutiples', selector: 'app-mutiples',
templateUrl: './mutiples.component.html', templateUrl: './mutiples.component.html',
styleUrls: ['./mutiples.component.scss'], styleUrls: ['./mutiples.component.scss'],
}) })
export class MutiplesComponent implements OnInit { export class MutiplesComponent implements OnInit {
timesTableForm = new FormGroup({ timesTableForm = new FormGroup({
input: new FormControl(Math.round(Math.random() * 100) % 10), input: new FormControl(Math.round(Math.random() * 100) % 10),
}); });
multiples: number[] = []; multiples: number[] = [];
constructor( constructor(
private electronIpc: ElectronIpcService, private electronIpc: ElectronIpcService,
private translate: TranslateService private translate: TranslateService
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
// Specifying what to do with received data from main process // Specifying what to do with received data from main process
this.electronIpc.receive<number[]>( this.electronIpc.receive<number[]>(
WindowApiConst.MULTIPLES_OUTPUT, WindowApiConst.MULTIPLES_OUTPUT,
(output: number[]) => { (output: number[]) => {
// Update current data // Update current data
this.multiples = output; this.multiples = output;
} }
); );
// Reset multiples on form changes // Reset multiples on form changes
this.timesTableForm.valueChanges.subscribe(() => { this.timesTableForm.valueChanges.subscribe(() => {
this.multiples = []; this.multiples = [];
}); });
// Init time tables with given random value // Init time tables with given random value
this.onSubmit(); this.onSubmit();
} }
translateIn(lang: string): void { translateIn(lang: string): void {
this.translate.use(lang); this.translate.use(lang);
} }
onSubmit(): void { onSubmit(): void {
const input = this.timesTableForm.value.input; const input = this.timesTableForm.value.input;
this.electronIpc.send(WindowApiConst.MULTIPLES_INPUT, input); this.electronIpc.send(WindowApiConst.MULTIPLES_INPUT, input);
} }
} }

View File

@@ -2,39 +2,39 @@ import { Injectable, NgZone } from '@angular/core';
import { WindowApi } from 'shared-lib'; import { WindowApi } from 'shared-lib';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class ElectronIpcService { export class ElectronIpcService {
private _api!: WindowApi; private _api!: WindowApi;
constructor(private zone: NgZone) { constructor(private zone: NgZone) {
if (window && (window as Window).api) { if (window && (window as Window).api) {
this._api = (window as Window).api; this._api = (window as Window).api;
console.log('Preloader API has been loaded successfully'); console.log('Preloader API has been loaded successfully');
} else { } else {
console.warn('Preloader API is not loaded'); console.warn('Preloader API is not loaded');
} }
} }
public receive<Out>(channel: string, func: (output: Out) => void): void { public receive<Out>(channel: string, func: (output: Out) => void): void {
if (this._api) { if (this._api) {
this._api.receive<Out>(channel, (output) => { this._api.receive<Out>(channel, (output) => {
console.log(`Received from main process channel [${channel}]`, output); console.log(`Received from main process channel [${channel}]`, output);
// Next code might run outside of Angular zone and therefore Angular // Next code might run outside of Angular zone and therefore Angular
// doesn't recognize it needs to run change detection // doesn't recognize it needs to run change detection
// Further details on SO : https://stackoverflow.com/a/49136353/11480016 // Further details on SO : https://stackoverflow.com/a/49136353/11480016
this.zone.run(() => { this.zone.run(() => {
func(output); func(output);
}); });
}); });
} }
} }
public send<In>(channel: string, input: In): void { public send<In>(channel: string, input: In): void {
if (this._api) { if (this._api) {
console.log(`Sending to main process channel [${channel}]`, input); console.log(`Sending to main process channel [${channel}]`, input);
this._api.send<In>(channel, input); this._api.send<In>(channel, input);
} }
} }
} }

View File

@@ -1,31 +1,28 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */ /* To learn more about this file see: https://angular.io/config/tsconfig. */
{ {
"compileOnSave": false, "compileOnSave": false,
"compilerOptions": { "compilerOptions": {
"baseUrl": "./", "baseUrl": "./",
"outDir": "./.dist/out-tsc", "outDir": "./.dist/out-tsc",
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strict": true, "strict": true,
"noImplicitReturns": true, "noImplicitReturns": true,
"noFallthroughCasesInSwitch": true, "noFallthroughCasesInSwitch": true,
"sourceMap": true, "sourceMap": true,
"declaration": false, "declaration": false,
"downlevelIteration": true, "downlevelIteration": true,
"experimentalDecorators": true, "experimentalDecorators": true,
"moduleResolution": "node", "moduleResolution": "node",
"importHelpers": true, "importHelpers": true,
"target": "es2017", "target": "es2017",
"module": "es2020", "module": "es2020",
"lib": [ "lib": ["es2018", "dom"]
"es2018", },
"dom" "angularCompilerOptions": {
] "enableI18nLegacyMessageIdFormat": false,
}, "strictInjectionParameters": true,
"angularCompilerOptions": { "strictInputAccessModifiers": true,
"enableI18nLegacyMessageIdFormat": false, "strictTemplates": true
"strictInjectionParameters": true, },
"strictInputAccessModifiers": true, "references": [{ "path": "../shared-lib" }]
"strictTemplates": true
},
"references": [{ "path": "../shared-lib" }]
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,35 +1,35 @@
// To secure user platform when running renderer process stuff, // To secure user platform when running renderer process stuff,
// Node.JS and Electron APIs are only available in this script // Node.JS and Electron APIs are only available in this script
import { contextBridge, ipcRenderer, IpcRendererEvent } from "electron"; import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
import { WindowApi, WindowApiConst } from "shared-lib"; import { WindowApi, WindowApiConst } from 'shared-lib';
// So we expose protected methods that allow the renderer process // So we expose protected methods that allow the renderer process
// to use the ipcRenderer without exposing the entire object // to use the ipcRenderer without exposing the entire object
const windowApi: WindowApi = { const windowApi: WindowApi = {
send: <In>(channel: string, input: In) => { send: <In>(channel: string, input: In) => {
if (WindowApiConst.SENDING_SAFE_CHANNELS.includes(channel)) { if (WindowApiConst.SENDING_SAFE_CHANNELS.includes(channel)) {
ipcRenderer.send(channel, input); ipcRenderer.send(channel, input);
} }
}, },
receive: <Out>(channel: string, func: (output: Out) => void) => { receive: <Out>(channel: string, func: (output: Out) => void) => {
if (WindowApiConst.RECEIVING_SAFE_CHANNELS.includes(channel)) { if (WindowApiConst.RECEIVING_SAFE_CHANNELS.includes(channel)) {
// Deliberately strip event as it includes `sender` // Deliberately strip event as it includes `sender`
ipcRenderer.on(channel, (event: IpcRendererEvent, ...args: any[]) => ipcRenderer.on(channel, (event: IpcRendererEvent, ...args: any[]) =>
func(args[0]) func(args[0])
); );
} }
}, },
}; };
declare const window: Window; declare const window: Window;
if (process.env.X_NODE_ENV === "e2e-test") { if (process.env.X_NODE_ENV === 'e2e-test') {
// Injecting windowApi directly // Injecting windowApi directly
window.api = windowApi; window.api = windowApi;
} else { } else {
// ContextBridge API can only be used when contextIsolation is enabled // ContextBridge API can only be used when contextIsolation is enabled
// which is normally the case except in e2e test mode // which is normally the case except in e2e test mode
contextBridge.exposeInMainWorld("api", windowApi); contextBridge.exposeInMainWorld('api', windowApi);
} }
console.log(typeof window); console.log(typeof window);
console.log("The preload script has been injected successfully."); console.log('The preload script has been injected successfully.');

View File

@@ -1,22 +1,24 @@
const Jasmine = require("jasmine"); const Jasmine = require('jasmine');
const { SpecReporter } = require("jasmine-spec-reporter"); const { SpecReporter } = require('jasmine-spec-reporter');
const jasmine = new Jasmine(); const jasmine = new Jasmine();
jasmine.loadConfig({ jasmine.loadConfig({
showColors: true, showColors: true,
defaultTimeoutInterval: 15000, defaultTimeoutInterval: 15000,
spec_dir: "workspaces/electron-e2e", spec_dir: 'workspaces/electron-e2e',
spec_files: ["./**/*-spec.ts"], spec_files: ['./**/*-spec.ts'],
helpers: ["./**/*-helper.ts"], helpers: ['./**/*-helper.ts'],
random: false, random: false,
seed: undefined, seed: undefined,
stopSpecOnExpectationFailure: false, stopSpecOnExpectationFailure: false,
}); });
jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
require("ts-node").register({ require('ts-node').register({
project: require("path").join(__dirname, "./tsconfig.json"), project: require('path').join(__dirname, './tsconfig.json'),
}); });
jasmine.env.clearReporters(); jasmine.env.clearReporters();
jasmine.addReporter(new SpecReporter({ spec: { displayStacktrace: 'pretty' } })); jasmine.addReporter(
new SpecReporter({ spec: { displayStacktrace: 'pretty' } })
);
jasmine.execute(); jasmine.execute();

View File

@@ -1,35 +1,35 @@
import * as path from "path"; import * as path from 'path';
import { Application } from "spectron"; import { Application } from 'spectron';
describe("A simple test to verify a visible window is opened with a title", () => { describe('A simple test to verify a visible window is opened with a title', () => {
// Init local app // Init local app
const app = new Application({ const app = new Application({
path: path.join(__dirname, "../../../node_modules/.bin/electron"), path: path.join(__dirname, '../../../node_modules/.bin/electron'),
args: [path.join(__dirname, "../../../.webpack/main/index.js")], args: [path.join(__dirname, '../../../.webpack/main/index.js')],
}); });
beforeAll(async () => { beforeAll(async () => {
// Init local app and wait until window loaded // Init local app and wait until window loaded
await app.start(); await app.start();
await app.client.waitUntilWindowLoaded(); await app.client.waitUntilWindowLoaded();
}); });
afterAll(async () => { afterAll(async () => {
if (app && app.isRunning()) { if (app && app.isRunning()) {
// Wait 1 second and then stop local app // Wait 1 second and then stop local app
await new Promise((resolve) => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
await app.stop(); await app.stop();
} }
}); });
it("shows an initial window", async () => { it('shows an initial window', async () => {
// Checking there is one visible window // Checking there is one visible window
expect(await app.browserWindow.isVisible()).toEqual(true); expect(await app.browserWindow.isVisible()).toEqual(true);
// Please note that getWindowCount() will return 2 if `dev tools` are opened. // Please note that getWindowCount() will return 2 if `dev tools` are opened.
expect(await app.client.getWindowCount()).toEqual(1); expect(await app.client.getWindowCount()).toEqual(1);
}); });
it("should have expected title", async () => { it('should have expected title', async () => {
expect(await app.client.getTitle()).toEqual("ElectronAngularQuickStart"); expect(await app.client.getTitle()).toEqual('ElectronAngularQuickStart');
}); });
}); });

View File

@@ -1,10 +1,10 @@
{ {
"extends": "../../tsconfig.json", "extends": "../../tsconfig.json",
"compilerOptions": { "compilerOptions": {
"outDir": "./.dist", "outDir": "./.dist",
"module": "commonjs", "module": "commonjs",
"target": "es5", "target": "es5",
"noEmit": true, "noEmit": true,
"types": ["jasmine", "jasminewd2", "node"] "types": ["jasmine", "jasminewd2", "node"]
} }
} }

View File

@@ -1,15 +1,15 @@
export interface WindowApi { export interface WindowApi {
/** /**
* This method is used by the renderer process to receive data from the main process * This method is used by the renderer process to receive data from the main process
* @param channel used by the renderer to receive data and by the main to send them * @param channel used by the renderer to receive data and by the main to send them
* @param func the callback function to execute when data are available * @param func the callback function to execute when data are available
*/ */
receive<Out>(channel: string, func: (output: Out) => void): void; receive<Out>(channel: string, func: (output: Out) => void): void;
/** /**
* This method is used by the renderer process to send data to the main process * This method is used by the renderer process to send data to the main process
* @param channel used by the renderer to send data and by the main to receive them * @param channel used by the renderer to send data and by the main to receive them
* @param data the data sent by the renderer process to the main process * @param data the data sent by the renderer process to the main process
*/ */
send<In>(channel: string, input: In): void; send<In>(channel: string, input: In): void;
} }

View File

@@ -1,11 +1,11 @@
import { WindowApi } from "./apis/window-api"; import { WindowApi } from './apis/window-api';
export * from "./apis/window-api"; export * from './apis/window-api';
export * from "./apis/window-api-consts"; export * from './apis/window-api-consts';
export * from "./models/config/app-config"; export * from './models/config/app-config';
declare global { declare global {
// Global augmentation of the `Window` interface // Global augmentation of the `Window` interface
interface Window { interface Window {
api: WindowApi; api: WindowApi;
} }
} }

View File

@@ -1,25 +1,25 @@
export interface AppConfig { export interface AppConfig {
/** The configuration identifier */ /** The configuration identifier */
configId: string; configId: string;
/** The main logger output file path */ /** The main logger output file path */
mainLogFile: string; mainLogFile: string;
/** The main logger output level */ /** The main logger output level */
mainLogLevel: string; mainLogLevel: string;
/** Tells if we should try to load app icon */ /** Tells if we should try to load app icon */
isIconAvailable: boolean; isIconAvailable: boolean;
/** Tells if `nodeIntegration` webPreference is enabled */ /** Tells if `nodeIntegration` webPreference is enabled */
isNodeIntegration: boolean; isNodeIntegration: boolean;
/** Tells if `contextIsolation` and `worldSafeExecuteJavaScript` webPreferences are enabled */ /** Tells if `contextIsolation` and `worldSafeExecuteJavaScript` webPreferences are enabled */
isContextIsolation: boolean; isContextIsolation: boolean;
/** Tells if `isEnableRemoteModule` webPreference is enabled */ /** Tells if `isEnableRemoteModule` webPreference is enabled */
isEnableRemoteModule: boolean; isEnableRemoteModule: boolean;
/** Tells if we should open dev tools */ /** Tells if we should open dev tools */
isOpenDevTools: boolean; isOpenDevTools: boolean;
} }

View File

@@ -1,9 +1,8 @@
{ {
"name": "shared-lib", "name": "shared-lib",
"version": "2.0.0", "version": "2.0.0",
"main": ".dist/index.js", "main": ".dist/index.js",
"scripts": { "scripts": {},
}, "author": "Sourcygen",
"author": "Sourcygen", "license": "MIT"
"license": "MIT"
} }

View File

@@ -1,10 +1,10 @@
{ {
"extends": "../../tsconfig.json", "extends": "../../tsconfig.json",
"compilerOptions": { "compilerOptions": {
"sourceMap": true, "sourceMap": true,
"composite": true, "composite": true,
"declaration": true, "declaration": true,
"outDir": "./.dist" "outDir": "./.dist"
}, },
"include": ["./**/*.ts"] "include": ["./**/*.ts"]
} }