[chore] fix some sonarcloud code smells

This commit is contained in:
MathieuG-P
2023-07-01 16:48:30 +02:00
parent 8a99c412fd
commit 7e261fcb93
23 changed files with 88 additions and 100 deletions
+4 -5
View File
@@ -1,22 +1,21 @@
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
import { IpcChannel } from 'shared/models/ipc/ipc-response.interface';
contextBridge.exposeInMainWorld('electron', {
ipcRenderer: {
sendMessage(channel: IpcChannel, args: unknown[]) {
sendMessage(channel: string, args: unknown[]) {
ipcRenderer.send(channel, args);
},
on(channel: IpcChannel, func: (...args: unknown[]) => void) {
on(channel: string, func: (...args: unknown[]) => void) {
const subscription = (_event: IpcRendererEvent, ...args: unknown[]) =>
func(...args);
ipcRenderer.on(channel, subscription);
return () => ipcRenderer.removeListener(channel, subscription);
},
once(channel: IpcChannel, func: (...args: unknown[]) => void) {
once(channel: string, func: (...args: unknown[]) => void) {
ipcRenderer.once(channel, (_event, ...args) => func(...args));
},
removeAllListeners(channel: IpcChannel) {
removeAllListeners(channel: string) {
ipcRenderer.removeAllListeners(channel);
}
},
+31 -37
View File
@@ -1,4 +1,3 @@
/* eslint-disable prefer-promise-reject-errors */
import { BS_APP_ID, BS_DEPOT } from "../constants";
import path from "path";
import { BSVersion, PartialBSVersion } from 'shared/bs-version.interface';
@@ -10,57 +9,52 @@ import { ctrlc } from "ctrlc-windows";
import { BSLocalVersionService } from "./bs-local-version.service";
import isOnline from 'is-online';
import { WindowManagerService } from "./window-manager.service";
import { copy, copySync } from "fs-extra";
import { clean, satisfies } from "semver";
import { copy } from "fs-extra";
import { ensureFolderExist, pathExist } from "../helpers/fs.helpers";
export class BSInstallerService{
private static instance: BSInstallerService;
private static instance: BSInstallerService;
private readonly utils: UtilsService;
private readonly installLocationService: InstallationLocationService;
private readonly localVersionService: BSLocalVersionService;
private readonly windows: WindowManagerService;
private readonly utils: UtilsService;
private readonly installLocationService: InstallationLocationService;
private readonly localVersionService: BSLocalVersionService;
private readonly windows: WindowManagerService;
private downloadProcess: ChildProcessWithoutNullStreams;
private downloadProcess: ChildProcessWithoutNullStreams;
private constructor(){
this.utils = UtilsService.getInstance();
this.installLocationService = InstallationLocationService.getInstance();
this.localVersionService = BSLocalVersionService.getInstance();
this.windows = WindowManagerService.getInstance();
private constructor(){
this.utils = UtilsService.getInstance();
this.installLocationService = InstallationLocationService.getInstance();
this.localVersionService = BSLocalVersionService.getInstance();
this.windows = WindowManagerService.getInstance();
this.windows.getWindow("index.html")?.on("close", () => {
this.killDownloadProcess();
});
}
this.windows.getWindow("index.html")?.on("close", () => {
this.killDownloadProcess();
});
}
public static getInstance(){
if(!BSInstallerService.instance){ BSInstallerService.instance = new BSInstallerService(); }
return BSInstallerService.instance;
}
private escapeSpaces(path: string): string{
return `\"${path}\"`
public static getInstance(){
if(!BSInstallerService.instance){ BSInstallerService.instance = new BSInstallerService(); }
return BSInstallerService.instance;
}
private getDepotDownloaderExePath(): string{
return this.escapeSpaces(path.join(this.utils.getAssetsScriptsPath(), 'depot-downloader', 'DepotDownloader.exe'));
return path.join(this.utils.getAssetsScriptsPath(), 'depot-downloader', 'DepotDownloader.exe');
}
private removeSpecialSchar(txt: string): string{ return txt.replaceAll(/\[|\]/g, ""); }
private removeSpecialSchar(txt: string): string{ return txt.replaceAll(/[\[\]]/g, ""); }
private sendDownloadEvent(event: DownloadEventType, data?: string|number, success = true): void{
if(typeof data === "string"){ data = this.removeSpecialSchar(data); }
this.utils.ipcSend(`bs-download.${event}`, { success, data });
}
public sendInputProcess(input: string){
if(this.downloadProcess.stdin.writable){
this.downloadProcess.stdin.write(`${input}\n`);
public sendInputProcess(input: string){
if(this.downloadProcess.stdin.writable){
this.downloadProcess.stdin.write(`${input}\n`);
}
}
}
public killDownloadProcess(): Promise<boolean>{
return new Promise(resolve => {
@@ -78,7 +72,7 @@ export class BSInstallerService{
public async isDotNet6Installed(): Promise<boolean>{
try{
const process = spawnSync(this.getDepotDownloaderExePath(), {shell: true});
const process = spawnSync(`"${this.getDepotDownloaderExePath()}"`, {shell: true});
if(process.stderr.toString()){
log.error("no dotnet", process.stderr.toString());
return false;
@@ -93,9 +87,9 @@ export class BSInstallerService{
public async downloadBsVersion(downloadInfos: DownloadInfo): Promise<DownloadEvent>{
// TODO : Can be a lot improved by using ipcV2 and Observable
// TODO : Can be a lot improved by using ipcV2 and Observable -- This will be reworked for qrCode login
if(this.downloadProcess && this.downloadProcess.connected){ throw "AlreadyDownloading"; }
if(this.downloadProcess?.connected){ throw "AlreadyDownloading"; }
const {bsVersion} = downloadInfos;
if(!bsVersion){ return {type: "[Error]"}; }
if(!(await isOnline({timeout: 1500}))){ throw "no-internet"; }
@@ -109,13 +103,13 @@ export class BSInstallerService{
const downloadVersion: BSVersion = {...downloadInfos.bsVersion, ...(path.basename(dest) !== downloadInfos.bsVersion.BSVersion && {name: path.basename(dest)})}
this.downloadProcess = spawn(
this.getDepotDownloaderExePath(),
`"${this.getDepotDownloaderExePath()}"`,
[
`-app ${BS_APP_ID}`,
`-depot ${BS_DEPOT}`,
`-manifest ${bsVersion.BSManifest}`,
`-username \"${downloadInfos.username}\"`,
`-dir \"${this.localVersionService.getVersionFolder(downloadVersion)}\"`
`-username "${downloadInfos.username}"`,
`-dir "${this.localVersionService.getVersionFolder(downloadVersion)}"`
],
{shell: true, cwd: this.installLocationService.versionsDirectory}
);
+2 -2
View File
@@ -106,10 +106,10 @@ export class BSLauncherService{
}
if(launchOptions.debug){
this.bsProcess = spawn(`\"${exePath}\"`, launchArgs, {shell: true, cwd, env: {...process.env, "SteamAppId": BS_APP_ID}, detached: true, windowsVerbatimArguments: true });
this.bsProcess = spawn(`"${exePath}"`, launchArgs, {shell: true, cwd, env: {...process.env, "SteamAppId": BS_APP_ID}, detached: true, windowsVerbatimArguments: true });
}
else{
this.bsProcess = spawn(`\"${exePath}\"`, launchArgs, {shell: true, cwd, env: {...process.env, "SteamAppId": BS_APP_ID} });
this.bsProcess = spawn(`"${exePath}"`, launchArgs, {shell: true, cwd, env: {...process.env, "SteamAppId": BS_APP_ID} });
}
this.bsProcess.on('error', err => log.error(err));
+6 -6
View File
@@ -52,16 +52,16 @@ export class BSVersionLibService{
], {keepStructure: true});
let resVersions = localVersions;
if(remoteVersions && remoteVersions.length){ resVersions = remoteVersions; this.updateLocalVersions(resVersions); }
if(remoteVersions?.length){ resVersions = remoteVersions; this.updateLocalVersions(resVersions); }
this.bsVersions = resVersions;
return this.bsVersions;
}
public async getAvailableVersions(): Promise<BSVersion[]>{
const bsVersions = await this.loadBsVersions();
if(!bsVersions || !bsVersions.length){ return []; }
return bsVersions;
}
public async getAvailableVersions(): Promise<BSVersion[]>{
const bsVersions = await this.loadBsVersions();
if(!bsVersions?.length){ return []; }
return bsVersions;
}
public async getVersionDetails(version: string): Promise<BSVersion>{
const versions = await this.getAvailableVersions();
+11 -9
View File
@@ -1,7 +1,7 @@
import { IpcRequest } from "shared/models/ipc";
import { ipcMain } from "electron";
import { Observable } from "rxjs";
import { IpcChannel, IpcCompleteChannel, IpcErrorChannel } from "shared/models/ipc/ipc-response.interface";
import { IpcCompleteChannel, IpcErrorChannel } from "shared/models/ipc/ipc-response.interface";
import { AppWindow } from "shared/models/window-manager/app-window.model";
import { WindowManagerService } from "./window-manager.service";
import { IpcReplier } from "shared/models/ipc/ipc-request.interface";
@@ -22,15 +22,15 @@ export class IpcService {
this.windows = WindowManagerService.getInstance();
}
private getErrorChannel(channel: IpcChannel): IpcErrorChannel {
private getErrorChannel(channel: string): IpcErrorChannel {
return `${channel}_error`;
}
private getCompleteChannel(channel: IpcChannel): IpcCompleteChannel {
private getCompleteChannel(channel: string): IpcCompleteChannel {
return `${channel}_complete`;
}
private buildProxyListener<T>(listener: (req: IpcRequest<T>, replier: IpcReplier) => void) {
private buildProxyListener<T>(listener: IpcListener<T>) {
return (event: Electron.IpcMainEvent, req: IpcRequest<T>) => {
const window = this.windows.getAppWindowFromWebContents(event.sender);
@@ -40,11 +40,11 @@ export class IpcService {
}
public send<T>(channel: IpcChannel, window: AppWindow, response?: T|Error): void{
public send<T>(channel: string, window: AppWindow, response?: T|Error): void{
this.windows.getWindow(window)?.webContents?.send(channel, response);
}
private connectStream(channel: IpcChannel, window: AppWindow, observable: Observable<unknown>): void{
private connectStream(channel: string, window: AppWindow, observable: Observable<unknown>): void{
observable.subscribe(data => {
this.send(channel, window, data);
}, error => {
@@ -55,12 +55,14 @@ export class IpcService {
});
}
public on<T>(channel: IpcChannel, listener: (req: IpcRequest<T>, replier: IpcReplier) => void): void{
public on<T>(channel: string, listener: IpcListener<T>): void{
ipcMain.on(channel, this.buildProxyListener(listener));
}
public once<T>(channel: IpcChannel, listener: (req: IpcRequest<T>, replier: IpcReplier) => void): void{
public once<T>(channel: string, listener: IpcListener<T>): void{
ipcMain.once(channel, this.buildProxyListener(listener));
}
}
}
type IpcListener<T = unknown> = (req: IpcRequest<T>, replier: IpcReplier) => void|Promise<void>;
@@ -269,7 +269,7 @@ export class BsModsManagerService {
}
public async installMods(mods: Mod[], version: BSVersion): Promise<InstallModsResult>{
if(!mods || !mods.length){ throw "no-mods"; }
if(!mods?.length){ throw "no-mods"; }
const deps = await this.resolveDependencies(mods, version);
mods.push(...deps);
@@ -296,7 +296,7 @@ export class BsModsManagerService {
}
public async uninstallMods(mods: Mod[], version: BSVersion): Promise<UninstallModsResult>{
if(!mods || !mods.length){ throw "no-mods"; }
if(!mods?.length){ throw "no-mods"; }
this.nbModsToUninstall = mods.length;
this.nbUninstalledMods = 0;
@@ -314,7 +314,7 @@ export class BsModsManagerService {
public async uninstallAllMods(version: BSVersion): Promise<UninstallModsResult>{
const mods = await this.getInstalledMods(version);
if(!mods || !mods.length){ throw "no-mods"; }
if(!mods?.length){ throw "no-mods"; }
this.nbModsToUninstall = mods.length;
this.nbUninstalledMods = 0;
@@ -329,8 +329,6 @@ export class BsModsManagerService {
await deleteFolder(path.join(versionPath, ModsInstallFolder.LIBS));
await deleteFolder(path.join(versionPath, ModsInstallFolder.IPA));
path.resolve
return {
nbModsToUninstall: this.nbModsToUninstall,
nbUninstalledMods: this.nbUninstalledMods
@@ -92,9 +92,9 @@ export class ModelSaberService {
const res = await this.modelSaberApi.searchModel(query);
if(res.status !== 200){ observer.error(res.status); }
observer.next(Object.values(res.data).map(model => {
if(!model || !model.name){ return model; }
(model as MSModel).name = striptags(model.name);
(model as MSModel).author = striptags(model.author);
if(!model?.name){ return null; }
model.name = striptags(model.name);
model.author = striptags(model.author);
return model;
}));
})().catch(e => observer.error(e)).then(() => observer.complete());