web/website/scripts/deploy-canonical-menu.mjs
2026-11-27 23:43:05 +00:00

14 lines
3.2 KiB
JavaScript

// Scoped publication: retain every existing runtime/API byte outside the asset map.
import{readFileSync,writeFileSync,existsSync,mkdirSync}from'node:fs';import{createHash}from'node:crypto';import path from'node:path';import{fileURLToPath}from'node:url';import{withCanonicalMenu}from'./main-menu.mjs';import{PORTAL_NAV}from'./portal-navigation.mjs';
const navPattern=/<nav\b(?=[^>]*aria-label=["']Main navigation["'])[^>]*>[^]*?<\/nav>/g;
export function canonicalHeaderHtml(html){return html.replace(navPattern,PORTAL_NAV).replace(/(?:portal-navigation|dune-navigation)\.js\?v=[a-zA-Z0-9_.-]+/g,m=>m.split('?')[0]+'?v=canonical-362');}
export function deployCanonicalMenu({release,nativeRoot,backupDir,extraFiles=[]}){
for(const p of[release,backupDir,...(nativeRoot?[nativeRoot]:[])])if(!path.isAbsolute(p))throw Error('Absolute paths required');mkdirSync(backupDir,{recursive:true,mode:0o700});const changed=[];
const save=(file,content,key)=>{if(existsSync(file)){const old=readFileSync(file);if(old.equals(Buffer.from(content)))return;const backup=path.join(backupDir,key);mkdirSync(path.dirname(backup),{recursive:true});if(!existsSync(backup))writeFileSync(backup,old,{mode:0o600});}writeFileSync(file,content,{mode:0o644});changed.push(file);};
const worker=path.join(release,'dist/server/index.js'),before=readFileSync(worker,'utf8'),prefix='const ASSET_DATA=',end=before.indexOf(';\n');if(!before.startsWith(prefix)||end<0)throw Error('Unknown asset bundle');const assets=JSON.parse(before.slice(prefix.length,end));if(!assets['/portal-navigation.js'])throw Error('No shared script');const script=withCanonicalMenu(Buffer.from(assets['/portal-navigation.js'][1],'base64').toString());assets['/portal-navigation.js'][1]=Buffer.from(script).toString('base64');let pages=0;for(const asset of Object.values(assets))if(asset[0].startsWith('text/html')){const old=Buffer.from(asset[1],'base64').toString(),next=canonicalHeaderHtml(old);if(next!==old){asset[1]=Buffer.from(next).toString('base64');pages++;}}
const after=prefix+JSON.stringify(assets)+before.slice(end);save(worker,after,'worker.js');save(path.join(release,'web/portal-navigation.js'),script,'web-script.js');
for(const file of[path.join(release,'control-panel/public/cp-v2/index.html'),path.join(release,'node/public/forums.html'),...extraFiles])if(existsSync(file))save(file,canonicalHeaderHtml(readFileSync(file,'utf8')),'templates/'+createHash('sha256').update(file).digest('hex')+'.html');
if(nativeRoot){save(path.join(nativeRoot,'shared-main-menu.html'),PORTAL_NAV,'shared-main-menu.html');for(const name of['shared-header.html','shared-header-head.html']){const file=path.join(nativeRoot,name);if(existsSync(file))save(file,canonicalHeaderHtml(readFileSync(file,'utf8')),name);}}
const apiUnchanged=createHash('sha256').update(before.slice(end)).digest('hex')===createHash('sha256').update(after.slice(after.indexOf(';\n'))).digest('hex');if(!apiUnchanged)throw Error('Runtime tail changed');return{changed,pages,apiUnchanged};
}
if(process.argv[1]&&path.resolve(process.argv[1])===fileURLToPath(import.meta.url)){const[release,nativeRoot,backupDir]=process.argv.slice(2);console.log(JSON.stringify(deployCanonicalMenu({release,nativeRoot,backupDir,extraFiles:['/home/dunerealms/htdocs/clans.dunerealms.com/index.html']})));}