import { Command, Option } from 'commander'; import SessionCommand from './session/SessionCommand'; import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; import AssignmentCommand from './assignment/AssignmentCommand'; import ExerciseCommand from './exercise/ExerciseCommand'; import SharedConfig from '../shared/config/SharedConfig'; import boxen from 'boxen'; import { stateConfigFile } from '../config/ConfigFiles'; import semver from 'semver/preload'; import { version } from '../config/Version'; import Config from '../config/Config'; import {getBashCompletion, getFishCompletion} from '../helpers/AutoCompletionHelper'; class CommanderApp { program: Command = new Command(); constructor() { this.program .name('dojo') .description('CLI of the Dojo application') .version('{{VERSION}}') .showHelpAfterError() .configureHelp({ showGlobalOptions: true, sortOptions : true, sortSubcommands : true }) .option('-H, --host <string>', 'override the Dojo API endpoint', ClientsSharedConfig.apiURL) .addOption(new Option('--debug').hideHelp()) .hook('preAction', () => { this.warnDevelopmentVersion(); }).hook('postAction', () => { this.informNewVersion(); }); this.program.on('option:host', () => { ClientsSharedConfig.apiURL = this.program.opts().host; }); this.program.on('option:debug', () => { SharedConfig.debug = true; }); this.registerCommands(); getBashCompletion(this.program, "bash_completion.sh") getFishCompletion(this.program, "dojo.fish") this.program.parse(); } private warnDevelopmentVersion() { if ( !SharedConfig.production ) { console.log(boxen(`This is a development (unstable) version of the DojoCLI. If you want to use the stable version, please install the package from the following url: https://gitedu.hesge.ch/dojo_project/projects/ui/dojocli/-/releases/Latest`, { title : 'Warning', titleAlignment: 'center', borderColor : 'red', borderStyle : 'bold', margin : 1, padding : 1, textAlignment : 'left' })); } } 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; if ( semver.lt(version, latestDojoCliVersion) ) { if ( (new Date()).getTime() - latestDojoCliVersionNotification >= Config.versionUpdateInformationPeriodHours * 60 * 60 * 1000 ) { console.log(boxen(`The ${ latestDojoCliVersion } version of the DojoCLI is available: https://gitedu.hesge.ch/dojo_project/projects/ui/dojocli/-/releases/Latest`, { title : 'Information', titleAlignment: 'center', borderColor : 'blue', borderStyle : 'bold', margin : 1, padding : 1, textAlignment : 'left' })); stateConfigFile.setParam('latestDojoCliVersionNotification', (new Date()).getTime()); } } } } private registerCommands() { SessionCommand.registerOnCommand(this.program); AssignmentCommand.registerOnCommand(this.program); ExerciseCommand.registerOnCommand(this.program); } } export default CommanderApp;