Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Harrison committed Dec 22, 2023
2 parents 622cd32 + bb1e9f4 commit 419b889
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
80 changes: 80 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"nanoid": "^3.3.4",
"nodemailer": "^6.7.8",
"pm2": "^5.2.0",
"redis": "^4.6.12",
"shortid": "^2.2.16",
"uuid": "^9.0.0",
"validator": "^13.7.0",
Expand Down
91 changes: 91 additions & 0 deletions setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require("fs");
const cryptolib = require("crypto");
const inquirer = require("inquirer");
const mongodb = require("mongodb");
const redis = require("redis");
const { exec } = require("child_process");

dotenv.config();
Expand Down Expand Up @@ -44,6 +45,79 @@ async function accessAndRefreshTokens(
}
}

// Check connection to Redis with the specified URL.
async function checkRedisConnection(url: string): Promise<boolean> {
let response = false;
const client = redis.createClient(url);

console.log("\nChecking Redis connection....");

try {
await client.connect();
console.log("\nConnection to Redis successful! 🎉");
response = true;
} catch (error) {
console.log(`\nConnection to Redis failed. Please try again.\n`);
}
client.quit();
return response;
}

// Redis url prompt
async function askForRedisUrl(): Promise<{
host: string;
port: number;
password: string;
}> {
const { host, port, password } = await inquirer.prompt([
{
type: "input",
name: "host",
message: "Enter Redis hostname (default: localhost):",
default: "localhost",
},
{
type: "input",
name: "port",
message: "Enter Redis port (default: 6379):",
default: 6379,
},
{
type: "password",
name: "password",
message:
"Enter Redis password (optional : Leave empty for local connections) :",
},
]);

return { host, port, password };
}

// get the redis url
async function redisConfiguration(): Promise<void> {
const REDIS_URL = process.env.REDIS_URL;

try {
let isConnected = false,
url = "";
while (!isConnected) {
const { host, port, password } = await askForRedisUrl();
url = `redis://${password ? password + "@" : ""}${host}:${port}`;
isConnected = await checkRedisConnection(url);
}

const config = dotenv.parse(fs.readFileSync(".env"));
config.REDIS_URL = url;
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
}
} catch (err) {
console.error(err);
abort();
}
}

// Check the connection to MongoDB with the specified URL.
async function checkConnection(url: string): Promise<boolean> {
let response = false;
Expand Down Expand Up @@ -349,6 +423,23 @@ async function main(): Promise<void> {
default: false,
});
if (!isDockerInstallation) {
// Redis configuration
if (process.env.REDIS_URL) {
console.log(
`\nRedis URL already exists with the value:\n${process.env.REDIS_URL}`
);
}
const { shouldSetRedis } = await inquirer.prompt({
type: "confirm",
name: "shouldSetRedis",
message: "Would you like to set up a Redis URL?",
default: true,
});
if (shouldSetRedis) {
await redisConfiguration();
}

// MongoDB configuration
if (process.env.MONGO_DB_URL) {
console.log(
`\nMongoDB URL already exists with the value:\n${process.env.MONGO_DB_URL}`
Expand Down

0 comments on commit 419b889

Please sign in to comment.