Skip to content

Commit 6e66364

Browse files
Add getCommandLineInput function for user prompts in repairUsers script
1 parent 5d9346c commit 6e66364

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/Utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,26 @@ export async function repairUser(
142142

143143
return user as User;
144144
}
145+
146+
/**
147+
* Intended for use in scripts
148+
*
149+
* @param prompt What to ask the user
150+
* @returns The user's input
151+
*/
152+
export function getCommandLineInput(prompt: string) {
153+
return new Promise<string>((resolve) => {
154+
const readline = require("readline").createInterface({
155+
input: process.stdin,
156+
output: process.stdout,
157+
});
158+
159+
readline.question(
160+
prompt + " ",
161+
(answer: string) => {
162+
readline.close();
163+
resolve(answer);
164+
},
165+
);
166+
});
167+
}

scripts/repairUsers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import CollectionId from "@/lib/client/CollectionId";
22
import { getDatabase } from "@/lib/MongoDB";
3-
import { repairUser } from "@/lib/Utils";
3+
import { getCommandLineInput, repairUser } from "@/lib/Utils";
44

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

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

25+
if (await getCommandLineInput("Do you want to continue? (yes/no)") !== "yes") {
26+
console.log("Exiting...");
27+
process.exit(0);
28+
}
29+
2530
for (let i = 0; i < users.length; i++) {
2631
const user = users[i];
2732
console.log(`Repairing user ${user._id} (${i + 1}/${users.length})`);

0 commit comments

Comments
 (0)