Skip to content

Commit

Permalink
feat: migrate Bash builder script to node.js for enhanced cross-platf…
Browse files Browse the repository at this point in the history
…orm compatibility
  • Loading branch information
mathiasschopmans committed Mar 6, 2024
1 parent bedb662 commit ba82063
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 51 deletions.
107 changes: 107 additions & 0 deletions bin/techradar.js
Original file line number Diff line number Diff line change
@@ -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"));
}
49 changes: 0 additions & 49 deletions bin/techradar.sh

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit ba82063

Please sign in to comment.