import { createHash } from 'node:crypto' import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { createRequire } from 'node:module' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { compileEarthRuntime } from '../../frontend/scripts/compile-earth-runtime.mjs' import { createTarGz } from '../src/main/tar-gz.ts' const __dirname = dirname(fileURLToPath(import.meta.url)) const desktopRoot = join(__dirname, '..') const repoRoot = join(desktopRoot, '..') const earthSourceDir = join(repoRoot, 'frontend/public/earth') const bundleEarthDir = join(desktopRoot, '.earth-bundle/earth') const require = createRequire(join(desktopRoot, 'package.json')) const VENDOR_PACKAGES = [ { id: 'three', outFile: 'three.mjs', resolvePath: 'three/build/three.module.js' }, { id: 'pmtiles', outFile: 'pmtiles.mjs' }, { id: '@mapbox/vector-tile', outFile: 'mapbox-vector-tile.mjs' }, { id: 'pbf', outFile: 'pbf.mjs' }, { id: 'simplex-noise', outFile: 'simplex-noise.mjs' }, { id: 'satellite.js', outFile: 'satellite-js.mjs', resolvePath: 'satellite.js/dist/satellite.es.js' }, { id: 'hls.js', outFile: 'hls.mjs', resolvePath: 'hls.js/dist/hls.mjs' }, { id: 'astronomy-engine', outFile: 'astronomy-engine.mjs' }, ] rmSync(bundleEarthDir, { recursive: true, force: true }) mkdirSync(join(bundleEarthDir, 'js'), { recursive: true }) mkdirSync(join(bundleEarthDir, 'vendor'), { recursive: true }) const importMapOverrides = {} for (const pkg of VENDOR_PACKAGES) { const entry = fileURLToPath(import.meta.resolve(pkg.resolvePath || pkg.id)) const result = await Bun.build({ entrypoints: [entry], target: 'browser', format: 'esm', minify: true, }) if (!result.success) { throw new AggregateError(result.logs, `Failed to bundle vendor package: ${pkg.id}`) } const source = await result.outputs[0].text() writeFileSync(join(bundleEarthDir, 'vendor', pkg.outFile), source, 'utf8') importMapOverrides[pkg.id] = `./vendor/${pkg.outFile}` } const { assets, html } = await compileEarthRuntime({ earthDir: earthSourceDir, importMapOverrides, }) writeFileSync(join(bundleEarthDir, 'index.html'), html, 'utf8') for (const [name, asset] of assets) { writeFileSync(join(bundleEarthDir, 'js', name), asset.source, 'utf8') } const workerEntrypoints = [ join(earthSourceDir, 'js/satellite-position-worker.js'), join(earthSourceDir, 'js/surface-line-lod-worker.js'), ] const workerBuild = await Bun.build({ entrypoints: workerEntrypoints, outdir: join(bundleEarthDir, 'js'), target: 'browser', format: 'esm', minify: true, naming: '[name].js', }) if (!workerBuild.success) { throw new AggregateError(workerBuild.logs, 'Failed to bundle Earth Web Workers') } for (const folder of ['css', 'assets', 'data']) { const srcFolder = join(earthSourceDir, folder) if (existsSync(srcFolder)) { cpSync(srcFolder, join(bundleEarthDir, folder), { recursive: true }) } } const zjuEmblemSrc = join(desktopRoot, 'resources/zju-emblem.png') if (existsSync(zjuEmblemSrc)) { const brandOutDir = join(bundleEarthDir, 'assets/brand') mkdirSync(brandOutDir, { recursive: true }) cpSync(zjuEmblemSrc, join(brandOutDir, 'zju-emblem.png')) } const mediaPipePkgDir = dirname(require.resolve('@mediapipe/tasks-vision/package.json')) const mediaPipeOutDir = join(bundleEarthDir, 'vendor/mediapipe') mkdirSync(mediaPipeOutDir, { recursive: true }) const mediaPipeBundleSrc = join(mediaPipePkgDir, 'vision_bundle.mjs') if (existsSync(mediaPipeBundleSrc)) { cpSync(mediaPipeBundleSrc, join(mediaPipeOutDir, 'vision_bundle.mjs')) } const mediaPipeWasmDir = join(mediaPipePkgDir, 'wasm') if (existsSync(mediaPipeWasmDir)) { cpSync(mediaPipeWasmDir, join(mediaPipeOutDir, 'wasm'), { recursive: true }) } const poseModelSrc = join(earthSourceDir, 'vendor/mediapipe/pose_landmarker_lite.task') if (!existsSync(poseModelSrc)) { throw new Error(`Missing bundled MediaPipe pose model: ${poseModelSrc}`) } cpSync(poseModelSrc, join(mediaPipeOutDir, 'pose_landmarker_lite.task')) console.log(`Built offline Earth bundle at ${bundleEarthDir}`) const clientPkg = JSON.parse(readFileSync(join(desktopRoot, 'package.json'), 'utf8')) const bundleVersion = String(clientPkg.version || '0.0.0').trim() const metaPayload = { version: bundleVersion, shellVersion: bundleVersion, builtAt: new Date().toISOString(), } writeFileSync(join(bundleEarthDir, 'bundle-meta.json'), `${JSON.stringify(metaPayload, null, 2)}\n`, 'utf8') function collectHotfixEntries(rootDir) { const entries = [] for (const topFile of ['index.html', 'bundle-meta.json']) { const filePath = join(rootDir, topFile) if (existsSync(filePath)) { entries.push({ path: topFile, data: readFileSync(filePath) }) } } const walk = (relDir) => { const absDir = join(rootDir, relDir) if (!existsSync(absDir)) return for (const dirent of readdirSync(absDir, { withFileTypes: true })) { const relPath = `${relDir}/${dirent.name}` if (dirent.isDirectory()) { walk(relPath) } else if (dirent.isFile()) { entries.push({ path: relPath, data: readFileSync(join(rootDir, relPath)) }) } } } walk('js') walk('css') return entries } const hotfixEntries = collectHotfixEntries(bundleEarthDir) const hotfixArchive = createTarGz(hotfixEntries) const hotfixSha256 = createHash('sha256').update(hotfixArchive).digest('hex') const releaseOutDir = join(desktopRoot, 'release') mkdirSync(releaseOutDir, { recursive: true }) const hotfixName = `planet-earth-bundle-${bundleVersion}.tar.gz` const hotfixPath = join(releaseOutDir, hotfixName) writeFileSync(hotfixPath, hotfixArchive) writeFileSync(join(releaseOutDir, `${hotfixName}.sha256`), `${hotfixSha256} ${hotfixName}\n`, 'utf8') console.log( `Built L1 hotfix bundle ${hotfixPath} (${(hotfixArchive.byteLength / 1024).toFixed(1)} KiB, sha256=${hotfixSha256})`, )