Skip to content

Instantly share code, notes, and snippets.

@agustinhaller
Last active August 18, 2017 07:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save agustinhaller/5e489e5419e43b11d7b7 to your computer and use it in GitHub Desktop.
Save agustinhaller/5e489e5419e43b11d7b7 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 foldersToProcess = [
'js'
];
foldersToProcess.forEach(function(folder) {
processFiles("www/" + folder);
});
function processFiles(dir, callback) {
var errorCount = 0;
fs.readdir(dir, function(err, list) {
if (err) {
console.log('processFiles err: ' + err);
return;
}
async.eachSeries(list, function(file, innercallback) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if(!stat.isDirectory()) {
if(path.extname(file) === ".js") {
lintFile(file, function(hasError) {
if(hasError) {
errorCount++;
}
innercallback();
});
} else {
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.log('Errors in file ' + file);
var out = jshint.data(),
errors = out.errors;
for(var j = 0; j < errors.length; j++) {
console.log(errors[j].line + ':' + errors[j].character + ' -> ' + errors[j].reason + ' -> ' +
errors[j].evidence);
}
console.log('-----------------------------------------');
callback(true);
}
});
}
@LimeBlast
Copy link

Rather than putting all my code into the js folder, I've instead set-up a folder structure inside an app folder, which contains subfolders with additional javascript in them. How can I change this to search in subfolders too?

I've changed line 9 to 'app', and that picks up the app.js file inside it, bit nothing in any of the subfolders.

Cheers.

@paulking00
Copy link

Great, thank you!

I have forked a version to add 1st level folder recursion

https://gist.github.com/paulking00/28f38ed875589c94f2f1

@krysalead
Copy link

Thanks for this script
I have modified with recursion, coloring, bug fixing
https://gist.github.com/49b36b4c7c3b5984d833.git

@johnrobertcobbold
Copy link

Hi Agustin,
Thanks for this. I ended up adding an array of files to ignore, i.e var filesToIgnore = [ 'angularfire.min.js', 'firebase.js', ... ] and checking if the iterated file was one of them as these different files, which are not under my control, kept returning dozens of errors.

@LimeBlast
Copy link

Thank you, @paulking00, that seems to work

@vinodpandey
Copy link

Thanks Agustin. This script is really useful. I have modified this to implement recursion. https://gist.github.com/vinodpandey/06d4bcb080d51d3d148c36ba2410fc0a

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