feat: migrate Bash builder script to node.js for enhanced cross-platform compatibility

This commit is contained in:
Mathias Schopmans
2024-03-06 14:20:30 +01:00
committed by Mathias Schopmans
parent b7efe8060f
commit 7ed4ca9eb2
3 changed files with 109 additions and 48 deletions

107
bin/techradar.js Normal file
View File

@@ -0,0 +1,107 @@
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const crypto = require("crypto");
const CWD = process.cwd();
const BUILDER_DIR = path.join(CWD, ".techradar");
const SOURCE_DIR = path.join(CWD, "node_modules", "aoe_technology_radar");
const HASH_FILE = path.join(BUILDER_DIR, "hash");
const PARAMETER = process.argv[2]; // "build" or "serve"
function info(message) {
console.log(`\x1b[32m${message}\x1b[0m`);
}
function error(message) {
console.error(`Error: ${message}`);
process.exit(1);
}
// Calculate current hash of package.json
function calculateHash(file) {
const fileBuffer = fs.readFileSync(file);
const hashSum = crypto.createHash("sha256");
hashSum.update(fileBuffer);
return hashSum.digest("hex");
}
const CURRENT_HASH = calculateHash(path.join(CWD, "package.json"));
// Check if builder dir needs to be recreated
let RECREATE_DIR = false;
if (
!fs.existsSync(BUILDER_DIR) ||
!fs.existsSync(HASH_FILE) ||
fs.readFileSync(HASH_FILE, "utf8") !== CURRENT_HASH
) {
RECREATE_DIR = true;
}
if (RECREATE_DIR) {
// Remove existing builder dir if it exists
if (fs.existsSync(BUILDER_DIR)) {
fs.rmdirSync(BUILDER_DIR, { recursive: true });
}
// Copy source dir to builder dir
try {
fs.cpSync(SOURCE_DIR, BUILDER_DIR, { recursive: true });
fs.writeFileSync(HASH_FILE, CURRENT_HASH);
} catch (e) {
error(`Could not copy ${SOURCE_DIR} to ${BUILDER_DIR}`);
}
try {
process.chdir(BUILDER_DIR);
info("Installing npm packages");
execSync("npm install", { stdio: "inherit" });
} catch (e) {
error("Could not install npm packages");
}
}
if (fs.existsSync(path.join(BUILDER_DIR, "data", "radar"))) {
fs.rmdirSync(path.join(BUILDER_DIR, "data", "radar"), { recursive: true });
}
try {
fs.cpSync(path.join(CWD, "radar"), path.join(BUILDER_DIR, "data", "radar"), {
recursive: true,
});
fs.cpSync(path.join(CWD, "public"), path.join(BUILDER_DIR, "public"), {
recursive: true,
});
fs.copyFileSync(
path.join(CWD, "about.md"),
path.join(BUILDER_DIR, "data", "about.md"),
);
fs.copyFileSync(
path.join(CWD, "config.json"),
path.join(BUILDER_DIR, "data", "config.json"),
);
process.chdir(BUILDER_DIR);
} catch (e) {
error(e.message);
}
info("Building data");
execSync("npm run build:data", { stdio: "inherit" });
if (PARAMETER === "serve") {
info("Starting techradar");
execSync("npm run dev", { stdio: "inherit" });
}
if (PARAMETER === "build") {
info("Building techradar");
execSync("npm run build", { stdio: "inherit" });
if (fs.existsSync(path.join(CWD, "build"))) {
fs.rmdirSync(path.join(CWD, "build"), { recursive: true });
}
info(`Copying techradar to ${path.join(CWD, "build")}`);
fs.renameSync(path.join(BUILDER_DIR, "out"), path.join(CWD, "build"));
}

View File

@@ -1,46 +0,0 @@
#!/bin/bash
CWD=$(pwd)
BUILDER_DIR="$CWD/.techradar"
SOURCE_DIR="$CWD/node_modules/aoe_technology_radar"
PARAMETER=$1 # "build" or "serve"
function info {
echo -e "\033[32m$1\033[0m"
}
function error {
echo "Error: $1"
exit 1
}
# create builder dir by copying source dir if it does not exist
if [ ! -d "$BUILDER_DIR" ]; then
cp -R "$SOURCE_DIR" "$BUILDER_DIR" || error "Could not copy $SOURCE_DIR to $BUILDER_DIR"
cd "$BUILDER_DIR" || error "Could not change to $BUILDER_DIR"
info "Installing npm packages"
npm install || error "Could not install npm packages"
fi
cp -R "$CWD/radar" "$BUILDER_DIR/data/radar" || error "Could not copy $CWD/radar to $BUILDER_DIR/data/radar"
cp -R $CWD/public/* "$BUILDER_DIR/public/" || error "Could not copy $CWD/public to $BUILDER_DIR/public"
cd "$BUILDER_DIR" || error "Could not change to $BUILDER_DIR"
info "Building data"
npm run build:data
if [ "$PARAMETER" == "serve" ]; then
info "Starting techradar"
npm run dev
fi
if [ "$PARAMETER" == "build" ]; then
info "Building techradar"
npm run build
if [ -d "$CWD/build" ]; then
rm -rf "$CWD/build"
fi
info "Copying techradar to $CWD/build"
mv "$BUILDER_DIR/out" "$CWD/build"
fi