From 6f8a6776497f8c42b07e057a9068e7a1daefa40d Mon Sep 17 00:00:00 2001 From: drieJAC Date: Wed, 28 Aug 2024 14:45:45 +0100 Subject: [PATCH] Added a nodeScript which prompts the user for feedback (#1179) --- nodeScripts/temp/testUserFeedback.js | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 nodeScripts/temp/testUserFeedback.js diff --git a/nodeScripts/temp/testUserFeedback.js b/nodeScripts/temp/testUserFeedback.js new file mode 100644 index 000000000..b178e8d53 --- /dev/null +++ b/nodeScripts/temp/testUserFeedback.js @@ -0,0 +1,51 @@ + +/** + * This script demonstrates how to prompt the user for feedback when running a script. + * The script below checks if the user really wants to run this script. + * If they confirm then it runs and if not it's cancelled. + * + * EXAMPLE USAGE: + * ``` + * npm run nodeScript temp/testUserFeedback + * ``` + */ + 'use strict'; + +// Import necessary modules +import readline from 'readline'; + +// Create an interface for reading from the command line +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +// Function to prompt the user +const askConfirmation = () => { + return new Promise((resolve) => { + rl.question('Are you sure you want to run the script? (yes/no): ', (answer) => { + resolve(answer.trim().toLowerCase() === 'yes'); // Check if the answer is 'yes' + }); + }); +}; + +const main = async () => { + // Ask for confirmation + const confirmed = await askConfirmation(); + rl.close(); + + if (!confirmed) { + console.log('Script execution cancelled.'); + process.exit(0); // Exit the script + } + + // Continue with the rest of your script here + console.log('Running the script...'); + // Your script logic goes here +}; + +// Run the main function +main().catch((err) => { + console.error('Error:', err); + process.exit(1); +});