This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
197 lines (157 loc) · 4.61 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const Git = require('nodegit');
const fs = require('fs');
const util = require('util');
const path = require('path');
const express = require('express');
const http = require('http');
const reload = require('reload/lib/reload');
const cheerio = require('cheerio');
const cors = require('cors');
const clientsideCodeGenerator = require('./client.js');
const app = express()
app.use(cors())
const readFileAsync = util.promisify(fs.readFile);
const rmdirAsync = util.promisify(fs.rmdir);
const tempDir = path.join(__dirname, 'tmp');
const IS_PROD = process.env.NODE_ENV === 'production';
let works = [];
let currentWorkIndex = -1;
let lastRunFailed = false;
let reloadInstance = null;
app.set('port', process.env.PORT || 8765);
/*
we need to attach our own reload injection stuff
here because reload doesn't expose a method for
using the same port for ws and http by default
so we gotta do this manually
*/
const RELOAD_FILE = path.join(__dirname, './node_modules/reload/lib/reload-client.js');
let reloadCode = fs.readFileSync(RELOAD_FILE, 'utf8');
const webSocketString = IS_PROD ? 'wss://$3' : 'ws$2://$3$4';
reloadCode = reloadCode.replace(
'socketUrl.replace()',
'socketUrl.replace(/(^http(s?):\\/\\/)(.*:)(.*)/,' + '\'' + webSocketString + '\')');
const reloadRoute = '/reload/reload.js';
app.get(reloadRoute, function (req, res) {
res.type('text/javascript')
res.send(reloadCode)
});
app.get(['/', '/index.html', '/index.htm'], function (req, res) {
const work = works[currentWorkIndex];
let workDir = tempDir;
if (works[currentWorkIndex].dir) {
workDir = path.join(tempDir, works[currentWorkIndex].dir);
}
app.use('/', express.static(workDir));
let html = '';
try {
html = fs.readFileSync(path.join(workDir, 'index.html'), 'utf8');
} catch(e) {
try {
html = fs.readFileSync(path.join('./', 'loading.html'), 'utf8');
} catch(e) {
console.error(e);
return;
}
}
const $ = cheerio.load(html);
const scriptNode = '<script src="/reload/reload.js"></script>';
$('body').append(scriptNode);
$('html').attr('lang', 'en');
$('title').remove();
$('head').append(`<title>${work.title} - ${work.author} | frameV</title>`);
$('head').append(`<meta name="description" content="Framed Visuals: collaborative generative art.">`);
const viewport = `
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="https://v36p9.csb.app/style.css" rel="stylesheet" />
<style>
.work-details {
position: absolute;
font-size: 2rem;
bottom: 1rem;
left: 1rem;
font-weight: 100;
mix-blend-mode: difference;
color: #fff;
text-decoration: none;
}
.contribute-link {
font-size: 1rem;
position: absolute;
right: 1rem;
top: 1rem;
font-weight: 100;
mix-blend-mode: difference;
color: #fff;
text-decoration: none;
}
.modal-hidden {
display: none;
}
.modal-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
padding: 1em;
box-sizing: border-box;
background-color: #fff;
}
@media (prefers-reduced-motion: reduce) {
.show-if-prefers-reduced-motion {
display: block;
}
}
</style>`;
$('head').append(viewport);
const clientStrings = clientsideCodeGenerator(work)
$('body').append(clientStrings);
res.send($.html());
})
const server = http.createServer(app)
async function start() {
let worksJson;
try {
worksJson = await readFileAsync('./works.json');
} catch (err) {
console.error(err);
return;
}
works = JSON.parse(worksJson);
try {
reloadInstance = await reload(app, {}, server);
} catch (err) {
console.error('Reload could not start, could not start server/sample app', err);
process.exit();
}
server.listen(app.get('port'), () => {
console.log('Web server listening on port', app.get('port'));
loadNext();
});
}
async function loadNext() {
const newWorkIndex = Math.floor(Math.random() * works.length);
if (currentWorkIndex === newWorkIndex && lastRunFailed) {
process.exit();
}
currentWorkIndex = newWorkIndex;
const work = works[currentWorkIndex];
try {
await rmdirAsync(tempDir, { recursive: true });
} catch(e) {
console.error(e);
}
try {
gitResult = await Git.Clone(work.repo, tempDir);
} catch(e) {
lastRunFailed = true;
console.error(e);
loadNext();
return;
}
lastRunFailed = false;
reloadInstance.reload();
setTimeout(loadNext, 1000 * 60);
}
start();