Renames fetchMetroConfig into loadMetroConfig

Reviewed By: davidaurelio

Differential Revision: D6834104

fbshipit-source-id: fc8e9300dd43b2212c067595063fc2f98dbc8e3b
This commit is contained in:
Maël Nison 2018-02-02 04:35:18 -08:00 committed by Facebook Github Bot
parent c020e40894
commit 4c4eaeac8a
4 changed files with 60 additions and 72 deletions

View File

@ -12,19 +12,7 @@
'use strict';
const Config = require('./Config');
const fs = require('fs-extra');
const path = require('path');
import type {ConfigT} from './Config';
const METRO_CONFIG_FILENAME = 'metro.config.js';
type MetroConfigSearchOptions = {|
cwd?: string,
basename?: string,
|};
exports.watchFile = async function(
filename: string,
@ -37,49 +25,6 @@ exports.watchFile = async function(
await callback();
};
exports.findMetroConfig = async function(
filename: ?string,
{
cwd = process.cwd(),
basename = METRO_CONFIG_FILENAME,
}: MetroConfigSearchOptions = {},
): Promise<?string> {
if (filename) {
return path.resolve(cwd, filename);
} else {
let previous;
let current = cwd;
do {
const filename = path.join(current, basename);
if (fs.existsSync(filename)) {
return filename;
}
previous = current;
current = path.dirname(current);
} while (previous !== current);
return null;
}
};
exports.fetchMetroConfig = async function(
filename: ?string,
// $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
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
exports.makeAsyncCommand = (command: (argv: any) => Promise<*>) => (
// eslint-disable-next-line no-unclear-flowtypes
argv: any,

View File

@ -16,7 +16,7 @@ const MetroApi = require('..');
const os = require('os');
const {fetchMetroConfig, makeAsyncCommand} = require('../cli-utils');
const {makeAsyncCommand} = require('../cli-utils');
import typeof Yargs from 'yargs';
@ -56,7 +56,7 @@ exports.builder = (yargs: Yargs) => {
// eslint-disable-next-line no-unclear-flowtypes
exports.handler = makeAsyncCommand(async (argv: any) => {
// $FlowFixMe: Flow + Promises don't work consistently https://fb.facebook.com/groups/flow/permalink/1772334656148475/
const config = await fetchMetroConfig(argv.config);
const config = await MetroApi.loadMetroConfig(argv.config);
if (argv.projectRoots) {
config.getProjectRoots = () => argv.projectRoots;

View File

@ -16,12 +16,7 @@ const MetroApi = require('..');
const os = require('os');
const {
findMetroConfig,
fetchMetroConfig,
watchFile,
makeAsyncCommand,
} = require('../cli-utils');
const {watchFile, makeAsyncCommand} = require('../cli-utils');
const {promisify} = require('util');
import typeof Yargs from 'yargs';
@ -74,7 +69,7 @@ exports.handler = makeAsyncCommand(async (argv: any) => {
}
// $FlowFixMe: Flow + Promises don't work consistently https://fb.facebook.com/groups/flow/permalink/1772334656148475/
const config = await fetchMetroConfig(argv.config);
const config = await MetroApi.loadMetroConfig(argv.config);
if (argv.projectRoots) {
config.getProjectRoots = () => argv.projectRoots;
@ -88,7 +83,7 @@ exports.handler = makeAsyncCommand(async (argv: any) => {
restarting = false;
}
const metroConfigLocation = await findMetroConfig(argv.config);
const metroConfigLocation = await MetroApi.findMetroConfig(argv.config);
if (metroConfigLocation) {
await watchFile(metroConfigLocation, restart);

View File

@ -13,8 +13,6 @@
'use strict';
const Config = require('./Config');
const Http = require('http');
const Https = require('https');
const MetroBundler = require('./shared/output/bundle');
const MetroHmrServer = require('./HmrServer');
const MetroServer = require('./Server');
@ -23,6 +21,10 @@ const TransformCaching = require('./lib/TransformCaching');
const attachWebsocketServer = require('./lib/attachWebsocketServer');
const defaults = require('./defaults');
const fs = require('fs');
const http = require('http');
const https = require('https');
const path = require('path');
const {realpath} = require('fs');
const {readFile} = require('fs-extra');
@ -32,13 +34,11 @@ import type {ConfigT} from './Config';
import type {Reporter} from './lib/reporting';
import type {RequestOptions, OutputOptions} from './shared/types.flow.js';
import type {Options as ServerOptions} from './shared/types.flow';
import type {Server as HttpServer, IncomingMessage, ServerResponse} from 'http';
import type {Server as HttpServer} from 'http';
import type {Server as HttpsServer} from 'https';
export type {ConfigT} from './Config';
type Middleware = (IncomingMessage, ServerResponse, ?() => mixed) => mixed;
type PublicMetroOptions = {|
config?: ConfigT,
maxWorkers?: number,
@ -53,6 +53,13 @@ type PrivateMetroOptions = {|
watch?: boolean,
|};
type MetroConfigSearchOptions = {|
cwd?: string,
basename?: string,
|};
const METRO_CONFIG_FILENAME = 'metro.config.js';
// We'll be able to remove this to use the one provided by modern versions of
// fs-extra once https://github.com/jprichardson/node-fs-extra/pull/520 will
// have been merged (until then, they'll break on devservers/Sandcastle)
@ -223,7 +230,7 @@ exports.runServer = async (options: RunServerOptions) => {
let httpServer;
if (options.secure) {
httpServer = Https.createServer(
httpServer = https.createServer(
{
key: await readFile(options.secureKey),
cert: await readFile(options.secureCert),
@ -231,7 +238,7 @@ exports.runServer = async (options: RunServerOptions) => {
serverApp,
);
} else {
httpServer = Http.createServer(serverApp);
httpServer = http.createServer(serverApp);
}
if (options.hmrEnabled) {
@ -303,6 +310,47 @@ exports.runBuild = async (options: RunBuildOptions) => {
return {metroServer, metroBundle};
};
exports.findMetroConfig = function(
filename: ?string,
{
cwd = process.cwd(),
basename = METRO_CONFIG_FILENAME,
}: MetroConfigSearchOptions = {},
): ?string {
if (filename) {
return path.resolve(cwd, filename);
} else {
let previous;
let current = cwd;
do {
const filename = path.join(current, basename);
if (fs.existsSync(filename)) {
return filename;
}
previous = current;
current = path.dirname(current);
} while (previous !== current);
return null;
}
};
exports.loadMetroConfig = function(
filename: ?string,
// $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 = {},
): ConfigT {
const location = exports.findMetroConfig(filename, searchOptions);
// $FlowFixMe: We want this require to be dynamic
const config = location ? require(location) : null;
return config ? Config.normalize(config) : Config.DEFAULT;
};
exports.Config = Config;
exports.defaults = defaults;