Skip to content

Commit

Permalink
updated setup.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
VanshikaSabharwal committed Nov 18, 2024
1 parent f1cd66a commit 15282bc
Showing 1 changed file with 61 additions and 10 deletions.
71 changes: 61 additions & 10 deletions setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,42 @@ const askAndUpdatePort = async (): Promise<void> => {

if (shouldSetCustomPortResponse) {
const customPort = await askForCustomPort();
if (customPort < 1024 || customPort > 65535) {
throw new Error('Port must be between 1024 and 65535');
}

updateEnvFile('PORT', String(customPort));
}
};

const askAndSetDockerOption = async (): Promise<void> => {
const { useDocker } = await inquirer.prompt({
type: 'confirm',
name: 'useDocker',
message: 'Would you like to set up with Docker?',
default: false,
});

if (useDocker) {
console.log('Setting up with Docker...');
updateEnvFile('USE_DOCKER', 'YES');
const dockerUrl = process.env.REACT_APP_TALAWA_URL?.replace(
'localhost',
'host.docker.internal',
);
if (dockerUrl) {
updateEnvFile('REACT_APP_DOCKER_TALAWA_URL', dockerUrl);
} else {
console.warn(
'⚠️ Docker URL setup skipped as no Talawa API URL was provided.',
);
}
} else {
console.log('Setting up without Docker...');
updateEnvFile('USE_DOCKER', 'NO');
}
};

// Ask and update the Talawa API URL
const askAndUpdateTalawaApiUrl = async (): Promise<void> => {
const { shouldSetTalawaApiUrlResponse } = await inquirer.prompt({
Expand All @@ -60,18 +92,30 @@ const askAndUpdateTalawaApiUrl = async (): Promise<void> => {
});

if (shouldSetTalawaApiUrlResponse) {
let endpoint = ' ';
let endpoint = '';
let isConnected = false;

while (!isConnected) {
endpoint = await askForTalawaApiUrl();
const url = new URL(endpoint);
if (!['http:', 'https:'].includes(url.protocol)) {
throw new Error('Invalid URL protocol.Must be http or https');
}
isConnected = await checkConnection(url.origin);
}

updateEnvFile('REACT_APP_TALAWA_URL', endpoint);
const websocketUrl = endpoint.replace(/^http(s)?:\/\//, 'ws$1://');
try {
new URL(websocketUrl);
} catch {
throw new Error('Invalid WebSocket URL generated');
}
updateEnvFile('REACT_APP_BACKEND_WEBSOCKET_URL', websocketUrl);
if (endpoint.includes('localhost')) {
const dockerUrl = endpoint.replace('localhost', 'host.docker.internal');
updateEnvFile('REACT_APP_DOCKER_TALAWA_URL', dockerUrl);
}
}
};

Expand Down Expand Up @@ -120,18 +164,25 @@ const askAndSetLogErrors = async (): Promise<void> => {

// Main function to run the setup process
export async function main(): Promise<void> {
console.log('Welcome to the Talawa Admin setup! 🚀');
try {
console.log('Welcome to the Talawa Admin setup! 🚀');

handleEnvFile();
handleEnvFile();

await askAndUpdatePort();
await askAndUpdateTalawaApiUrl();
await askAndSetRecaptcha();
await askAndSetLogErrors();
await askAndSetDockerOption();
await askAndUpdatePort();
await askAndUpdateTalawaApiUrl();
await askAndSetRecaptcha();
await askAndSetLogErrors();

console.log(
'\nCongratulations! Talawa Admin has been successfully set up! 🥂🎉',
);
console.log(
'\nCongratulations! Talawa Admin has been successfully set up! 🥂🎉',
);
} catch (error) {
console.error('\n❌ Setup failed:', error);
console.log('\nPlease try again or contact support if the issue persists.');
process.exit(1);
}
}

main();

0 comments on commit 15282bc

Please sign in to comment.