/
findOptionsInFiles.js
41 lines (36 loc) · 1.38 KB
/
findOptionsInFiles.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
// implements the search algorithm for finding react-server config files. see
// the README.md for a description.
import path from "path"
import fs from "fs"
const REACT_SERVER_RC = ".reactserverrc";
const PACKAGE_JSON = "package.json";
// returns an options object that represents the **first** config file found in the
// search path. if none are found, returns null.
// as described in the README, this method starts at dir, and for each directory
// looks first for a .reactserverrc file, then for a "reactServer" section of
// the package.json. if it finds neither, it goes up a directory and looks again.
// it returns the contents of the first config file found; it never merges multiple
// configurations.
export default (dir = process.cwd()) => {
do {
let reactServerRc = null;
try {
// readFileSync throws if the file doesn't exist.
reactServerRc = fs.readFileSync(path.join(dir, REACT_SERVER_RC));
} catch (e) {} //eslint-disable-line no-empty
if (reactServerRc) {
return JSON.parse(reactServerRc);
}
let packageJson = null;
try {
packageJson = fs.readFileSync(path.join(dir, PACKAGE_JSON));
} catch (e) {} //eslint-disable-line no-empty
if (packageJson) {
const parsedPackageJson = JSON.parse(packageJson);
if (parsedPackageJson.reactServer) {
return parsedPackageJson.reactServer;
}
}
} while (dir !== (dir = path.dirname(dir)))
return null;
}