Created
July 9, 2013 21:56
-
-
Save jrm2k6/5961636 to your computer and use it in GitHub Desktop.
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
var fs = require('fs'); | |
var rest = require('restler'); | |
var program = require('commander'); | |
var cheerio = require('cheerio'); | |
var HTMLFILE_DEFAULT = "index.html"; | |
var CHECKSFILE_DEFAULT = "checks.json"; | |
var URL_DEFAULT = "http://floating-waters-9907.herokuapp.com/"; | |
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 loadUsingCheerio = function(result){ | |
return cheerio.load(result); | |
} | |
var cheerioURLContent = function(url, callback) { | |
rest.get(url).on("complete", function(result){ | |
console.log(result); | |
return callback(result); | |
}); | |
} | |
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 checkContentFromURL = function(contentFromUrl, checksfile) { | |
$ = cheerioURLContent(contentFromUrl, loadUsingCheerio); | |
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('-u, --url <url>', 'URL to index.html') | |
.option('-f, --file <html_file>', 'Path to index.html', clone(assertFileExists)) | |
.parse(process.argv); | |
var checkJson; | |
if (program.url) { | |
console.log("using url"); | |
checkJson = checkContentFromURL(program.url, program.checks); | |
} | |
else { | |
console.log("using file"); | |
checkJson = checkHtmlFile(program.file, program.checks); | |
} | |
var outJson = JSON.stringify(checkJson, null, 4); | |
console.log(outJson); | |
} else { | |
exports.checkHtmlFile = checkHtmlFile; | |
exports.checkContentFromURL = checkContentFromURL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment