Add deploy site script

This commit is contained in:
Diego Marcos Segura
2025-05-28 23:14:51 -07:00
parent f09b8ef468
commit 6ec3927a28
5 changed files with 72 additions and 3 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
import fs from "node:fs";
import { copyFile, mkdir, rename } from "node:fs/promises";
import { copyFile, mkdir, rename, rmSync } from "node:fs/promises";
import path from "node:path";
function copyDir(src, dest) {
+68
View File
@@ -0,0 +1,68 @@
import { execSync } from "node:child_process";
import fs from "node:fs/promises";
import { rm } from "node:fs/promises";
import path from "node:path";
const siteRemoteURL = "git@github.com:forge-gfx/forge-gfx.github.io.git";
const siteRepoDir = "site-repo";
const siteBuild = "site";
function run(cmd, options = {}) {
console.log(`> ${cmd}`);
execSync(cmd, { stdio: "inherit", ...options });
}
async function copyDir(src, dest) {
await fs.mkdir(dest, { recursive: true });
const entries = await fs.readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
await copyDir(srcPath, destPath);
} else {
await fs.copyFile(srcPath, destPath);
}
}
}
async function emptyDirectory(directoryPath) {
try {
const files = await fs.readdir(directoryPath);
for (const file of files) {
if (file === ".git" || file === "CNAME") continue;
const filePath = path.join(directoryPath, file);
const stat = await fs.lstat(filePath);
if (stat.isDirectory()) {
await fs.rm(filePath, { recursive: true, force: true });
} else {
await fs.unlink(filePath);
}
console.log("Deleted:", filePath);
}
} catch (err) {
console.error("Error processing directory:", err);
}
}
try {
await fs.access(siteRepoDir);
} catch {
run(`git clone ${siteRemoteURL} ${siteRepoDir}`);
}
run("git reset --hard", { cwd: siteRepoDir });
run("git fetch origin", { cwd: siteRepoDir });
run("git merge --ff-only origin/main", { cwd: siteRepoDir });
await emptyDirectory(siteRepoDir);
await copyDir(siteBuild, siteRepoDir);
run("git add .", { cwd: siteRepoDir });
run('git commit -m "Update Forge site"', { cwd: siteRepoDir });
run("git push origin main", { cwd: siteRepoDir });