-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver-debug.js
More file actions
54 lines (46 loc) · 1.52 KB
/
server-debug.js
File metadata and controls
54 lines (46 loc) · 1.52 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
const path = require('path');
const fs = require('fs-extra');
const express = require('express');
const { createServer } = require('http');
const logger = require('./src/server/utils/logger');
const assetsPath = './src/client/assets';
const assetsBackupPath = './src/client/assets/backup';
async function updateSpritesheets(req, resp) {
const filename = req.params.filename;
if (!filename) {
logger('Missing filename in request');
resp.status(400).send('Missing filename in request');
return;
}
const fullPath = path.join(assetsPath, filename);
if (!(await fs.pathExists(fullPath))) {
logger(`File does not exist: ${fullPath}`);
resp.status(400).send('File does not exist');
return;
}
const date = new Date();
const backupFullPath = path.join(assetsBackupPath, `${filename}.${date.getTime()}.bak`);
try {
await fs.move(fullPath, backupFullPath)
} catch (e) {
logger('Error occurred while moving:', e.stack);
resp.status(500).send('Error occurred while moving');
return;
}
try {
await fs.writeJSON(fullPath, req.body, { spaces: 2 });
} catch (e) {
logger('Error occurred while writing:', e.stack);
resp.status(500).send('Error occurred while writing');
return;
}
logger('Written to', fullPath);
resp.send('ok');
}
const app = express();
app.use(express.json({ limit: '50mb' }));
app.use(express.static('dist-debug'));
app.post('/spritesheets/:filename', updateSpritesheets);
const server = createServer(app);
server.listen(8080);
logger('Server Debug ready');