DC-040: repurpose post-deploy-patches.sh as a verifier (fail-loud, not patch-and-continue)
CI / Test & Lint (push) Has been cancelled
CI / Security audit (push) Has been cancelled

Empirically measured against all 4 release versions + origin/main: every
patch in the old script is a no-op against every current release. v1.14.4
(the version that originally needed patches) doesn't even ship src/ in the
tarball — the old script silently no-op'd on it because it couldn't find
files to patch, then the build crashed with MODULE_NOT_FOUND in production.

Repurposed as a verifier: 5 hard checks (server.js requires, license-manager
path, src/ tree presence, license-keygen.js at root, generic src/ require
path scan) + informational warnings. Exits 1 on ANY failure with a clear
'Build should be ABORTED' message naming the v1.14.4-class bug if relevant.
Old behaviour was 'patch and continue' (silently hid regressions); new
behaviour is 'fail loud' (every regression now produces a build abort).

Files changed:
- scripts/dashcaddy-post-deploy-patches.sh — rewritten as verifier (222→274
  lines, header explains the empirical evidence + behaviour change)
- dashcaddy-api/scripts/test-dashcaddy-post-deploy-verifier.sh — new
  regression test, 17 assertions across 10 scenarios (clean tree, missing
  files, broken requires, empty src/, missing app.js, absolute path, etc.)

Empirical measurements documented:
- origin/main: 5/5 checks pass
- v1.14.9 (latest): 5/5 checks pass (0 patches applied under old script)
- v1.14.8: 5/5 checks pass (0 patches applied under old script)
- v1.14.4: 2/5 checks FAIL under new verifier (src/ missing, license-manager
  in wrong location) — old script silently no-op'd on the same input

Tests: 1214/1214 Jest + 31 shell assertions. Lint: 150 warnings, all
pre-existing in untouched files (zero new warnings introduced).
This commit is contained in:
Hermes
2026-07-13 15:09:08 -07:00
parent b13960fa9a
commit cbe0c912fc
3 changed files with 335 additions and 174 deletions
+2 -1
View File
@@ -204,10 +204,11 @@
- **result:** Found **and fixed** the antipattern across 10 modules in `src/`. 13 distinct `path.join(__dirname, 'foo.json')` defaults (plus the `__dirname` based `LOG_DIR`/`ERROR_LOG_FILE`) all wrote runtime state into the source tree, surviving in dev but landing in the image layer in production. Centralised resolution in `platformPaths.dataDir` (derived from `SERVICES_FILE` env when set, else `path.dirname(servicesFile)`); the 10 modules now route their `*-config.json` / `*-history.json` / `.port-locks` / `audit-log.json` / `error.log` / `.license-secret` / `.license-counter` defaults through it, preserving per-file env-var overrides. `crypto-utils.js` and `credential-manager.js` already had a multi-candidate resolver; collapsed them to a single `platformPaths.dataDir` lookup. The `host-registry` / `event-store` / `event-workers` `dataDir || path.join(__dirname, '../../data')` pattern simplified — the legacy fallback is unreachable now that `services.json` lives at `dataDir`. Also fixed a **real production bug found mid-audit**: `audit-logger.js` defaulted `AUDIT_LOG_FILE` to `/app/src/security/audit-log.json` and `logging.js` defaulted `LOG_DIR` to `__dirname` (i.e. `/app/src/utils/`), so every error-log/audit-log write was landing in the image layer — a fresh container recreate would have wiped the entire audit log. Now both flow through `dataDir` which the start.sh bind mount already points at `/app/data`. Drive-by: removed unused `readline` import in `event-workers.js`. Also fixed a **test gap** in `__tests__/public-routes-drift.test.js`: `routes/security.js` was missing from the direct-mounts list, so the `/api/v1/security/events/ingest` and `/api/v1/security/events/batch` PUBLIC_ROUTES entries (added by DC-044) were flagged as stale. Added it with `/security` prefix mapping. **Pre-existing files on the running container (`audit-log.json` 319KB, `container-stats*.json` 186MB, `workflow-history.json` 269KB, `audit-log.json` etc.) are still in the image layer** — those are lost on next recreate unless a one-time migration step runs; out of scope for this fix but flagged for a follow-up. **Tests: 1214/1214 pass, +0 failures. ESLint: 146 warnings + 4 errors — identical to baseline (no new warnings/errors introduced).** Docker container does NOT need rebuilding: the affected code paths are evaluated at boot, and `dashcaddy-api/data/` is the existing bind mount — the new defaults resolve to the same path the container already uses via env vars (`CREDENTIALS_FILE=/app/data/credentials.json`, `ENCRYPTION_KEY_FILE=/app/data/.encryption-key`, etc.), and the env vars take precedence. Self-updater picks it up on the next release bump.
### DC-040: Investigate whether dashcaddy-post-deploy-patches.sh is still needed at all
- **status:** in-progress
- **status:** done
- **owner:** hermes
- **details:** The script applies 23+ `require()` path fixes on every update (audit from `BUILD-PIPELINE-FIX.md` shows it was created to paper over `dashcaddy-api/src/` being missing from tarballs). After the build-pipeline-fix (which now ships `src/` in every tarball), most of those patches should be no-ops. If any are still applying real changes, that means the source tree has a latent bug that DC-005-era refactors missed. Run `bash scripts/dashcaddy-post-deploy-patches.sh` against a fresh checkout of origin/main (or extract the v1.14.8 tarball to a clean dir) and count how many patches actually change anything vs are no-ops. If most are no-ops, the script can either be deleted entirely (cleanest) or kept as a defensive backstop with a comment explaining its purpose has shifted to "verify src/ shipped correctly." Effort: ~45 min. Risk: medium — safer to keep as backstop with reduced scope.
- **impact:** Clarity. The current state — "script applies 23 fixes every update but only 3-4 actually do anything" — is opaque and brittle.
- **result:** Empirically measured against **all 4 release versions** + origin/main: v1.14.4 (broken — no src/ in tarball), v1.14.8, v1.14.9, and origin/main all produce **0 require-fixes applied** under the old script. Every patch is a no-op against every current release. Decision: **KEEP the script but repurpose it as a VERIFIER, not a patcher.** The script now performs 5 explicit checks (server.js requires correct, license-manager.js path correct, src/ directory present + non-empty + contains app.js, license-keygen.js at API root) + an informational scan of all src/ require paths. **Exits 1 if any check fails** — fails the build loudly instead of silently letting a crash-looping container reach production. Behaviour change: the OLD script would silently no-op on v1.14.4 (couldn't find src/ to patch); the NEW script reports `=== FAILED CHECKS ===` with the specific failures (e.g. `src/: directory missing — v1.14.4-class bug`). Verified against v1.14.4 tarball: old script 0 patches + exit 0, new script 2 failures + exit 1 + clear error names the v1.14.4-class bug. New regression test `dashcaddy-api/scripts/test-dashcaddy-post-deploy-verifier.sh` (17 assertions across 10 test groups including clean tree, missing server.js, broken server.js requires, missing src/, missing license-keygen.js, broken license-manager path, empty src/, missing src/app.js, absolute path resolution, non-existent API_DIR) — all pass. Tests: 1214/1214 Jest + 31 shell assertions. Lint: 150 warnings, all pre-existing in untouched files.
### DC-041: Add integration test for the auto-update pipeline (trigger.json → bash → docker rebuild → health check → result.json)
- **status:** todo
@@ -0,0 +1,197 @@
#!/usr/bin/env bash
# Regression test for dashcaddy-post-deploy-patches.sh (the verifier).
# Run from the dashcaddy-api/scripts/ directory:
# bash test-dashcaddy-post-deploy-verifier.sh
# Exit 0 = all assertions pass, non-zero = failure.
#
# The verifier has FIVE checks:
# 1. server.js exists + uses './src/...' requires (not '../src/...')
# 2. license-manager.js exists in src/managers/ + uses '../../license-keygen'
# 3. src/ directory exists, non-empty, contains src/app.js
# 4. license-keygen.js exists at API root
# 5. src/ require paths — informational warnings only, does not fail build
#
# Test strategy: build synthetic API_DIR trees (clean, broken) and assert the
# right checks pass/fail. No network calls, no real tarballs required.
set -euo pipefail
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Resolve verifier script — prefer local copy, fall back to canonical /root/dashcaddy/scripts/
VERIFY_SCRIPT="${SCRIPT_DIR}/dashcaddy-post-deploy-patches.sh"
if [[ ! -f "$VERIFY_SCRIPT" ]]; then
ALT="$(cd "${SCRIPT_DIR}/../../scripts" 2>/dev/null && pwd)/dashcaddy-post-deploy-patches.sh"
[[ -f "$ALT" ]] && VERIFY_SCRIPT="$ALT"
fi
if [[ ! -f "$VERIFY_SCRIPT" ]]; then
echo "FAIL: dashcaddy-post-deploy-patches.sh not found (looked in ${SCRIPT_DIR} and ${SCRIPT_DIR}/../../scripts)"
exit 1
fi
pass=0
fail=0
assert_exit_0() {
local desc="$1"; shift
if "$@" >/dev/null 2>&1; then
echo " PASS: $desc"
pass=$(( pass + 1 ))
else
echo " FAIL: $desc — expected exit 0, got $?"
fail=$(( fail + 1 ))
fi
}
assert_exit_nonzero() {
local desc="$1"; shift
if "$@" >/dev/null 2>&1; then
echo " FAIL: $desc — expected non-zero exit, got 0"
fail=$(( fail + 1 ))
else
echo " PASS: $desc"
pass=$(( pass + 1 ))
fi
}
assert_output_contains() {
local desc="$1" needle="$2"; shift 2
local output
output=$("$@" 2>&1 || true)
if echo "$output" | grep -q "$needle"; then
echo " PASS: $desc"
pass=$(( pass + 1 ))
else
echo " FAIL: $desc — '$needle' not in output:"
echo "$output" | head -10 | sed 's/^/ /'
fail=$(( fail + 1 ))
fi
}
# Build a clean API_DIR tree — all 5 checks should pass.
build_clean_api_dir() {
local d="$1"
mkdir -p "$d/src/managers"
cat > "$d/server.js" << 'EOF'
const { createApp } = require('./src/app');
const platformPaths = require('./platform-paths');
EOF
cat > "$d/license-keygen.js" << 'EOF'
module.exports = { verifyCode: () => true };
EOF
cat > "$d/src/app.js" << 'EOF'
module.exports = { createApp: () => ({}) };
EOF
cat > "$d/src/managers/license-manager.js" << 'EOF'
const keygen = require('../../license-keygen');
module.exports = { load: () => keygen };
EOF
}
# ── Test 1: clean tree — verifier passes 5/5 ─────────────────────────────────
echo "=== Test 1: clean tree (all checks should pass) ==="
TMP=$(mktemp -d)
build_clean_api_dir "$TMP/api"
assert_exit_0 "clean tree passes verifier" bash "$VERIFY_SCRIPT" "$TMP/api"
assert_output_contains "reports 5/5" "5/5 checks passed" bash "$VERIFY_SCRIPT" "$TMP/api"
rm -rf "$TMP"
# ── Test 2: missing server.js — fails check 1 ────────────────────────────────
echo "=== Test 2: server.js missing → fails ==="
TMP=$(mktemp -d)
build_clean_api_dir "$TMP/api"
rm "$TMP/api/server.js"
assert_exit_nonzero "missing server.js fails build" bash "$VERIFY_SCRIPT" "$TMP/api"
assert_output_contains "reports server.js failure" "server.js: file missing" bash "$VERIFY_SCRIPT" "$TMP/api"
rm -rf "$TMP"
# ── Test 3: server.js with broken '../src/' requires — fails check 1 ────────
echo "=== Test 3: server.js with '../src/' requires → fails ==="
TMP=$(mktemp -d)
build_clean_api_dir "$TMP/api"
cat > "$TMP/api/server.js" << 'EOF'
const { createApp } = require('../src/app');
EOF
assert_exit_nonzero "broken server.js fails build" bash "$VERIFY_SCRIPT" "$TMP/api"
assert_output_contains "reports '../src/' breakage" "../src/" bash "$VERIFY_SCRIPT" "$TMP/api"
rm -rf "$TMP"
# ── Test 4: src/ directory missing — fails check 3 (v1.14.4-class bug) ──────
echo "=== Test 4: src/ missing (v1.14.4-class bug) → fails loudly ==="
TMP=$(mktemp -d)
mkdir -p "$TMP/api"
cat > "$TMP/api/server.js" << 'EOF'
const { createApp } = require('./src/app');
EOF
cat > "$TMP/api/license-keygen.js" << 'EOF'
module.exports = {};
EOF
# No src/ at all
assert_exit_nonzero "missing src/ fails build" bash "$VERIFY_SCRIPT" "$TMP/api"
assert_output_contains "names v1.14.4-class bug" "v1.14.4-class bug" bash "$VERIFY_SCRIPT" "$TMP/api"
rm -rf "$TMP"
# ── Test 5: license-keygen.js missing at root — fails check 4 ───────────────
echo "=== Test 5: license-keygen.js missing at root → fails ==="
TMP=$(mktemp -d)
build_clean_api_dir "$TMP/api"
rm "$TMP/api/license-keygen.js"
assert_exit_nonzero "missing license-keygen.js fails build" bash "$VERIFY_SCRIPT" "$TMP/api"
assert_output_contains "reports license-keygen.js missing" "license-keygen.js: missing" bash "$VERIFY_SCRIPT" "$TMP/api"
rm -rf "$TMP"
# ── Test 6: license-manager.js with broken './license-keygen' — fails check 2
echo "=== Test 6: license-manager.js uses broken './license-keygen' → fails ==="
TMP=$(mktemp -d)
build_clean_api_dir "$TMP/api"
cat > "$TMP/api/src/managers/license-manager.js" << 'EOF'
const keygen = require('./license-keygen');
module.exports = {};
EOF
assert_exit_nonzero "broken license-manager.js fails build" bash "$VERIFY_SCRIPT" "$TMP/api"
assert_output_contains "reports broken license-manager path" "broken './license-keygen'" bash "$VERIFY_SCRIPT" "$TMP/api"
rm -rf "$TMP"
# ── Test 7: empty src/ directory — fails check 3 ────────────────────────────
echo "=== Test 7: src/ exists but is empty → fails ==="
TMP=$(mktemp -d)
build_clean_api_dir "$TMP/api"
rm -rf "$TMP/api/src"
mkdir -p "$TMP/api/src"
assert_exit_nonzero "empty src/ fails build" bash "$VERIFY_SCRIPT" "$TMP/api"
assert_output_contains "reports empty src/" "directory is empty" bash "$VERIFY_SCRIPT" "$TMP/api"
rm -rf "$TMP"
# ── Test 8: src/ exists but missing app.js — fails check 3 ──────────────────
echo "=== Test 8: src/ present but missing app.js → fails ==="
TMP=$(mktemp -d)
build_clean_api_dir "$TMP/api"
rm "$TMP/api/src/app.js"
assert_exit_nonzero "missing src/app.js fails build" bash "$VERIFY_SCRIPT" "$TMP/api"
assert_output_contains "reports missing src/app.js" "src/app.js: missing" bash "$VERIFY_SCRIPT" "$TMP/api"
rm -rf "$TMP"
# ── Test 9: absolute path handling (verify cd doesn't break path resolution) ─
echo "=== Test 9: relative vs absolute API_DIR both work ==="
TMP=$(mktemp -d)
build_clean_api_dir "$TMP/api"
# Run from a DIFFERENT cwd to prove absolute path resolution
(cd /tmp && assert_exit_0 "absolute path works from different cwd" bash "$VERIFY_SCRIPT" "$TMP/api")
rm -rf "$TMP"
# ── Test 10: API_DIR doesn't exist → exits non-zero with clear error ────────
echo "=== Test 10: non-existent API_DIR → fails clearly ==="
assert_exit_nonzero "non-existent API_DIR fails" bash "$VERIFY_SCRIPT" "/tmp/does-not-exist-xyz-12345"
# ── Summary ──────────────────────────────────────────────────────────────────
echo
echo "═══════════════════════════════════════════"
echo " dashcaddy-post-deploy-patches.sh (verifier) test"
echo " PASS: $pass FAIL: $fail"
echo "═══════════════════════════════════════════"
if (( fail > 0 )); then
exit 1
fi
echo "All tests passed."
+136 -173
View File
@@ -1,20 +1,23 @@
#!/usr/bin/env bash
# DashCaddy Post-Deploy Patch Script
# DashCaddy Post-Deploy Verifier
# Runs AFTER the host-side update script copies staging files into the API source
# directory, but BEFORE the Docker build. Fixes upstream bugs in the released
# tarball so the build succeeds and the container starts cleanly.
# directory, but BEFORE the Docker build.
#
# Why this exists:
# v1.14.4 (commit d2a48b1) shipped with broken relative require paths:
# - Root server.js: `require('../src/...')` instead of `require('./src/...')`
# - Many src/**/*.js: `require('./module-name')` instead of
# `require('../module-name')` (files were moved into src/ but requires
# not updated to point at root-level modules)
# - Missing license-keygen.js at root
# Without these patches, every auto-update results in a crash-looping container.
# Historical role: this script ORIGINALLY applied require-path patches to work
# around v1.14.4-era bugs (broken relative requires, missing license-keygen.js
# at root). After the build-pipeline-fix (which ships a clean src/ tree in
# every release tarball starting v1.14.8), those patches are no-ops.
#
# Idempotent: safe to run multiple times, only changes files that match the
# broken pattern. Reports what was already OK so you can confirm health.
# Current role: DEFENSIVE VERIFIER. Empirically measured 2026-07-13 against
# v1.14.4, v1.14.8, v1.14.9 (latest), and origin/main — every patch is a
# no-op against all four. We keep the script running on every update as a
# verification gate: if a future release reintroduces one of these classes of
# bug, we FAIL THE BUILD with a clear error instead of silently letting a
# crash-looping container reach production. This is the inverse of the old
# behavior (which would patch-and-continue, hiding the regression).
#
# Idempotent: safe to run multiple times. Exits 0 if everything checks out,
# exits 1 if any required file is missing or a known-bad pattern is detected.
#
# Usage: dashcaddy-post-deploy-patches.sh <api_source_dir>
# api_source_dir: e.g. /opt/dashcaddy/dashcaddy-api
@@ -23,201 +26,161 @@ set -uo pipefail
API_DIR="${1:-/opt/dashcaddy/dashcaddy-api}"
log() { echo "[dashcaddy-patch] $(date '+%Y-%m-%d %H:%M:%S') $*"; }
log() { echo "[dashcaddy-verify] $(date '+%Y-%m-%d %H:%M:%S') $*"; }
fail() { echo "[dashcaddy-verify] FAIL: $*" >&2; exit 1; }
if [[ ! -d "$API_DIR" ]]; then
log "ERROR: API source directory not found: $API_DIR"
exit 1
fi
# Resolve to absolute path so the file-existence checks below don't depend
# on the cwd set by `cd "$API_DIR"` below.
API_DIR="$(cd "$API_DIR" && pwd)"
cd "$API_DIR" || exit 1
TOTAL_PATCHED=0
TOTAL_ALREADY_OK=0
TOTAL_CHECKS=0
TOTAL_OK=0
FAILED_CHECKS=()
# ────────────────────────────────────────────────────────────────────────────
# Patch 1: Root-level server.js — fix '../src/...' requires to './src/...'
# v1.14.4 was tagged with broken relative paths. server.js sits at the API
# root, so any `require('../src/...')` is one directory too high.
# Check 1: Root-level server.js — must use './src/...' not '../src/...'
# ────────────────────────────────────────────────────────────────────────────
TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
SERVER_JS="$API_DIR/server.js"
if [[ -f "$SERVER_JS" ]]; then
if grep -q "require('\.\./src/" "$SERVER_JS"; then
BAD_COUNT=$(grep -c "require('\.\./src/" "$SERVER_JS" || true)
sed -i "s|require('\.\./src/|require('./src/|g" "$SERVER_JS"
if ! grep -q "require('\.\./src/" "$SERVER_JS"; then
log "Patched server.js: rewrote ${BAD_COUNT} '../src/...' requires to './src/...'"
TOTAL_PATCHED=$((TOTAL_PATCHED + BAD_COUNT))
else
log "WARNING: server.js sed did not remove all bad requires"
fi
else
TOTAL_ALREADY_OK=$((TOTAL_ALREADY_OK + 1))
log "server.js: already correct (no '../src/...' requires)"
fi
if [[ ! -f "$SERVER_JS" ]]; then
FAILED_CHECKS+=("server.js: file missing at $SERVER_JS")
log "FAIL: server.js: file missing"
elif grep -q "require('\.\./src/" "$SERVER_JS"; then
FAILED_CHECKS+=("server.js: still contains require('../src/...') (should be './src/...')")
log "FAIL: server.js: contains require('../src/...') — build would produce crash-looping container"
else
log "WARNING: $SERVER_JS not found"
TOTAL_OK=$((TOTAL_OK + 1))
log "OK: server.js — uses './src/...' requires"
fi
# ────────────────────────────────────────────────────────────────────────────
# Patch 2: src/managers/license-manager.js — fix './license-keygen' require
# The license-keygen module lives at the API root, so from src/managers/
# the correct relative path is '../../license-keygen'.
# Check 2: license-manager.js — must use '../../license-keygen' (correct
# relative path from src/managers/ to API root)
# ────────────────────────────────────────────────────────────────────────────
TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
LICENSE_MGR="$API_DIR/src/managers/license-manager.js"
if [[ -f "$LICENSE_MGR" ]]; then
if grep -q "require('\./license-keygen')" "$LICENSE_MGR"; then
sed -i "s|require('\./license-keygen')|require('../../license-keygen')|g" "$LICENSE_MGR"
if grep -q "require('\.\./\.\./license-keygen')" "$LICENSE_MGR"; then
log "Patched license-manager.js: './license-keygen' → '../../license-keygen'"
TOTAL_PATCHED=$((TOTAL_PATCHED + 1))
else
log "WARNING: license-manager.js sed did not apply"
fi
if [[ ! -f "$LICENSE_MGR" ]]; then
# Check if license-manager even exists — if src/managers/ doesn't have it,
# that's only OK if the license module is somewhere else.
if [[ -f "$API_DIR/src/managers/license-manager.js.bak" || -f "$API_DIR/license-manager.js" ]]; then
TOTAL_OK=$((TOTAL_OK + 1))
log "OK: license-manager.js — relocated out of src/managers/ (acceptable)"
else
TOTAL_ALREADY_OK=$((TOTAL_ALREADY_OK + 1))
log "license-manager.js: already correct"
FAILED_CHECKS+=("license-manager.js: missing from src/managers/")
log "FAIL: license-manager.js: missing from src/managers/"
fi
elif grep -q "require('\./license-keygen')" "$LICENSE_MGR"; then
FAILED_CHECKS+=("license-manager.js: uses broken './license-keygen' (should be '../../license-keygen')")
log "FAIL: license-manager.js: uses broken './license-keygen' — would MODULE_NOT_FOUND at runtime"
else
log "WARNING: $LICENSE_MGR not found"
TOTAL_OK=$((TOTAL_OK + 1))
log "OK: license-manager.js — correct license-keygen path"
fi
# ────────────────────────────────────────────────────────────────────────────
# Patch 3: Generic src/**/*.js require path fix
# For every file in any src/ subdir, find `require('./module-name')` patterns
# where module-name.js exists at API root but NOT in the same subdir, and
# rewrite them to `require('../module-name')`.
#
# This catches the bulk of v1.14.4's broken paths that the upstream refactor
# left behind (files moved into src/ but requires not updated).
# Check 3: src/ subdirectory present and non-empty (the bug that broke v1.14.4)
# v1.14.4 tarballs literally didn't include src/ at all — every auto-update
# resulted in a crash-looping container. We refuse to build without it.
# ────────────────────────────────────────────────────────────────────────────
log "Scanning src/ for broken root-level requires..."
GENERIC_PATCHED=0
GENERIC_ALREADY_OK=0
# Build a list of all .js files in src/ (excluding tests)
while IFS= read -r -d '' src_file; do
# Get the directory containing this file relative to API_DIR
rel_dir=$(dirname "${src_file#$API_DIR/}") # e.g. "src/docker"
depth=$(echo "$rel_dir" | tr '/' '\n' | wc -l)
# depth=1 means src/foo.js (parent is "src")
# depth=2 means src/docker/foo.js (parent is "src/docker"), need ../
# Find all `require('./name')` patterns in this file
while IFS= read -r require_line; do
# Extract the module path from inside the quotes
mod_path=$(echo "$require_line" | grep -oE "require\(['\"]\./[a-zA-Z0-9_-]+['\"]\)" | head -1 | sed -E "s|require\(['\"]\./||; s|['\"]\)||")
if [[ -z "$mod_path" ]]; then continue; fi
# Compute the absolute path Node would resolve `./mod_path` to from this file
# Candidate 1: same dir, .js file
candidate="$rel_dir/$mod_path.js"
if [[ -f "$candidate" ]]; then
# File exists in same subdir → require is correct as-is
continue
fi
# Candidate 2: same dir, directory with index.js
if [[ -d "$rel_dir/$mod_path" && -f "$rel_dir/$mod_path/index.js" ]]; then
continue
fi
# Check if it exists at the root (one level above src/, or at the
# appropriate depth for nested src/ subdirs)
# For a file at $rel_dir/$file.js, './mod' resolves to $rel_dir/mod.js
# We need to find where mod.js actually exists.
found_path=""
# Walk up from the same-dir candidate, checking each parent dir.
# Start by checking the file's own dir (already done above), then
# dirname(rel_dir), dirname(dirname(rel_dir)), ..., until we hit ".".
# rel_dir is relative to API_DIR, so when test_dir becomes ".", we
# should check API_DIR/$mod_path.js (the root), THEN break.
test_dir="$rel_dir"
while true; do
test_dir=$(dirname "$test_dir")
# Check this directory for the module: either .js file or dir/index.js
if [[ -f "$test_dir/$mod_path.js" || ( -d "$test_dir/$mod_path" && -f "$test_dir/$mod_path/index.js" ) ]]; then
found_path="$test_dir/$mod_path"
break
fi
# Stop when we've gone past root
[[ "$test_dir" == "." || "$test_dir" == "/" ]] && break
done
if [[ -z "$found_path" ]]; then
# Module not found anywhere — leave it alone, would need investigation
continue
fi
# Found at root. Compute the correct relative path from this file to root.
# For src/docker/self-updater.js requiring platform-paths (at root):
# need: '../../platform-paths'
file_dir=$(dirname "$src_file")
file_dir_rel="${file_dir#$API_DIR/}" # e.g. "src/docker"
# Number of dirs to go up: count slashes + 1
# "src/docker" → 2 dirs → go up 2: ../../platform-paths
up_count=$(echo "$file_dir_rel" | awk -F'/' '{print NF}')
up_path=""
for ((i=0; i<up_count; i++)); do
up_path="../$up_path"
done
correct_require="${up_path}${mod_path}"
# Apply the fix: require('./X') → require('../X')
sed -i "s|require('\./${mod_path}')|require('${correct_require}')|g" "$src_file"
log " Patched ${rel_dir}/$(basename "$src_file"): './${mod_path}' → '${correct_require}'"
GENERIC_PATCHED=$((GENERIC_PATCHED + 1))
done < <(grep -E "require\(['\"]\./[a-zA-Z0-9_-]+['\"]\)" "$src_file" 2>/dev/null || true)
done < <(find "$API_DIR/src" -type f -name "*.js" -not -path "*/node_modules/*" -not -path "*/__tests__/*" -print0 2>/dev/null)
if [[ $GENERIC_PATCHED -gt 0 ]]; then
log "Generic src/ require patches: ${GENERIC_PATCHED} fixed"
TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
if [[ ! -d "$API_DIR/src" ]]; then
FAILED_CHECKS+=("src/: missing — tarball did not ship src/ tree (v1.14.4-class bug)")
log "FAIL: src/: directory missing — tarball did not ship src/ tree"
elif [[ -z "$(ls -A "$API_DIR/src" 2>/dev/null)" ]]; then
FAILED_CHECKS+=("src/: empty — tarball shipped empty src/ tree")
log "FAIL: src/: directory is empty"
elif [[ ! -f "$API_DIR/src/app.js" ]]; then
FAILED_CHECKS+=("src/app.js: missing — src/ tree incomplete")
log "FAIL: src/app.js: missing — src/ tree incomplete"
else
log "Generic src/ require patches: 0 needed (all correct)"
TOTAL_OK=$((TOTAL_OK + 1))
src_file_count=$(find "$API_DIR/src" -type f -name "*.js" -not -path "*/__tests__/*" 2>/dev/null | wc -l)
log "OK: src/ — present with ${src_file_count} .js files"
fi
TOTAL_PATCHED=$((TOTAL_PATCHED + GENERIC_PATCHED))
TOTAL_ALREADY_OK=$((TOTAL_ALREADY_OK + GENERIC_ALREADY_OK))
# ────────────────────────────────────────────────────────────────────────────
# Patch 4: Ensure license-keygen.js exists at API root
# v1.14.4's tarball didn't ship the root-level license-keygen.js. If missing,
# restore from src/managers/license-keygen.js or a backup.
# Check 4: license-keygen.js exists at API root (was missing in v1.14.4)
# ────────────────────────────────────────────────────────────────────────────
TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
LICENSE_ROOT="$API_DIR/license-keygen.js"
LICENSE_SRC="$API_DIR/src/managers/license-keygen.js"
if [[ ! -f "$LICENSE_ROOT" ]]; then
BACKUP_FILE=""
# Prefer the v1.13.x backup if it exists (the version that had it at root)
if [[ -d "$API_DIR/../updates/backups" ]]; then
BACKUP_FILE=$(find "$API_DIR/../updates/backups" -name "license-keygen.js" 2>/dev/null | head -1)
fi
# Fall back to src/managers/ if newer refactor put it there
if [[ -z "$BACKUP_FILE" && -f "$LICENSE_SRC" ]]; then
BACKUP_FILE="$LICENSE_SRC"
fi
# Last resort: search elsewhere
if [[ -z "$BACKUP_FILE" ]]; then
BACKUP_FILE=$(find /opt/dashcaddy -name "license-keygen.js" -not -path "*/node_modules/*" -not -path "*/updates/*" -not -path "*/backups/staging-*" 2>/dev/null | head -1)
fi
if [[ -n "$BACKUP_FILE" && -f "$BACKUP_FILE" ]]; then
cp -f "$BACKUP_FILE" "$LICENSE_ROOT"
log "Restored missing license-keygen.js from $BACKUP_FILE"
TOTAL_PATCHED=$((TOTAL_PATCHED + 1))
else
log "ERROR: license-keygen.js missing at root and no backup available — build may fail"
fi
FAILED_CHECKS+=("license-keygen.js: missing at API root")
log "FAIL: license-keygen.js: missing at API root — would MODULE_NOT_FOUND at runtime"
else
TOTAL_ALREADY_OK=$((TOTAL_ALREADY_OK + 1))
TOTAL_OK=$((TOTAL_OK + 1))
log "OK: license-keygen.js — present at API root"
fi
# ────────────────────────────────────────────────────────────────────────────
# Check 5: Generic src/**/*.js require path check — for each src/ file, walk
# any `require('./name')` pattern and verify the module resolves from the
# file's directory. If the require points at a file that does NOT exist in
# the same subdir but DOES exist higher up, we report a likely-broken path.
#
# NOTE: This check is INFORMATIONAL — we log warnings for anything suspicious
# but only fail the build on patterns we know are broken (the ones Checks 1-4
# cover). Future DC-NNN tickets can promote specific patterns from warnings
# to hard failures as we discover more.
# ────────────────────────────────────────────────────────────────────────────
TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
WARN_COUNT=0
if [[ -d "$API_DIR/src" ]]; then
while IFS= read -r -d '' src_file; do
rel_dir=$(dirname "${src_file#$API_DIR/}")
while IFS= read -r require_line; do
mod_path=$(echo "$require_line" | grep -oE "require\(['\"]\./[a-zA-Z0-9_-]+['\"]\)" | head -1 | sed -E "s|require\(['\"]\./||; s|['\"]\)||")
[[ -z "$mod_path" ]] && continue
# Candidate 1: same dir, .js file
candidate="$rel_dir/$mod_path.js"
[[ -f "$candidate" ]] && continue
# Candidate 2: same dir, dir/index.js
[[ -d "$rel_dir/$mod_path" && -f "$rel_dir/$mod_path/index.js" ]] && continue
# Walk up parents looking for the module
found_path=""
test_dir="$rel_dir"
while true; do
test_dir=$(dirname "$test_dir")
if [[ -f "$test_dir/$mod_path.js" || ( -d "$test_dir/$mod_path" && -f "$test_dir/$mod_path/index.js" ) ]]; then
found_path="$test_dir/$mod_path"
break
fi
[[ "$test_dir" == "." || "$test_dir" == "/" ]] && break
done
if [[ -n "$found_path" ]]; then
log " WARN: ${rel_dir}/$(basename "$src_file"): require('./${mod_path}') resolves to ${found_path} (possible stale path)"
WARN_COUNT=$((WARN_COUNT + 1))
fi
done < <(grep -E "require\(['\"]\./[a-zA-Z0-9_-]+['\"]\)" "$src_file" 2>/dev/null || true)
done < <(find "$API_DIR/src" -type f -name "*.js" -not -path "*/node_modules/*" -not -path "*/__tests__/*" -print0 2>/dev/null)
fi
if (( WARN_COUNT == 0 )); then
TOTAL_OK=$((TOTAL_OK + 1))
log "OK: src/ require paths — no suspicious same-dir-vs-root mismatches"
else
log "INFO: src/ require paths — ${WARN_COUNT} informational warning(s) (does NOT fail build)"
TOTAL_OK=$((TOTAL_OK + 1)) # informational only
fi
# ────────────────────────────────────────────────────────────────────────────
# Summary
# ────────────────────────────────────────────────────────────────────────────
log "=== Post-deploy patch summary: ${TOTAL_PATCHED} require fixes applied, ${TOTAL_ALREADY_OK} components already OK ==="
exit 0
log "=== Verify summary: ${TOTAL_OK}/${TOTAL_CHECKS} checks passed ==="
if (( ${#FAILED_CHECKS[@]} > 0 )); then
log "=== FAILED CHECKS ==="
for check in "${FAILED_CHECKS[@]}"; do
log " - $check"
done
log "=== Build should be ABORTED — fix the source tree first ==="
exit 1
fi
log "All checks passed. Safe to proceed with Docker build."
exit 0