Skip to content

Commit

Permalink
Adding lint checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed May 2, 2013
1 parent 20cb73b commit 5e4b6ae
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/**
public/**
14 changes: 14 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"node": true,
"indent": 2,
"asi": true,
"curly": true,
"laxbreak": true,
"latedef": true,
"quotmark": "single",
"undef": true,
"trailing": true,
"maxdepth": 3,
"maxstatements": 40,
"maxcomplexity": 8
}
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js
node_js:
- "0.10"
before_script:
- npm run-script lint
41 changes: 21 additions & 20 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,48 +50,49 @@ fsTools.findSorted('public/data', /[^.]+\.metadata.json/, function(err, files) {

var countryNames = ['U.S.'];

for (index in files) {
for (var index in files) {
var metadataFilePath = files[index];
var locationName = path.basename(metadataFilePath, '.metadata.json')

// Exclude template file
if (locationName != "_TEMPLATE") {
if (locationName != '_TEMPLATE') {
// Flag error and exit if metadata is not found
if (!fs.existsSync(metadataFilePath)) {
console.error("Metadata file not found for '" + locationName +
"'. Aborting server start.");
console.error('Metadata file not found for \'' + locationName +
'\'. Aborting server start.');
process.exit(1);
}

metadata[locationName] =
metadata[locationName] =
JSON.parse(fs.readFileSync(metadataFilePath, 'utf8'));

// Combine a list of country names.
var countryName = metadata[locationName].countryName;
if (countryName && countryNames.indexOf(countryName) == -1) {
countryNames.push(countryName);
}

// Parse GeoJSON file, find the first available latitude/longitude,
// and add them to the metadata.

geoJsonFilePath = 'public/data/' + locationName + '.geojson';
var geoJsonFilePath = 'public/data/' + locationName + '.geojson';
if (!fs.existsSync(geoJsonFilePath)) {
console.error("GeoJSON file not found for '" + locationName +
"'. Aborting server start.");
console.error('GeoJSON file not found for \'' + locationName +
'\'. Aborting server start.');
process.exit(1);
}

var geoJsonData = JSON.parse(fs.readFileSync(geoJsonFilePath, 'utf8'));

var latLon = geoJsonData.features[0].geometry.coordinates[0][0];

var lat, lon
if (latLon[0][0]) {
var lat = latLon[0][0];
var lon = latLon[0][1];
lat = latLon[0][0];
lon = latLon[0][1];
} else {
var lat = latLon[0];
var lon = latLon[1];
lat = latLon[0];
lon = latLon[1];
}

metadata[locationName].sampleLatLon = [lat, lon];
Expand All @@ -102,12 +103,12 @@ fsTools.findSorted('public/data', /[^.]+\.metadata.json/, function(err, files) {
return (normalizeCountryName(a) > normalizeCountryName(b)) ? 1 : -1;
});

var metadataFileContents =
"//\n// This file is AUTO-GENERATED each time the " +
"application is restarted.\n//\n\n" +
"var CITY_DATA = " + JSON.stringify(metadata) + ";\n" +
"var COUNTRY_NAMES = " + JSON.stringify(countryNames) + ";\n";
fs.writeFileSync("public/js/data.js", metadataFileContents);
var metadataFileContents =
'//\n// This file is AUTO-GENERATED each time the ' +
'application is restarted.\n//\n\n' +
'var CITY_DATA = ' + JSON.stringify(metadata) + ';\n' +
'var COUNTRY_NAMES = ' + JSON.stringify(countryNames) + ';\n';
fs.writeFileSync('public/js/data.js', metadataFileContents);

startApp();
startApp();
});
60 changes: 30 additions & 30 deletions bin/validator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var fs = require("fs")
var fs = require('fs')

var validator = function(metadataFilePath) {

Expand All @@ -17,9 +17,9 @@ var validator = function(metadataFilePath) {

var validateMetadataFileExists = function() {
if (metadataFileExists()) {
validationSuccesses.push("Metadata file exists");
validationSuccesses.push('Metadata file exists');
} else {
validationFailures.push("No metadata file found at " + metadataFilePath)
validationFailures.push('No metadata file found at ' + metadataFilePath)
}
}

Expand All @@ -28,24 +28,24 @@ var validator = function(metadataFilePath) {
if (metadataFileExists()) {

var requiredFieldsMap = {
locationName: "location name",
dataUrl: "data source URL",
dataTitle: "data source title",
authorTwitter: "author's twitter ID"
locationName: 'location name',
dataUrl: 'data source URL',
dataTitle: 'data source title',
authorTwitter: 'author\'s twitter ID'
}

var j = getMetadataJson()
for (field in requiredFieldsMap) {
for (var field in requiredFieldsMap) {
var label = requiredFieldsMap[field]
if (j.hasOwnProperty(field)) {
validationSuccesses.push("Metadata JSON contains " + label + " (" + field + ")")
validationSuccesses.push('Metadata JSON contains ' + label + ' (' + field + ')')
} else {
validationFailures.push("Please specify " + label + " (" + field + ") in metadata JSON")
validationFailures.push('Please specify ' + label + ' (' + field + ') in metadata JSON')
}

}

}
}

}

Expand All @@ -55,9 +55,9 @@ var validator = function(metadataFilePath) {

var j = getMetadataJson()
if (j.dataUrl.match(/^https?\:\/\/.*\..+/)) {
validationSuccesses.push("Metadata JSON contains valid data source URL (dataUrl)")
validationSuccesses.push('Metadata JSON contains valid data source URL (dataUrl)')
} else {
validationFailures.push("Please specify valid data source URL (dataUrl) in metadata JSON")
validationFailures.push('Please specify valid data source URL (dataUrl) in metadata JSON')
}

}
Expand All @@ -68,9 +68,9 @@ var validator = function(metadataFilePath) {
var dataFilePath = getDataFilePath()
var exists = fs.existsSync(dataFilePath)
if (exists) {
validationSuccesses.push("Data file exists")
validationSuccesses.push('Data file exists')
} else {
validationFailures.push("No data file found at " + dataFilePath)
validationFailures.push('No data file found at ' + dataFilePath)
}
}

Expand All @@ -79,14 +79,14 @@ var validator = function(metadataFilePath) {
if (metadataFileExists()) {

var metadataJson

return function() {

if (!metadataJson) {
metadataJson = JSON.parse(fs.readFileSync(metadataFilePath))
}
return metadataJson

}

}
Expand Down Expand Up @@ -119,18 +119,18 @@ var validator = function(metadataFilePath) {
},

getValidationReport: function() {
var report = "Validation report:\n\n"
var report = 'Validation report:\n\n'

report += "Successes:\n"
for (successIndex in validationSuccesses) {
report += 'Successes:\n'
for (var successIndex in validationSuccesses) {
var success = validationSuccesses[successIndex]
report += "\t+ " + success + "\n"
report += '\t+ ' + success + '\n'
}

report += "Failures:\n"
for (failureIndex in validationFailures) {
report += 'Failures:\n'
for (var failureIndex in validationFailures) {
var failure = validationFailures[failureIndex]
report += "\t+ " + failure + "\n"
report += '\t+ ' + failure + '\n'
}

return report
Expand All @@ -142,8 +142,8 @@ var validator = function(metadataFilePath) {

var printUsage = function(exitCode) {

console.log("Usage: ")
console.log("\tnode validator.js <location name>")
console.log('Usage: ')
console.log('\tnode validator.js <location name>')
console.log()

process.exit(exitCode)
Expand All @@ -152,11 +152,11 @@ var printUsage = function(exitCode) {

// Main
if (process.argv.length != 3) {
console.error("Location name not specified.")
console.error('Location name not specified.')
printUsage(1)
}

var metadataFilePath = __dirname + "/../public/data/" + process.argv[2] + ".metadata.json";
var metadataFilePath = __dirname + '/../public/data/' + process.argv[2] + '.metadata.json';

var v = validator(metadataFilePath)
v.validate()
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"fs-tools": "0.2.8",
"config": "0.4.18"
},
"devDependencies": {
"jshint": "1.1.0"
},
"scripts": {
"lint": "jshint ."
},
"engines": {
"node": "0.10.x",
"npm": "1.2.x"
Expand Down

0 comments on commit 5e4b6ae

Please sign in to comment.