-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild.js
More file actions
79 lines (69 loc) · 2.33 KB
/
build.js
File metadata and controls
79 lines (69 loc) · 2.33 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
const fs = require('fs');
const path = require('path');
// Helper to copy recursively
function copyRecursiveSync(src, dest) {
const exists = fs.existsSync(src);
const stats = exists && fs.statSync(src);
const isDirectory = exists && stats.isDirectory();
if (isDirectory) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
fs.readdirSync(src).forEach(childItemName => {
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName));
});
} else {
fs.copyFileSync(src, dest);
}
}
const distDir = path.join(__dirname, 'public');
console.log(`Building to ${distDir}...`);
// 1. Clean/Create dist
if (fs.existsSync(distDir)) {
fs.rmSync(distDir, { recursive: true, force: true });
}
fs.mkdirSync(distDir);
// 2. Parsers for .env (if local)
const envPath = path.join(__dirname, '.env');
if (fs.existsSync(envPath)) {
const envConfig = fs.readFileSync(envPath, 'utf8');
envConfig.split('\n').forEach(line => {
const [key, value] = line.split('=');
if (key && value) {
process.env[key.trim()] = value.trim();
}
});
}
// 3. Copy Static Assets
const itemsToCopy = ['index.html', 'sw.js', 'manifest.json', 'sitemap.xml', 'robots.txt', 'CSS', 'JS', 'HTML', 'assets', 'vercel.json'];
itemsToCopy.forEach(item => {
const src = path.join(__dirname, item);
const dest = path.join(distDir, item);
if (fs.existsSync(src)) {
copyRecursiveSync(src, dest);
} else {
console.warn(`Warning: source file/dir not found: ${src}`);
}
});
// 4. Generate Config in DIST
const configContent = `const config = {
SUPABASE_URL: '${process.env.SUPABASE_URL || ""}',
SUPABASE_ANON_KEY: '${process.env.SUPABASE_ANON_KEY || ""}',
GEMINI_API_KEY: '${process.env.GEMINI_API_KEY || ""}'
};
export default config;
`;
// Ensure JS dir exists in dist (it should from copy, but safety check)
const distJsDir = path.join(distDir, 'JS');
if (!fs.existsSync(distJsDir)) {
fs.mkdirSync(distJsDir);
}
const configPath = path.join(distJsDir, 'config.js');
try {
fs.writeFileSync(configPath, configContent);
console.log('Successfully generated dist/JS/config.js');
console.log('Build Complete.');
} catch (error) {
console.error('Error generating configuration:', error);
process.exit(1);
}