Skip to content
Snippets Groups Projects
Commit a488fc6d authored by michael.minelli's avatar michael.minelli
Browse files

Commands => Add exercise correction command

parent 706ea013
No related branches found
No related tags found
No related merge requests found
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> { }
......
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment