maestri-remote/scripts/build-bundle.js

59 lines
1.9 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import { execSync } from 'node:child_process';
const PUBLIC_DIR = path.resolve('public');
const BUNDLES_DIR = path.join(PUBLIC_DIR, 'bundles');
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const version = pkg.version || '1.0.0';
if (!fs.existsSync(BUNDLES_DIR)) {
fs.mkdirSync(BUNDLES_DIR, { recursive: true });
}
console.log(`[BuildBundle] Criando bundle OTA v${version}...`);
const zipName = `bundle-${version}.zip`;
const latestZipName = `bundle-latest.zip`;
const destZip = path.join(BUNDLES_DIR, zipName);
const latestDestZip = path.join(BUNDLES_DIR, latestZipName);
// Coleta os arquivos de public/ (exceto a pasta bundles/)
const filesToPack = fs.readdirSync(PUBLIC_DIR)
.filter(f => f !== 'bundles')
.map(f => path.join('public', f));
// Usa o tar nativo do Windows/Linux para gerar zip
const zipCommand = `tar -a -c -f "${destZip}" -C "${PUBLIC_DIR}" index.html app.js sw.js style.css manifest.json icon.svg ota-bootstrapper.js`;
try {
execSync(zipCommand, { stdio: 'inherit' });
fs.copyFileSync(destZip, latestDestZip);
// Calcula checksum SHA256
const buffer = fs.readFileSync(destZip);
const checksum = crypto.createHash('sha256').update(buffer).digest('hex');
const buildId = crypto.randomBytes(4).toString('hex');
const manifest = {
version,
buildId,
releasedAt: new Date().toISOString(),
bundleUrl: `/bundles/${zipName}`,
latestUrl: `/bundles/${latestZipName}`,
checksum,
sizeBytes: buffer.length
};
fs.writeFileSync(path.join(BUNDLES_DIR, 'manifest.json'), JSON.stringify(manifest, null, 2));
console.log(`[BuildBundle] Sucesso!`);
console.log(`- Arquivo: ${destZip} (${buffer.length} bytes)`);
console.log(`- SHA-256: ${checksum}`);
console.log(`- Build ID: ${buildId}`);
} catch (err) {
console.error('[BuildBundle] Erro ao compactar bundle:', err.message);
process.exit(1);
}