-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
224 lines (192 loc) · 6.74 KB
/
setup.js
File metadata and controls
224 lines (192 loc) · 6.74 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
/**
* Golobe Travel Agency - Setup Script
*
* This script helps you set up the environment variables for the project.
* Run this script after cloning the repository to configure your local environment.
*/
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
console.log("🚀 Welcome to Golobe Travel Agency Setup!");
console.log(
"This script will help you configure your environment variables.\n",
);
// Function to generate random strings
function generateRandomString(length = 32) {
return crypto.randomBytes(length).toString("hex");
}
// Function to prompt user for input
function prompt(question) {
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer);
});
});
}
// Function to create environment file
async function createEnvFile() {
const envPath = path.join(process.cwd(), ".env.local");
// Check if .env.local already exists
if (fs.existsSync(envPath)) {
const overwrite = await prompt(
"⚠️ .env.local already exists. Do you want to overwrite it? (y/N): ",
);
if (overwrite.toLowerCase() !== "y" && overwrite.toLowerCase() !== "yes") {
console.log(
"❌ Setup cancelled. Your existing .env.local file was preserved.",
);
return;
}
}
console.log("\n📝 Let's configure your environment variables...\n");
// Collect user inputs
const mongodbUri =
(await prompt(
"🔗 MongoDB URI (default: mongodb://localhost:27017/golobe_travel_agency): ",
)) || "mongodb://localhost:27017/golobe_travel_agency";
const baseUrl =
(await prompt("🌐 Base URL (default: http://localhost:3000): ")) ||
"http://localhost:3000";
const stripeSecretKey = await prompt(
"💳 Stripe Secret Key (starts with sk_test_): ",
);
const stripePublishableKey = await prompt(
"💳 Stripe Publishable Key (starts with pk_test_): ",
);
const stripeWebhookSecret = await prompt(
"🔗 Stripe Webhook Secret (starts with whsec_): ",
);
const mailjetApiToken = await prompt("📧 Mailjet API Token: ");
const mailjetSecretToken = await prompt("📧 Mailjet Secret Token: ");
const senderEmail = await prompt("📧 Sender Email: ");
// Generate random secrets
const authSecret = generateRandomString(32);
const apiSecretToken = generateRandomString(32);
const cronSecret = generateRandomString(32);
// Create environment file content
const envContent = `# ===========================================
# Golobe Travel Agency - Environment Variables
# ===========================================
# Generated by setup script on ${new Date().toISOString()}
# ===========================================
# DATABASE CONFIGURATION
# ===========================================
MONGODB_URI=${mongodbUri}
# ===========================================
# AUTHENTICATION (NextAuth.js)
# ===========================================
AUTH_SECRET=${authSecret}
# ===========================================
# APPLICATION CONFIGURATION
# ===========================================
NEXT_PUBLIC_BASE_URL=${baseUrl}
NEXT_PUBLIC_REVALIDATION_TIME=600
# ===========================================
# API SECURITY
# ===========================================
API_SECRET_TOKEN=${apiSecretToken}
CRON_SECRET=${cronSecret}
# ===========================================
# STRIPE PAYMENT PROCESSING
# ===========================================
STRIPE_SECRET_KEY=${stripeSecretKey}
NEXT_PUBLIC_STRIPE_PK=${stripePublishableKey}
STRIPE_WEBHOOK_SECRET=${stripeWebhookSecret}
# ===========================================
# EMAIL CONFIGURATION (Mailjet)
# ===========================================
MAIL_API_TOKEN=${mailjetApiToken}
MAIL_SECRET_TOKEN=${mailjetSecretToken}
MAIL_SENDER_EMAIL=${senderEmail}
# ===========================================
# DEVELOPMENT NOTES
# ===========================================
# 1. Make sure all required services are configured
# 2. Test the application with: npm run dev
# 3. Generate sample data using the API endpoints
# 4. Never commit this file to version control
`;
// Write the file
try {
fs.writeFileSync(envPath, envContent);
console.log("✅ Environment file created successfully!");
console.log(`📁 File location: ${envPath}`);
} catch (error) {
console.error("❌ Error creating environment file:", error.message);
return;
}
// Display next steps
console.log("\n🎉 Setup completed successfully!");
console.log("\n📋 Next steps:");
console.log("1. Install dependencies: npm install");
console.log("2. Start the development server: npm run dev");
console.log("3. Generate sample data using the API endpoints");
console.log("4. Check the GETTING_STARTED.md file for detailed instructions");
console.log("\n🔑 Generated secrets:");
console.log(` AUTH_SECRET: ${authSecret}`);
console.log(` API_SECRET_TOKEN: ${apiSecretToken}`);
console.log(` CRON_SECRET: ${cronSecret}`);
console.log("\n⚠️ Important:");
console.log("- Keep these secrets secure and never share them");
console.log(
"- Update the environment variables with your actual service credentials",
);
console.log(
"- For production, use production keys and rotate secrets regularly",
);
}
// Function to check prerequisites
function checkPrerequisites() {
console.log("🔍 Checking prerequisites...\n");
// Check Node.js version
const nodeVersion = process.version;
const nodeVersionNum = parseInt(nodeVersion.slice(1).split(".")[0]);
if (nodeVersionNum < 18) {
console.log("❌ Node.js version 18 or higher is required");
console.log(` Current version: ${nodeVersion}`);
console.log(" Download from: https://nodejs.org/");
process.exit(1);
} else {
console.log(`✅ Node.js version: ${nodeVersion}`);
}
// Check npm
try {
const npmVersion = require("child_process")
.execSync("npm --version", { encoding: "utf8" })
.trim();
console.log(`✅ npm version: ${npmVersion}`);
} catch (error) {
console.log("❌ npm is not installed or not accessible");
process.exit(1);
}
// Check if package.json exists
if (!fs.existsSync("package.json")) {
console.log(
"❌ package.json not found. Make sure you're in the project root directory.",
);
process.exit(1);
}
console.log("✅ All prerequisites met!\n");
}
// Main function
async function main() {
try {
checkPrerequisites();
await createEnvFile();
} catch (error) {
console.error("❌ Setup failed:", error.message);
process.exit(1);
}
}
// Run the setup
if (require.main === module) {
main();
}
module.exports = { createEnvFile, checkPrerequisites };