-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
71 lines (59 loc) · 1.66 KB
/
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
const path = require('path');
const fs = require('fs');
const { readFile, existsFile } = require('./utils');
const CWD = process.cwd();
const validate = (obj) => {
return (
'interpreter' in obj &&
typeof obj.interpreter === 'string' &&
obj.interpreter !== '' &&
'files' in obj &&
Array.isArray(obj.files) &&
obj.files.length &&
obj.files.filter((file) => fs.existsSync(path.join(CWD, file))).length
);
};
const loadConfigFromPackageJson = () => {
const packageJsonPath = path.join(CWD, 'package.json');
if (existsFile(packageJsonPath)) {
const packageJson = JSON.parse(readFile(packageJsonPath));
if (
packageJson &&
'shebang' in packageJson &&
Array.isArray(packageJson.shebang) &&
packageJson.shebang.length
) {
return packageJson.shebang;
}
}
return [];
};
const loadConfigFromShebangRc = () => {
const shebangRcPath = path.join(CWD, '.shebangrc');
if (existsFile(shebangRcPath)) {
const shebangRc = JSON.parse(readFile(shebangRcPath));
if (shebangRc && Array.isArray(shebangRc) && shebangRc.length) {
return shebangRc;
}
}
return [];
};
const toAbsolutePath = (files) => files.map((file) => path.join(CWD, file));
const getConfig = (dynamicConfig) => {
const conf = [];
let configData = dynamicConfig
? dynamicConfig
: [...loadConfigFromPackageJson(), ...loadConfigFromShebangRc()];
if (Array.isArray(configData) && configData.length) {
configData
.filter((elem) => validate(elem))
.forEach((elem) => {
elem.files = toAbsolutePath(elem.files);
conf.push(elem);
});
}
return conf;
};
module.exports = {
getConfig,
};