From 101cc26895eb0b5fe97e03bb96039e0cddd94391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me> Date: Fri, 10 Nov 2023 12:14:11 +0100 Subject: [PATCH] Typescript => ESLint --- helpers/ArchiveHelper.ts | 6 ++--- helpers/Dojo/SharedAssignmentHelper.ts | 2 +- helpers/Dojo/SharedExerciseHelper.ts | 4 ++-- helpers/LazyVal.ts | 11 +++++---- helpers/Toolbox.ts | 6 ++--- helpers/TypeScriptExtensions.ts | 13 ++++++---- .../RecursiveFilesStats.ts | 2 +- managers/SharedGitlabManager.ts | 24 ++++++++----------- types/Gitlab/GitlabRoute.ts | 4 ++-- 9 files changed, 38 insertions(+), 34 deletions(-) diff --git a/helpers/ArchiveHelper.ts b/helpers/ArchiveHelper.ts index 6104299..f4449d6 100644 --- a/helpers/ArchiveHelper.ts +++ b/helpers/ArchiveHelper.ts @@ -41,7 +41,7 @@ class ArchiveHelper { } public async getBase64(folderPath: string): Promise<string> { - let data: any; + let data: string; const tarDataStream = new stream.Writable({ write(this: Writable, chunk: Buffer, _encoding: BufferEncoding, next: (error?: Error | null) => void) { if ( data ) { @@ -55,9 +55,9 @@ class ArchiveHelper { await this.compress(folderPath, tarDataStream); - await (new Promise((resolve, reject) => { + data = await (new Promise((resolve) => { tarDataStream.on('close', () => { - resolve(0); + resolve(data); }); })); diff --git a/helpers/Dojo/SharedAssignmentHelper.ts b/helpers/Dojo/SharedAssignmentHelper.ts index f17bef9..94e5005 100644 --- a/helpers/Dojo/SharedAssignmentHelper.ts +++ b/helpers/Dojo/SharedAssignmentHelper.ts @@ -48,7 +48,7 @@ class SharedAssignmentHelper { const isValid = validator(results); return { - results: isValid ? results : results as any, + results: isValid ? results : results as AssignmentFile, isValid: isValid, errors : validator.errors }; diff --git a/helpers/Dojo/SharedExerciseHelper.ts b/helpers/Dojo/SharedExerciseHelper.ts index 6f81054..e99fe0a 100644 --- a/helpers/Dojo/SharedExerciseHelper.ts +++ b/helpers/Dojo/SharedExerciseHelper.ts @@ -5,7 +5,7 @@ import JSON5 from 'json5'; class SharedExerciseHelper { - validateResultFile(resultsFilePathOrStr: string, isFile: boolean = true): { results: ExerciseResultsFile | undefined, isValid: boolean, errors: Array<ErrorObject<string, Record<string, any>, unknown> | string> | null | undefined } { + validateResultFile(resultsFilePathOrStr: string, isFile: boolean = true): { results: ExerciseResultsFile | undefined, isValid: boolean, errors: Array<ErrorObject | string> | null | undefined } { const ajv = new Ajv(); const schema: JTDSchemaType<ExerciseResultsFile> = { @@ -49,7 +49,7 @@ class SharedExerciseHelper { } return { - results: isValid ? results : results as any, + results: isValid ? results : results as ExerciseResultsFile, isValid: isValid, errors : validator.errors }; diff --git a/helpers/LazyVal.ts b/helpers/LazyVal.ts index ee0d8d4..4613c4b 100644 --- a/helpers/LazyVal.ts +++ b/helpers/LazyVal.ts @@ -4,12 +4,15 @@ class LazyVal<T> { constructor(private valLoader: () => Promise<T> | T) {} get value(): Promise<T> { - return new Promise<T>(async (resolve) => { + return new Promise<T>((resolve) => { if ( this.val === undefined ) { - this.val = await this.valLoader(); + Promise.resolve(this.valLoader()).then((value: T) => { + this.val = value; + resolve(value); + }); + } else { + resolve(this.val); } - - resolve(this.val); }); } diff --git a/helpers/Toolbox.ts b/helpers/Toolbox.ts index f1b3974..8a76beb 100644 --- a/helpers/Toolbox.ts +++ b/helpers/Toolbox.ts @@ -11,7 +11,7 @@ class Toolbox { Source of getAllFiles and getTotalSize (modified for this project): https://coderrocketfuel.com/article/get-the-total-size-of-all-files-in-a-directory-using-node-js */ private async getAllFiles(dirPath: string, arrayOfFiles: Array<string> = []): Promise<Array<string>> { - let files = await fs.readdir(dirPath); + const files = await fs.readdir(dirPath); await Promise.all(files.map(async file => { if ( (await fs.stat(dirPath + '/' + file)).isDirectory() ) { @@ -22,7 +22,7 @@ class Toolbox { })); return arrayOfFiles; - }; + } private async getTotalSize(directoryPath: string): Promise<number> { const arrayOfFiles = await this.getAllFiles(directoryPath); @@ -34,7 +34,7 @@ class Toolbox { } return totalSize; - }; + } get fs() { return { diff --git a/helpers/TypeScriptExtensions.ts b/helpers/TypeScriptExtensions.ts index c5093d8..fd730ad 100644 --- a/helpers/TypeScriptExtensions.ts +++ b/helpers/TypeScriptExtensions.ts @@ -1,6 +1,11 @@ declare global { + interface BigInt { + toJSON: () => string; + } + + interface Array<T> { - removeObjectDuplicates: (getProperty: (item: T) => any) => Array<T>; + removeObjectDuplicates: (getProperty: (item: T) => unknown) => Array<T>; } @@ -22,15 +27,15 @@ function registerAll() { } function registerBigIntJson() { - (BigInt.prototype as any).toJSON = function () { + BigInt.prototype.toJSON = function () { return this.toString(); }; } function registerArrayRemoveObjectDuplicates() { - Array.prototype.removeObjectDuplicates = function <T>(this: Array<T>, getProperty: (item: T) => any): Array<T> { + Array.prototype.removeObjectDuplicates = function <T>(this: Array<T>, getProperty: (item: T) => unknown): Array<T> { return this.reduce((accumulator: Array<T>, current: T) => { - if ( !accumulator.find((item: any) => getProperty(item) === getProperty(current)) ) { + if ( !accumulator.find((item: T) => getProperty(item) === getProperty(current)) ) { accumulator.push(current); } return accumulator; diff --git a/helpers/recursiveFilesStats/RecursiveFilesStats.ts b/helpers/recursiveFilesStats/RecursiveFilesStats.ts index 5a97fda..a571aaa 100644 --- a/helpers/recursiveFilesStats/RecursiveFilesStats.ts +++ b/helpers/recursiveFilesStats/RecursiveFilesStats.ts @@ -131,7 +131,7 @@ class RecursiveFilesStats { } return stat; - }; + } /** * Get ext diff --git a/managers/SharedGitlabManager.ts b/managers/SharedGitlabManager.ts index aca93d0..68ff699 100644 --- a/managers/SharedGitlabManager.ts +++ b/managers/SharedGitlabManager.ts @@ -11,20 +11,16 @@ class GitlabManager { } async getTokens(codeOrRefresh: string, isRefresh: boolean = false, clientSecret: string = ''): Promise<GitlabToken> { - try { - const response = await axios.post<GitlabToken>(SharedConfig.login.gitlab.url.token, { - client_id : SharedConfig.login.gitlab.client.id, - client_secret: clientSecret, - grant_type : isRefresh ? 'refresh_token' : 'authorization_code', - refresh_token: codeOrRefresh, - code : codeOrRefresh, - redirect_uri : SharedConfig.login.gitlab.url.redirect - }); - - return response.data; - } catch ( error ) { - throw error; - } + const response = await axios.post<GitlabToken>(SharedConfig.login.gitlab.url.token, { + client_id : SharedConfig.login.gitlab.client.id, + client_secret: clientSecret, + grant_type : isRefresh ? 'refresh_token' : 'authorization_code', + refresh_token: codeOrRefresh, + code : codeOrRefresh, + redirect_uri : SharedConfig.login.gitlab.url.redirect + }); + + return response.data; } async getRepositoryPipelines(repoId: number, branch: string = 'main'): Promise<Array<GitlabPipeline>> { diff --git a/types/Gitlab/GitlabRoute.ts b/types/Gitlab/GitlabRoute.ts index ec5f0d8..76c3a80 100644 --- a/types/Gitlab/GitlabRoute.ts +++ b/types/Gitlab/GitlabRoute.ts @@ -3,8 +3,8 @@ enum GitlabRoute { PROFILE_GET = '/user', USERS_GET = '/users', REPOSITORY_GET = '/projects/{{id}}', - REPOSITORY_CREATE = '/projects', - REPOSITORY_DELETE = '/projects/{{id}}', + REPOSITORY_CREATE = '/projects', // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values + REPOSITORY_DELETE = '/projects/{{id}}', // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values REPOSITORY_EDIT = '/projects/{{id}}', REPOSITORY_FORK = '/projects/{{id}}/fork', REPOSITORY_MEMBER_ADD = '/projects/{{id}}/members', -- GitLab