-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-mongodb.js
More file actions
80 lines (73 loc) · 3.36 KB
/
setup-mongodb.js
File metadata and controls
80 lines (73 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
console.log('MongoDB Compass Connection Setup Helper');
console.log('=====================================');
console.log('');
console.log('This script will help you set up your MongoDB connection.');
console.log('Since you have MongoDB Compass installed, you can use it to connect to MongoDB.');
console.log('');
console.log('Steps to follow:');
console.log('1. Open MongoDB Compass');
console.log('2. Connect to your local MongoDB instance (usually mongodb://localhost:27017)');
console.log(' - If you haven\'t started MongoDB yet, start it from Compass');
console.log('3. Create a new database called "inventory-management"');
console.log('');
// Check if .env.local exists, if not create it
const envPath = path.join(__dirname, '.env.local');
if (!fs.existsSync(envPath)) {
console.log('Creating .env.local file with MongoDB connection string...');
fs.writeFileSync(envPath, 'MONGODB_URI=mongodb://localhost:27017/inventory-management\nNEXTAUTH_SECRET=your-secret-key-for-next-auth\n');
console.log('.env.local file created successfully!');
} else {
console.log('.env.local file already exists. Checking for MongoDB connection string...');
const envContent = fs.readFileSync(envPath, 'utf8');
if (!envContent.includes('MONGODB_URI=')) {
console.log('Adding MongoDB connection string to .env.local...');
fs.appendFileSync(envPath, '\nMONGODB_URI=mongodb://localhost:27017/inventory-management\n');
console.log('MongoDB connection string added to .env.local!');
} else {
console.log('MongoDB connection string already exists in .env.local.');
}
if (!envContent.includes('NEXTAUTH_SECRET=')) {
console.log('Adding NextAuth secret to .env.local...');
fs.appendFileSync(envPath, '\nNEXTAUTH_SECRET=your-secret-key-for-next-auth\n');
console.log('NextAuth secret added to .env.local!');
}
}
console.log('');
console.log('MongoDB setup complete!');
console.log('');
console.log('To start your application with the local MongoDB connection:');
console.log('1. Make sure MongoDB is running through Compass');
console.log('2. Run "npm run dev" to start your application');
console.log('');
console.log('If you encounter any issues, please ensure that:');
console.log('1. MongoDB Compass is open and connected to mongodb://localhost:27017');
console.log('2. You\'ve created the "inventory-management" database in Compass');
console.log('');
// Try to open MongoDB Compass if it's installed in the default location
console.log('Attempting to open MongoDB Compass...');
const compassPaths = [
'C:\\Program Files\\MongoDB Compass\\MongoDBCompass.exe',
'C:\\Program Files (x86)\\MongoDB Compass\\MongoDBCompass.exe',
path.join(process.env.LOCALAPPDATA || '', 'Programs', 'MongoDB Compass', 'MongoDBCompass.exe')
];
let compassFound = false;
for (const compassPath of compassPaths) {
if (fs.existsSync(compassPath)) {
console.log(`Found MongoDB Compass at: ${compassPath}`);
console.log('Launching MongoDB Compass...');
exec(`"${compassPath}"`, (error) => {
if (error) {
console.error(`Error launching MongoDB Compass: ${error.message}`);
}
});
compassFound = true;
break;
}
}
if (!compassFound) {
console.log('Could not find MongoDB Compass in the default installation paths.');
console.log('Please open MongoDB Compass manually.');
}