76 lines
2.5 KiB
JavaScript
76 lines
2.5 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import zlib from 'node:zlib';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const rootDir = path.resolve(__dirname, '..');
|
|
const distDir = path.join(rootDir, 'dist');
|
|
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
|
|
if (!fs.existsSync(distDir)) {
|
|
fs.mkdirSync(distDir, { recursive: true });
|
|
}
|
|
|
|
console.log('Packaging application code 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');
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// Pack src/ and package.json
|
|
const payload = packDirectory(srcDir);
|
|
const pkgPath = path.join(rootDir, 'package.json');
|
|
if (fs.existsSync(pkgPath)) {
|
|
payload['package.json'] = fs.readFileSync(pkgPath).toString('base64');
|
|
}
|
|
|
|
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
|
|
const config = {
|
|
main: 'src/sea-entry.cjs',
|
|
output: 'dist/dashback',
|
|
assets: {
|
|
'app-payload.bin': 'dist/app-payload.bin',
|
|
},
|
|
disableExperimentalSEAWarning: true,
|
|
};
|
|
|
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n');
|
|
|
|
console.log('Generating Single Executable Application (SEA)...');
|
|
try {
|
|
execFileSync(process.execPath, ['--build-sea', 'sea-config.json'], {
|
|
cwd: rootDir,
|
|
stdio: 'inherit',
|
|
});
|
|
|
|
const outputPath = path.join(distDir, 'dashback');
|
|
if (process.platform !== 'win32' && fs.existsSync(outputPath)) {
|
|
fs.chmodSync(outputPath, 0o755);
|
|
}
|
|
|
|
console.log(`\nSuccessfully built standalone SEA: ${path.relative(rootDir, outputPath)}`);
|
|
} catch (err) {
|
|
console.error('\nFailed to build SEA:', err.message);
|
|
process.exit(1);
|
|
}
|