I want to write something like this
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
r1.setprompt('press 1 to load fle, press 2 to exit');
rl.prompt();
rl.on('line', (line) => {
switch(line.trim()) {
case '1':
r1.setPrompt('Enter file name to add');
r1.prompt()
r1.on('line',(filename)=>{
settings = JSON.parse(fs.readFileSync(filename, 'utf8'));
});
break;
case '2':
console.log('Exit');
process.exit(0);
break;
default:
console.log(`Invalid command '${line.trim()}'`);
break;
}
rl.prompt();
}).on('close', () => {
process.exit(0);
});
I want the menu to be flashed again and again until user selects option 2. what I've written is wrong ,because r1 is undefined in the switch case. Is there any other correct way to achieve it.