u
This commit is contained in:
@@ -11,40 +11,88 @@ const srcDir = path.join(rootDir, 'src');
|
||||
const configPath = path.join(rootDir, 'sea-config.json');
|
||||
const payloadPath = path.join(distDir, 'app-payload.bin');
|
||||
|
||||
// Ensure dist directory exists
|
||||
const dbschemaDir = path.resolve(rootDir, '../dashdbschema');
|
||||
const llmorbiterDir = path.resolve(rootDir, '../llmorbiter');
|
||||
|
||||
if (!fs.existsSync(distDir)) {
|
||||
fs.mkdirSync(distDir, { recursive: true });
|
||||
}
|
||||
|
||||
console.log('Packaging application code into SEA payload...');
|
||||
console.log('Packaging application, siblings, and JavaScript dependencies into SEA payload...');
|
||||
|
||||
function packDirectory(dir, baseDir = dir) {
|
||||
const result = {};
|
||||
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||
const full = path.join(dir, entry.name);
|
||||
const rel = path.relative(baseDir, full);
|
||||
if (entry.isDirectory()) {
|
||||
Object.assign(result, packDirectory(full, baseDir));
|
||||
} else if (entry.isFile()) {
|
||||
result[rel] = fs.readFileSync(full).toString('base64');
|
||||
// Native C++ addon folders that will be loaded from the host filesystem via dlopen
|
||||
const NATIVE_ADDON_PKGS = new Set(['better-sqlite3', 'canvas', 'sharp', '@img']);
|
||||
|
||||
function packDirectory(dir, prefix, result) {
|
||||
if (!fs.existsSync(dir)) return;
|
||||
try {
|
||||
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||
const full = path.join(dir, entry.name);
|
||||
const rel = path.join(prefix, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
if (entry.name === 'test' || entry.name === 'tests' || entry.name === 'docs' || entry.name === '.github' || entry.name === '.git') continue;
|
||||
packDirectory(full, rel, result);
|
||||
} else if (entry.isFile()) {
|
||||
// Ignore compiled native binary extensions which are linked from the host
|
||||
if (entry.name.endsWith('.node') || entry.name.endsWith('.so') || entry.name.endsWith('.dylib') || entry.name.endsWith('.dll') || entry.name.endsWith('.a')) continue;
|
||||
result[rel] = fs.readFileSync(full).toString('base64');
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Pack src/ and package.json
|
||||
const payload = packDirectory(srcDir);
|
||||
const payload = {};
|
||||
|
||||
// 1. Pack dashback src and package.json
|
||||
packDirectory(srcDir, 'src', payload);
|
||||
const pkgPath = path.join(rootDir, 'package.json');
|
||||
if (fs.existsSync(pkgPath)) {
|
||||
payload['package.json'] = fs.readFileSync(pkgPath).toString('base64');
|
||||
}
|
||||
|
||||
// 2. Pack sibling dependencies (libdashstore and libllmorbiter) directly into node_modules
|
||||
if (fs.existsSync(path.join(dbschemaDir, 'src'))) {
|
||||
packDirectory(path.join(dbschemaDir, 'src'), 'node_modules/libdashstore/src', payload);
|
||||
const dbschemaPkg = path.join(dbschemaDir, 'package.json');
|
||||
if (fs.existsSync(dbschemaPkg)) {
|
||||
payload['node_modules/libdashstore/package.json'] = fs.readFileSync(dbschemaPkg).toString('base64');
|
||||
}
|
||||
}
|
||||
|
||||
if (fs.existsSync(path.join(llmorbiterDir, 'src'))) {
|
||||
packDirectory(path.join(llmorbiterDir, 'src'), 'node_modules/libllmorbiter/src', payload);
|
||||
const llmorbiterPkg = path.join(llmorbiterDir, 'package.json');
|
||||
if (fs.existsSync(llmorbiterPkg)) {
|
||||
payload['node_modules/libllmorbiter/package.json'] = fs.readFileSync(llmorbiterPkg).toString('base64');
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Pack all non-native node_modules dependencies
|
||||
function packNodeModules(nmPath) {
|
||||
if (!fs.existsSync(nmPath)) return;
|
||||
for (const name of fs.readdirSync(nmPath)) {
|
||||
if (NATIVE_ADDON_PKGS.has(name) || name.startsWith('.') || name === 'libdashstore' || name === 'libllmorbiter') continue;
|
||||
const pkgDir = path.join(nmPath, name);
|
||||
if (name.startsWith('@')) {
|
||||
for (const sub of fs.readdirSync(pkgDir)) {
|
||||
if (NATIVE_ADDON_PKGS.has(sub) || NATIVE_ADDON_PKGS.has(`${name}/${sub}`)) continue;
|
||||
packDirectory(path.join(pkgDir, sub), `node_modules/${name}/${sub}`, payload);
|
||||
}
|
||||
} else {
|
||||
packDirectory(pkgDir, `node_modules/${name}`, payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
packNodeModules(path.join(rootDir, 'node_modules'));
|
||||
packNodeModules(path.join(dbschemaDir, 'node_modules'));
|
||||
|
||||
const compressedPayload = zlib.deflateSync(Buffer.from(JSON.stringify(payload)));
|
||||
fs.writeFileSync(payloadPath, compressedPayload);
|
||||
|
||||
console.log(`Packed ${Object.keys(payload).length} files (${(compressedPayload.length / 1024 / 1024).toFixed(2)} MB compressed)`);
|
||||
|
||||
// Prepare sea-config.json with payload asset
|
||||
// Prepare sea-config.json
|
||||
const config = {
|
||||
main: 'src/sea-entry.cjs',
|
||||
output: 'dist/dashback',
|
||||
|
||||
Reference in New Issue
Block a user