forked from hssh/node-js-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version of grader.js and checks.json
- Loading branch information
1 parent
703dc75
commit b1d3bfa
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
["h1", | ||
".navigation"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env node | ||
|
||
var fs = require('fs'); | ||
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); | ||
} | ||
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(); | ||
var out = {}; | ||
for(var ii in checks) { | ||
var present = $(checks[ii]).length > 0; | ||
out[checks[ii]] = present; | ||
} | ||
return out; | ||
}; | ||
|
||
var clone = function(fn) { | ||
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), HTMLFILE_DEFAULT) | ||
.parse(process.argv); | ||
var checkJson = checkHtmlFile(program.file, program.checks); | ||
var outJson = JSON.stringify(checkJson, null, 4); | ||
console.log(outJson); | ||
}else{ | ||
exports.checkHtmlFile = checkHtmlFile; | ||
} |