From a488fc6dbbad788db8bac62271d9053d40190635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me> Date: Wed, 21 Feb 2024 21:13:22 +0100 Subject: [PATCH] Commands => Add exercise correction command --- .../src/commander/exercise/ExerciseCommand.ts | 8 +- .../subcommands/ExerciseCorrectionCommand.ts | 76 +++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts diff --git a/NodeApp/src/commander/exercise/ExerciseCommand.ts b/NodeApp/src/commander/exercise/ExerciseCommand.ts index a475d4f..2a93b7a 100644 --- a/NodeApp/src/commander/exercise/ExerciseCommand.ts +++ b/NodeApp/src/commander/exercise/ExerciseCommand.ts @@ -1,6 +1,7 @@ -import CommanderCommand from '../CommanderCommand'; -import ExerciseCreateCommand from './subcommands/ExerciseCreateCommand'; -import ExerciseRunCommand from './subcommands/ExerciseRunCommand'; +import CommanderCommand from '../CommanderCommand'; +import ExerciseCreateCommand from './subcommands/ExerciseCreateCommand'; +import ExerciseRunCommand from './subcommands/ExerciseRunCommand'; +import ExerciseCorrectionCommand from './subcommands/ExerciseCorrectionCommand'; class ExerciseCommand extends CommanderCommand { @@ -14,6 +15,7 @@ class ExerciseCommand extends CommanderCommand { protected defineSubCommands() { ExerciseCreateCommand.registerOnCommand(this.command); ExerciseRunCommand.registerOnCommand(this.command); + ExerciseCorrectionCommand.registerOnCommand(this.command); } protected async commandAction(): Promise<void> { } diff --git a/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts b/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts new file mode 100644 index 0000000..0704ebf --- /dev/null +++ b/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts @@ -0,0 +1,76 @@ +import CommanderCommand from '../../CommanderCommand'; +import ora from 'ora'; +import DojoBackendManager from '../../../managers/DojoBackendManager'; +import Config from '../../../config/Config'; +import Assignment from '../../../sharedByClients/models/Assignment'; +import inquirer from 'inquirer'; +import open from 'open'; +import chalk from 'chalk'; + + +type CorrectionResume = { name: string, value: string } + + +class ExerciseCorrectionCommand extends CommanderCommand { + protected commandName: string = 'correction'; + + protected defineCommand() { + this.command + .description('link an exercise repo as a correction for an assignment') + .requiredOption('-a, --assignment <string>', 'id or url of the assignment of the correction') + .action(this.commandAction.bind(this)); + } + + protected async commandAction(options: { assignment: string }): Promise<void> { + const assignmentGetSpinner: ora.Ora = ora('Fetching assignment data').start(); + const assignment = await DojoBackendManager.getAssignment(options.assignment); + if ( !assignment ) { + assignmentGetSpinner.fail(`The assignment doesn't exists`); + return; + } + + if ( assignment.corrections && assignment.corrections.length > 0 ) { + Config.interactiveMode ? await this.showCorrectionsInteractive(assignment, assignmentGetSpinner) : this.showCorrections(assignment, assignmentGetSpinner); + } else { + assignmentGetSpinner.fail(`The assignment doesn't have any corrections yet`); + return; + } + } + + private getCorrections(assignment: Assignment): Array<CorrectionResume> { + return assignment.corrections.map(correction => { + return { + name : correction.name.replace(correction.assignmentName, '').split('-')[2].trim(), + value: correction.correctionCommit!.web_url?.replace('/commit/', '/tree/') ?? '' + }; + }); + } + + private showCorrections(assignment: Assignment, spinner: ora.Ora) { + spinner.succeed(`Here are corrections of the assignment '${ assignment.name }':`); + + this.getCorrections(assignment).forEach(correction => { + console.log(chalk.green(`- ${ correction.name }`)); + console.log(` ${ correction.value }`); + }); + } + + private async showCorrectionsInteractive(assignment: Assignment, spinner: ora.Ora) { + spinner.stop(); + + const correctionUrl: string = (await inquirer.prompt({ + name : 'correctionUrl', + message: 'Which correction do you want to consult? (use arrow keys then enter)', + type : 'list', + choices: this.getCorrections(assignment), + default: false + })).correctionUrl; + + console.log(chalk.green(correctionUrl)); + + open(correctionUrl).then(); + } +} + + +export default new ExerciseCorrectionCommand(); \ No newline at end of file -- GitLab