admin-speed-patch.mjs 1.7 KB

12345678910111213
  1. import ts from 'typescript';
  2. import {readFileSync} from 'node:fs';
  3. const gateway=readFileSync(new URL('./deploy-admin-functions.mjs',import.meta.url),'utf8').match(/const operationsGatewayCode = String.raw`([\s\S]*?)\n`;/)[1];
  4. const sources=readFileSync(new URL('./cloud/lesson-records.js',import.meta.url),'utf8')+'\n'+gateway+'\n'+readFileSync(new URL('./cloud/payroll-integrity.js',import.meta.url),'utf8');
  5. export const speedHelpers=['businessDate','payrollBusinessDate','queryUsers','coachRows','allAppointmentRows','appointmentRowsInRange','professionalScheduleData','professionalDashboardFast','scheduleTrendFast','learningReportQueryParts','vocabularyTree','vocabularyRows','specialRows','lessonHistoryBase','lessonHistoryPage','payrollOverviewData','payrollCanonicalSnapshot','professionalCoachRows','storeRows','usersPage'];
  6. function functions(code){const file=ts.createSourceFile('gateway.js',code,ts.ScriptTarget.Latest,true,ts.ScriptKind.JS);return new Map(file.statements.filter(ts.isFunctionDeclaration).map(node=>[node.name?.text,{start:node.getStart(file),end:node.end,text:node.getText(file)}]));}
  7. export function patchAdminSpeed(original){
  8. const current=functions(original),next=functions(sources);
  9. const edits=speedHelpers.map(name=>{if(!current.has(name)||!next.has(name))throw Error('Missing helper '+name);return {...current.get(name),text:next.get(name).text};}).sort((a,b)=>b.start-a.start);
  10. let result=original;for(const edit of edits)result=result.slice(0,edit.start)+edit.text+result.slice(edit.end);
  11. for(const name of ['lessonHistoryLabelColumns','lessonHistoryLabelJoins']){if(current.has(name))throw Error('Unexpected existing helper '+name);result+='\n'+next.get(name).text;}
  12. new Function(result);return result;
  13. }