Skip to content
Snippets Groups Projects
Select Git revision
  • 706ea01362de3ba126039580c2109225a8d1a084
  • main default protected
  • v3.3.0
  • add_link_in_readme
  • v3.2.1
  • v3.2.0
  • v3.1.2
  • 3.3.0-dev
  • 3.2.0
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
  • 3.0.0
  • 2.2.0
  • 2.1.0
  • 2.0.0
  • v1.0.1
18 results

CommanderApp.ts

Blame
  • Forked from Dojo Project (HES-SO) / Projects / UI / DojoCLI
    Source project has a limited visibility.
    CommanderApp.ts 3.97 KiB
    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';
    
    
    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)
            .option('-I, --interactive', 'show interactive interface when available', Config.interactiveMode)
            .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:interactive', () => {
                Config.interactiveMode = this.program.opts().interactive;
            });
    
            this.program.on('option:debug', () => {
                SharedConfig.debug = true;
            });
    
            this.registerCommands();
    
            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;