From ec244e221c91c525a474e46233fbbd7d1683caeb Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 5 Aug 2020 20:05:26 -0400 Subject: [PATCH] Correct user confirmation function to properly block program --- package.json | 3 ++- src/helper_functions.js | 36 ++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index c7af6d5..f70d9e7 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ }, "dependencies": { "chalk": "^4.1.0", - "mysql2": "^2.1.0" + "mysql2": "^2.1.0", + "readline-sync": "^1.4.10" } } diff --git a/src/helper_functions.js b/src/helper_functions.js index 77a3d6f..efa0fbc 100644 --- a/src/helper_functions.js +++ b/src/helper_functions.js @@ -4,7 +4,7 @@ // System Imports. const chalk = require('chalk'); - const readline = require('readline'); + const readline = require('readline-sync'); // User Imports. @@ -48,26 +48,26 @@ function getCurrentDate() { * * "Readline" logic from https://stackoverflow.com/a/50890409 */ -async function userConfirmation() { - // Create "readline" instance to get user input. - const read = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); +function userConfirmation() { // Get user input. - return new Promise(resolve => read.question(' Is this okay? ' + chalk.blue('[ Yes | No ]\n'), (user_input) => { - // Close "readline" instance to save processing power. - read.close(); - - // Handle based on user response. - user_input = user_input.toLowerCase(); - if (user_input == 'yes' || user_input == 'ye' || user_input == 'y') { - resolve(true); - } else { - resolve(false); + try { + var user_input = readline.question('Is this okay? {}\n'.format(chalk.blue('[ Yes | No ]'))); + } catch (err) { + // Ignore "timeout" error. Display all others. + if (err.code != 'ETIMEDOUT') { + console.log(err); + throw err; } - })); + } + + // Handle based on user response. + user_input = user_input.toLowerCase(); + if (user_input == 'yes' || user_input == 'ye' || user_input == 'y') { + return true; + } else { + return false; + } } -- GitLab