Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalabasa committed May 10, 2024
1 parent 7a18be7 commit 44c0eb9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
File renamed without changes.
74 changes: 59 additions & 15 deletions lathala/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@ import {
rmSync,
statSync,
default as fs,
openSync,
closeSync,
} from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { cwd, exit, stdin, stdout } from "node:process";
import { chdir, cwd, exit, stdin, stdout } from "node:process";
import { createInterface } from "node:readline/promises";
import { goTop } from "../tools/top.js";
import { execSync } from "node:child_process";
const require = createRequire(import.meta.url);

goTop();
const topDir = path.resolve();

const args = arg({
"--dry-run": Boolean,
"--no-prepare": Boolean,
"--no-deploy": Boolean,
});
const argDirs = args._;

const rsyncArgs =
" --checksum --del --progress --recursive" +
(args["--dry-run"] ? " --dry-run" : "");

// Read configs
const configs = glob
.sync("*/lathala.json")
Expand All @@ -33,6 +42,8 @@ const configs = glob
.sort((a, b) => a.dstDir.length - b.dstDir.length);

const wwwDir = path.resolve("www");
const wwwStagingDir = `${wwwDir}/staging`;
const wwwProdDir = `${wwwDir}/prod`;

// Pre-process commands
const commands = [];
Expand All @@ -47,7 +58,7 @@ for (const argDir of argDirs) {

const rootDir = path.resolve(config.rootDir);
const srcDir = path.resolve(rootDir, config.srcDir);
const dstDir = path.resolve(wwwDir, config.dstDir);
const dstDir = path.resolve(wwwStagingDir, config.dstDir);
const prepareScriptPath =
config.prepare && path.resolve(rootDir, config.prepare);

Expand All @@ -57,7 +68,7 @@ for (const argDir of argDirs) {
continue;
}

if (prepareScriptPath) {
if (!args["--no-prepare"] && prepareScriptPath) {
const stat = statSync(prepareScriptPath);
if (!stat.isFile || !(stat.mode & fs.constants.S_IXUSR)) {
console.error(
Expand All @@ -75,13 +86,14 @@ for (const argDir of argDirs) {
.filter(
(otherDir) =>
config.dstDir !== otherDir &&
!path.relative(dstDir, path.resolve(wwwDir, otherDir)).startsWith("..")
!path
.relative(dstDir, path.resolve(wwwStagingDir, otherDir))
.startsWith("..")
)
.map((otherDir) => `--exclude '${otherDir}'`)
.join(" ");
.map((otherDir) => ` --exclude '${otherDir}'`);
commands.push(
`rsync -Pr --del ` +
(args["--dry-run"] ? " --dry-run" : "") +
'rsync' +
rsyncArgs +
excludes +
` '${path.relative(".", srcDir)}/'` +
` '${path.relative(".", dstDir)}/'`
Expand All @@ -92,24 +104,56 @@ if (invalidState || !commands.length) {
exit(1);
}

// Confirm
// Confirm run
console.log(`Preview: \n${commands.map((cmd) => "> " + cmd).join("\n")}`);
const rl = createInterface({ input: stdin, output: stdout });
const answer = await rl.question("Run commands? (y/n) ");
const runCommandsAnswer = await rl.question("Run commands? (y/n) ");
rl.close();
if (answer.toLowerCase() !== "y") exit(0);
if (runCommandsAnswer.toLowerCase() !== "y") exit(0);

// Setup working directory
try {
mkdirSync(wwwDir, { recursive: true });
mkdirSync(wwwStagingDir, { recursive: true });

// Run commands
for (const command of commands) {
execSync(command, { stdio: "inherit" });
}
} finally {
// Clean up
// if (!args["--clean-"]) {
// rmSync(wwwDir, { recursive: true });
// }
// Clean up?
}

if (!args["--no-deploy"]) {
// Confirm deploy
console.log(
`Website files in ${path.relative(".", wwwStagingDir)} up to date`
);
const rl = createInterface({ input: stdin, output: stdout });
const deployWebsiteAnswer = await rl.question("Deploy website? (y/n) ");
rl.close();
if (deployWebsiteAnswer.toLowerCase() !== "y") exit(0);

try {
rmSync(wwwProdDir, { recursive: true, force: true });
execSync("git fetch", { stdio: "inherit" });
execSync(
`git worktree add -f ${path.relative(".", wwwProdDir)} origin/master`,
{ stdio: "inherit" }
);

closeSync(openSync(`${wwwProdDir}/.nojekyll`, "a"));

execSync(`rsync ${rsyncArgs} '.github/' '${wwwProdDir}/.github/'`, {
stdio: "inherit",
});
execSync(
`rsync ${rsyncArgs} '${wwwStagingDir}/' '${wwwProdDir}/docs/'`,
{ stdio: "inherit" }
);
} finally {
goTop();
// rmSync(wwwProdDir, { recursive: true });
execSync("git worktree prune", { stdio: "inherit" });
}
}
Empty file removed root/prod-static/.nojekyll
Empty file.
12 changes: 8 additions & 4 deletions tools/top.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const ChildProcess = require("node:child_process");
const { execSync } = require("node:child_process");
const path = require("node:path");

module.exports = {
goTop() {
const top = ChildProcess.execSync("git rev-parse --show-toplevel");
const top = path.resolve(__dirname, "..");
if (!top.endsWith("/kalabasa.github.io")) {
throw new Error("Unexpected top directory!");
}
process.chdir(top.toString().trim());
console.log(">", process.cwd());
}
}
},
};

0 comments on commit 44c0eb9

Please sign in to comment.