-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathserver.mjs
More file actions
107 lines (89 loc) · 3.23 KB
/
Copy pathserver.mjs
File metadata and controls
107 lines (89 loc) · 3.23 KB
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
97
98
99
100
101
102
103
104
105
106
107
import ecstatic from 'ecstatic';
import { build } from 'esbuild';
import fs from 'fs';
import http from 'http';
import minimist from 'minimist';
import path from 'path';
import { localDevReglCodegenConfig as config } from '../../esbuild-config.js';
import constants from '../../tasks/util/constants.js';
import {
createMocksList,
getMockFiles,
readFiles,
saveMockListToFile,
saveReglTracesToFile
} from '../dashboard_utilities.mjs';
var args = minimist(process.argv.slice(2), {});
var PORT = args.port || 3000;
var reglTraceList = ['parcoords', 'scattergl', 'scatterpolargl', 'splom'];
// Create server
var _static = ecstatic({
root: constants.pathToRoot,
cache: 0,
gzip: true,
cors: true
});
var tracesReceived = [];
var server = http.createServer(function (req, res) {
if (req.method === 'POST' && req.url === '/api/submit-code') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var data = JSON.parse(body);
tracesReceived.push(data.trace);
handleCodegen(data);
res.statusCode = 200;
res.end();
});
} else if (req.method === 'GET' && req.url === '/api/codegen-done') {
console.log('Codegen complete');
console.log('Traces received:', tracesReceived);
res.statusCode = 200;
res.end();
setTimeout(process.exit, 1000);
} else {
_static(req, res);
}
});
// Build and bundle all the things!
await getMockFiles()
.then(readFiles)
.then(createMocksList)
.then(saveMockListToFile)
.then(saveReglTracesToFile.bind(null, reglTraceList));
// Start the server up!
server.listen(PORT);
// open up browser window
const serverUrl = `http://localhost:${PORT}/devtools/regl_codegen/index.html`;
console.log('\x1b[31mOpen the following link to launch a browser window and precompile the regl shaders.');
console.log('This can be processor and memory intensive.\x1b[0m\n');
console.log(` \x1b[1m\x1b[32m➜\x1b[0m \x1b[1mLocal:\x1b[0m \x1b[1m\x1b[36m${serverUrl}\x1b[0m\n`);
await build(config);
function handleCodegen(data) {
var trace = data.trace;
var generated = data.generated;
var pathToReglCodegenSrc = constants.pathToReglCodegenSrc;
var pathToReglPrecompiledSrc = path.join(constants.pathToSrc, 'traces', trace, 'regl_precompiled.js');
var header = ["'use strict';", ''].join('\n');
var imports = '';
var exports = ['', '/* eslint-disable quote-props */', 'module.exports = {', ''].join('\n');
var varId = 0;
Object.entries(generated).forEach(function (kv) {
var key = kv[0];
var value = kv[1];
var filePath = path.join(pathToReglCodegenSrc, key);
fs.writeFileSync(filePath, 'module.exports = ' + value);
imports += 'var v' + varId + " = require('../../" + path.join(constants.reglCodegenSubdir, key) + "');\n";
exports += " '" + key + "': v" + varId + ',\n';
varId++;
});
if (varId > 0) {
exports = exports.slice(0, -2) + '\n};\n';
} else {
exports = 'module.exports = {};\n';
}
var precompiled = header + imports + exports;
fs.writeFileSync(pathToReglPrecompiledSrc, precompiled);
}