packager: server.js: @flow

Summary: I'd like to start typing the front-end so that it's easier to track adding new options, etc.

Reviewed By: davidaurelio

Differential Revision: D5069868

fbshipit-source-id: 9a18dca52efd486ca18f17d0ec434a5ec1c1649c
This commit is contained in:
Jean Lauliac 2017-05-17 04:56:02 -07:00 committed by Facebook Github Bot
parent b98c33f1b4
commit c0e8d67e01
5 changed files with 37 additions and 21 deletions

View File

@ -23,7 +23,7 @@ export type CommandT = {
command: string,
description?: string,
parse?: (val: string) => any,
default?: (config: RNConfig) => any | any,
default?: ((config: RNConfig) => mixed) | mixed,
}>,
examples?: Array<{
desc: string,

View File

@ -37,8 +37,9 @@ const unless = require('./middleware/unless');
const webSocketProxy = require('./util/webSocketProxy.js');
import type {ConfigT} from '../util/Config';
import type {Reporter} from '../../packager/src/lib/reporting';
type Args = {|
export type Args = {|
+assetExts: $ReadOnlyArray<string>,
+host: string,
+nonPersistent: boolean,
@ -53,8 +54,10 @@ type Args = {|
function runServer(
args: Args,
config: ConfigT,
startedCallback: () => mixed,
readyCallback: () => mixed,
// FIXME: this is weird design. The top-level should pass down a custom
// reporter rather than passing it up as argument to an event.
startedCallback: (reporter: Reporter) => mixed,
readyCallback: (reporter: Reporter) => mixed,
) {
var wsProxy = null;
var ms = null;

View File

@ -5,17 +5,25 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
const path = require('path');
const runServer = require('./runServer');
import type {RNConfig} from '../core';
import type {ConfigT} from '../util/Config';
import type {Args as RunServerArgs} from './runServer';
/**
* Starts the React Native Packager Server.
*/
function server(argv, config, args) {
args.projectRoots = args.projectRoots.concat(args.root);
function server(argv: mixed, config: RNConfig, allArgs: Object) {
const {root, ...args} = allArgs;
args.projectRoots = args.projectRoots.concat(root);
const startedCallback = logReporter => {
logReporter.update({
@ -41,7 +49,12 @@ function server(argv, config, args) {
});
};
runServer(args, config, startedCallback, readyCallback);
/* $FlowFixMe: we would have either to invariant() everything, or to
* auto-generate CLI args Flow types. */
const runServerArgs: RunServerArgs = args;
/* $FlowFixMe: ConfigT shouldn't be extendable. */
const configT: ConfigT = config;
runServer(runServerArgs, configT, startedCallback, readyCallback);
}
module.exports = {
@ -51,40 +64,40 @@ module.exports = {
options: [{
command: '--port [number]',
default: 8081,
parse: (val) => Number(val),
parse: (val: string) => Number(val),
}, {
command: '--host [string]',
default: '',
}, {
command: '--root [list]',
description: 'add another root(s) to be used by the packager in this project',
parse: (val) => val.split(',').map(root => path.resolve(root)),
parse: (val: string) => val.split(',').map(root => path.resolve(root)),
default: [],
}, {
command: '--projectRoots [list]',
description: 'override the root(s) to be used by the packager',
parse: (val) => val.split(','),
default: (config) => config.getProjectRoots(),
parse: (val: string) => val.split(','),
default: (config: ConfigT) => config.getProjectRoots(),
}, {
command: '--assetExts [list]',
description: 'Specify any additional asset extensions to be used by the packager',
parse: (val) => val.split(','),
default: (config) => config.getAssetExts(),
parse: (val: string) => val.split(','),
default: (config: ConfigT) => config.getAssetExts(),
}, {
command: '--sourceExts [list]',
description: 'Specify any additional source extensions to be used by the packager',
parse: (val) => val.split(','),
default: (config) => config.getSourceExts(),
parse: (val: string) => val.split(','),
default: (config: ConfigT) => config.getSourceExts(),
}, {
command: '--platforms [list]',
description: 'Specify any additional platforms to be used by the packager',
parse: (val) => val.split(','),
default: (config) => config.getPlatforms(),
parse: (val: string) => val.split(','),
default: (config: ConfigT) => config.getPlatforms(),
}, {
command: '--providesModuleNodeModules [list]',
description: 'Specify any npm packages that import dependencies with providesModule',
parse: (val) => val.split(','),
default: (config) => {
parse: (val: string) => val.split(','),
default: (config: RNConfig) => {
if (typeof config.getProvidesModuleNodeModules === 'function') {
return config.getProvidesModuleNodeModules();
}

View File

@ -135,7 +135,7 @@ class TerminalReporter {
}
}
_logPackagerInitializing(port: number, projectRoots: Array<string>) {
_logPackagerInitializing(port: number, projectRoots: $ReadOnlyArray<string>) {
terminal.log(
formatBanner(
'Running packager on port ' +

View File

@ -24,7 +24,7 @@ export type GlobalCacheDisabledReason = 'too_many_errors' | 'too_many_misses';
*/
export type ReportableEvent = {
port: number,
projectRoots: Array<string>,
projectRoots: $ReadOnlyArray<string>,
type: 'initialize_packager_started',
} | {
type: 'initialize_packager_done',