-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
183 lines (161 loc) · 5.19 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const path = require('path');
const fs = require('fs');
const JSON5 = require('json5');
const pathExists = require('path-exists');
const INFINITY = 1 / 0;
const BABELRC_FILENAME = '.babelrc';
const BABELRC_JS_FILENAME = '.babelrc.js';
const BABEL_CONFIG_JS_FILENAME = 'babel.config.js';
const PACKAGE_FILENAME = 'package.json';
const nullConf = { file: null, config: null };
function getBabelJsConfig(file) {
// eslint-disable-next-line global-require, import/no-dynamic-require
const configModule = require(file);
if (typeof configModule === 'function') {
return configModule();
}
// eslint-disable-next-line no-underscore-dangle
return configModule && configModule.__esModule ? configModule.default : configModule;
}
function asyncFind(resolve, dir, depth) {
if (depth < 0) {
return resolve(nullConf);
}
const babelrc = path.join(dir, BABELRC_FILENAME);
return pathExists(babelrc)
.then((exists) => {
if (exists) {
fs.readFile(babelrc, 'utf8', (err, data) => {
if (!err) {
resolve({
file: babelrc,
config: JSON5.parse(data),
});
}
});
}
return exists;
})
.then((exists) => {
if (!exists) {
const babelJSrc = path.join(dir, BABELRC_JS_FILENAME);
return pathExists(babelJSrc).then((ex) => {
if (ex) {
const config = getBabelJsConfig(babelJSrc);
resolve({
file: babelJSrc,
config,
});
}
});
}
return exists;
})
.then((exists) => {
if (!exists) {
const packageFile = path.join(dir, PACKAGE_FILENAME);
return pathExists(packageFile).then((ex) => {
if (ex) {
fs.readFile(packageFile, 'utf8', (err, data) => {
const packageJson = JSON.parse(data);
if (packageJson.babel) {
resolve({
file: packageFile,
config: packageJson.babel,
});
}
});
}
});
}
return exists;
})
.then((exists) => {
if (!exists) {
const babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME);
return pathExists(babelConfigJSrc).then((ex) => {
if (ex) {
const config = getBabelJsConfig(babelConfigJSrc);
resolve({
file: babelConfigJSrc,
config,
});
}
});
}
return exists;
})
.then((exists) => {
if (!exists) {
const nextDir = path.dirname(dir);
if (nextDir === dir) {
resolve(nullConf);
} else {
asyncFind(resolve, nextDir, depth - 1);
}
}
});
}
module.exports = function findBabelConfig(start, depth = INFINITY) {
if (!start) {
return new Promise(resolve => resolve(nullConf));
}
const dir = path.isAbsolute(start)
? start
: path.join(process.cwd(), start);
return new Promise((resolve) => {
asyncFind(resolve, dir, depth);
});
};
module.exports.sync = function findBabelConfigSync(start, depth = INFINITY) {
if (!start) {
return nullConf;
}
let dir = path.isAbsolute(start)
? start
: path.join(process.cwd(), start);
let loopLeft = depth;
// eslint-disable-next-line no-cond-assign
do {
const babelrc = path.join(dir, BABELRC_FILENAME);
if (pathExists.sync(babelrc)) {
const babelrcContent = fs.readFileSync(babelrc, 'utf8');
return {
file: babelrc,
config: JSON5.parse(babelrcContent),
};
}
const babelJSrc = path.join(dir, BABELRC_JS_FILENAME);
if (pathExists.sync(babelJSrc)) {
const config = getBabelJsConfig(babelJSrc);
return {
file: babelJSrc,
config,
};
}
const babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME);
if (pathExists.sync(babelConfigJSrc)) {
const config = getBabelJsConfig(babelConfigJSrc);
return {
file: babelConfigJSrc,
config,
};
}
const packageFile = path.join(dir, PACKAGE_FILENAME);
if (pathExists.sync(packageFile)) {
const packageContent = fs.readFileSync(packageFile, 'utf8');
const packageJson = JSON.parse(packageContent);
if (packageJson.babel) {
return {
file: packageFile,
config: packageJson.babel,
};
}
}
if (loopLeft === 0) {
return nullConf;
}
loopLeft -= 1;
} while (dir !== (dir = path.dirname(dir)));
return nullConf;
};