Skip to content

Instantly share code, notes, and snippets.

@akishore15
Created February 18, 2025 00:35
Show Gist options
  • Save akishore15/2ecc34f5bae61182fccd21eb9ff94812 to your computer and use it in GitHub Desktop.
Save akishore15/2ecc34f5bae61182fccd21eb9ff94812 to your computer and use it in GitHub Desktop.
Shell
const readline = require('readline');
const { execSync } = require('child_process');
// Create an interface for reading input from the command line
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'shell> '
});
// Function to execute commands
const executeCommand = (command) => {
try {
const output = execSync(command, { stdio: 'pipe' }).toString();
console.log(output);
} catch (error) {
console.error(`Error: ${error.message}`);
}
};
// Display the prompt and wait for user input
rl.prompt();
rl.on('line', (line) => {
const command = line.trim();
if (command.toLowerCase() === 'exit') {
rl.close();
} else {
executeCommand(command);
rl.prompt();
}
}).on('close', () => {
console.log('Exiting shell...');
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment