Skip to content

Instantly share code, notes, and snippets.

@vinodpandey
Forked from agustinhaller/01_jshint.js
Last active November 21, 2018 05:16
Show Gist options
  • Save vinodpandey/06d4bcb080d51d3d148c36ba2410fc0a to your computer and use it in GitHub Desktop.
Save vinodpandey/06d4bcb080d51d3d148c36ba2410fc0a to your computer and use it in GitHub Desktop.
ionic Before Prepare Hooks
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var jshint = require('jshint').JSHINT;
var async = require('async');
var glob = require("glob");
var foldersToProcess = [
'js'
];
foldersToProcess.forEach(function(folder) {
processFiles("www/" + folder);
});
function processFiles(dir, callback) {
var errorCount = 0;
glob(dir + "/**/*.js", function(err, list) {
if (err) {
console.log('processFiles err: ' + err);
return;
}
async.eachSeries(list, function(file, innercallback) {
fs.stat(file, function(err, stat) {
if(path.extname(file) === ".js") {
lintFile(file, function(hasError) {
if(hasError) {
errorCount++;
}
innercallback();
});
} else {
innercallback();
}
});
}, function(error) {
if(errorCount > 0) {
process.exit(1);
}
});
});
}
function lintFile(file, callback) {
console.log("Linting " + file);
fs.readFile(file, function(err, data) {
if(err) {
console.log('Error: ' + err);
return;
}
if(jshint(data.toString())) {
console.log('File ' + file + ' has no errors.');
console.log('-----------------------------------------');
callback(false);
} else {
console.error('Errors in file ' + file);
var out = jshint.data(),
errors = out.errors;
for(var j = 0; j < errors.length; j++) {
console.error(errors[j].line + ':' + errors[j].character + ' -> ' + errors[j].reason + ' -> ' +
errors[j].evidence);
}
console.error('-----------------------------------------');
callback(true);
}
});
}
@cumibulat
Copy link

works as needed, compare to few other gist that saying it can do recursion.. great job !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment