Build the website on server side

main
Aditya Thakral 1 year ago
parent 2d884595df
commit f3b0472a4a
  1. 57
      index.ts

@ -1,19 +1,18 @@
import { execSync } from "child_process";
import path from "path";
import dotenv from "dotenv";
import Express from "express";
dotenv.config();
import Express from "express";
const app = Express();
const PORT = process.env.PORT;
const base64 = Buffer.from(
`${process.env.GITLAB_USER}:${process.env.PASS}`,
`${process.env.GITEA_USER}:${process.env.PASS}`,
).toString("base64");
app.use(Express.static(__dirname + "/out"));
app.post("/", (req, res) => {
const header = req.header("authorization");
@ -39,29 +38,49 @@ app.post("/", (req, res) => {
return;
}
updateWebsite();
updateWebsite("main");
res.send("Updating website ...");
res.send("Updated!");
});
app.listen(PORT, () => {
console.log(`👀 http://localhost:${PORT}`);
});
async function updateWebsite() {
const branch = "main";
const job = "staging";
const url = `https://git.uwaterloo.ca/csc/website/-/jobs/artifacts/${branch}/download?job=${job}`;
const USER = process.env.USER!;
const REPO_NAME = "www-new";
const URL = `https://git.csclub.uwaterloo.ca/www/${REPO_NAME}.git`;
const WWW_DIR = process.env.WWW_DIR ?? "~/www/csc";
const $ = (cmd: string, cwd?: string) => {
console.log(cwd ? `${cwd} >` : ">", cmd);
const zipPath = `zips/${branch}.zip`;
const out = execSync(cmd, { cwd, encoding: "utf8" });
console.log(out);
// Wait for pipeline to succeed and artifacts to be available
await sleep(120);
return out;
};
execSync(`wget -O ${zipPath} '${url}'`);
execSync(`yes All | unzip ${zipPath}`);
}
// Makes sure that the directory is present
$(`mkdir -p ${WWW_DIR}`);
function updateWebsite(branch: string) {
const tmpDir = $("mktemp --directory").trim();
$(`git clone ${URL}`, tmpDir);
const repo_$ = (cmd: string) => $(cmd, path.join(tmpDir, REPO_NAME));
repo_$(`git checkout ${branch}`);
repo_$(`npm install`);
repo_$(`NEXT_PUBLIC_BASE_PATH="/~${USER}/csc/${branch}" npm run build`);
repo_$(`npm run export`);
const finalDir = path.join(WWW_DIR, branch);
$(`rm -rf ${finalDir}`);
$(`mkdir -p ${finalDir}`);
repo_$(`mv ./out/* ${finalDir}`);
function sleep(seconds: number) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
$(`rm -rf ${tmpDir}`);
}

Loading…
Cancel
Save