forked from heroku/node-js-sample
-
Notifications
You must be signed in to change notification settings - Fork 2
/
grader.js
executable file
·121 lines (104 loc) · 4 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
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
#!/usr/bin/env node
/*
Automatically grade files for the presence of specified HTML tags/attributes.
Uses commander.js and cheerio. Teaches command line application development
and basic DOM parsing.
References:
+ cheerio
- https://github.com/MatthewMueller/cheerio
- http://encosia.com/cheerio-faster-windows-friendly-alternative-jsdom/
- http://maxogden.com/scraping-with-node.html
+ commander.js
- https://github.com/visionmedia/commander.js
- http://tjholowaychuk.com/post/9103188408/commander-js-nodejs-command-line-interfaces-made-easy
+ JSON
- http://en.wikipedia.org/wiki/JSON
- https://developer.mozilla.org/en-US/docs/JSON
- https://developer.mozilla.org/en-US/docs/JSON#JSON_in_Firefox_2
*/
var fs = require('fs');
var program = require('commander');
var cheerio = require('cheerio');
var restler = require('restler');
var HTMLFILE_DEFAULT = "index.html";
var CHECKSFILE_DEFAULT = "checks.json";
var URL_HTMLFILE_DEFAULT = "urlIndex.html";
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 assertUrlExists = function(fileUrl) { return fileUrl;};
var urltoHtmlFile = function(fileUrl) {
var instr = fileUrl.toString();
var fileData = "";
var event = 0;
restler.get(instr).on('complete', function(result) {
if (result instanceof Error) {
console.log("Error : " + result.message);
process.exit(1);
} else {
fileData = result;
console.log("File Data : " + fileData);
}
});
wait_for_signal(event,5000);
console.log(" after Signal : " + fileData);
return fileData;
};
var cheerioHtmlFile = function(fileData) {
return cheerio.load(fileData);
};
var loadChecks = function(checksfile) {
return JSON.parse(fs.readFileSync(checksfile));
};
var checkHtmlFile = function(fileData, checksfile) {
$ = cheerioHtmlFile(fileData);
var checks = loadChecks(checksfile).sort();
var out = {};
for(var ii in checks) {
var present = $(checks[ii]).length > 0;
out[checks[ii]] = present;
}
return out;
};
var clone = function(fn) {
// Workaround for commander.js issue.
// http://stackoverflow.com/a/6772648
return fn.bind({});
};
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), "")
.option('-u, --url <html_file_url>', 'url path to index.html', clone(assertUrlExists), "")
.parse(process.argv);
var checkJson = ""
if (program.file) {
console.log("File option is selected");
checkJson = checkHtmlFile(fs.readFileSync(htmlfile), program.checks);
var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);
}else if (program.url) {
// console.log("URL option is selected");
// console.log(" URL : " + program.url);
restler.get(program.url).on('complete', function(result) {
if (result instanceof Error) {
console.log("Error : " + result.message);
process.exit(1);
} else {
checkJson = checkHtmlFile(result, program.checks);
var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);
}
});
} else {
console.log("Please provide right options");
}
} else {
exports.checkHtmlFile = checkHtmlFile;
}