'use strict'; const fs=require('fs'); const path=require('path'); const vm=require('vm'); const root=path.resolve(__dirname,'../..'); const read=name=>fs.readFileSync(path.join(root,name),'utf8'); const app=read('app.js'),html=read('index.html'),css=`${read('style.css')}\n${read('client/styles/tokens.css')}\n${read('client/styles/base.css')}\n${read('client/ui/cursor.css')}\n${read('client/styles/accessibility.css')}`,worker=read('puzzle-worker.js'),appLogicSource=read('app-logic.js'),buildMetaSource=read('build-meta.js'); const buildMeta=require(path.join(root,'build-meta.js')); function assert(value,message){if(!value)throw new Error(message)} function functionSource(name,source=app){ let start=source.indexOf(`async function ${name}(`);if(start<0)start=source.indexOf(`function* ${name}(`);if(start<0)start=source.indexOf(`function ${name}(`); assert(start>=0,`Missing ${name}`); let paren=source.indexOf('(',start),parenDepth=0,body=-1; for(let i=paren;i=0,`Missing body for ${name}`);let depth=0; for(let i=body;ifunctionSource(name)).join('\n'); vm.runInContext(`${sources}\nthis.logic={${expose.join(',')}};`,context); return context.logic; } function loadBendPuzzle(){ delete global.BendPuzzle; delete require.cache[require.resolve(path.join(root,'puzzle-core.js'))]; require(path.join(root,'puzzle-core.js')); return global.BendPuzzle; } function loadAppLogic(){ delete require.cache[require.resolve(path.join(root,'app-logic.js'))]; return require(path.join(root,'app-logic.js')); } function starterPuzzle(){ const match=app.match(/const STARTER_PUZZLE=Object\.freeze\((\{.*\})\);\nconst SCORE_VERSION/s); assert(match,'Bundled origin puzzle missing'); return JSON.parse(match[1]); } module.exports={root,read,app,html,css,worker,appLogicSource,buildMetaSource,buildMeta,assert,functionSource,createContext,runFunctions,loadBendPuzzle,loadAppLogic,starterPuzzle,vm,path,fs};