-
Notifications
You must be signed in to change notification settings - Fork 1
/
dvcs_jslint.js
75 lines (72 loc) · 2.16 KB
/
dvcs_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
/*jslint strict: false, maxerr: 5, maxlen: 80, indent: 2*/
/*global process: false, require: false */
(function () {
var jslintPath = process.argv[2],
repoType = process.argv[3],
exec = require('child_process').exec,
utils = require("util"),
jsExtRegExp = /\.js\s*$/,
okRegExp = /No problems found/,
gitModRegExp = /(new file|modified)\:\s*(\S+\.js)\s*/,
gitNotUpdated = "Changed but not updated",
gitUntracked = "Untracked files:",
cmd = "node " + jslintPath + " ",
dvCmd = repoType === "hg" ? "hg status" : "git status",
processStatus, getHgFiles, getGitFiles, processFiles;
(function () {
if (repoType !== "hg" && repoType !== "git") {
throw "Unsupported repo type: " + repoType +
". Valid values are hg or git.";
}
//Get list of changed files.
exec(dvCmd, function (error, stdout, stderr) {
processStatus(stdout.split("\n"));
});
}());
processStatus = function (lines) {
var fn;
if (lines.length) {
if (repoType === "hg") {
fn = getHgFiles;
} else if (repoType === "git") {
fn = getGitFiles;
}
processFiles(fn(lines));
}
};
getHgFiles = function (lines) {
var files = [];
lines.forEach(function (line) {
if ((line.charAt(0) === "A" || line.charAt(0) === "M") &&
jsExtRegExp.test(line)) {
//Added or modified JS file, add to list.
files.push(line.substring(2, line.length));
}
});
return files;
};
getGitFiles = function (lines) {
var files = [];
lines.forEach(function (line) {
if ((line.indexOf(gitNotUpdated) !== -1) ||
(line.indexOf(gitUntracked) !== -1)) {
return;
}
var match = gitModRegExp.exec(line);
if (match) {
files.push(match[2]);
}
});
return files;
};
processFiles = function (files) {
files.forEach(function (file) {
exec(cmd + file, function (error, stdout, stderr) {
if (((stdout.length > 0) && !okRegExp.test(stdout)) ||
(stderr.length > 0)) {
throw "JSLint error in " + file + ":\n" + stdout;
}
});
});
};
}());