20 lines
1.5 KiB
JavaScript
20 lines
1.5 KiB
JavaScript
|
|
'use strict';
|
||
|
|
const fs=require('fs');
|
||
|
|
const path=require('path');
|
||
|
|
|
||
|
|
const root=path.resolve(__dirname,'..');
|
||
|
|
const corePath=path.join(root,'puzzle-core.js');
|
||
|
|
const patternsPath=path.join(root,'puzzle-patterns.js');
|
||
|
|
const source=fs.readFileSync(corePath,'utf8');
|
||
|
|
const loader="const PuzzlePatterns=root.BendPuzzlePatterns||(typeof module==='object'&&module.exports?require('./puzzle-patterns'):null);\nif(!PuzzlePatterns)throw new Error('BendPuzzlePatterns is not loaded');\nconst MICRO_PATTERN_SETS=PuzzlePatterns.MICRO_PATTERN_SETS;\n";
|
||
|
|
|
||
|
|
if(source.includes("require('./puzzle-patterns')")){
|
||
|
|
if(!fs.existsSync(patternsPath))throw new Error('puzzle-core.js is split but puzzle-patterns.js is missing');
|
||
|
|
process.exit(0);
|
||
|
|
}
|
||
|
|
const start=source.indexOf('const MICRO_PATTERN_SETS='),end=source.indexOf('\nconst H_PORT_PROFILES=',start);
|
||
|
|
if(start<0||end<0)throw new Error('Could not locate the generated micro-pattern table');
|
||
|
|
const declaration=source.slice(start,end),expression=declaration.slice('const MICRO_PATTERN_SETS='.length,-1);
|
||
|
|
const patternModule=`'use strict';\n// Generated puzzle routing patterns, split from puzzle-core.js. Do not edit by hand.\n(function(root,factory){const api=factory();if(typeof module==='object'&&module.exports)module.exports=api;if(root)root.BendPuzzlePatterns=api})(typeof globalThis!=='undefined'?globalThis:this,()=>Object.freeze({MICRO_PATTERN_SETS:Object.freeze(${expression})}));\n`;
|
||
|
|
fs.writeFileSync(patternsPath,patternModule);
|
||
|
|
fs.writeFileSync(corePath,source.slice(0,start)+loader+source.slice(end+1));
|