import fs from 'node:fs'; import path from 'node:path'; import {fileURLToPath} from 'node:url'; import crypto from 'node:crypto'; const ROOT=path.dirname(path.dirname(fileURLToPath(import.meta.url))); const data=JSON.parse(fs.readFileSync(path.join(ROOT,'references/runtime-data.json'),'utf8')); fs.mkdirSync(path.join(ROOT,'dist'),{recursive:true}); const common=['postgres.mjs','files.mjs','core.mjs','confirmation.mjs','workshop.mjs','board-actions.mjs','bootstrap.mjs'].map(file=>fs.readFileSync(path.join(ROOT,'src',file),'utf8').replace(/^export /gm,'')).join('\n'); const helpers={ksfUploadFile:'/ksf/upload',ksfReadFile:'/ksf/file',ksfConfirmationPage:'/ksf/confirm',ksfCreateReportPreview:'/ksfCreateReportPreview',ksfGetReportPreview:'/ksfGetReportPreview',ksfEmployeeConfirmation:'/ksfEmployeeConfirmation',ksfGetPreviewConfirmation:'/ksfGetPreviewConfirmation'}; const html=fs.readFileSync(path.join(ROOT,'web/confirm.html'),'utf8');const manifest=[]; for(const name of [...new Set([...data.functionNames,...Object.keys(helpers),'ksfCheckDuplicates','ksfStartWorkshopSession','ksfSaveWorkshopTask','ksfSaveWorkshopConcept','ksfGetWorkshopProject','ksfGetPublicBoard','ksfSetPublicOpportunityStatus','ksfRatePublicOpportunity'])]){ const code=common+'\nasync function handler(request,response){return runDatabaseFunction('+JSON.stringify(name)+',request,response,'+JSON.stringify(data)+','+JSON.stringify(html)+');}\n'; new Function(code); // Syntax check the exact code that will be stored in Function.code. fs.writeFileSync(path.join(ROOT,'dist',name+'.js'),code); manifest.push({name,path:helpers[name]||'/'+name,file:'dist/'+name+'.js',sha256:crypto.createHash('sha256').update(code).digest('hex')}); } fs.writeFileSync(path.join(ROOT,'dist/manifest.json'),JSON.stringify({release:data.release,schemaVersion:data.schemaVersion,contractRevision:data.contractRevision,functions:manifest},null,2)); console.log(JSON.stringify({built:manifest.length,businessFunctions:data.functionNames.length,helpers:Object.keys(helpers).length}));