Files
vibemake-pcb-studio/scripts/package-pcb.mjs
T

59 lines
3.5 KiB
JavaScript

// Package only the built PCB application; never include server configuration or secrets.
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { execFileSync } from 'node:child_process';
const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)));
export function packagePcb({ source = join(ROOT, 'designer/dist'), output = join(ROOT, 'dist') } = {}) {
source = resolve(source);
output = resolve(output);
if (!existsSync(join(source, 'index.html'))) throw new Error('PCB build missing: run ./build.sh first.');
const config = JSON.parse(readFileSync(join(source, 'pcb-service-config.json'), 'utf8'));
if (config.base !== '/pcb/') throw new Error('The independent PCB package requires the /pcb/ build base.');
mkdirSync(output, { recursive: true });
const stage = mkdtempSync(join(output, '.pcb-package-'));
const directory = join(output, 'vibemake-pcb');
const archive = join(output, 'vibemake-pcb.zip');
const buildId = Date.now().toString(36);
try {
const pcb = join(stage, 'contents');
cpSync(source, pcb, {
recursive: true,
filter: file => !/(?:^|\/)(?:\.DS_Store|Thumbs\.db|\.env(?:\.[^/]*)?|node_modules)(?:\/|$)/.test(file),
});
writeFileSync(join(pcb, 'build-info.json'), JSON.stringify({
scope: 'pcb', buildId, builtAt: new Date().toISOString(), base: '/pcb/', archive: 'vibemake-pcb.zip',
}, null, 2) + '\n');
writeFileSync(join(pcb, 'DEPLOY.md'), `# PCB independent deployment
Extract vibemake-pcb.zip directly into <website-root>/pcb/. The ZIP root contains index.html, assets/ and the other PCB frontend files, with no enclosing pcb/ or vibemake-pcb/ directory.
mkdir -p /path/to/vibemake/pcb
unzip -o /path/to/vibemake-pcb.zip -d /path/to/vibemake/pcb
The result must be <website-root>/pcb/index.html, never pcb/pcb/index.html. Do not extract this package into the website root or copy the ZIP without extracting it.
This package updates only pcb/. Preserve the main site, cad/, js/cad/, existing server configuration and backend services. Never clear the website or PCB directory before extraction.
Check /pcb/build-info.json after deployment: scope=pcb, buildId=${buildId}.
Configure frontend routes to fall back to /pcb/index.html. Keep /pcb/pcb-api/ routed to the separately running AI PCB service before applying the frontend fallback; static hosting does not start that backend. Keep existing account /api/ proxy and CORS settings.
Serve index.html, sw.js, registerSW.js, build-info.json and the web manifest with Cache-Control: no-cache. Hashed assets can use immutable caching. Keep old hashed assets for already open editors.
`);
execFileSync('zip', ['-qr', join(stage, 'vibemake-pcb.zip'), '.'], { cwd: pcb });
execFileSync('unzip', ['-tq', join(stage, 'vibemake-pcb.zip')], { stdio: 'pipe' });
rmSync(directory, { recursive: true, force: true });
renameSync(pcb, directory);
renameSync(join(stage, 'vibemake-pcb.zip'), archive);
return { archive, directory, buildId };
} finally {
rmSync(stage, { recursive: true, force: true });
}
}
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
const result = packagePcb();
console.log(`PCB directory: ${result.directory}`);
console.log(`PCB package ZIP: ${result.archive}`);
console.log('Extract the ZIP directly into <website-root>/pcb/; index.html is at the ZIP root.');
}