bin/cli.js
/**
   * Handle the mMeddle CLI on a CliConsole.
   * @param {Commander} program Command line options for the program
   */
Handle the mMeddle CLI on a CliConsole.
@param {Commander} program Command line options for the program
Handle the mMeddle CLI on a CliConsole . @param { Commander } program Command line options for the program
function _runCli (program) {
    mm.log.setCliConsole(mConsole);
    mConsole.clearScreen();
    mm.log('----- mMeddle CLI v{0} - {1} -----', mm.version, mm.config.serverStartTime);
    if (mm.config.welcomeMessage) mm.log(mm.config.welcomeMessage);
    if (mm.config.mMeddleTS) {
      mm.log('- mMeddle.js:', mm.config.mMeddleTS);    
    }
    if (mm.config.inBrowser) {
      mm.loggers.debugLogger.removeDestination('*');
    }
    mm.log.debug('----- CLI Configuration -----\n', mm.config);
    var host = mm.configHost();
    var appName = mm.config.inNode ? 'CLI' : 'WebCLI';
    var cs = new ClientSession(appName);
    var mmc = new MMeddleClient(host, cs);
    var cliCmds = new CliCommands(mConsole, cs);
    cs.bindClient(mmc);
    cs.loadLocalWorkspace();
    cs.loadLocalUser(mConsole);
    if (!cs.user.isAnonymous()) {
      mm.log('- Hello {0} {1}.', cs.user.firstName, cs.user.lastName);    
    }
    if (cs.ws.name) {
      mm.log('- Loaded Local Workspace [{0}]: {1} variables', 
          cs.ws.name, cs.ws.varsCount);
    }
    else {
      mm.log('- Loaded Local Workspace: {0} variables', cs.ws.varsCount);
    }
    mmc.connectWorkspace()
    .then(function() {
      mm.log('- Connected to server {0}', host);
      cs.emitLogMessage('CLI connected!');
    })
    cliCmds.rootCommandSet.setDefaultHandler(evaluateExpression);
    mConsole.onClose(function() {
      mm.log('- CLI Closed.');
      mm.log();
      process.exit(0);
    });
    function readAnother() {
      mConsole.readLine('->')
      .then(function (line) {
        return handleLine(line)
        .then(function () {
          readAnother(); 
        });
      },
      function(e) {
        console.log('+++++++mConsole readline failed', e.stack);
      });
    }
    readAnother();
    function evaluateExpression(context) {
      var line = context.text;
      var expr = line.trim();
      if (expr) {
        if ((_.startsWith(expr, '``') ||
             _.startsWith(expr, '`>')) &&
             cs.isAdmin()) {
          return cs.doCommand(expr)
          .then(function (rs) {
            mm.log('- Cmd Result:', rs.content);
          });
        }
        var rv = cs.ws.evaluate(expr);
        if (rv instanceof Error) {
          throw (rv);
        }
        else {
          mm.log('- Result:', rv);
        }
      }
      return true;
    }
    function handleLine(line) {
      return cliCmds.rootCommandSet.doCmd(line, cs)
      .then(function (d) {
        if (d instanceof Error) {
          mm.log('Good Command error??? :', d);
        }
        return true;
      },
      function (e) {
        mm.log.error('Failed:', e.message);
        return e;
      });
    } 
  }
function _runCli ( program ) { mm . log . setCliConsole ( mConsole ) ; mConsole . clearScreen ( ) ; mm . log ( '----- mMeddle CLI v{0} - {1} -----' , mm . version , mm . config . serverStartTime ) ; if ( mm . config . welcomeMessage ) mm . log ( mm . config . welcomeMessage ) ; if ( mm . config . mMeddleTS ) { mm . log ( '- mMeddle.js:' , mm . config . mMeddleTS ) ; } if ( mm . config . inBrowser ) { mm . loggers . debugLogger . removeDestination ( '*' ) ; } mm . log . debug ( '----- CLI Configuration -----\n' , mm . config ) ; var host = mm . configHost ( ) ; var appName = mm . config . inNode ? 'CLI' : 'WebCLI' ; var cs = new ClientSession ( appName ) ; var mmc = new MMeddleClient ( host , cs ) ; var cliCmds = new CliCommands ( mConsole , cs ) ; cs . bindClient ( mmc ) ; cs . loadLocalWorkspace ( ) ; cs . loadLocalUser ( mConsole ) ; if ( ! cs . user . isAnonymous ( ) ) { mm . log ( '- Hello {0} {1}.' , cs . user . firstName , cs . user . lastName ) ; } if ( cs . ws . name ) { mm . log ( '- Loaded Local Workspace [{0}]: {1} variables' , cs . ws . name , cs . ws . varsCount ) ; } else { mm . log ( '- Loaded Local Workspace: {0} variables' , cs . ws . varsCount ) ; } mmc . connectWorkspace ( ) . then ( function ( ) { mm . log ( '- Connected to server {0}' , host ) ; cs . emitLogMessage ( 'CLI connected!' ) ; } ) cliCmds . rootCommandSet . setDefaultHandler ( evaluateExpression ) ; mConsole . onClose ( function ( ) { mm . log ( '- CLI Closed.' ) ; mm . log ( ) ; process . exit ( 0 ) ; } ) ; function readAnother ( ) { mConsole . readLine ( '->' ) . then ( function ( line ) { return handleLine ( line ) . then ( function ( ) { readAnother ( ) ; } ) ; } , function ( e ) { console . log ( '+++++++mConsole readline failed' , e . stack ) ; } ) ; } readAnother ( ) ; function evaluateExpression ( context ) { var line = context . text ; var expr = line . trim ( ) ; if ( expr ) { if ( ( _ . startsWith ( expr , '``' ) || _ . startsWith ( expr , '`>' ) ) && cs . isAdmin ( ) ) { return cs . doCommand ( expr ) . then ( function ( rs ) { mm . log ( '- Cmd Result:' , rs . content ) ; } ) ; } var rv = cs . ws . evaluate ( expr ) ; if ( rv instanceof Error ) { throw ( rv ) ; } else { mm . log ( '- Result:' , rv ) ; } } return true ; } function handleLine ( line ) { return cliCmds . rootCommandSet . doCmd ( line , cs ) . then ( function ( d ) { if ( d instanceof Error ) { mm . log ( 'Good Command error??? :' , d ) ; } return true ; } , function ( e ) { mm . log . error ( 'Failed:' , e . message ) ; return e ; } ) ; } }
Handle the mMeddle CLI on a CliConsole.
Handle the mMeddle CLI on a CliConsole .
// Sets up a log file that receives timestamped log messages from the // various logger types. // Not actually recursion. //mm.log('evaluateExpression(context)', context); // evaluate expression //try {       //mm.log('---------------- [', expr, ']'); //} catch (e) { mm.log('eeeeeeeeeeeeeeeeeeeeee', e.stack) } //mm.log.error('Command failure:', e.stack); // End of handler.