web/website/scripts/public-exchange-assets.mjs

28 lines
2 KiB
JavaScript

import {readFileSync,writeFileSync,renameSync} from 'node:fs';
import path from 'node:path';
export const EXCHANGE_ASSETS=[
['cp-v2/cp-v2-exchange-design.mjs','codex-exchange.js'],['cp-v2/cp-v2-exchange-design.css','codex-exchange.css'],
['organisation-tabs.mjs','codex-tabs.js'],['organisation-tabs.css','codex-tabs.css'],['jarvis-brand.mjs','jarvis-brand.js'],
['cp-v2/cp-v2-exchange-cards.mjs','exchange-cards.js'],['cp-v2/cp-v2-exchange-cards.css','exchange-cards.css'],
...['item-lore-model.mjs','item-lore-view.mjs','item-lore.css','item-design-model.mjs','item-metadata-policy.mjs','tool-template-policy.mjs','equipment-template-policy.mjs','organisation-refit-policy.mjs','organisation-research-policy.mjs','mining-tool-policy.mjs','kit-design-model.mjs','starter-kit-policy.mjs'].map(name=>[name,name])
];
export function publishExchangeAssets(sourceDirectory,targetDirectory){
const targets=new Map(EXCHANGE_ASSETS);
// Validate the complete import graph before publishing any member of it.
const prepared=EXCHANGE_ASSETS.map(([source,target])=>{
let content=readFileSync(path.join(sourceDirectory,source),'utf8');
if(/\.(?:mjs|js)$/.test(source)){
for(const match of content.matchAll(/\b(?:from\s*|import\s*)(['"])(\.{1,2}\/[^'"]+)\1/g)){
const dependency=path.posix.normalize(path.posix.join(path.posix.dirname(source),match[2]));
if(!targets.has(dependency))throw Error('Unpublished Exchange dependency: '+source+' → '+dependency);
}
content=content.replace(/(['"])(\.{1,2}\/[^'"]+)\1/g,(original,quote,relative)=>{
const dependency=path.posix.normalize(path.posix.join(path.posix.dirname(source),relative));
return targets.has(dependency)?quote+'./'+targets.get(dependency)+quote:original;
});
}
return [target,content];
});
for(const [target,content]of prepared){const file=path.join(targetDirectory,target),temporary=file+'.build-'+process.pid;writeFileSync(temporary,content);renameSync(temporary,file);}
return prepared.map(([target])=>target);
}