-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a nodeScript which prompts the user for feedback (#1179)
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |