-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprecommit.js
executable file
·123 lines (114 loc) · 3.45 KB
/
precommit.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
#!/usr/bin/env node
var fs = require('fs');
var path = require("path");
var JSLINT = require('./jslint.js');
var config = require('./config.json');
// merge `process.cwd()/jslint_config.json`
var configPath = path.join(process.cwd(), 'jslint_config.json');
if (path.existsSync(configPath)) {
var customConfig = require(configPath);
var k;
for (k in customConfig) {
if (customConfig.hasOwnProperty(k)) {
config[k] = customConfig[k];
}
}
}
var argv = process.argv;
var recursion = argv[3] === "-r";
var ignores = ["node_modules", "build", "bin", "test", "public"];
if (!argv[2]) {
console.log("Usage: node precommit.js file.js");
console.log("Usage: node precommit.js folder [-r]");
process.exit(1);
}
function removeFirstComment(s) {
if (s.substring(0, 2) === '#!') {
s = s.substring(s.indexOf('\n'));
}
return s;
}
var processFolder = function (folder) {
fs.stat(folder, function (err, stats) {
if (err) {
console.log(folder);
console.log(err.stack);
return;
}
if (stats.isDirectory()) {
fs.readdir(folder, function (err, files) {
var scripts = files.filter(function (val, index) {
return path.extname(val) === ".js";
});
scripts.forEach(function (filename, index) {
var input = fs.readFileSync(folder + "/" + filename, "utf8");
input = removeFirstComment(input);
if (!JSLINT(input, config)) {
console.log("File: " + folder + "/" + filename);
var i;
for (i = 0; i < JSLINT.errors.length; i += 1) {
var e = JSLINT.errors[i];
if (e) {
console.log('Lint at line ' + e.line + ' character ' +
e.character + ': ' + e.reason);
console.log((e.evidence || '').
replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
console.log('');
}
}
} else {
console.log("jslint: No problems found in " + folder + "/" + filename);
}
});
if (recursion) {
var folders = files.filter(function (val, index) {
if (path.extname(val) !== "") {
return false;
}
var i, len;
for (i = 0, len = ignores.length; i < len; i++) {
if (val === ignores[i]) {
return false;
}
}
return true;
});
folders.forEach(function (dict, index) {
processFolder(folder + "/" + dict);
});
}
});
}
});
};
fs.stat(argv[2], function (err, stats) {
if (stats.isFile()) {
var input = fs.readFileSync(argv[2], "utf-8");
if (!input) {
console.log("jslint: Couldn't open file '" + argv[2] + "'.");
process.exit(1);
}
input = removeFirstComment(input);
if (!JSLINT(input, config)) {
var i;
for (i = 0; i < JSLINT.errors.length; i += 1) {
var e = JSLINT.errors[i];
if (e) {
console.log('Lint at line ' + e.line + ' character ' +
e.character + ': ' + e.reason);
console.log((e.evidence || '').
replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
console.log('');
}
}
process.exit(2);
} else {
console.log("jslint: No problems found in " + argv[2]);
process.exit();
}
} else if (stats.isDirectory()) {
processFolder(argv[2]);
} else {
process.exit();
}
});