Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

202 lines
6.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env tsx
/**
* Build script for UI Apps.
*
* Auto-discovers UI apps from src/ui-apps/apps/ and builds each one
* as separate JS + CSS files served via Workers Static Assets.
* Apps are built in parallel for speed, each in its own Vite process
* with inlineDynamicImports to prevent shared chunks.
*
* Usage:
* pnpm run build:ui-apps # Build all apps
* pnpm run build:ui-apps:watch # Watch mode for all apps
*/
import { exec } from 'child_process'
import { config as dotenvConfig } from 'dotenv'
import { existsSync, rmSync } from 'fs'
import { join, resolve } from 'path'
import { discoverApps, MCP_ROOT_DIR, ROOT_DIR } from './utils'
// Where to store the built files
const OUT_DIR = resolve(MCP_ROOT_DIR, 'public/ui-apps')
// Load environment variables from .env (see .env.example)
const dotEnvPath = resolve(MCP_ROOT_DIR, '.env')
if (existsSync(dotEnvPath)) {
const output = dotenvConfig({ path: dotEnvPath })
const loadedKeys = Object.keys(output.parsed || {})
console.info('šŸ“ Loaded environment from .env', loadedKeys)
} else if (existsSync(resolve(MCP_ROOT_DIR, '.dev.vars'))) {
console.warn(
'āš ļø Support for `.dev.vars` was removed - move your local config to `.env` (see .env.example). ' +
'Building with fallback values (no analytics token).'
)
}
function buildAppAsync(appName: string): Promise<void> {
if (!/^(?:generated\/)?[a-z_-]+$/.test(appName)) {
return Promise.reject(
new Error(`Invalid app name "${appName}": must only contain a-z, underscore, hyphen, or generated/ prefix`)
)
}
return new Promise((resolve, reject) => {
// nosemgrep: javascript.lang.security.detect-child-process.detect-child-process
const child = exec('vite build --config vite.ui-apps.config.ts', {
cwd: MCP_ROOT_DIR,
env: {
...process.env,
UI_APP: appName,
BROWSERSLIST_IGNORE_OLD_DATA: '1',
VITE_CJS_IGNORE_WARNING: '1',
},
})
let stderr = ''
child.stderr?.on('data', (data) => {
stderr += data
})
child.on('close', (code) => {
if (code !== 0) {
console.error(`āŒ ${appName} failed:\n${stderr}`)
reject(new Error(`Build failed for ${appName} (exit code ${code})`))
} else {
console.info(` āœ“ ${appName}`)
resolve()
}
})
})
}
async function buildAllAppsParallel(apps: string[]): Promise<void> {
// Each Vite build peaks at a few hundred MB, so cap parallelism: the summed
// peak of an unbounded run (one process per app) OOM-kills memory-constrained
// builders — e.g. the Docker image build, where 27 concurrent Vite processes
// saturated the builder. The constraint is memory, not which environment we run in.
const concurrency = Math.min(apps.length, 4)
if (concurrency < apps.length) {
console.info(`\nšŸ“¦ Building ${apps.length} apps (concurrency: ${concurrency})...`)
const remaining = [...apps]
const workers: Promise<void>[] = []
async function next(): Promise<void> {
while (remaining.length > 0) {
const app = remaining.shift()!
await buildAppAsync(app)
}
}
for (let i = 0; i < concurrency; i++) {
workers.push(next())
}
await Promise.all(workers)
} else {
console.info(`\nšŸ“¦ Building ${apps.length} apps in parallel...`)
await Promise.all(apps.map((app) => buildAppAsync(app)))
}
}
async function watchApps(apps: string[]): Promise<void> {
console.info(`\nšŸ‘€ Starting watch mode for ${apps.length} apps: ${apps.join(', ')}`)
await buildAllAppsParallel(apps)
console.info('\nāœ… Initial build complete. Watching for changes...')
import('chokidar').then(({ default: chokidar }) => {
let isBuilding = false
let pendingBuild = false
const rebuild = async (): Promise<void> => {
if (isBuilding) {
pendingBuild = true
return
}
isBuilding = true
console.info('\nšŸ”„ Rebuilding all apps...')
try {
await buildAllAppsParallel(apps)
console.info('āœ… Rebuild complete.')
} catch (e) {
console.error('āŒ Build failed:', e)
}
isBuilding = false
if (pendingBuild) {
pendingBuild = false
setTimeout(rebuild, 100)
}
}
const watcher = chokidar.watch(
[
join(MCP_ROOT_DIR, 'src/ui-apps/**/*.{ts,tsx,css}'),
join(ROOT_DIR, 'products/**/mcp/apps/**/*.{ts,tsx,css}'),
],
{
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 100,
pollInterval: 50,
},
}
)
watcher.on('change', (path) => {
console.info(`\nšŸ“ File changed: ${path}`)
rebuild()
})
watcher.on('add', (path) => {
console.info(`\nāž• File added: ${path}`)
rebuild()
})
const cleanup = (): void => {
console.info('\nšŸ›‘ Stopping watcher...')
watcher.close()
process.exit(0)
}
process.on('SIGINT', cleanup)
process.on('SIGTERM', cleanup)
})
}
async function main(): Promise<void> {
const isWatch = process.argv.includes('--watch')
const apps = discoverApps()
if (apps.length === 0) {
console.error('āŒ No UI apps found in src/ui-apps/apps/')
process.exit(1)
}
console.info(`šŸ” Discovered ${apps.length} UI app(s): ${apps.join(', ')}`)
// Clean output directory before building
if (existsSync(OUT_DIR)) {
rmSync(OUT_DIR, { recursive: true })
}
if (isWatch) {
await watchApps(apps)
} else {
await buildAllAppsParallel(apps)
console.info('\nāœ… All UI apps built successfully!')
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})