Initial commit

This commit is contained in:
Arne
2023-04-02 20:35:10 +02:00
parent 80ffb8b70c
commit 7611f10b1c
55 changed files with 1678 additions and 683 deletions

View File

@@ -1,26 +1,30 @@
import { Door } from 'shared-lib';
import * as remoteMain from '@electron/remote/main';
import { app, BrowserWindow, ipcMain, nativeImage } from 'electron';
import * as path from 'node:path';
import { AbstractService } from '../services/abstract-service';
import { MultiplesService } from '../services/multiples-service';
import { FileListService } from '../services/file-list-service';
import { PrintService } from './../services/print-service';
import { Logger } from '../utils/logger';
import { DoorService } from '../services/door-service';
declare const global: Global;
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
export class Window {
private _electronWindow: BrowserWindow | undefined;
private _printService: PrintService;
constructor() {
this.createWindow();
this.loadRenderer();
this.registerService<number, number[]>(new MultiplesService());
this.registerService<void, any[]>(new FileListService());
this.registerService<string, Door[]>(new DoorService());
}
private createWindow(): void {
this._electronWindow = new BrowserWindow({
width: 1280,
height: 720,
fullscreen: false,
backgroundColor: '#FFFFFF',
icon: this.loadIcon(),
webPreferences: {
@@ -41,6 +45,10 @@ export class Window {
},
});
this._printService = new PrintService();
this._printService.setWindow(this._electronWindow);
this.registerService<string, void>(this._printService);
// Disable the remote module to enhance security
// except in e2e test when that access is required
if (global.appConfig.isEnableRemoteModule) {
@@ -105,16 +113,16 @@ export class Window {
// Handling input
const input = parameters[0];
Logger.debug(`[${service.receptionChannel()}] =====> `, input);
const output: Out = service.process(input);
// Handling output
if (service.sendingChannel()) {
Logger.debug(`[${service.sendingChannel()}] =====> `, output);
this._electronWindow.webContents.send(
service.sendingChannel(),
output
);
}
service.process(input).then((output: Out) => {
// Handling output
if (service.sendingChannel()) {
Logger.debug(`[${service.sendingChannel()}] =====> `, output);
this._electronWindow.webContents.send(
service.sendingChannel(),
output
);
}
});
}
);
}

View File

@@ -8,7 +8,7 @@ export class AbstractService<In, Out> {
throw new Error(NOT_IMPEMENTED_YET);
}
process(_input: In): Out {
process(_input: In): Promise<Out> {
throw new Error(NOT_IMPEMENTED_YET);
}
}

View File

@@ -0,0 +1,43 @@
import { WindowApiConst, Door } from 'shared-lib';
import { AbstractService } from './abstract-service';
import { readFile, set_fs } from 'xlsx';
import * as fs from 'node:fs';
set_fs(fs);
export class DoorService extends AbstractService<string, Door[]> {
receptionChannel(): string {
return WindowApiConst.PROJECT_INPUT;
}
sendingChannel(): string {
return WindowApiConst.PROJECT_OUTPUT;
}
process(input: string): Promise<Door[]> {
return new Promise((resolve) => {
const workbook = readFile(`./assets/${input}`);
const workingSheet = workbook.Sheets['DEURLIJST'];
const doorList: Door[] = [];
// remove 2 top rows
// stop at 19
for (let index = 4; index < 19; index++) {
const door: Door = {};
door.nr = workingSheet[`A${index}`]?.v;
if (door.nr) {
door.lr = workingSheet[`H${index}`]?.v;
door.krukSlot = workingSheet[`K${index}`]?.v;
door.pivot = workingSheet[`Q${index}`]?.v;
door.type = workingSheet[`S${index}`]?.v;
door.modelKruk = workingSheet[`T${index}`]?.v;
door.remark = workingSheet[`U${index}`]?.v;
doorList.push(door);
}
}
resolve(doorList);
});
}
}

View File

@@ -0,0 +1,51 @@
import { readdir } from 'node:fs';
import { WindowApiConst } from 'shared-lib';
import { AbstractService } from './abstract-service';
export class FileListService extends AbstractService<void, any[]> {
private _paths: string[] = ['./assets'];
receptionChannel(): string {
return WindowApiConst.FILELIST_INPUT;
}
sendingChannel(): string {
return WindowApiConst.FILELIST_OUTPUT;
}
process(): Promise<any[]> {
return new Promise((resolve) => {
const projects: any[] = [];
Promise.all(this._paths.map((path) => this.readPath(path))).then((result: any) => {
for (const entry of result.entries()) {
for (const file of entry[1].result) {
projects.push({
path: entry[1].path,
name: file,
});
}
}
resolve(projects);
})
});
}
private readPath(path: string): Promise<any> {
return new Promise((resolve) => {
readdir(path, (error, fileList: string[]) => {
if (!error) {
const newList: string[] = [];
for (const file of fileList) {
newList.push(file);
}
return resolve({
path,
result: newList,
});
}
return {};
});
});
}
}

View File

@@ -1,21 +0,0 @@
import { WindowApiConst } from 'shared-lib';
import { AbstractService } from './abstract-service';
export class MultiplesService extends AbstractService<number, number[]> {
receptionChannel(): string {
return WindowApiConst.MULTIPLES_INPUT;
}
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;
}
}

View File

@@ -0,0 +1,27 @@
import { BrowserWindow } from 'electron';
import { WindowApiConst } from 'shared-lib';
import { AbstractService } from './abstract-service';
export class PrintService extends AbstractService<string, void> {
private _browserWindow: BrowserWindow;
sendingChannel(): string {
return WindowApiConst.PRINT_OUTPUT;
}
receptionChannel(): string {
return WindowApiConst.PRINT_INPUT;
}
process(): Promise<void> {
return new Promise((resolve) => {
if (this._browserWindow) {
this._browserWindow.webContents.print({ silent: true });
}
resolve();
});
}
public setWindow(window: BrowserWindow): void {
this._browserWindow = window;
}
}