Uses findMetroConfig and fetchMetroConfig in the internal RN cli

Reviewed By: davidaurelio

Differential Revision: D6819226

fbshipit-source-id: 6436855cc5dce9ce7922d89df99c3cb90abb095e
This commit is contained in:
Maël Nison 2018-02-02 04:35:11 -08:00 committed by Facebook Github Bot
parent 7a3275a1e8
commit 2312bf2929
1 changed files with 27 additions and 12 deletions

View File

@ -12,6 +12,8 @@
'use strict';
const Config = require('./Config');
const fs = require('fs-extra');
const path = require('path');
@ -19,6 +21,11 @@ import type {ConfigT} from './Config';
const METRO_CONFIG_FILENAME = 'metro.config.js';
type MetroConfigSearchOptions = {|
cwd?: string,
basename?: string,
|};
exports.watchFile = async function(
filename: string,
callback: () => *,
@ -30,15 +37,21 @@ exports.watchFile = async function(
await callback();
};
exports.findMetroConfig = async function(filename: ?string): Promise<?string> {
exports.findMetroConfig = async function(
filename: ?string,
{
cwd = process.cwd(),
basename = METRO_CONFIG_FILENAME,
}: MetroConfigSearchOptions = {},
): Promise<?string> {
if (filename) {
return path.resolve(process.cwd(), filename);
return path.resolve(cwd, filename);
} else {
let previous;
let current = process.cwd();
let current = cwd;
do {
const filename = path.join(current, METRO_CONFIG_FILENAME);
const filename = path.join(current, basename);
if (fs.existsSync(filename)) {
return filename;
@ -54,14 +67,16 @@ exports.findMetroConfig = async function(filename: ?string): Promise<?string> {
exports.fetchMetroConfig = async function(
filename: ?string,
): Promise<$Shape<ConfigT>> {
const location = await exports.findMetroConfig(filename);
if (location) {
// $FlowFixMe: This is a known Flow issue where it doesn't detect that an empty object is a valid value for a strict shape where all the members are optionals
searchOptions: MetroConfigSearchOptions = {},
): Promise<ConfigT> {
const location = await exports.findMetroConfig(filename, searchOptions);
// $FlowFixMe: We want this require to be dynamic
return require(location);
} else {
return {};
}
const config = location ? require(location) : null;
// $FlowFixMe: For some reason, Flow doesn't recognize the return value as a promise
return config ? Config.normalize(config) : Config.DEFAULT;
};
// eslint-disable-next-line no-unclear-flowtypes