forked from Workiva/paw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.js
executable file
·93 lines (83 loc) · 2.74 KB
/
record.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
#!/usr/bin/env node
/*
* Copyright 2014 Workiva, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var connect = require('connect');
var http = require('http');
var getRawBody = require('raw-body');
var path = require('path');
var mkdirp = require('mkdirp');
var fs = require('fs');
var open = require('open');
var ip = require('ip');
var moment = require('moment');
var CODE_DIR = path.resolve(process.cwd(), "src");
var acceptedFileRegex = /^\w+\.js$/i;
function writeCodeToFile(data, res, next) {
var filename = data.filename || 'CustomGestures.js';
if (!acceptedFileRegex.test(filename)) {
next('Invalid filename');
}
var filepath = path.join(CODE_DIR, filename);
mkdirp(CODE_DIR, function(err) {
if (err) {
next(err);
return;
}
if (fs.existsSync(filepath)) {
var old = filepath.replace('.js', moment().format('.MM-DD-YYYY_HH-mm') + '.js');
fs.renameSync(filepath, old)
}
fs.writeFile(filepath, data.code, function(err) {
if (err) {
next(err);
} else {
res.write('Ok');
res.end();
next();
}
});
});
}
function customGestures(req, res, next) {
if (req.url === '/CustomGestures') {
getRawBody(req, {
length: req.headers['content-length'],
limit: '1mb',
encoding: 'utf8'
},
function(err, string) {
if (err) {
return next(err);
}
if (req.method === 'POST') {
writeCodeToFile(JSON.parse(string), res, next);
} else if (req.method === 'GET') {
// serve the code back!
}
});
} else {
next(); // TODO pass through requests that aren't for me
}
}
var app = connect()
.use(connect.favicon())
.use(customGestures)
.use(connect.static(path.resolve(__dirname)))
var port = process.env.PAW_PORT || process.env.PORT || 3000;
http.createServer(app).listen(port);
var url = "http://" + ip.address() + ':' + port + '/examples/record/index.html';
console.log('To record gestures open: ', url);
open(url);