-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (44 loc) · 1.34 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
'use strict';
const fs = require('fs');
const path = require('path');
const loadJsonFile = require('load-json-file');
const requireDirectory = require('require-directory');
const detectors = requireDirectory(module, './lib', { recurse: false });
function createLoadPackageJson(dir) {
let packageJsonPromise;
return () => {
if (!packageJsonPromise) {
packageJsonPromise = loadJsonFile(path.join(dir, 'package.json'))
.catch((err) => {
if (err.code === 'ENOENT') {
return null;
}
throw err;
});
}
return packageJsonPromise;
};
}
function detectRepoLinters(dir) {
// Check if dir exists
return new Promise((resolve, reject) => {
fs.stat(dir, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
})
// Run the linter detectors and build the results
.then(() => {
const loadPackageJson = createLoadPackageJson(dir);
return Promise.all(
Object.keys(detectors)
.map((name) => detectors[name](dir, loadPackageJson)
.then((detected) => detected && name))
);
})
.then((linters) => linters.filter((linter) => linter));
}
module.exports = detectRepoLinters;