mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
[chore] add @Log decorator + add logs for LivShortcuts
This commit is contained in:
@@ -47,6 +47,7 @@ module.exports = {
|
||||
"import/no-cycle": "off",
|
||||
"prefer-promise-reject-errors": "off",
|
||||
"react/jsx-no-target-blank": "off"
|
||||
"@typescript-eslint/ban-types": "off"
|
||||
},
|
||||
parserOptions: {
|
||||
ecmaVersion: 2020,
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import log from "electron-log";
|
||||
import { isPromise } from "../../shared/helpers/promise.helpers";
|
||||
import { tryit } from "../../shared/helpers/error.helpers";
|
||||
|
||||
type LogOptions = {
|
||||
logInput?: boolean;
|
||||
logOutput?: boolean;
|
||||
logArgs?: boolean;
|
||||
};
|
||||
|
||||
const stringifyArgs = (args: unknown[]) => {
|
||||
return args?.map(a => JSON.stringify(a)).join(', ');
|
||||
};
|
||||
|
||||
const logInfo = (message: string, propertyKey: string, args: unknown[], logArgs: boolean, result: unknown) => {
|
||||
log.info(`[${message}] ${propertyKey}(${logArgs ? stringifyArgs(args) : ''})`, result);
|
||||
};
|
||||
|
||||
const logError = (propertyKey: string, args: unknown[], logArgs: boolean, error: unknown) => {
|
||||
log.error(`[ERROR] ${propertyKey}(${logArgs ? stringifyArgs(args) : ''})`, error);
|
||||
throw error;
|
||||
};
|
||||
|
||||
export function Log(options?: LogOptions){
|
||||
|
||||
const logInput = options?.logInput ?? false;
|
||||
const logOutput = options?.logOutput ?? true;
|
||||
const logArgs = options?.logArgs ?? true;
|
||||
|
||||
return (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) => {
|
||||
const originalMethod = descriptor.value;
|
||||
|
||||
descriptor.value = function(...args: unknown[]) {
|
||||
|
||||
if (logInput) {
|
||||
logInfo('INPUT', propertyKey, args, logArgs, null);
|
||||
}
|
||||
|
||||
const outcome = tryit(() => originalMethod.apply(this, args));
|
||||
|
||||
if (isPromise(outcome)) {
|
||||
return outcome.then(({ result, error }) => {
|
||||
|
||||
if (error) {
|
||||
logError(propertyKey, args, logArgs, error);
|
||||
}
|
||||
if (logOutput) {
|
||||
logInfo('OUTPUT', propertyKey, args, logArgs, result);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
const { result, error } = outcome;
|
||||
|
||||
if (error) {
|
||||
logError(propertyKey, args, logArgs, error);
|
||||
}
|
||||
|
||||
if (logOutput) {
|
||||
logInfo('OUTPUT', propertyKey, args, logArgs, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { execOnOs } from "../../helpers/env.helpers";
|
||||
import { list, createKey, putValue, deleteKey, RegSzValue } from "regedit-rs";
|
||||
import path from "path";
|
||||
import { Log } from "../../decorators/log.decorator";
|
||||
|
||||
export class LivService {
|
||||
|
||||
@@ -20,6 +21,7 @@ export class LivService {
|
||||
|
||||
}
|
||||
|
||||
@Log()
|
||||
public async isLivInstalled(): Promise<boolean> {
|
||||
return execOnOs({
|
||||
win32: async () => {
|
||||
@@ -29,6 +31,7 @@ export class LivService {
|
||||
}, true);
|
||||
}
|
||||
|
||||
@Log()
|
||||
public async createLivShortcut(entry: LivEntry): Promise<void> {
|
||||
return execOnOs({
|
||||
win32: async () => {
|
||||
@@ -46,6 +49,7 @@ export class LivService {
|
||||
});
|
||||
}
|
||||
|
||||
@Log()
|
||||
public async deleteLivShortcuts(ids: string[]): Promise<void> {
|
||||
return execOnOs({
|
||||
win32: async () => {
|
||||
@@ -55,11 +59,13 @@ export class LivService {
|
||||
})
|
||||
}
|
||||
|
||||
@Log()
|
||||
public getLivShortcuts(): Promise<LivEntry[]> {
|
||||
|
||||
return execOnOs({
|
||||
win32: async () => {
|
||||
const regRes = await list(this.livExternalAppsRegeditKey).then(res => res[this.livExternalAppsRegeditKey]);
|
||||
|
||||
|
||||
if(!regRes.exists){
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -1,7 +1,22 @@
|
||||
export function tryit<Return>(func: () => Return): {error: Error, result: Return} {
|
||||
import { isPromise } from "./promise.helpers";
|
||||
|
||||
type TryitReturn<Return> = Return extends Promise<any> ? Promise<{ error: Error | null, result: Awaited<Return> | null }> : { error: Error | null, result: Return | null };
|
||||
|
||||
export function tryit<Return>(func: () => Return): TryitReturn<Return> {
|
||||
try {
|
||||
return { error: null, result: func() };
|
||||
const result = func();
|
||||
|
||||
if(isPromise(result)){
|
||||
return result
|
||||
.then((value) => ({ error: null, result: value }))
|
||||
.catch((err) => ({ error: err instanceof Error ? err : new Error(`${err}`), result: null })) as Return extends Promise<any>
|
||||
? Promise<{error: Error, result: undefined} | {error: undefined, result: Awaited<Return>}>
|
||||
: {error: Error, result: undefined} | {error: undefined, result: Return};
|
||||
}
|
||||
|
||||
return { error: undefined, result } as TryitReturn<Return>;
|
||||
|
||||
} catch (err) {
|
||||
return { error: err instanceof Error ? err : new Error(`${err}`), result: null }
|
||||
return { error: err, result: undefined } as TryitReturn<Return>;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export function isFunction(value: any): value is Function {
|
||||
return !!(value && value.constructor && value.call && value.apply)
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { isFunction } from "./function.helpers";
|
||||
|
||||
export type AllSettledHelperOptions = {
|
||||
keepStructure?: boolean;
|
||||
removeFalsy?: boolean;
|
||||
@@ -19,3 +21,10 @@ export async function allSettled<T>(promises: Promise<T>[], options?: AllSettled
|
||||
return acc;
|
||||
}, []);
|
||||
}
|
||||
|
||||
export function isPromise(value: any): value is Promise<unknown> {
|
||||
if(!value) { return false; }
|
||||
if(!value.then) { return false; }
|
||||
if(!isFunction(value.then)) { return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
+2
-1
@@ -20,7 +20,8 @@
|
||||
"resolveJsonModule": true,
|
||||
"allowJs": true,
|
||||
"outDir": "release/app/dist",
|
||||
"strictNullChecks": false
|
||||
"strictNullChecks": false,
|
||||
"experimentalDecorators": true
|
||||
},
|
||||
"exclude": ["test", "release/build", "release/app/dist", ".erb/dll"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user