forked from pose/mocha-only-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (62 loc) · 1.61 KB
/
index.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
var assert = require('assert');
var fs = require('fs');
var glob = require('glob');
var esprima = require('esprima');
var find = require('esprimaq');
function checkEsprima(syntaxTree, cb) {
var result = find(syntaxTree)
.callMethod('describe', 'only')
.callMethod('it', 'only')
.exec();
if (result.length > 0) {
return cb(new Error('Contains describe.only or it.only'));
}
return cb();
}
function checkString(code, cb) {
var syntaxTree;
try {
syntaxTree = esprima.parse(code, { tolerant: true, loc: true });
} catch (e) {
return cb(e);
}
checkEsprima(syntaxTree, cb);
}
function checkFile(file, cb) {
fs.readFile(file, {encoding: 'utf8'}, function (err, content) {
if (err) { return cb(err); }
checkString(content, function (err) {
if (err) {
return cb(new Error('Error while checking ' + file + ': ' + err.message));
}
return cb();
});
});
}
function checkFolder(globExp, cb) {
glob(globExp, function (err, files) {
if (err) { return cb(err); }
var result = [];
files.forEach(function (file) {
checkFile(file, function (err) {
result.push(err);
if (result.length === files.length) {
result = result.filter(function (x) { return x; });
if (result.length === 0) {
return cb();
}
cb(result);
}
});
});
if (files.length === 0) {
return cb();
}
});
}
module.exports = {
checkEsprima: checkEsprima,
checkString: checkString,
checkFile: checkFile,
checkFolder: checkFolder
};