diff --git a/index.ts b/index.ts index 4cc7efa..bab6c75 100644 --- a/index.ts +++ b/index.ts @@ -38,9 +38,7 @@ app.post("/", (req, res) => { return; } - updateWebsite("main"); - - res.send("Updated!"); + res.send(updateWebsite("main")); }); app.listen(PORT, () => { @@ -51,19 +49,39 @@ 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 $ = ( + cmd: string, + cwd?: string, + log: (..._: string[]) => void = console.log, +) => { + log(cwd ? `${cwd} >` : ">", cmd); const out = execSync(cmd, { cwd, encoding: "utf8" }); - console.log(out); + log(out); return out; }; +function createLogger$() { + const logs: string[] = []; + + const log = (...args: string[]) => { + console.log(...args); + logs.push(args.join(" ")); + }; + + return { + $: (cmd: string, cwd?: string) => $(cmd, cwd, log), + logs, + }; +} + // Makes sure that the directory is present $(`mkdir -p ${WWW_DIR}`); function updateWebsite(branch: string) { + const { $, logs } = createLogger$(); + const tmpDir = $("mktemp --directory").trim(); $(`git clone ${URL}`, tmpDir); @@ -84,4 +102,6 @@ function updateWebsite(branch: string) { $(`rm -rf ${tmpDir}`); console.log("Done!"); + + return logs.join("\n"); }