-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJakefile.js
222 lines (191 loc) · 5.57 KB
/
Jakefile.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*global desc: true, task: true, fail: true, complete: true */
(function() {
"use strict";
var childProcess = require('child_process');
var fs = require('fs');
var os = require('os');
var path = require('path');
var _ = require('underscore');
desc('Run jslint');
task('default', ['lint']);
var backtick = function(command, args, options, callback) {
var stream = childProcess.spawn(command, args, options);
var stdoutData = '';
var stderrData = '';
stream.stdout.on('data', function(data) {
if (callback) {
stdoutData += data;
} else {
process.stdout.write(data);
}
});
stream.stderr.on('data', function(data) {
if (callback) {
stderrData += data;
} else {
process.stderr.write(data);
}
});
if (callback) {
stream.on('exit', function() {
callback(stderrData, stdoutData);
});
}
};
var excludedJSFiles = [
/node_modules/
];
var getJSFiles = function(callback) {
var command = ['.', '-name', '*.js'];
backtick('find', command, null, function(err, out) {
if (err) { throw new Error(err); }
var files = out.split('\n');
files = _.filter(files, function(f) {
if (!f) { return false; }
if (f === __dirname) { return false; }
return excludedJSFiles.every(function(rule) {
return !f.match(rule);
});
});
callback(files);
});
};
var lint = function(complete) {
var jshint = require('jshint').JSHINT;
var commonOptions = {
//
// undef: true,
// asi: false,
// noarg: true,
// trailing: false,
// es5: false,
// strict: true,
camelcase: true,
curly: true,
eqeqeq: true,
forin: true,
immed: true,
latedef: true,
newcap: true,
noempty: true,
nonew: true,
quotmark: true,
regexp: false,
undef: true,
unused: true,
strict: true,
trailing: true,
asi: false,
es5: false,
evil: false
};
var nodeOptions = {
node: true,
predef: ['console', 'exports', 'clearTimeout', 'setTimeout', 'setInterval', 'escape', 'unescape']
};
// var browserOptions = {
// browser: true,
// jquery: true,
// predef: ['async', '_', 'console', 'window', 'document', "_strawberry", 'require', 'module', 'phantom']
// };
var start = new Date().getTime();
getJSFiles(function(files) {
console.log("Linting files:");
_.each(files, function(file) {
console.log(" " + file);
});
// Can't async this since jshint uses globals...
var errors = files.reduce(function(errors, file) {
var options = Object.keys(commonOptions).reduce(function(c, key) {
c[key] = commonOptions[key];
return c;
}, {});
var extraOptions = nodeOptions;
var isBrowserFile = false;
Object.keys(extraOptions).forEach(function(key) {
options[key] = extraOptions[key];
});
var fileData = fs.readFileSync(file, 'utf-8');
var error = {
file: file.replace(__dirname, '.'),
errors: []
};
if (!jshint(fileData, options)) {
error.errors = jshint.errors;
}
// Custom checks
fileData.split(/\r?\n/).forEach(function(line, i) {
// ES5 slipups
var m = new RegExp(
'Object\\.keys|\\w+[^)_]\\.(forEach|map|filter|some|reduce|every)\\b'
).exec(line);
if (m && isBrowserFile) {
error.errors.push({
line: i+1,
character: m.index,
reason: m[0] + ' may not be supported on all browsers ' +
'(wrap in parens if this is a jQuery object)'
});
}
m = /\w[^)_]\.bind\s*\(\s*function/.exec(line);
if (m && isBrowserFile) {
error.errors.push({
line: i+1,
character: m.index,
reason: m[0] + ' may not be supported on all browsers'
});
}
m = /typeof\s*\(/i.exec(line);
if (m) {
error.errors.push({
line: i+1,
character: m.index,
reason: 'typeof is not a function (remove parens)'
});
}
m = /\s+$/i.exec(line);
if (m) {
error.errors.push({
line: i+1,
character: m.index,
reason: 'line-ending whitespace'
});
}
});
if (error.errors.length) {
errors.push(error);
}
return errors;
}, []);
if (errors.length > 0) {
errors.forEach(function(error) {
console.error(error.file);
error.errors.forEach(function(error) {
if (!error) { return; }
console.error('\tline %d col %d: %s',
error.line, error.character, error.reason);
});
console.error();
});
fail('Lint errors, quitting.');
}
else {
console.log('Linted %d files in %d ms',
files.length, new Date().getTime() - start);
complete();
}
});
};
desc('Run js lint on all files');
task('lint', [], function() {
lint(complete);
}, true);
desc('Install node package dependencies local');
task('deps', [], function() {
backtick('npm', ['install'], null, function(err, out) {
if (err.trim().length > 0) { console.error(err); }
if (out.trim().length > 0) { console.error(out); }
complete();
});
}, true);
}());