forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-config.js
43 lines (33 loc) · 1 KB
/
find-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
// @ts-check
/**
* Find a config file path, starting in the current directory and looking up to the Git root directory
* (which contain .git) or the drive root.
* @param {string} configName - Config file name. If an absolute path, will be returned unmodified.
* @param {string} [cwd] optional different cwd
* @returns The config file's path, or undefined if not found
*/
function findConfig(configName, cwd) {
if (!configName) {
return undefined;
}
const fs = require('fs');
const path = require('path');
if (path.isAbsolute(configName)) {
return configName;
}
const rootPath = path.resolve('/');
cwd = cwd || process.cwd();
let foundGitRoot = false;
while (cwd !== rootPath && !foundGitRoot) {
const configPath = path.join(cwd, configName);
if (fs.existsSync(configPath)) {
return configPath;
}
if (fs.existsSync(path.join(cwd, '.git'))) {
foundGitRoot = true;
}
cwd = path.dirname(cwd);
}
return undefined;
}
module.exports = findConfig;