Skip to content

Commit c9bdbd9

Browse files
committed
simplying setup files and remove runCommand
1 parent 2c41abb commit c9bdbd9

2 files changed

Lines changed: 39 additions & 50 deletions

File tree

src/install/setupDocker.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { execSync } from 'node:child_process';
4+
import inquirer from 'inquirer';
45

56
const ROOT_DIR = process.cwd();
67
const ENV_DEV_SOURCE = path.join(ROOT_DIR, 'envFiles', '.env.devcontainer');
@@ -37,7 +38,7 @@ async function main() {
3738
if (process.platform === 'linux') {
3839
console.log('\n🔧 Adding user to docker group...');
3940
console.log('DEBUG: process.platform =', process.platform);
40-
execSync('sudo usermod -a -G docker $USER}');
41+
execSync('sudo usermod -a -G docker $USER');
4142
console.log('==========sud command check 1===========')
4243
execSync ('sudo su $USER -')
4344
console.log('==========sud command check ===========')
@@ -50,6 +51,29 @@ async function main() {
5051
console.log('\n🚀 Starting DevContainer...');
5152
execSync('devcontainer up --workspace-folder .');
5253

54+
console.log('\n📊 Applying database migrations...');
55+
execSync('pnpm run apply_drizzle_migrations');
56+
57+
const dataAnswer = await inquirer.prompt([
58+
{
59+
type: 'confirm',
60+
name: 'addSampleData',
61+
message: 'Do you want to seed the database with sample data?',
62+
default: true,
63+
},
64+
]);
65+
66+
if (dataAnswer.addSampleData) {
67+
console.log('\n🌱 Seeding Sample Data...');
68+
try {
69+
execSync(`docker exec talawa-api-1 /bin/bash -c 'pnpm run add:sample_data'`, { stdio: 'inherit' });
70+
console.log('✅ Sample data seeded successfully');
71+
} catch (error) {
72+
console.log('⚠️ Sample data seeding failed, but you can run it manually later:');
73+
console.log(` docker exec talawa-api-1 /bin/bash -c 'pnpm run add:sample_data'`);
74+
}
75+
}
76+
5377
console.log('\n🚀 Starting API Server...');
5478
execSync('docker exec talawa-api-1 /bin/bash -c "nohup pnpm run start_development_server > /dev/null 2>&1 &"');
5579

@@ -58,13 +82,9 @@ async function main() {
5882

5983
console.log('\n✅ DevContainer Setup Complete!');
6084
console.log('------------------------------------------------');
61-
console.log('✓ API Server is running at: http://localhost:4000');
85+
console.log('✓ API Server is running in detached mode at: http://localhost:4000');
6286
console.log('✓ GraphQL Playground: http://localhost:4000/graphql');
63-
console.log('');
64-
console.log('Useful Commands:');
65-
console.log(' View logs: docker logs -f talawa-api-1');
66-
console.log(' Stop server: docker exec talawa-api-1 pkill -f "pnpm run start_development_server"');
67-
console.log(' Restart: docker restart talawa-api-1');
87+
console.log(' Stop server: docker compose down');
6888
console.log('------------------------------------------------');
6989
}
7090

src/install/setupLocal.ts

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,6 @@ const needsSudoForDocker = (): boolean => {
1616
}
1717
};
1818

19-
const runCommand = (command: string, throwOnError = true) => {
20-
try {
21-
let finalCommand = command;
22-
if (command.includes('docker') && needsSudoForDocker()) {
23-
finalCommand = `sudo ${command}`;
24-
console.log('ℹ️ Using sudo for Docker commands (run without sudo after logging out/in)');
25-
}
26-
execSync(finalCommand, { stdio: 'inherit', cwd: ROOT_DIR, env: process.env });
27-
} catch (error) {
28-
console.error(`❌ Command failed: ${command}`);
29-
if (throwOnError) {
30-
process.exit(1);
31-
} else {
32-
throw error;
33-
}
34-
}
35-
};
36-
3719
async function main() {
3820
console.log('\n🚀 Talawa API Local Setup\n');
3921

@@ -45,7 +27,7 @@ async function main() {
4527
}
4628

4729
console.log('\n⚙️ Running database setup (generating JWT secret, configuring services)...');
48-
runCommand('pnpm tsx setup.ts');
30+
execSync('pnpm tsx setup.ts');
4931

5032
console.log('\n🔄 Adjusting .env for local machine access...');
5133
let envContent = fs.readFileSync(ENV_DEST, 'utf-8');
@@ -83,18 +65,18 @@ async function main() {
8365
console.log('✅ .env updated: Database services point to localhost');
8466

8567
console.log('\n📦 Installing DevContainer CLI...');
86-
runCommand('pnpm install -g @devcontainers/cli');
68+
execSync('pnpm install -g @devcontainers/cli');
8769

8870
// Add user to docker group if needed (Linux only)
8971
if (process.platform === 'linux' && needsSudoForDocker()) {
9072
console.log('\n🔧 Adding user to docker group...');
91-
const username = process.env.USER || process.env.USERNAME || 'ubuntu';
92-
runCommand(`sudo usermod -a -G docker ${username}`);
73+
execSync('sudo usermod -a -G docker $USER');
74+
execSync('sudo su $USER -')
9375
console.log('ℹ️ You may need to log out and back in for docker group changes to take effect');
9476
}
9577

9678
console.log('\n🐳 Starting Database Containers...');
97-
runCommand('devcontainer up --workspace-folder . --skip-post-create');
79+
execSync('devcontainer up --workspace-folder . --skip-post-create');
9880

9981
console.log('\n⏳ Waiting for database services to be healthy...');
10082
let retries = 10;
@@ -114,7 +96,7 @@ async function main() {
11496
}
11597

11698
console.log('\n📊 Applying database migrations...');
117-
runCommand('pnpm run apply_drizzle_migrations');
99+
execSync('pnpm run apply_drizzle_migrations');
118100

119101
const dataAnswer = await inquirer.prompt([
120102
{
@@ -128,32 +110,19 @@ async function main() {
128110
if (dataAnswer.addSampleData) {
129111
console.log('\n🌱 Seeding Sample Data...');
130112
try {
131-
runCommand('pnpm run add:sample_data', false);
113+
execSync('pnpm run add:sample_data', { stdio: 'inherit' });
132114
console.log('✅ Sample data seeded successfully');
133115
} catch (error) {
134116
console.log('⚠️ Sample data seeding failed, but you can run it manually later:');
135117
console.log(' pnpm run add:sample_data');
136118
}
137119
}
138120

139-
const startAnswer = await inquirer.prompt([
140-
{
141-
type: 'confirm',
142-
name: 'startServer',
143-
message: 'Do you want to start the development server now?',
144-
default: true,
145-
},
146-
]);
147-
148-
if (startAnswer.startServer) {
149-
console.log('\n🚀 Starting Development Server...');
150-
try {
151-
execSync('pnpm run start_development_server', { stdio: 'inherit' });
152-
} catch (e) {
153-
console.log('\nServer stopped.');
154-
}
155-
} else {
156-
console.log('⚠️ Restart your terminal to use pnpm manually.');
121+
console.log('\n🚀 Starting Development Server...');
122+
try {
123+
execSync('pnpm run start_development_server', { stdio: 'inherit' });
124+
} catch (e) {
125+
console.log('\nServer stopped.');
157126
}
158127
}
159128

0 commit comments

Comments
 (0)