This repository has been archived by the owner on Jul 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
96 lines (82 loc) · 2.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const path = require('path');
const { exec } = require('child_process');
const { watch } = require('chokidar');
const WebSocket = require('ws');
const express = require('express');
const opn = require('opn');
const argv = require('yargs-parser')(process.argv.slice(2), {
boolean: ['open'],
string: ['lovr'],
default: { open: true, lovr: 'https://lovr.org/static/js' }
});
if (argv._.length < 1) {
console.error('usage: lovr-webvr-server [--port <port>] [--[no-]open] [--lovr <path>] project');
return process.exit(1);
}
const source = argv._[0];
const project = path.basename(path.resolve(source));
const port = argv.port || 8080;
const open = argv.open;
const lovr = argv.lovr;
let updated = false;
const compile = (req, res, next) => {
if (updated) return next();
updated = true;
const filePackager = path.join(__dirname, 'emscripten/tools/file_packager.py');
const dataOutput = path.join(__dirname, 'build', `${project}.data`);
const preload = path.resolve(source);
const jsOutput = path.join(__dirname, 'build', `${project}.js`);
const command = [
'python',
`"${filePackager}"`,
`"${dataOutput}"`,
'--no-heap-copy',
`--preload "${preload}"@/`,
`--js-output="${jsOutput}"`
].join(' ');
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(stderr);
updated = false;
return next();
}
compile(req, res, next);
});
};
const refresh = () => {
connections.forEach(connection => {
if (connection.readyState === WebSocket.OPEN) {
connection.send('refresh');
}
});
};
let ready = false;
watch(source).
on('ready', () => ready = true).
on('all', event => {
if (ready && updated) {
updated = false;
compile(null, null, refresh);
}
});
const socketServer = new WebSocket.Server({ port: port + 1 });
let connections = [];
socketServer.on('connection', connection => {
connections.push(connection);
connection.on('error', () => {});
connection.on('close', () => connections.splice(connections.indexOf(connection)));
});
const isRemote = lovr.startsWith('http');
const base = isRemote ? lovr.replace(/\/+$/, '') : '';
const baseServe = isRemote ? function(req, res, next) { next(); } : express.static(path.resolve(lovr));
express().
set('view engine', 'ejs').
set('views', path.join(__dirname, './views')).
get('/', compile, (req, res) => res.render('index', { project, base })).
use(express.static(path.join(__dirname, 'build'))).
use(baseServe).
listen(port, function() {
const address = this.address();
console.log(`Listening on ${address.port}`);
open && opn(`http://localhost:${address.port}`);
});