Get your own Node server
const readline = require('readline');
const fs = require('fs');
const path = require('path');

// History file path
const historyFile = path.join(__dirname, '.command_history');

// Load command history if it exists
let commandHistory = [];
try {
  if (fs.existsSync(historyFile)) {
    commandHistory = fs.readFileSync(historyFile, 'utf8')
      .split('\n')
      .filter(cmd => cmd.trim());
  }
} catch (err) {
  console.error('Error loading history:', err.message);
}

// Create the interface with custom configuration
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: 'cli> ',
  historySize: 100,
  history: commandHistory
});

// Available commands
const commands = {
  help: () => {
    console.log('\nAvailable commands:');
    console.log('  help     - Show this help message');
    console.log('  hello    - Say hello');
    console.log('  date     - Show current date and time');
    console.log('  clear    - Clear the console');
    console.log('  exit     - Exit the CLI');
    rl.prompt();
  },
  hello: () => {
    console.log('Hello, world!');
    rl.prompt();
  },
  date: () => {
    console.log(new Date().toLocaleString());
    rl.prompt();
  },
  clear: () => {
    process.stdout.write('\x1Bc');
    rl.prompt();
  },
  exit: () => {
    // Save command history to file
    try {
      fs.writeFileSync(historyFile, rl.history.join('\n'));
      console.log(`Command history saved to ${historyFile}`);
    } catch (err) {
      console.error('Error saving history:', err.message);
    }
    
    console.log('Goodbye!');
    rl.close();
  }
};

// Display welcome message
console.log('Simple CLI Example');
console.log('Type "help" for available commands');

// Display the prompt
rl.prompt();

// Handle input
rl.on('line', (line) => {
  const input = line.trim();
  
  if (input === '') {
    rl.prompt();
    return;
  }
  
  const command = input.toLowerCase();
  
  if (commands[command]) {
    commands[command]();
  } else {
    console.log(`Command not found: ${input}`);
    console.log('Type "help" for available commands');
    rl.prompt();
  }
}).on('close', () => {
  process.exit(0);
});

// Handle Ctrl+C (SIGINT)
rl.on('SIGINT', () => {
  rl.question('Are you sure you want to exit? (y/n) ', (answer) => {
    if (answer.toLowerCase() === 'y') {
      commands.exit();
    } else {
      rl.prompt();
    }
  });
});

              
Simple CLI Example
Type "help" for available commands
cli> help

Available commands:
  help     - Show this help message
  hello    - Say hello
  date     - Show current date and time
  clear    - Clear the console
  exit     - Exit the CLI
cli> hello
Hello, world!
cli> date
6/18/2025, 8:34:55 AM
cli> exit
Goodbye!