Files
dashback/src/sea-entry.cjs
2026-10-27 16:20:02 +01:00

78 lines
2.7 KiB
JavaScript

/**
* SEA Entry point for dashback.
* Embedded inside the Node.js single executable binary.
*
* Unpacks the bundled application, sibling packages, and all dependencies
* (including native C++ addons) to a runtime directory.
*/
const sea = require('node:sea');
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const zlib = require('node:zlib');
const { createRequire } = require('node:module');
function getProjectRoot() {
// If embedded application payload is present in SEA
if (sea.isSea && sea.isSea() && sea.getAssetKeys && sea.getAssetKeys().includes('app-payload.bin')) {
const runtimeDir = process.env.DASHBACK_RUNTIME_DIR || path.join(os.tmpdir(), 'dashback-runtime');
const markerFile = path.join(runtimeDir, '.unpacked');
if (!fs.existsSync(markerFile)) {
const compressed = Buffer.from(sea.getRawAsset('app-payload.bin'));
const json = zlib.inflateSync(compressed).toString('utf8');
const files = JSON.parse(json);
for (const [relPath, b64] of Object.entries(files)) {
const targetFile = path.join(runtimeDir, relPath);
fs.mkdirSync(path.dirname(targetFile), { recursive: true });
fs.writeFileSync(targetFile, Buffer.from(b64, 'base64'));
}
fs.writeFileSync(markerFile, '1');
}
// Link host .env if present so config/env.js can find it
const hostEnv = path.resolve(process.cwd(), '.env');
const runtimeEnv = path.join(runtimeDir, '.env');
if (fs.existsSync(hostEnv) && !fs.existsSync(runtimeEnv)) {
try {
fs.symlinkSync(hostEnv, runtimeEnv, 'file');
} catch {
try {
fs.copyFileSync(hostEnv, runtimeEnv);
} catch {}
}
}
return runtimeDir;
}
// Fallback if not running as packed SEA (local development)
if (process.env.DASHBACK_ROOT && fs.existsSync(path.join(process.env.DASHBACK_ROOT, 'src/index.js'))) {
return path.resolve(process.env.DASHBACK_ROOT);
}
const cwdEntry = path.join(process.cwd(), 'src/index.js');
if (fs.existsSync(cwdEntry)) {
return process.cwd();
}
const parentEntry = path.join(__dirname, '..', 'src/index.js');
if (fs.existsSync(parentEntry)) {
return path.resolve(__dirname, '..');
}
return process.cwd();
}
const projectRoot = getProjectRoot();
const entryFile = path.join(projectRoot, 'src', 'index.js');
if (!fs.existsSync(entryFile)) {
console.error(`[SEA] Could not find dashback entry point at ${entryFile}.`);
process.exit(1);
}
process.argv[1] = entryFile;
const req = createRequire(entryFile);
req(entryFile);