-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the dependency on the "rc" module (#3063)
* Removes dependency on the "rc" module * Removers shebang-loader, not used anymore * Fixes flow errors * Fixes tests on Windows
- Loading branch information
Showing
6 changed files
with
76 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* @flow */ | ||
|
||
import {readFileSync} from 'fs'; | ||
import {basename, dirname, join} from 'path'; | ||
|
||
const etc = '/etc'; | ||
const isWin = process.platform === 'win32'; | ||
const home = isWin ? process.env.USERPROFILE : process.env.HOME; | ||
|
||
export function findRc(name: string, parser: Function): Object { | ||
let configPaths = []; | ||
|
||
function addConfigPath(... segments) { | ||
configPaths.push(join(... segments)); | ||
} | ||
|
||
function addRecursiveConfigPath(... segments) { | ||
const queue = []; | ||
|
||
let oldPath; | ||
let path = join(... segments); | ||
|
||
do { | ||
queue.unshift(path); | ||
|
||
oldPath = path; | ||
path = join(dirname(dirname(path)), basename(path)); | ||
} while (path !== oldPath); | ||
|
||
configPaths = configPaths.concat(queue); | ||
} | ||
|
||
function fetchConfigs(): Object { | ||
return Object.assign({}, ... configPaths.map((path) => { | ||
try { | ||
return parser(readFileSync(path).toString(), path); | ||
} catch (error) { | ||
return {}; | ||
} | ||
})); | ||
} | ||
|
||
if (!isWin) { | ||
addConfigPath(etc, name, 'config'); | ||
addConfigPath(etc, `${name}rc`); | ||
} | ||
|
||
if (home) { | ||
addConfigPath(home, '.config', name, 'config'); | ||
addConfigPath(home, '.config', name); | ||
addConfigPath(home, `.${name}`, 'config'); | ||
addConfigPath(home, `.${name}rc`); | ||
} | ||
|
||
addRecursiveConfigPath(process.cwd(), `.${name}rc`); | ||
|
||
const envVariable = `${name}_config`.toUpperCase(); | ||
|
||
if (process.env[envVariable]) { | ||
addConfigPath(process.env[envVariable]); | ||
} | ||
|
||
return fetchConfigs(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters