Skip to content

Commit dd41c8c

Browse files
authored
✨ Feat: Add warning message to seed command (#534)
* 🧹 Chore: Add Node version to package.json * ✨ Feat: Add seed CLI command * ✨ Feat: Add warning message to seed command
1 parent 546e295 commit dd41c8c

File tree

5 files changed

+131
-2
lines changed

5 files changed

+131
-2
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"private": true,
77
"browserslist": "last 2 Chrome versions",
88
"main": "",
9+
"engines": {
10+
"node": ">=18.18.0"
11+
},
912
"lint-staged": {
1013
"**/*": "prettier --write --ignore-unknown"
1114
},

packages/core/src/util/test/ccal.event.factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dayjs from "dayjs";
22
import timezone from "dayjs/plugin/timezone";
33
import { ObjectId } from "mongodb";
4-
import { faker } from "@faker-js/faker/.";
4+
import { faker } from "@faker-js/faker";
55
import { Origin, Priorities } from "@core/constants/core.constants";
66
import {
77
Schema_Event,

packages/scripts/src/cli.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { CliValidator } from "./cli.validator";
66
import { runBuild } from "./commands/build";
77
import { startDeleteFlow } from "./commands/delete";
88
import { inviteWaitlist } from "./commands/invite";
9+
import { runSeed } from "./commands/seed";
910
import { ALL_PACKAGES, CATEGORY_VM } from "./common/cli.constants";
1011

1112
class CompassCli {
@@ -38,6 +39,11 @@ class CompassCli {
3839
await inviteWaitlist();
3940
break;
4041
}
42+
case cmd === "seed": {
43+
this.validator.validateSeed(options);
44+
await runSeed(user as string, force);
45+
break;
46+
}
4147
default:
4248
this.validator.exitHelpfully(
4349
"root",
@@ -76,6 +82,14 @@ class CompassCli {
7682
);
7783

7884
program.command("invite").description("invite users from the waitlist");
85+
86+
program
87+
.command("seed")
88+
.description("seed the database with events")
89+
.option(
90+
"-u, --user [id | email]",
91+
"specify which user to seed events for",
92+
);
7993
return program;
8094
}
8195
}

packages/scripts/src/cli.validator.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export class CliValidator {
1717
this.program = program;
1818
}
1919

20-
public exitHelpfully(cmd: "root" | "build" | "delete", msg?: string) {
20+
public exitHelpfully(
21+
cmd: "root" | "build" | "delete" | "seed",
22+
msg?: string,
23+
) {
2124
msg && log.error(msg);
2225

2326
if (cmd === "root") {
@@ -62,6 +65,13 @@ export class CliValidator {
6265
}
6366
}
6467

68+
public validateSeed(options: Options_Cli) {
69+
const { user } = options;
70+
if (!user || typeof user !== "string") {
71+
this.exitHelpfully("seed", "You must supply a user");
72+
}
73+
}
74+
6575
private _getBuildOptions() {
6676
const buildOpts: Options_Cli_Build = {};
6777

@@ -107,6 +117,20 @@ export class CliValidator {
107117
return deleteOpts;
108118
}
109119

120+
private _getSeedOptions() {
121+
const seedOpts: Options_Cli_Delete = {};
122+
123+
const seedCmd = this.program.commands.find((cmd) => cmd.name() === "seed");
124+
if (seedCmd) {
125+
const user = seedCmd?.opts()["user"] as Options_Cli["user"];
126+
if (user) {
127+
seedOpts.user = user;
128+
}
129+
}
130+
131+
return seedOpts;
132+
}
133+
110134
private _mergeOptions = (): Options_Cli => {
111135
const _options = this.program.opts();
112136
let options: Options_Cli = {
@@ -130,6 +154,14 @@ export class CliValidator {
130154
};
131155
}
132156

157+
const seedOptions = this._getSeedOptions();
158+
if (Object.keys(seedOptions).length > 0) {
159+
options = {
160+
...options,
161+
...seedOptions,
162+
};
163+
}
164+
133165
return options;
134166
};
135167

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import pkg from "inquirer";
2+
import { log } from "@scripts/common/cli.utils";
3+
import mongoService from "@backend/common/services/mongo.service";
4+
import { findCompassUserBy } from "@backend/user/queries/user.queries";
5+
6+
const { prompt } = pkg;
7+
8+
async function seedEvents(userInput: string) {
9+
try {
10+
// Connect to MongoDB
11+
await mongoService.waitUntilConnected();
12+
13+
// Determine if input is email or ID and get the user ID
14+
const isEmail = userInput.includes("@");
15+
const user = await findCompassUserBy(isEmail ? "email" : "_id", userInput);
16+
17+
if (!user) {
18+
log.error(
19+
`User not found with ${isEmail ? "email" : "ID"}: ${userInput}`,
20+
);
21+
process.exit(1);
22+
}
23+
24+
const userId = user._id.toString();
25+
log.info(`Running seed command for user: ${userId}...`);
26+
27+
// TODO: Add logic to create events
28+
29+
log.success(`Successfully created events for user: ${userInput}`);
30+
} catch (error) {
31+
log.error("Failed to seed events:");
32+
console.error(error);
33+
}
34+
35+
process.exit(0);
36+
}
37+
38+
export async function runSeed(userInput: string, force = false) {
39+
log.info("Connecting to db...");
40+
await mongoService.waitUntilConnected();
41+
42+
if (force === true) {
43+
await seedEvents(userInput);
44+
return;
45+
}
46+
47+
const warning = [
48+
"⚠️ WARNING ⚠️",
49+
"",
50+
"This command will:",
51+
"• Create multiple events in your Compass calendar database",
52+
"• Create multiple events in your primary Google calendar",
53+
"",
54+
"🔔 RECOMMENDATION:",
55+
"It's strongly recommended to use this command with a test account",
56+
"rather than your personal account to avoid cluttering your calendar.",
57+
].join("\n");
58+
59+
log.warning(warning);
60+
61+
const questions = [
62+
{
63+
type: "confirm",
64+
name: "proceed",
65+
message: `Do you want to proceed with seeding events for: >> ${userInput} <<`,
66+
},
67+
];
68+
69+
prompt(questions)
70+
.then((answer: { proceed: boolean }) => {
71+
if (answer.proceed) {
72+
log.info("Starting event seeding process...");
73+
seedEvents(userInput).catch((e) => console.log(e));
74+
} else {
75+
log.info("Operation cancelled. No events were created.");
76+
process.exit(0);
77+
}
78+
})
79+
.catch((err) => console.log(err));
80+
}

0 commit comments

Comments
 (0)