Created
          May 4, 2015 10:49 
        
      - 
      
- 
        Save sandcastle/87eee0f66fdb77c95985 to your computer and use it in GitHub Desktop. 
    A basic node.js REPL written in ES6.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import readline from 'readline'; | |
| import util from 'util'; | |
| import chalk from 'chalk'; | |
| let terminal = readline.createInterface(process.stdin, process.stdout); | |
| export default function cli(commands){ | |
| return (new CLI(commands)).repl(); | |
| } | |
| class CLI { | |
| constructor(commands){ | |
| this.commands = commands || new Map(); | |
| } | |
| /** | |
| * Creates a REPL interface for the user to interact with. | |
| */ | |
| repl(){ | |
| terminal.setPrompt('cli> '); | |
| terminal.prompt(); | |
| terminal.on('line', (line) => { | |
| let normalized = line.trim(); | |
| let command = this.findCommand(normalized); | |
| if (!command){ | |
| this.showUsage(normalized); | |
| } | |
| else{ | |
| command.exec(normalized.split(' ')); | |
| } | |
| terminal.prompt(); | |
| }); | |
| terminal.on('close', () => { | |
| process.exit(0); | |
| }); | |
| } | |
| /** | |
| * Attempts to find the specified command and returns null if it | |
| * cannot be found. | |
| * @param {String} command The command to find. | |
| * @returns {Object} The command or null. | |
| */ | |
| findCommand(command){ | |
| let normalized = command.trim().toLowerCase(); | |
| if (this.commands.has(normalized)){ | |
| return this.commands.get(normalized); | |
| } | |
| return null; | |
| } | |
| /** | |
| * Shows the usage of the application based on the registered | |
| * commands found in the CLI registry. | |
| */ | |
| showUsage(invalidCommand){ | |
| terminal.write(util.format('%s\n\n', chalk.blue('== THE CLI ==')); | |
| for (let i =0; i < this.commands.length; i++){ | |
| terminal.write(util.format('--%s ', chalk.gray(this.commands[i].name) ||'')); | |
| } | |
| terminal.write('\n'); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment