-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathcheck-env.js
More file actions
73 lines (60 loc) · 2.01 KB
/
check-env.js
File metadata and controls
73 lines (60 loc) · 2.01 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// Secure path resolution
const projectRoot = process.cwd();
const envPath = path.resolve(projectRoot, '.env');
// Security: Validate path is within project
if (!envPath.startsWith(projectRoot)) {
console.error('Security error: Invalid .env path');
process.exit(1);
}
// Check if .env exists
if (!fs.existsSync(envPath)) {
console.error('ERROR: .env file not found!');
console.error('Copy .env.example to .env and add your credentials:');
console.error(' cp .env.example .env');
process.exit(1);
}
// Parse .env file
const envContent = fs.readFileSync(envPath, 'utf8');
const envVars = {};
envContent.split('\n').forEach(line => {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) return;
const [key, ...valueParts] = trimmed.split('=');
if (key) {
const value = valueParts.join('=').trim().replace(/^["']|["']$/g, '');
envVars[key.trim()] = value;
}
});
// Only check ESSENTIAL variables
const required = ['SUPABASE_URL', 'SUPABASE_SERVICE_KEY'];
const errors = [];
required.forEach(varName => {
if (!envVars[varName] || envVars[varName] === '') {
errors.push(`Missing: ${varName}`);
}
});
if (errors.length > 0) {
console.error('ERROR: Required environment variables missing:');
errors.forEach(err => console.error(` - ${err}`));
console.error('\nPlease add these to your .env file');
process.exit(1);
}
// Validate URL format
try {
new URL(envVars['SUPABASE_URL']);
} catch (e) {
console.error('ERROR: SUPABASE_URL is not a valid URL');
console.error(` Found: ${envVars['SUPABASE_URL']}`);
console.error(' Expected format: https://your-project.supabase.co');
process.exit(1);
}
// Basic validation for service key
if (envVars['SUPABASE_SERVICE_KEY'].length < 10) {
console.error('ERROR: SUPABASE_SERVICE_KEY appears to be invalid (too short)');
console.error(' Please check your Supabase project settings');
process.exit(1);
}
console.log('✓ Environment configured correctly');