forked from keturn/lintnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
130 lines (106 loc) · 3.52 KB
/
app.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
/* HTTP interface to JSHint.
Takes roughly half the time to jslint something with this than to
start up a new rhino instance on every invocation.
Invoke from bash script like:
curl --form source="<${1}" ${JSHINT_URL}
If you use source="@${1}" instead, curl does it like a file upload.
That includes a filename, which is nice, but has express make a
temp file for the upload.
*/
/*global process, require */
var express = require("express");
var JSHINT = require('./jshint/jshint');
var fs = require('fs');
var app = express.createServer();
app.configure(function () {
app.use(require('connect-form')({keepExtensions: true}));
app.use(express.errorHandler(
{ dumpExceptions: true, showStack: true }));
app.use(express.bodyParser());
});
var jslint_port = 3003;
/* copied from jslint's rhino.js */
var theGoodParts = {
bitwise: true,
immed: true,
newcap: true,
nomen: true,
onevar: true,
plusplus: true,
regexp: true,
undef: true,
white: true
};
var outputErrors = function (errors) {
var e, i, len, output = [];
// debug("Handling " + errors.length + "errors" + '\n');
function write(s) {
output.push(s + '\n');
}
for (i = 0, len = errors.length; i < len; i += 1) {
e = errors[i];
if (e) {
write('Line ' + e.line + ', character ' + e.character + ': ' + e.evidence);
write(e.reason);
if (i != len-1) write('');
}
}
return output.join('');
};
app.get('/', function (req, res) {
res.render('upload.haml');
});
app.post('/jslint', function (request, res) {
var filename;
if (! request.form) {
throw new TypeError("form data required");
}
return request.form.complete(function (err, fields, files) {
var headers = {'Content-Type': 'text/plain'};
var jsHintOpts = (fields.opts)?JSON.parse(fields.opts):theGoodParts;
var passed = false;
if (files.source) {
// FIXME: It's pretty silly that we have express write the upload to
// a tempfile only to read the entire thing back into memory
// again.
filename = files.source.filename;
fs.readFile(files.source.path, 'utf8',
function (err, sourcedata) {
var results;
results = doLint(sourcedata);
res.send(results, headers, 200);
fs.unlink(files.source.path);
});
} else {
filename = fields.filename;
passed = JSHINT.JSHINT(fields.source, jsHintOpts);
if (passed) {
res.send("OK\n", headers, 200);
} else {
res.send(outputErrors(JSHINT.JSHINT.errors)+"\n"+JSHINT.JSHINT.errors.length+" error(s)\n", headers, 400);
}
}
});
});
/* This action always return some JSHint problems. */
var exampleFunc = function (req, res) {
JSHINT.JSHINT("a = function(){ return 7 + x }()",
jslint_options);
res.send(outputErrors(JSHINT.JSHINT.errors),
{'Content-Type': 'text/plain'});
};
app.get('/example/errors', exampleFunc);
app.post('/example/errors', exampleFunc);
/* This action always returns JSHint's a-okay message. */
app.post('/example/ok', function (req, res) {
res.send("jslint: No problems found in example.js\n",
{'Content-Type': 'text/plain'});
});
function parseCommandLine() {
var port_index = process.ARGV.indexOf('--port');
if (port_index > -1) {
jslint_port = process.ARGV[port_index + 1];
}
}
parseCommandLine();
app.listen(jslint_port);