forked from heroku/node-js-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrader.js
executable file
·86 lines (82 loc) · 2.15 KB
/
grader.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
//#!usr/bin/env node
/*
References:
+cheerio
+commander.js
+JSON
*/
var fs = require('fs');
var rst = require('restler');
var program = require('commander');
var cheerio = require('cheerio');
var HTMLFILE_DEFAULT = "index.html";
var CHECKSFILE_DEFAULT = "checks.json";
var assertFileExists = function(infile) {
var instr = infile.toString();
if(!fs.existsSync(instr)) {
console.log("%s does not exist. Exiting.", instr);
process.exit(1); //http://nodejs.org/api/process.html#process_process_exit_code
};
return instr;
};
var cheerioHtmlFile = function(htmlfile) {
return cheerio.load(fs.readFileSync(htmlfile));
};
var loadChecks = function(checksfile) {
return JSON.parse(fs.readFileSync(checksfile));
};
var checkHtmlFile = function(htmlfile, checksfile) {
$ = cheerioHtmlFile(htmlfile);
var checks = loadChecks(checksfile).sort();
//console.log(checks);
var out = {};
for(var ii in checks){
var present = $(checks[ii]).length > 0;
//console.log(present);
out[checks[ii]] = present;
}
return out;
};
var checkURLFile = function(urlResult, checksfile){
$ = cheerio.load(urlResult);
//console.log("$ is ......" + $);
var checks = loadChecks(checksfile).sort();
//console.log(checks);
var out = {};
for(var ii in checks){
var present = $(checks[ii]).length > 0;
//console.log("In url present");
out[checks[ii]] = present;
}
//console.log(out);
return out;
};
var clone = function(fn) {
return fn.bind({});
}
var checkJson;
if(require.main == module){
program.option('-c, --checks <check_file>', 'Path to checks.json', clone(assertFileExists), CHECKSFILE_DEFAULT)
.option('-f, --file <html_file>', 'Path to index.html', clone(assertFileExists), HTMLFILE_DEFAULT)
.option('-u, --url <url_pointer>', 'Url link')
.parse(process.argv);
if(program.url)
{
rst.get(program.url)
.on('complete' ,function(result)
{
//console.log("result----" + result);
checkJson = checkURLFile(result, program.checks);
var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);
});
}
else
{
checkJson = checkHtmlFile(program.file, program.checks);
var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);
}
//var outJson = JSON.stringify(checkJson, null, 4);
//console.log('json' + outJson);
}