Add spinner feedback to database setup workflows

This commit is contained in:
Aman Varshney
2025-04-11 15:45:41 +05:30
parent 37217f4e18
commit 30efd64fc1
6 changed files with 258 additions and 125 deletions

View File

@@ -1,5 +1,5 @@
import path from "node:path";
import { cancel, isCancel, log, text } from "@clack/prompts";
import { cancel, isCancel, log, spinner, text } from "@clack/prompts";
import { execa } from "execa";
import fs from "fs-extra";
import pc from "picocolors";
@@ -10,7 +10,21 @@ type MongoDBConfig = {
};
async function checkAtlasCLI(): Promise<boolean> {
return commandExists("atlas");
const s = spinner();
s.start("Checking for MongoDB Atlas CLI");
try {
const exists = await commandExists("atlas");
s.stop(
exists
? "MongoDB Atlas CLI found"
: pc.yellow("MongoDB Atlas CLI not found"),
);
return exists;
} catch (error) {
s.stop(pc.red("Error checking for MongoDB Atlas CLI"));
return false;
}
}
async function initMongoDBAtlas(
@@ -29,20 +43,23 @@ async function initMongoDBAtlas(
return null;
}
log.info(pc.yellow("Setting up MongoDB Atlas..."));
log.info(pc.blue("Running MongoDB Atlas setup..."));
await execa("atlas", ["deployments", "setup"], {
cwd: serverDir,
stdio: "inherit",
});
log.info(pc.green("Atlas setup complete!"));
const connectionString = await text({
message: "Paste your complete MongoDB connection string:",
placeholder: "mongodb://USERNAME:PASSWORD@HOST/DATABASE",
message: "Enter your MongoDB connection string:",
placeholder:
"mongodb+srv://username:password@cluster.mongodb.net/database",
validate(value) {
if (!value) return "Please enter a connection string";
if (!value.startsWith("mongodb")) {
return "URL should start with mongodb";
return "URL should start with mongodb:// or mongodb+srv://";
}
},
});
@@ -64,57 +81,88 @@ async function initMongoDBAtlas(
}
async function writeEnvFile(projectDir: string, config?: MongoDBConfig) {
const envPath = path.join(projectDir, "apps/server", ".env");
let envContent = "";
try {
const envPath = path.join(projectDir, "apps/server", ".env");
await fs.ensureDir(path.dirname(envPath));
if (await fs.pathExists(envPath)) {
envContent = await fs.readFile(envPath, "utf8");
let envContent = "";
if (await fs.pathExists(envPath)) {
envContent = await fs.readFile(envPath, "utf8");
}
const mongoUrlLine = config
? `DATABASE_URL="${config.connectionString}"`
: `DATABASE_URL="mongodb://localhost:27017/mydb"`;
if (!envContent.includes("DATABASE_URL=")) {
envContent += `\n${mongoUrlLine}`;
} else {
envContent = envContent.replace(
/DATABASE_URL=.*(\r?\n|$)/,
`${mongoUrlLine}$1`,
);
}
await fs.writeFile(envPath, envContent.trim());
} catch (error) {
log.error("Failed to update environment configuration");
throw error;
}
const mongoUrlLine = config
? `DATABASE_URL="${config.connectionString}"`
: `DATABASE_URL="mongodb://localhost:27017/mydb"`;
if (!envContent.includes("DATABASE_URL=")) {
envContent += `\n${mongoUrlLine}`;
} else {
envContent = envContent.replace(
/DATABASE_URL=.*(\r?\n|$)/,
`${mongoUrlLine}$1`,
);
}
await fs.writeFile(envPath, envContent.trim());
}
function displayManualSetupInstructions() {
log.info(`MongoDB Atlas Setup:
log.info(`
${pc.green("MongoDB Atlas Manual Setup Instructions:")}
1. Install Atlas CLI: https://www.mongodb.com/docs/atlas/cli/stable/install-atlas-cli/
2. Run 'atlas deployments setup' and follow prompts
3. Get your connection string from the output
4. Format: mongodb+srv://USERNAME:PASSWORD@CLUSTER.mongodb.net/DATABASE_NAME
5. Add to .env as DATABASE_URL="your_connection_string"`);
1. Install Atlas CLI:
${pc.blue("https://www.mongodb.com/docs/atlas/cli/stable/install-atlas-cli/")}
2. Run the following command and follow the prompts:
${pc.blue("atlas deployments setup")}
3. Get your connection string from the Atlas dashboard:
Format: ${pc.dim("mongodb+srv://USERNAME:PASSWORD@CLUSTER.mongodb.net/DATABASE_NAME")}
4. Add the connection string to your .env file:
${pc.dim('DATABASE_URL="your_connection_string"')}
`);
}
export async function setupMongoDBAtlas(projectDir: string) {
const serverDir = path.join(projectDir, "apps/server");
const mainSpinner = spinner();
mainSpinner.start("Setting up MongoDB Atlas");
const serverDir = path.join(projectDir, "apps/server");
try {
await fs.ensureDir(serverDir);
mainSpinner.stop("Starting MongoDB Atlas setup");
const config = await initMongoDBAtlas(serverDir);
if (config) {
await writeEnvFile(projectDir, config);
log.success(
pc.green("MongoDB Atlas connection string saved to .env file!"),
pc.green(
"MongoDB Atlas setup complete! Connection saved to .env file.",
),
);
} else {
log.warn(pc.yellow("Falling back to local MongoDB configuration"));
await writeEnvFile(projectDir);
displayManualSetupInstructions();
}
} catch (error) {
log.error(pc.red(`Error during MongoDB Atlas setup: ${error}`));
await writeEnvFile(projectDir);
displayManualSetupInstructions();
mainSpinner.stop(pc.red("MongoDB Atlas setup failed"));
log.error(
pc.red(
`Error during MongoDB Atlas setup: ${error instanceof Error ? error.message : String(error)}`,
),
);
try {
await writeEnvFile(projectDir);
displayManualSetupInstructions();
} catch {}
}
}