Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bugs to ensure proper flow of prompts and remove repetitive prompts #2845

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
90 changes: 43 additions & 47 deletions setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cryptolib from "crypto";
import dotenv from "dotenv";
import fs from "fs";
import inquirer from "inquirer";
import path from "path";
import path, { dirname } from "path";
import type { ExecException } from "child_process";
import { exec, spawn, execSync } from "child_process";
import { MongoClient } from "mongodb";
Expand Down Expand Up @@ -31,6 +31,7 @@ import { verifySmtpConnection } from "@setup/verifySmtpConnection";
import { loadDefaultOrganiation } from "@utilities/loadDefaultOrg";
import { isMinioInstalled } from "@setup/isMinioInstalled";
import { installMinio } from "@setup/installMinio";
import { fileURLToPath } from "url";

dotenv.config();

Expand Down Expand Up @@ -141,10 +142,11 @@ export async function accessAndRefreshTokens(

function transactionLogPath(logPath: string | null): void {
const config = dotenv.parse(fs.readFileSync(".env"));
const currDir = dirname(fileURLToPath(import.meta.url));
config.LOG = "true";
if (!logPath) {
// Check if the logs/transaction.log file exists, if not, create it
const defaultLogPath = path.resolve(__dirname, "logs");
const defaultLogPath = path.resolve(currDir, "logs");
const defaultLogFile = path.join(defaultLogPath, "transaction.log");
if (!fs.existsSync(defaultLogPath)) {
console.log("Creating logs/transaction.log file...");
Expand All @@ -154,7 +156,7 @@ function transactionLogPath(logPath: string | null): void {
config.LOG_PATH = defaultLogFile;
} else {
// Remove the logs files, if exists
const logsDirPath = path.resolve(__dirname, "logs");
const logsDirPath = path.resolve(currDir, "logs");
if (fs.existsSync(logsDirPath)) {
fs.readdirSync(logsDirPath).forEach((file: string) => {
if (file !== "README.md") {
Expand Down Expand Up @@ -221,7 +223,7 @@ export async function wipeExistingData(url: string): Promise<void> {
console.log("All existing data has been deleted.");
}
} catch {
console.error("Could not connect to database to check for data");
throw new Error("Could not connect to database to check for data");
}
client.close();
// return shouldImport;
Expand All @@ -237,6 +239,7 @@ export async function wipeExistingData(url: string): Promise<void> {
export async function checkDb(url: string): Promise<boolean> {
let dbEmpty = false;
const client = new MongoClient(`${url}`);
console.log(`Connecting to database...`);
try {
await client.connect();
const db = client.db();
Expand All @@ -248,7 +251,7 @@ export async function checkDb(url: string): Promise<boolean> {
dbEmpty = true;
}
} catch {
console.error("Could not connect to database to check for data");
throw new Error("Could not connect to database to check for data");
}
client.close();
return dbEmpty;
Expand All @@ -267,7 +270,7 @@ export async function importData(): Promise<void> {

console.log("Importing sample data...");
if (process.env.NODE_ENV !== "test") {
await exec(
exec(
"npm run import:sample-data",
(error: ExecException | null, stdout: string, stderr: string) => {
if (error) {
Expand All @@ -278,7 +281,7 @@ export async function importData(): Promise<void> {
console.error(`Error: ${stderr}`);
abort();
}
console.log(`Output: ${stdout}`);
console.log(`\nOutput: ${stdout}`);
},
);
}
Expand Down Expand Up @@ -1227,36 +1230,32 @@ async function main(): Promise<void> {
console.log("Couldn't find mongodb url");
return;
}

const isDbEmpty = await checkDb(process.env.MONGO_DB_URL);
if (!isDbEmpty) {
const { shouldOverwriteData } = await inquirer.prompt({
type: "confirm",
name: "shouldOverwriteData",
message: "Do you want to overwrite the existing data?",
message:
"Do you want to delete the existing data and import required default Data to start using Talawa?",
default: false,
});
if (shouldOverwriteData) {
await wipeExistingData(process.env.MONGO_DB_URL);
const { overwriteDefaultData } = await inquirer.prompt({
// Import Default Data
await importDefaultData();

// Prompt to import Sample Data
const { importSampleData } = await inquirer.prompt({
type: "confirm",
name: "overwriteDefaultData",
name: "importSampleData",
message:
"Do you want to import the required default data to start using Talawa in a production environment?",
"Do you want to import Talawa sample data for testing and evaluation purposes?",
default: false,
});
if (overwriteDefaultData) {
await importDefaultData();
} else {
const { overwriteSampleData } = await inquirer.prompt({
type: "confirm",
name: "overwriteSampleData",
message:
"Do you want to import Talawa sample data for testing and evaluation purposes?",
default: false,
});
if (overwriteSampleData) {
await importData();
}

if (importSampleData) {
await importData();
}
}
} else {
Expand All @@ -1267,35 +1266,19 @@ async function main(): Promise<void> {
"Do you want to import Talawa sample data for testing and evaluation purposes?",
default: false,
});
await wipeExistingData(process.env.MONGO_DB_URL);
if (shouldImportSampleData) {
await importData();
} else {
await importDefaultData();
}
}
}

console.log(
"\nCongratulations! Talawa API has been successfully setup! 🥂🎉",
);

const { shouldStartDockerContainers } = await inquirer.prompt({
type: "confirm",
name: "shouldStartDockerContainers",
message: "Do you want to start the Docker containers now?",
default: true,
});

const { shouldImportSampleData } = await inquirer.prompt({
type: "confirm",
name: "shouldImportSampleData",
message:
"Do you want to import Talawa sample data for testing and evaluation purposes?",
default: true,
});

if (isDockerInstallation) {
} else {
const { shouldStartDockerContainers } = await inquirer.prompt({
type: "confirm",
name: "shouldStartDockerContainers",
message: "Do you want to start the Docker containers now?",
default: true,
});
if (shouldStartDockerContainers) {
console.log("Starting docker container...");
try {
Expand Down Expand Up @@ -1329,6 +1312,15 @@ async function main(): Promise<void> {
}
}

await importDefaultData();
const { shouldImportSampleData } = await inquirer.prompt({
type: "confirm",
name: "shouldImportSampleData",
message:
"Do you want to import Talawa sample data for testing and evaluation purposes?",
default: true,
});

if (shouldImportSampleData) {
await importData();
}
Expand All @@ -1337,6 +1329,10 @@ async function main(): Promise<void> {
}
}
}

console.log(
"\nCongratulations! Talawa API has been successfully setup! 🥂🎉",
);
}

main();
Loading