#!/usr/bin/env node /** * Pack the server archive that the installer (packages/launcher) downloads. * * The archive holds the same sidecar payload the desktop app ships, staged by * bundle-node, build-sidecar, bundle-native-deps and stage-sidecar-deps into * app/src-tauri/resources (with LICENSE and THIRD_PARTY_NOTICES.md next to * service.js), plus the built web UI under web/ and a manifest.json. Run it after those steps and the web build, on the system * the archive is for. * * Usage: * node scripts/build-server-archive.mjs [--resources DIR] [--web DIR] [--out DIR] * [--target TARGET] [--node-version X.Y.Z] * * Writes /waggle-server--.tar.gz and a .sha256 next to it. * The version is the installer's (packages/launcher/package.json): the * installer of version X downloads the archive of version X. */ import { spawnSync } from 'node:child_process'; import { createHash } from 'node:crypto'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); // Keep in step with releaseTarget() and assetName() in packages/launcher/src/platform.ts. const TARGETS = { 'win32-x64': 'windows-x64', 'darwin-arm64': 'macos-arm64', 'darwin-x64': 'macos-x64', 'linux-x64': 'linux-x64', }; function parseArgs(argv) { const options = { resources: path.join(root, 'app', 'src-tauri', 'resources'), web: path.join(root, 'apps', 'web', 'dist'), out: path.join(root, 'artifacts'), target: TARGETS[`${process.platform}-${process.env.TARGET_ARCH || process.arch}`], nodeVersion: null, }; const flags = { '--resources': 'resources', '--web': 'web', '--out': 'out', '--target': 'target', '--node-version': 'nodeVersion' }; for (let i = 0; i < argv.length; i++) { const key = flags[argv[i]]; if (!key || argv[i + 1] === undefined) throw new Error(`Unknown or incomplete option: ${argv[i]}`); options[key] = key === 'target' || key === 'nodeVersion' ? argv[++i] : path.resolve(argv[++i]); } if (!options.target || !Object.values(TARGETS).includes(options.target)) { throw new Error(`No release target for ${process.platform}-${process.env.TARGET_ARCH || process.arch}; pass --target`); } return options; } function requireFile(file, hint) { if (!fs.existsSync(file)) throw new Error(`${path.relative(root, file) || file} is missing. ${hint}`); } function sha256(file) { const hash = createHash('sha256'); const fd = fs.openSync(file, 'r'); try { const buffer = Buffer.alloc(1024 * 1024); let read; while ((read = fs.readSync(fd, buffer, 0, buffer.length, null)) > 0) hash.update(buffer.subarray(0, read)); } finally { fs.closeSync(fd); } return hash.digest('hex'); } try { const options = parseArgs(process.argv.slice(2)); const { version } = JSON.parse(fs.readFileSync(path.join(root, 'packages', 'launcher', 'package.json'), 'utf8')); const nodeName = options.target.startsWith('windows-') ? 'node.exe' : 'node'; const stageHint = 'Run the desktop staging steps first (see the tauri:build scripts in app/package.json).'; requireFile(path.join(options.resources, nodeName), stageHint); requireFile(path.join(options.resources, 'service.js'), stageHint); requireFile(path.join(options.resources, 'node_modules'), stageHint); // The license and the third-party notices must ship with the archive. requireFile(path.join(options.resources, 'LICENSE'), stageHint); requireFile(path.join(options.resources, 'THIRD_PARTY_NOTICES.md'), stageHint); requireFile(path.join(options.web, 'index.html'), 'Build the web UI first: cd apps/web && npx vite build'); let nodeVersion = options.nodeVersion; if (!nodeVersion) { const probe = spawnSync(path.join(options.resources, nodeName), ['-p', 'process.versions.node'], { encoding: 'utf8' }); if (probe.status !== 0) throw new Error(`Could not run the bundled Node to read its version; pass --node-version`); nodeVersion = probe.stdout.trim(); } const name = `waggle-server-${version}-${options.target}`; const stage = fs.mkdtempSync(path.join(os.tmpdir(), `${name}-`)); try { console.log(`[server-archive] Staging ${name}`); fs.cpSync(options.resources, stage, { recursive: true, verbatimSymlinks: true, filter: (source) => !source.endsWith('.map'), }); fs.cpSync(options.web, path.join(stage, 'web'), { recursive: true, verbatimSymlinks: true }); fs.writeFileSync( path.join(stage, 'manifest.json'), `${JSON.stringify({ name: 'waggle-server', version, target: options.target, node: nodeVersion }, null, 2)}\n`, ); fs.mkdirSync(options.out, { recursive: true }); const archive = path.join(options.out, `${name}.tar.gz`); fs.rmSync(archive, { force: true }); console.log(`[server-archive] Packing ${path.relative(root, archive) || archive}`); const tar = spawnSync('tar', ['-czf', archive, '-C', stage, '.'], { stdio: 'inherit' }); if (tar.status !== 0) throw new Error(`tar failed with exit code ${tar.status}`); const digest = sha256(archive); fs.writeFileSync(`${archive}.sha256`, `${digest} ${path.basename(archive)}\n`); const sizeMB = (fs.statSync(archive).size / 1024 / 1024).toFixed(1); console.log(`[server-archive] Done: ${sizeMB} MB, sha256 ${digest}`); } finally { fs.rmSync(stage, { recursive: true, force: true }); } } catch (error) { console.error(`[server-archive] ${error instanceof Error ? error.message : String(error)}`); process.exit(1); }