forked from sindresorhus/atom-jshint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-config.js
111 lines (95 loc) · 2.52 KB
/
load-config.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
'use babel';
import fs from 'fs';
import path from 'path';
import shjs from 'shelljs';
import cli from 'jshint/src/cli';
import userHome from 'user-home';
// from JSHint //
// Storage for memoized results from find file
// Should prevent lots of directory traversal &
// lookups when liniting an entire project
const findFileResults = {};
/**
* Searches for a file with a specified name starting with
* 'dir' and going all the way up either until it finds the file
* or hits the root.
*
* @param {string} name filename to search for (e.g. .jshintrc)
* @param {string} dir directory to start search from
*
* @returns {string} normalized filename
*/
const findFile = (name, dir) => {
const filename = path.normalize(path.join(dir, name));
if (findFileResults[filename] !== undefined) {
return findFileResults[filename];
}
const parent = path.resolve(dir, '../');
if (shjs.test('-e', filename)) {
findFileResults[filename] = filename;
return filename;
}
if (dir === parent) {
findFileResults[filename] = null;
return null;
}
return findFile(name, parent);
};
/**
* Tries to find a configuration file in either project directory
* or in the home directory. Configuration files are named
* '.jshintrc'.
*
* @param {string} file path to the file to be linted
* @returns {string} a path to the config file
*/
const findConfig = file => {
const dir = path.dirname(path.resolve(file));
const home = path.normalize(path.join(userHome, '.jshintrc'));
const proj = findFile('.jshintrc', dir);
if (proj) {
return proj;
}
if (shjs.test('-e', home)) {
return home;
}
return null;
};
/**
* Tries to find JSHint configuration within a package.json file
* (if any). It search in the current directory and then goes up
* all the way to the root just like findFile.
*
* @param {string} file path to the file to be linted
* @returns {object} config object
*/
const loadNpmConfig = file => {
const dir = path.dirname(path.resolve(file));
const fp = findFile('package.json', dir);
if (!fp) {
return null;
}
try {
return require(fp).jshintConfig;
} catch (e) {
return null;
}
};
// / //
const loadConfigIfValid = filename => {
const strip = require('strip-json-comments');
try {
JSON.parse(strip(fs.readFileSync(filename, 'utf8')));
return cli.loadConfig(filename);
} catch (e) {
}
return {};
};
const loadConfig = file => {
const config = loadNpmConfig(file) || loadConfigIfValid(findConfig(file));
if (config && config.dirname) {
delete config.dirname;
}
return config;
};
export default loadConfig;