-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
173 lines (135 loc) · 4.65 KB
/
build.js
File metadata and controls
173 lines (135 loc) · 4.65 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
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const { createWriteStream } = require("fs");
const archiver = require("archiver");
const PUBLISH_ONLY = process.argv.includes('--publishOnly');
const TMP_DIR = path.resolve(".tmp_build");
const BUILD_DIR = path.resolve("build");
const BACKEND_DIR = path.resolve("backend");
const viteFolder = path.join(BACKEND_DIR, "public", "build", ".vite");
const viteManifest = path.join(BACKEND_DIR, "public", "build", ".vite", "manifest.json");
const publicBuildTarget = path.join(BUILD_DIR, "public", "build");
function log(msg) {
console.log("🛠️ " + msg);
}
function run(cmd, cwd) {
execSync(cmd, { stdio: "inherit", cwd });
}
function copyRecursive(src, dest, excludes = []) {
if (!fs.existsSync(src)) return;
fs.mkdirSync(dest, { recursive: true });
for (const item of fs.readdirSync(src)) {
const from = path.join(src, item);
const to = path.join(dest, item);
if (excludes.some(ex => from.includes(ex))) continue;
const stats = fs.statSync(from);
if (stats.isDirectory()) {
copyRecursive(from, to, excludes);
} else {
fs.copyFileSync(from, to);
}
}
}
function cleanTmp(){
if (fs.existsSync(TMP_DIR)) fs.rmSync(TMP_DIR, { recursive: true, force: true });
fs.mkdirSync(TMP_DIR);
}
function clean() {
if (fs.existsSync(BUILD_DIR)) fs.rmSync(BUILD_DIR, { recursive: true, force: true });
fs.mkdirSync(BUILD_DIR);
}
function copyEnvFile() {
const envProd = path.join(BACKEND_DIR, ".env.production");
const env = path.join(BACKEND_DIR, ".env");
const target = path.join(TMP_DIR, ".env");
if (fs.existsSync(envProd)) {
log("Using .env.production");
fs.copyFileSync(envProd, target);
} else if (fs.existsSync(env)) {
log("Using fallback .env");
fs.copyFileSync(env, target);
} else {
throw new Error("No .env or .env.production found.");
}
}
function createZip(sourceDir, zipPath, excludes = []) {
return new Promise((resolve, reject) => {
const output = createWriteStream(zipPath);
const archive = archiver("zip", { zlib: { level: 9 } });
output.on("close", resolve);
archive.on("error", reject);
archive.pipe(output);
archive.glob("**/*", {
cwd: sourceDir,
ignore: excludes,
dot: true
});
archive.finalize();
});
}
// === MAIN ===
clean();
cleanTmp();
if (!PUBLISH_ONLY) {
log("Running frontend build...");
run("npm install", path.resolve("frontend"));
run("npm run build", path.resolve("frontend"));
if (!fs.existsSync(viteManifest)) {
throw new Error("❌ manifest.json not found. Did you forget to build the frontend?");
}
}
log("Copying frontend build to public/build...");
copyRecursive(viteFolder, publicBuildTarget);
if (!PUBLISH_ONLY) {
log("Installing Laravel dependencies...");
run("composer install --optimize-autoloader --no-dev", BACKEND_DIR);
log("Caching Laravel config...");
run("php artisan config:cache", BACKEND_DIR);
run("php artisan route:cache", BACKEND_DIR);
run("php artisan view:cache", BACKEND_DIR);
run("php artisan storage:link", BACKEND_DIR);
}
log("Copying Laravel structure...");
copyRecursive(BACKEND_DIR, TMP_DIR, [
"node_modules",
"tests",
".env",
".env.production",
"storage/logs",
]);
log("Injecting environment file...");
copyEnvFile();
// log("Writing .htaccess...");
// writeHtaccess();
console.log(`
📦 Deployment Instructions for cPanel / Nethely:
1. Upload the entire contents of the /build folder to your hosting account.
2. In cPanel or Nethely, set the domain’s root directory to:
→ /build/public (or use it as public_html)
✅ No need to modify index.php or run artisan on the server.
`.trim());
log("✅ DONE! Upload /build as-is to your hosting and point domain to /public.");
(async () => {
const maintenanceSrc = path.resolve("maintenance");
const maintenanceDest = path.join(BUILD_DIR, "public", "maintenance");
fs.mkdirSync(maintenanceDest, { recursive: true });
const zipPath = path.join(BUILD_DIR, "public", "maintenance", "astelium.zip");
log("📦 Creating astelium.zip...");
await createZip(TMP_DIR, zipPath, ["install.php", "astelium.zip"]);
copyRecursive(maintenanceSrc, maintenanceDest);
fs.writeFileSync(path.join(BUILD_DIR, "public", "index.php"),
'<?php\n' +
'header("Location: ./maintenance/");\n' +
'exit;')
// Console instructions
console.log(`
✅ Upload Instructions:
1. Upload everything from /build to your server
2. Set the domain root to /build/public (or public_html)
3. Open /install.php in browser and follow DB setup
ℹ️ After success, delete install.php for security.
`);
cleanTmp();
fs.rmdirSync(TMP_DIR);
})()