File size: 1,111 Bytes
9be786f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Node loader for one game: runs every <script> of <gameDir>/index.html in page order, minus the browser-only
// UI (engine/input.js, engine/app.js), so Node and the browser execute the same files in the same order.
// One game per process: games share the BB global (BB.ARENA, BB.VIEW, ...).
// usage: var BB = require('<web>/engine/load.js')(gameDir)
var path = require('path'), fs = require('fs');
var BROWSER_ONLY = /(^|\/)engine\/(input|app)\.js$/;
function scripts(gameDir) {
  var html = fs.readFileSync(path.join(gameDir, 'index.html'), 'utf8'), out = [], re = /<script src="([^"]+)"/g, m;
  while ((m = re.exec(html))) if (!BROWSER_ONLY.test(m[1])) out.push(m[1]);
  return out;
}
function load(gameDir) {
  gameDir = path.resolve(gameDir);
  if (globalThis.BB && globalThis.BB.GAME_DIR && globalThis.BB.GAME_DIR !== gameDir) throw new Error('one game per process: ' + globalThis.BB.GAME_DIR + ' already loaded');
  scripts(gameDir).forEach(function (s) { require(path.join(gameDir, s)); });
  globalThis.BB.GAME_DIR = gameDir;
  return globalThis.BB;
}
load.scripts = scripts;
module.exports = load;