-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigLoader.js
More file actions
50 lines (45 loc) · 1.54 KB
/
configLoader.js
File metadata and controls
50 lines (45 loc) · 1.54 KB
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
/**
* Loads an eslint config from either a file or a JSON object in package.json
* WARNING - relies on private module - API subject to change
*/
"use strict";
const log = require('debug')('configLoader');
const fs = require('fs');
const Promise = require('bluebird');
const readFileAsync = Promise.promisify(fs.readFile);
//not part of eslint's public API - so fetch by path
const esLintConfig = require('eslint/lib/config/config-file');
/**
* Has the repo specified got any eslint config file (or section in package.json)
* @param repoPath path to the repo
* @returns {boolean}
*/
module.exports.hasConfigFile = function(repoPath){
return new Promise(function(resolve, reject){
const configFile = esLintConfig.getFilenameForDirectory(repoPath);
log(`Config file: ${configFile}`);
if(configFile && configFile.endsWith('/package.json')) {
log('Using package.json for eslint config');
readFileAsync(configFile)
.then((fileContents) => {
log('have loaded package.json');
const packageJson = JSON.parse(fileContents);
resolve(packageJson['eslintConfig'] !== undefined);
});
} else {
resolve(configFile !== null);
}
});
};
/**
* fetch the eslint config for a repo
* @param repoPath the path to the repo
* @returns {Object} eslint config
*/
module.exports.loadConfig = function(repoPath){
var configFile = esLintConfig.getFilenameForDirectory(repoPath);
if(configFile){
var overrideWithEnv = true;
return esLintConfig.load(configFile, overrideWithEnv);
}
};