-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate-env.js
More file actions
75 lines (65 loc) · 2.63 KB
/
Copy pathgenerate-env.js
File metadata and controls
75 lines (65 loc) · 2.63 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
const fs = require('fs');
const path = require('path');
// Load .env file for local dev (dotenv-style, manual parse to avoid extra dependencies)
function loadEnvFile(filePath) {
if (!fs.existsSync(filePath)) return;
const lines = fs.readFileSync(filePath, 'utf8').split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const eqIdx = trimmed.indexOf('=');
if (eqIdx === -1) continue;
const key = trimmed.slice(0, eqIdx).trim();
const value = trimmed.slice(eqIdx + 1).trim().replace(/^["']|["']$/g, '');
if (!(key in process.env)) process.env[key] = value;
}
}
loadEnvFile(path.join(__dirname, '.env'));
loadEnvFile(path.join(__dirname, '.env.local'));
const DEFAULT_SUPABASE_URL = 'https://sozmatmywjpstwidzlss.supabase.co';
function readExistingEnvironmentValue(filePath, propertyName) {
if (!fs.existsSync(filePath)) return '';
const content = fs.readFileSync(filePath, 'utf8');
return content.match(new RegExp(`${propertyName}:\\\\s*['"]([^'"]+)['"]`))?.[1] || '';
}
const existingDevEnvPath = path.join(__dirname, 'src/environments/environment.ts');
const supabaseUrl = process.env.SUPABASE_URL || readExistingEnvironmentValue(existingDevEnvPath, 'url') || DEFAULT_SUPABASE_URL;
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY || readExistingEnvironmentValue(existingDevEnvPath, 'key');
if (!supabaseAnonKey) {
console.warn('[generate-env] WARNING: SUPABASE_ANON_KEY is not set. Environment files will have an empty anon key.');
}
const prodEnvContent = `
export const environment = {
production: true,
supabase: {
url: '${supabaseUrl}', // SAFE to expose — anon key with RLS enforced
key: '${supabaseAnonKey}' // SAFE to expose — anon key with RLS enforced
},
features: {
collectionsEnabled: false,
coolReactionsEnabled: false,
developerApiEnabled: true,
marketplaceEnabled: false
}
};
`;
const devEnvContent = `
// ⚠️ This file is gitignored and must NEVER be committed.
// Generated by generate-env.js — run: node generate-env.js
export const environment = {
production: false,
supabase: {
url: '${supabaseUrl}', // SAFE to expose — anon key with RLS enforced
key: '${supabaseAnonKey}' // SAFE to expose — anon key with RLS enforced
},
features: {
collectionsEnabled: true,
coolReactionsEnabled: true,
developerApiEnabled: true,
marketplaceEnabled: true
}
};
`;
fs.writeFileSync('src/environments/environment.prod.ts', prodEnvContent);
fs.writeFileSync('src/environments/environment.ts', devEnvContent);
console.log('[generate-env] environment.ts and environment.prod.ts written.');