Skip to content

Commit

Permalink
Add getCommandLineInput function for user prompts in repairUsers script
Browse files Browse the repository at this point in the history
  • Loading branch information
renatodellosso committed Feb 1, 2025
1 parent 5d9346c commit 6e66364
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,26 @@ export async function repairUser(

return user as User;
}

/**
* Intended for use in scripts
*
* @param prompt What to ask the user
* @returns The user's input
*/
export function getCommandLineInput(prompt: string) {
return new Promise<string>((resolve) => {
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});

readline.question(
prompt + " ",
(answer: string) => {
readline.close();
resolve(answer);
},
);
});
}
7 changes: 6 additions & 1 deletion scripts/repairUsers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CollectionId from "@/lib/client/CollectionId";
import { getDatabase } from "@/lib/MongoDB";
import { repairUser } from "@/lib/Utils";
import { getCommandLineInput, repairUser } from "@/lib/Utils";

async function repairUsers() {
console.log("Getting database...");
Expand All @@ -22,6 +22,11 @@ async function repairUsers() {

console.log(`Found ${users.length} incomplete users`);

if (await getCommandLineInput("Do you want to continue? (yes/no)") !== "yes") {
console.log("Exiting...");
process.exit(0);
}

for (let i = 0; i < users.length; i++) {
const user = users[i];
console.log(`Repairing user ${user._id} (${i + 1}/${users.length})`);
Expand Down

0 comments on commit 6e66364

Please sign in to comment.