67 lines
2.2 KiB
JavaScript
67 lines
2.2 KiB
JavaScript
/**
|
|||
|
|
* Post-build: make ./out a complete, uploadable cPanel document root.
|
||
|
|
* - copy deploy/htaccess.root → out/.htaccess
|
||
|
|
* - copy config/campuses.json → out/api/config/campuses.json (read by PHP)
|
||
|
|
* - ensure the private lib/cron folders keep their .htaccess deny rules
|
||
|
|
* - write out/BUILD.txt with the build id
|
||
|
|
*/
|
||
|
|
import { promises as fs } from "node:fs";
|
||
|
|
import path from "node:path";
|
||
|
|
import { fileURLToPath } from "node:url";
|
||
|
|
|
||
|
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||
|
|
const OUT = path.join(ROOT, "out");
|
||
|
|
|
||
|
|
async function exists(p) {
|
||
|
|
try {
|
||
|
|
await fs.access(p);
|
||
|
|
return true;
|
||
|
|
} catch {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
if (!(await exists(OUT))) throw new Error("out/ not found — run `next build` first");
|
||
|
|
|
||
|
|
await fs.copyFile(path.join(ROOT, "deploy/htaccess.root"), path.join(OUT, ".htaccess"));
|
||
|
|
|
||
|
|
await fs.mkdir(path.join(OUT, "api/config"), { recursive: true });
|
||
|
|
await fs.copyFile(path.join(ROOT, "config/campuses.json"), path.join(OUT, "api/config/campuses.json"));
|
||
|
|
await fs.writeFile(path.join(OUT, "api/config/.htaccess"), "Require all denied\n");
|
||
|
|
|
||
|
|
for (const dir of ["api/lib", "api/cron"]) {
|
||
|
|
const ht = path.join(OUT, dir, ".htaccess");
|
||
|
|
if (!(await exists(ht))) await fs.writeFile(ht, "Require all denied\n");
|
||
|
|
}
|
||
|
|
|
||
|
|
// Next writes 404.html for the not-found route; make sure it exists for ErrorDocument
|
||
|
|
if (!(await exists(path.join(OUT, "404.html")))) {
|
||
|
|
console.warn("warning: out/404.html missing — custom 404 will not work");
|
||
|
|
}
|
||
|
|
|
||
|
|
const pkg = JSON.parse(await fs.readFile(path.join(ROOT, "package.json"), "utf8"));
|
||
|
|
await fs.writeFile(path.join(OUT, "BUILD.txt"), `${pkg.name} ${pkg.version}\nbuilt ${new Date().toISOString()}\n`);
|
||
|
|
|
||
|
|
// Report
|
||
|
|
let files = 0;
|
||
|
|
let bytes = 0;
|
||
|
|
async function walk(d) {
|
||
|
|
for (const e of await fs.readdir(d, { withFileTypes: true })) {
|
||
|
|
const p = path.join(d, e.name);
|
||
|
|
if (e.isDirectory()) await walk(p);
|
||
|
|
else {
|
||
|
|
files++;
|
||
|
|
bytes += (await fs.stat(p)).size;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
await walk(OUT);
|
||
|
|
console.log(`postbuild: out/ ready — ${files} files, ${(bytes / 1024 / 1024).toFixed(1)} MB`);
|
||
|
|
}
|
||
|
|
|
||
|
|
main().catch((e) => {
|
||
|
|
console.error(e);
|
||
|
|
process.exit(1);
|
||
|
|
});
|