From ba8206361965293ea05af774a50557aa1b2d8d3e Mon Sep 17 00:00:00 2001 From: Mathias Schopmans Date: Wed, 6 Mar 2024 14:20:30 +0100 Subject: [PATCH] feat: migrate Bash builder script to node.js for enhanced cross-platform compatibility --- bin/techradar.js | 107 +++++++++++++++++++++++++++++++++++++++++++++++ bin/techradar.sh | 49 ---------------------- package.json | 4 +- 3 files changed, 109 insertions(+), 51 deletions(-) create mode 100644 bin/techradar.js delete mode 100755 bin/techradar.sh diff --git a/bin/techradar.js b/bin/techradar.js new file mode 100644 index 00000000..a9b05ff6 --- /dev/null +++ b/bin/techradar.js @@ -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")); +} diff --git a/bin/techradar.sh b/bin/techradar.sh deleted file mode 100755 index 7520aa0e..00000000 --- a/bin/techradar.sh +++ /dev/null @@ -1,49 +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 "$CWD/config.json" "$BUILDER_DIR/data/config.json" || error "Could not find configuration file $CWD/config.json" -cp -R "$CWD/radar" "$BUILDER_DIR/data/radar" || error "Could find radar data in $CWD/radar" -cp -R $CWD/public/* "$BUILDER_DIR/public/" || error "Could not copy $CWD/public to $BUILDER_DIR/public" -cp "$CWD/about.md" "$BUILDER_DIR/data/about.md" || error "Could not copy $CWD/about.md" - -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 diff --git a/package.json b/package.json index b02bf027..c5e78841 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "aoe_technology_radar", - "version": "4.0.0-alpha.5", + "version": "4.0.0-alpha.6", "private": true, "bin": { - "techradar": "./bin/techradar.sh" + "techradar": "./bin/techradar.js" }, "scripts": { "dev": "next dev --turbo",