u
This commit is contained in:
@@ -1,43 +1,74 @@
|
||||
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');
|
||||
|
||||
if (!fs.existsSync(configPath)) {
|
||||
console.error(`Error: Configuration file not found at ${configPath}`);
|
||||
process.exit(1);
|
||||
// Ensure dist directory exists
|
||||
if (!fs.existsSync(distDir)) {
|
||||
fs.mkdirSync(distDir, { recursive: true });
|
||||
}
|
||||
|
||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||||
const outputPath = path.resolve(rootDir, config.output || 'dist/dashback');
|
||||
const outputDir = path.dirname(outputPath);
|
||||
console.log('Packaging application code into SEA payload...');
|
||||
|
||||
// Ensure the target output directory exists
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
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;
|
||||
}
|
||||
|
||||
console.log('Building Node.js Single Executable Application (SEA)...');
|
||||
console.log(`Node version: ${process.version}`);
|
||||
console.log(`Config: ${path.relative(rootDir, configPath)}`);
|
||||
console.log(`Target: ${path.relative(rootDir, outputPath)}`);
|
||||
// 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', configPath], {
|
||||
execFileSync(process.execPath, ['--build-sea', 'sea-config.json'], {
|
||||
cwd: rootDir,
|
||||
stdio: 'inherit',
|
||||
});
|
||||
|
||||
// Ensure executable permissions on POSIX systems
|
||||
const outputPath = path.join(distDir, 'dashback');
|
||||
if (process.platform !== 'win32' && fs.existsSync(outputPath)) {
|
||||
fs.chmodSync(outputPath, 0o755);
|
||||
}
|
||||
|
||||
console.log(`\nSuccessfully built SEA executable: ${path.relative(rootDir, outputPath)}`);
|
||||
console.log(`\nSuccessfully built standalone SEA: ${path.relative(rootDir, outputPath)}`);
|
||||
} catch (err) {
|
||||
console.error('\nFailed to build SEA:', err.message);
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user