-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate Bash builder script to node.js for enhanced cross-platf…
…orm compatibility
- Loading branch information
1 parent
bedb662
commit ba82063
Showing
3 changed files
with
109 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters