diff --git a/NodeApp/src/commander/CommanderApp.ts b/NodeApp/src/commander/CommanderApp.ts index 30e9cfe3ba8195af46113d33add1ecff63aa2550..0ac70a8639467fb24bc9deeef285e8ebb6b05df9 100644 --- a/NodeApp/src/commander/CommanderApp.ts +++ b/NodeApp/src/commander/CommanderApp.ts @@ -1,19 +1,20 @@ import { Command, Option } from 'commander'; -import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig.js'; import AssignmentCommand from './assignment/AssignmentCommand.js'; import ExerciseCommand from './exercise/ExerciseCommand.js'; import SharedConfig from '../shared/config/SharedConfig.js'; import boxen from 'boxen'; -import { stateConfigFile } from '../config/ConfigFiles.js'; import semver from 'semver/preload.js'; import { version } from '../config/Version.js'; -import Config from '../config/Config.js'; import CompletionCommand from './completion/CompletionCommand.js'; import AuthCommand from './auth/AuthCommand.js'; import SessionCommand from './auth/SessionCommand.js'; import UpgradeCommand from './UpgradeCommand.js'; import TextStyle from '../types/TextStyle.js'; import TagCommand from './tag/TagCommand'; +import ConfigFiles from '../config/ConfigFiles'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; +import Config from '../config/Config'; +import SettingsCommand from './settings/SettingsCommand'; class CommanderApp { @@ -33,7 +34,7 @@ class CommanderApp { } } - constructor() { + public async init() { this.program .name('dojo') .description('CLI of the Dojo application') @@ -95,8 +96,8 @@ ${ TextStyle.CODE(' dojo upgrade ') }`, { private informNewVersion() { if ( SharedConfig.production ) { - const latestDojoCliVersion = stateConfigFile.getParam('latestDojoCliVersion') as string | null || '0.0.0'; - const latestDojoCliVersionNotification = stateConfigFile.getParam('latestDojoCliVersionNotification') as number | null || 0; + const latestDojoCliVersion = ConfigFiles.stateConfigFile.getParam('latestDojoCliVersion') as string | null || '0.0.0'; + const latestDojoCliVersionNotification = ConfigFiles.stateConfigFile.getParam('latestDojoCliVersionNotification') as number | null || 0; if ( semver.lt(version as string, latestDojoCliVersion) && (new Date()).getTime() - latestDojoCliVersionNotification >= Config.versionUpdateInformationPeriodHours * 60 * 60 * 1000 ) { console.log(boxen(`The ${ latestDojoCliVersion } version of the DojoCLI is available. You can upgrade the DojoCLI by executing this command: @@ -109,7 +110,7 @@ ${ TextStyle.CODE(' dojo upgrade ') }`, { padding : 1, textAlignment : 'left' })); - stateConfigFile.setParam('latestDojoCliVersionNotification', (new Date()).getTime()); + ConfigFiles.stateConfigFile.setParam('latestDojoCliVersionNotification', (new Date()).getTime()); } } } diff --git a/NodeApp/src/commander/UpgradeCommand.ts b/NodeApp/src/commander/UpgradeCommand.ts index 18a96ab9716fb49cdb63286c643e3d65f533d79a..653fdc8051207a4f1a6e590d07e484b1d41ef6d8 100644 --- a/NodeApp/src/commander/UpgradeCommand.ts +++ b/NodeApp/src/commander/UpgradeCommand.ts @@ -1,9 +1,9 @@ import CommanderCommand from './CommanderCommand.js'; import ora from 'ora'; -import Config from '../config/Config.js'; import TextStyle from '../types/TextStyle.js'; import os from 'os'; import { spawn } from 'child_process'; +import Config from '../config/Config'; class UpgradeCommand extends CommanderCommand { @@ -18,7 +18,7 @@ class UpgradeCommand extends CommanderCommand { private displayInstructions(upgradeCommand: string, preAlpha: boolean) { upgradeCommand = TextStyle.CODE(` ${ upgradeCommand } `); - + console.log(TextStyle.BLOCK(`You can upgrade DojoCLI on ${ os.platform() === 'darwin' ? 'macOS' : 'Linux' }:`)); ora().start().info(`By executing this command: ${ upgradeCommand }`); console.log('or'); diff --git a/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts b/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts index 86a5bc585c2f68c833fb8b3618792da99fa9b439..d5cab1979c72b38b08b4ce6e6e0e2c64e7ca879a 100644 --- a/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts +++ b/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts @@ -6,7 +6,7 @@ import DojoBackendManager from '../../../managers/DojoBackendManager.js'; import Toolbox from '../../../shared/helpers/Toolbox.js'; import * as Gitlab from '@gitbeaker/rest'; import TextStyle from '../../../types/TextStyle.js'; -import GitlabManager from '../../../managers/GitlabManager.js'; +import Config from '../../../config/Config'; type CommandOptions = { name: string, template?: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean } @@ -35,7 +35,7 @@ class AssignmentCreateCommand extends CommanderCommand { await AccessesHelper.checkTeachingStaff(); - this.members = await GitlabManager.fetchMembers(options); + this.members = await Config.gitlabManager.fetchMembers(options); if ( !this.members ) { throw new Error(); } @@ -82,7 +82,7 @@ class AssignmentCreateCommand extends CommanderCommand { if ( options.clone ) { console.log(TextStyle.BLOCK('Please wait while we are cloning the repository...')); - await GitlabManager.cloneRepository(options.clone, this.assignment.gitlabCreationInfo.ssh_url_to_repo, undefined, true, 0); + await Config.gitlabManager.cloneRepository(options.clone, this.assignment.gitlabCreationInfo.ssh_url_to_repo, undefined, true, 0); } } diff --git a/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts b/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts index e2e9d67ef39b3eeab6189f28272ae9abee1ccc3c..d75bc6268e5334376c584d2706239ba3c42bbf82 100644 --- a/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts +++ b/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts @@ -1,6 +1,6 @@ import CommanderCommand from '../../CommanderCommand.js'; import SessionManager from '../../../managers/SessionManager.js'; -import GitlabManager from '../../../managers/GitlabManager.js'; +import Config from '../../../config/Config'; class AuthTestCommand extends CommanderCommand { @@ -14,7 +14,7 @@ class AuthTestCommand extends CommanderCommand { protected async commandAction(): Promise<void> { await SessionManager.testSession(); - await GitlabManager.testToken(); + await Config.gitlabManager.testToken(); } } diff --git a/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts b/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts index 650d1504f725c7db60d9cb5b45140838d21d5300..dd3d066619e899be647a5a267e74492058934250 100644 --- a/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts +++ b/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts @@ -1,12 +1,12 @@ import CommanderCommand from '../../CommanderCommand.js'; import ora from 'ora'; import DojoBackendManager from '../../../managers/DojoBackendManager.js'; -import Config from '../../../config/Config.js'; import Assignment from '../../../sharedByClients/models/Assignment.js'; import inquirer from 'inquirer'; import open from 'open'; import chalk from 'chalk'; import TextStyle from '../../../types/TextStyle.js'; +import Config from '../../../config/Config'; type CorrectionResume = { name: string, value: string } diff --git a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts index 3db1510582c5927fcf506ac6acc360f04548aa8c..a94277533e02a8528ee69e4951f7a827a1a86f2a 100644 --- a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts +++ b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts @@ -6,8 +6,8 @@ import Assignment from '../../../sharedByClients/models/Assignment.js'; import Exercise from '../../../sharedByClients/models/Exercise.js'; import * as Gitlab from '@gitbeaker/rest'; import TextStyle from '../../../types/TextStyle.js'; -import GitlabManager from '../../../managers/GitlabManager.js'; import inquirer from 'inquirer'; +import Config from '../../../config/Config'; type CommandOptions = { assignment: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean } @@ -35,7 +35,7 @@ class ExerciseCreateCommand extends CommanderCommand { await AccessesHelper.checkStudent(); - this.members = await GitlabManager.fetchMembers(options); + this.members = await Config.gitlabManager.fetchMembers(options); if ( !this.members ) { throw new Error(); } @@ -116,7 +116,7 @@ class ExerciseCreateCommand extends CommanderCommand { if ( options.clone ) { console.log(TextStyle.BLOCK('Please wait while we are cloning the repository...')); - await GitlabManager.cloneRepository(options.clone, this.exercise.gitlabCreationInfo.ssh_url_to_repo, `DojoExercise_${ this.exercise.assignmentName }`, true, 0); + await Config.gitlabManager.cloneRepository(options.clone, this.exercise.gitlabCreationInfo.ssh_url_to_repo, `DojoExercise_${ this.exercise.assignmentName }`, true, 0); } } diff --git a/NodeApp/src/helpers/AccessesHelper.ts b/NodeApp/src/helpers/AccessesHelper.ts index a07b7aa24a40229be5460f1126e0e99eb4455138..cc5f2a4e2a3a838f4d9af497f47df8012e6aea06 100644 --- a/NodeApp/src/helpers/AccessesHelper.ts +++ b/NodeApp/src/helpers/AccessesHelper.ts @@ -1,5 +1,5 @@ import SessionManager from '../managers/SessionManager.js'; -import GitlabManager from '../managers/GitlabManager.js'; +import Config from '../config/Config'; class AccessesHelper { @@ -10,7 +10,7 @@ class AccessesHelper { throw new Error(); } - if ( testGitlab && !(await GitlabManager.testToken(true)).every(result => result) ) { + if ( testGitlab && !(await Config.gitlabManager.testToken(true)).every(result => result) ) { throw new Error(); } } diff --git a/NodeApp/src/helpers/Dojo/ExerciseRunHelper.ts b/NodeApp/src/helpers/Dojo/ExerciseRunHelper.ts index 235bc74664d12d874fe945f5582f652852c30568..0503c66adf4d4230ce8f2600d4e1512d96c3ef18 100644 --- a/NodeApp/src/helpers/Dojo/ExerciseRunHelper.ts +++ b/NodeApp/src/helpers/Dojo/ExerciseRunHelper.ts @@ -5,7 +5,6 @@ import AssignmentFile from '../../shared/types/Dojo/Assign import ExerciseDockerCompose from '../../sharedByClients/helpers/Dojo/ExerciseDockerCompose.js'; import ExerciseResultsSanitizerAndValidator from '../../sharedByClients/helpers/Dojo/ExerciseResultsSanitizerAndValidator.js'; import fs from 'node:fs'; -import ClientsSharedConfig from '../../sharedByClients/config/ClientsSharedConfig.js'; import SharedAssignmentHelper from '../../shared/helpers/Dojo/SharedAssignmentHelper.js'; import path from 'path'; import ExerciseCheckerError from '../../shared/types/Dojo/ExerciseCheckerError.js'; @@ -15,6 +14,7 @@ import util from 'util'; import { exec } from 'child_process'; import SharedConfig from '../../shared/config/SharedConfig.js'; import TextStyle from '../../types/TextStyle.js'; +import ClientsSharedConfig from '../../sharedByClients/config/ClientsSharedConfig'; const execAsync = util.promisify(exec); diff --git a/NodeApp/src/helpers/GlobalHelper.ts b/NodeApp/src/helpers/GlobalHelper.ts index d79317fd487a954157d4287071daaf8b345f578a..dc9d2442afe43d4e9b16d077fa3c0dd17ad3abf5 100644 --- a/NodeApp/src/helpers/GlobalHelper.ts +++ b/NodeApp/src/helpers/GlobalHelper.ts @@ -1,10 +1,10 @@ import { Command, Option } from 'commander'; -import Config from '../config/Config.js'; import SessionManager from '../managers/SessionManager.js'; import TextStyle from '../types/TextStyle.js'; import ora from 'ora'; import DojoBackendManager from '../managers/DojoBackendManager.js'; import Assignment from '../sharedByClients/models/Assignment.js'; +import Config from '../config/Config'; class GlobalHelper { diff --git a/NodeApp/src/managers/GitlabManager.ts b/NodeApp/src/managers/GitlabManager.ts index 0e6c92e364f91e6cfe1be20508acd20dd2aee73b..a39a26e477e2f2c532411344fdbf11ac7e11ff2f 100644 --- a/NodeApp/src/managers/GitlabManager.ts +++ b/NodeApp/src/managers/GitlabManager.ts @@ -11,8 +11,8 @@ type getGitlabUser = (param: number | string) => Promise<UserSchema | undefined> class GitlabManager extends SharedGitlabManager { - constructor() { - super('', GlobalHelper.refreshGitlabTokenFunction.bind(GlobalHelper)); + constructor(public gitlabUrl: string, clientId?: string, urlRedirect?: string, urlToken?: string) { + super(gitlabUrl, '', clientId, urlRedirect, urlToken, GlobalHelper.refreshGitlabTokenFunction.bind(GlobalHelper)); } public async testToken(verbose: boolean = true): Promise<[ boolean, boolean ]> { @@ -213,4 +213,4 @@ class GitlabManager extends SharedGitlabManager { } -export default new GitlabManager(); +export default GitlabManager; diff --git a/NodeApp/src/managers/HttpManager.ts b/NodeApp/src/managers/HttpManager.ts index 760f19c1f1f0b4d12089bd6f9badd84e68eb424d..ce1bc53e8c7a792b9c1c5c179fe894eecb0af788 100644 --- a/NodeApp/src/managers/HttpManager.ts +++ b/NodeApp/src/managers/HttpManager.ts @@ -2,13 +2,13 @@ import axios, { AxiosError, AxiosRequestHeaders } from 'axios'; import SessionManager from './SessionManager.js'; import FormData from 'form-data'; import { StatusCodes } from 'http-status-codes'; -import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig.js'; import { version } from '../config/Version.js'; import DojoBackendResponse from '../shared/types/Dojo/DojoBackendResponse.js'; import DojoStatusCode from '../shared/types/Dojo/DojoStatusCode.js'; import boxen from 'boxen'; -import { stateConfigFile } from '../config/ConfigFiles.js'; import TextStyle from '../types/TextStyle.js'; +import ConfigFiles from '../config/ConfigFiles'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; class HttpManager { @@ -25,9 +25,7 @@ class HttpManager { config.headers = { ...config.headers, ...config.data.getHeaders() } as AxiosRequestHeaders; } - if ( config.url && (config.url.indexOf(ClientsSharedConfig.apiURL) !== -1) ) { - - + if ( config.url && config.url.indexOf(ClientsSharedConfig.apiURL) !== -1 ) { config.headers['Accept-Encoding'] = 'gzip'; if ( config.data && Object.keys(config.data as { [key: string]: unknown }).length > 0 ) { @@ -106,11 +104,11 @@ class HttpManager { if ( response.headers['dojocli-latest-version'] ) { const latestDojoCliVersion = response.headers['dojocli-latest-version']; - const storedLatestDojoCliVersion = stateConfigFile.getParam('latestDojoCliVersion') as string | null || '0.0.0'; + const storedLatestDojoCliVersion = ConfigFiles.stateConfigFile.getParam('latestDojoCliVersion') as string | null || '0.0.0'; if ( latestDojoCliVersion !== storedLatestDojoCliVersion ) { - stateConfigFile.setParam('latestDojoCliVersion', latestDojoCliVersion); - stateConfigFile.setParam('latestDojoCliVersionNotification', 0); + ConfigFiles.stateConfigFile.setParam('latestDojoCliVersion', latestDojoCliVersion); + ConfigFiles.stateConfigFile.setParam('latestDojoCliVersionNotification', 0); } } diff --git a/NodeApp/src/managers/SessionManager.ts b/NodeApp/src/managers/SessionManager.ts index 7dd62ccf351bf0cf66acc08f17f907c7a6adfcfb..cffc6b979e3f1aa2e43f64a2944334461ea83865 100644 --- a/NodeApp/src/managers/SessionManager.ts +++ b/NodeApp/src/managers/SessionManager.ts @@ -7,21 +7,19 @@ import ora from 'ora'; import Permissions from '../types/Permissions.js'; import ApiRoute from '../sharedByClients/types/Dojo/ApiRoute.js'; import DojoBackendManager from './DojoBackendManager.js'; -import Config from '../config/Config.js'; -import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig.js'; import DojoGitlabCredentials from '../sharedByClients/types/Dojo/DojoGitlabCredentials.js'; import * as http from 'http'; import EventEmitter from 'events'; -import SharedConfig from '../shared/config/SharedConfig.js'; import chalk from 'chalk'; import inquirer from 'inquirer'; import GitlabToken from '../shared/types/Gitlab/GitlabToken.js'; import open from 'open'; -import { sessionConfigFile } from '../config/ConfigFiles.js'; import TextStyle from '../types/TextStyle.js'; import DojoBackendHelper from '../sharedByClients/helpers/Dojo/DojoBackendHelper.js'; -import GitlabManager from './GitlabManager.js'; import { StatusCodes } from 'http-status-codes'; +import ConfigFiles from '../config/ConfigFiles'; +import Config from '../config/Config'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; class LoginServer { @@ -82,7 +80,7 @@ class SessionManager { } get apiToken(): string { - const apisToken = sessionConfigFile.getParam(LocalConfigKeys.APIS_TOKEN) as null | { [key: string]: string }; + const apisToken = ConfigFiles.sessionConfigFile.getParam(LocalConfigKeys.APIS_TOKEN) as null | { [key: string]: string }; if ( apisToken !== null && ClientsSharedConfig.apiURL in apisToken ) { return apisToken[ClientsSharedConfig.apiURL]; @@ -92,12 +90,12 @@ class SessionManager { } set apiToken(token: string) { - let apisToken = sessionConfigFile.getParam(LocalConfigKeys.APIS_TOKEN) as null | { [key: string]: string }; + let apisToken = ConfigFiles.sessionConfigFile.getParam(LocalConfigKeys.APIS_TOKEN) as null | { [key: string]: string }; if ( apisToken === null ) { apisToken = {}; } apisToken[ClientsSharedConfig.apiURL] = token; - sessionConfigFile.setParam(LocalConfigKeys.APIS_TOKEN, apisToken); + ConfigFiles.sessionConfigFile.setParam(LocalConfigKeys.APIS_TOKEN, apisToken); try { const payload = jwt.decode(token); @@ -111,14 +109,14 @@ class SessionManager { } get gitlabCredentials(): DojoGitlabCredentials { - return sessionConfigFile.getParam(LocalConfigKeys.GITLAB) as DojoGitlabCredentials; + return ConfigFiles.sessionConfigFile.getParam(LocalConfigKeys.GITLAB) as DojoGitlabCredentials; } set gitlabCredentials(credentials: DojoGitlabCredentials) { - sessionConfigFile.setParam(LocalConfigKeys.GITLAB, credentials); + ConfigFiles.sessionConfigFile.setParam(LocalConfigKeys.GITLAB, credentials); if ( credentials.accessToken ) { - GitlabManager.setToken(credentials.accessToken); + Config.gitlabManager.setToken(credentials.accessToken); } } @@ -127,7 +125,7 @@ class SessionManager { console.log(`${ indent }Please open the following URL in your web browser and accept to give the requested permissions to Dojo:`); console.log(TextStyle.URL(`${ indent }${ Config.login.gitlab.url.code }`)); console.log(`${ indent }Then, copy the code at the end of the redirected url and paste it bellow.`); - console.log(`${ indent }Example of url (here the code is 123456): ${ TextStyle.URL(SharedConfig.login.gitlab.url.redirect + '?code=') }${ chalk.green('123456') }`); + console.log(`${ indent }Example of url (here the code is 123456): ${ TextStyle.URL(ClientsSharedConfig.login.gitlab.url.redirect + '?code=') }${ chalk.green('123456') }`); return (await inquirer.prompt({ type : 'password', name : 'code', @@ -194,7 +192,7 @@ class SessionManager { throw error; } - ora(`Login with Gitlab (${ SharedConfig.gitlab.URL }):`).start().info(); + ora(`Login with Gitlab (${ ClientsSharedConfig.gitlab.URL }):`).start().info(); let gitlabCode: string; if ( !headless ) { @@ -209,7 +207,7 @@ class SessionManager { }).start(); let gitlabTokens: GitlabToken; try { - gitlabTokens = await GitlabManager.getTokens(gitlabCode); + gitlabTokens = await Config.gitlabManager.getTokens(gitlabCode); this.gitlabCredentials = { refreshToken: gitlabTokens.refresh_token, accessToken : gitlabTokens.access_token @@ -220,7 +218,7 @@ class SessionManager { throw error; } - const isGitlabTokensValid = (await GitlabManager.testToken()).every(value => value); + const isGitlabTokensValid = (await Config.gitlabManager.testToken()).every(value => value); if ( !isGitlabTokensValid ) { throw new Error('Gitlab tokens are invalid'); }