-
Notifications
You must be signed in to change notification settings - Fork 0
/
growl-jslint.js
90 lines (82 loc) · 2.45 KB
/
growl-jslint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var sys = require('sys'),
fs = require('fs'),
path = require('path'),
growl = require('growl'),
nopt = require("nopt"),
jslint = require('/Users/pseidel/.nave/installed/0.4.8/lib/node/.npm/jslint/0.1.0/package/lib/linter');
/**
* Test single file with jslint
*/
function validate(filename, callback) {
fs.readFile(filename, 'utf8', function(err, file) {
sys.print('Checking ' + filename.replace(__dirname + '/', '') + '... ');
callback(jslint.lint(file));
});
}
/**
* Output status of JSLint validation
*/
function status(ok) {
var errors = ok.errors;
if(errors.length == 0) {
sys.puts("all right");
} else {
sys.puts(errors.length + ' errors');
for (var i = 0; i < errors.length; i += 1) {
if (errors[i]) {
sys.puts(' ' + errors[i].line + ': ' + errors[i].reason);
}
}
}
}
function status_and_growl(filename, ok) {
if(ok.errors.length > 0) {
growl.notify(filename + "\n" + ok.errors.length + ' errors (check console)');
}
status(ok);
}
/**
* Process file or directory through callback function
*/
function walk(filename, callback){
fs.stat(filename, function(err, stats) {
if(stats.isFile() && filename.match(/\.js$/)) {
// Filename - do callback
callback(filename);
} else if(stats.isDirectory()) {
// Directory - walk recursive
fs.readdir(filename, function(err, files) {
for(var i = 0; i < files.length; i++) {
walk(filename + '/' + files[i], callback);
}
});
}
});
}
var options = {
"directory" : [String],
"fileregex" : [String]
}
var shorthandOptions = {
"d" : "--directory",
"re" : "--fileregex"
}
var parsed = nopt(options, shorthandOptions);
var dirname = parsed.directory || __dirname;
var fregex = new RegExp(parsed.fileregex || ".*");
var _bind = function (f /* args */) {
var _args = Array.prototype.slice.call(arguments, 1);
return function() {
f.apply(this, _args.concat(Array.prototype.slice.call(arguments)));
}
}
walk(dirname, function(filename) {
if (filename.search(fregex) >= 0) {
validate(filename, _bind(status_and_growl, filename));
fs.watchFile(filename, function(curr, prev) {
if(curr.mtime - prev.mtime !== 0) {
validate(filename, _bind(status_and_growl, filename));
}
});
}
});