#!/usr/bin/env bash # Build a complete CAD website package and a separate engine-only update archive. set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" cd "$ROOT_DIR" for tool in node cargo rustc trunk zip unzip; do if ! command -v "$tool" >/dev/null 2>&1; then printf 'Error: required build tool is missing: %s\n' "$tool" >&2 exit 1 fi done # The platform frontend is maintained in VibeMake; publishing it never builds the main site. SITE_ROOT="$(node -p "require('node:path').resolve(process.env.VIBEMAKE_SITE_ROOT || '../vibemake')")" for file in scripts/build-cad.mjs cad.html cad/editor/index.html js/cad/app.js css/cad.css; do if [[ ! -f "$SITE_ROOT/$file" ]]; then printf 'Error: CAD frontend input is missing: %s/%s\n' "$SITE_ROOT" "$file" >&2 printf 'Set VIBEMAKE_SITE_ROOT to the VibeMake source directory.\n' >&2 exit 1 fi done if ! node --input-type=module - "$SITE_ROOT/scripts/lib/version-site-assets.mjs" <<'NODE' import { pathToFileURL } from 'node:url'; await import(pathToFileURL(process.argv[2]).href); NODE then printf 'Error: install the CAD frontend packaging dependencies with npm ci in %s.\n' "$SITE_ROOT" >&2 exit 1 fi WASM_LIB_DIR="$(rustc --print target-libdir --target wasm32-unknown-unknown)" if [[ ! -d "$WASM_LIB_DIR" ]]; then printf 'Error: install the WebAssembly target first:\n rustup target add wasm32-unknown-unknown\n' >&2 exit 1 fi export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-"$ROOT_DIR/target"}" ENGINE_DIR="$(node -p "require('node:path').resolve(process.env.VIBEMAKE_CAD_ENGINE_OUT || 'dist/vibemake-cad-engine')")" export VIBEMAKE_CAD_ENGINE_OUT="$ENGINE_DIR" ARCHIVE="$ROOT_DIR/dist/vibemake-cad-engine.zip" SITE_ARCHIVE="$ROOT_DIR/dist/vibemake-cad.zip" printf 'Building the release Web engine...\n' if command -v nice >/dev/null 2>&1; then nice node "$ROOT_DIR/scripts/build-vibemake-cad.mjs" else node "$ROOT_DIR/scripts/build-vibemake-cad.mjs" fi mkdir -p "$ROOT_DIR/dist" STAGING_DIR="$(mktemp -d "$ROOT_DIR/dist/.web-package.XXXXXX")" trap 'rm -rf -- "$STAGING_DIR"' EXIT node --input-type=module - "$ENGINE_DIR" "$STAGING_DIR" <<'NODE' import { createHash } from 'node:crypto'; import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { dirname, join, resolve, sep } from 'node:path'; const output = resolve(process.argv[2]); const stage = resolve(process.argv[3]); const manifestBytes = readFileSync(join(output, 'engine.json')); const manifest = JSON.parse(manifestBytes); if (!/^[a-f0-9]{24}$/.test(manifest.buildId) || !Array.isArray(manifest.files) || manifest.files.length === 0) { throw new Error('The Web engine manifest is invalid'); } const prefix = `/cad/_assets/${manifest.buildId}/`; const assetRoot = resolve(output, '_assets', manifest.buildId) + sep; for (const file of manifest.files) { if (typeof file.path !== 'string' || !file.path.startsWith(prefix)) { throw new Error('An engine asset is outside the current build'); } const relativePath = file.path.slice('/cad/'.length); const source = resolve(output, relativePath); if (!source.startsWith(assetRoot)) { throw new Error('An engine asset has an invalid path'); } const bytes = readFileSync(source); const sha256 = createHash('sha256').update(bytes).digest('hex'); if (bytes.length !== file.size || sha256 !== file.sha256) { throw new Error(`Engine asset verification failed: ${file.path}`); } const target = join(stage, 'cad', relativePath); mkdirSync(dirname(target), { recursive: true }); writeFileSync(target, bytes); } writeFileSync(join(stage, 'cad', 'engine.json'), manifestBytes); writeFileSync(join(stage, 'cad', 'sw.js'), readFileSync(join(output, 'sw.js'))); writeFileSync(join(stage, 'cad', 'ENGINE-ONLY.txt'), 'Engine update only: this archive contains no HTML pages. For a complete CAD deployment, use vibemake-cad.zip from the same build.\n'); console.log(`Verified ${manifest.files.length} assets for build ${manifest.buildId}`); NODE (cd "$STAGING_DIR" && zip -q -r vibemake-cad-engine.zip cad) mv -f "$STAGING_DIR/vibemake-cad-engine.zip" "$ARCHIVE" printf '\nPackaging CAD HTML, scripts, styles and the freshly built engine...\n' VIBEMAKE_CAD_ENGINE_DIST="$ENGINE_DIR" \ VIBEMAKE_CAD_PACKAGE_DIR="$ROOT_DIR/dist/vibemake-cad" \ VIBEMAKE_CAD_PACKAGE_ZIP="$SITE_ARCHIVE" \ node "$SITE_ROOT/scripts/build-cad.mjs" printf '\nWeb build completed successfully.\n' printf 'Engine directory: %s\n' "$ENGINE_DIR" printf 'Engine manifest: %s/engine.json\n' "$ENGINE_DIR" printf 'DEPLOY THIS COMPLETE CAD ZIP: %s\n' "$SITE_ARCHIVE" printf 'Engine-only update ZIP (no HTML): %s\n' "$ARCHIVE" printf '\nExtract the complete CAD ZIP directly into /cad/.\n' printf 'Its root contains index.html, editor/, js/, css/, vendor/ and the current engine.\n' printf 'Preserve the main site, PCB and existing CAD asset directories. Do not extract into cad/cad/.\n' printf 'The engine-only ZIP has a different layout: merge it into the website root.\n' printf 'No main-site build or deployment is required.\n'