#!/usr/bin/env node import { createHash } from "node:crypto"; import { readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const assetReferencePattern = /(\b(?:src|href)=")(\.\/assets\/[^"?]+\.(?:css|js))(?:\?v=[^"]+)?(")/g; export function cacheBustZooModelingAppAssets(outputDir) { const indexPath = path.join(outputDir, "index.html"); const source = readFileSync(indexPath, "utf8"); let versionedAssetCount = 0; const versionedSource = source.replace( assetReferencePattern, (_match, prefix, assetReference, suffix) => { const assetPath = path.join(outputDir, assetReference.slice(2)); const version = createHash("sha256") .update(readFileSync(assetPath)) .digest("hex") .slice(0, 12); versionedAssetCount += 1; return `${prefix}${assetReference}?v=${version}${suffix}`; } ); if (versionedAssetCount === 0) { throw new Error(`No versioned HiCAD assets found in ${indexPath}`); } writeFileSync(indexPath, versionedSource); return versionedAssetCount; } const invokedPath = process.argv[1] ? path.resolve(process.argv[1]) : ""; if (invokedPath === fileURLToPath(import.meta.url)) { const outputDir = path.resolve( process.argv[2] || "public/vendor/zoo-modeling-app" ); const assetCount = cacheBustZooModelingAppAssets(outputDir); console.log(`Versioned ${assetCount} HiCAD entry assets in ${outputDir}`); }