Fixes for the Metro cli

Reviewed By: BYK

Differential Revision: D6348698

fbshipit-source-id: 3a00d9af7e03c5d636e2a51ce95842ca7d45b6e1
This commit is contained in:
Maël Nison 2017-11-20 10:11:45 -08:00 committed by Facebook Github Bot
parent eb1ddfdcd9
commit c2c99c4c1b
6 changed files with 53 additions and 17 deletions

View File

@ -30,6 +30,7 @@
"debug": "^2.2.0",
"denodeify": "^1.2.1",
"fbjs": "^0.8.14",
"fs-extra": "^1.0.0",
"graceful-fs": "^4.1.3",
"image-size": "^0.6.0",
"jest-docblock": "21.3.0-beta.8",

View File

@ -19,9 +19,9 @@ import type {ConfigT} from './Config';
const METRO_CONFIG_FILENAME = 'metro.config.js';
exports.findMetroConfig = async (
exports.findMetroConfig = async function(
filename: ?string,
): Promise<$Shape<ConfigT>> => {
): Promise<$Shape<ConfigT>> {
if (filename) {
// $FlowFixMe: We want this require to be dynamic
return require(path.resolve(process.cwd(), filename));
@ -44,3 +44,14 @@ exports.findMetroConfig = async (
return {};
}
};
// eslint-disable-next-line no-unclear-flowtypes
exports.makeAsyncCommand = (command: (argv: any) => Promise<*>) => (
// eslint-disable-next-line no-unclear-flowtypes
argv: any,
) => {
Promise.resolve(command(argv)).catch(error => {
console.error(error.stack);
process.exitCode = 1;
});
};

View File

@ -13,10 +13,11 @@
'use strict';
const MetroApi = require('..');
const findMetroConfig = require('../cli-utils').findMetroConfig;
const os = require('os');
const {findMetroConfig, makeAsyncCommand} = require('../cli-utils');
import typeof Yargs from 'yargs';
exports.command = 'build <entry>';
@ -49,8 +50,8 @@ exports.builder = (yargs: Yargs) => {
yargs.option('config', {alias: 'c', type: 'string'});
};
exports.handler = async (argv: any) => {
// eslint-disable-next-line no-unclear-flowtypes
exports.handler = makeAsyncCommand(async (argv: any) => {
argv.config = await findMetroConfig(argv.config);
await MetroApi.runBuild(argv);
};
});

View File

@ -13,10 +13,11 @@
'use strict';
const MetroApi = require('..');
const findMetroConfig = require('../cli-utils').findMetroConfig;
const os = require('os');
const {findMetroConfig, makeAsyncCommand} = require('../cli-utils');
import typeof Yargs from 'yargs';
exports.command = 'serve';
@ -46,7 +47,8 @@ exports.builder = (yargs: Yargs) => {
yargs.option('config', {alias: 'c', type: 'string'});
};
exports.handler = async (argv: any) => {
// eslint-disable-next-line no-unclear-flowtypes
exports.handler = makeAsyncCommand(async (argv: any) => {
argv.config = await findMetroConfig(argv.config);
await MetroApi.runServer({
@ -58,4 +60,4 @@ exports.handler = async (argv: any) => {
);
},
});
};
});

View File

@ -22,8 +22,8 @@ const TransformCaching = require('./lib/TransformCaching');
const connect = require('connect');
const {realpath} = require('fs');
const {readFile} = require('fs-extra');
const {resolve} = require('path');
const defaultAssetExts = require('./defaults').assetExts;
const defaultSourceExts = require('./defaults').sourceExts;
@ -56,7 +56,22 @@ type PrivateMetroOptions = {|
watch?: boolean,
|};
function runMetro({
// 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)
async function asyncRealpath(path): Promise<string> {
return new Promise((resolve, reject) => {
realpath(path, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
async function runMetro({
config,
maxWorkers = 1,
projectRoots,
@ -105,9 +120,12 @@ function runMetro({
watch,
workerPath:
normalizedConfig.getWorkerPath && normalizedConfig.getWorkerPath(),
projectRoots: normalizedConfig
projectRoots: await Promise.all(
normalizedConfig
.getProjectRoots()
.concat([...projectRoots.map(path => resolve(path))]),
.concat(projectRoots)
.map(path => asyncRealpath(path)),
),
};
return new MetroServer(serverOptions);
@ -117,8 +135,10 @@ type CreateConnectMiddlewareOptions = {|
...PublicMetroOptions,
|};
exports.createConnectMiddleware = (options: CreateConnectMiddlewareOptions) => {
const metroServer = runMetro({
exports.createConnectMiddleware = async function(
options: CreateConnectMiddlewareOptions,
) {
const metroServer = await runMetro({
config: options.config,
maxWorkers: options.maxWorkers,
projectRoots: options.projectRoots,

View File

@ -31,7 +31,8 @@ module.exports = function(
const buffer: Buffer = asBuffer(code, encoding);
const hash = crypto.createHash('sha1');
hash.update(buffer);
const digest = hash.digest();
// $FlowFixMe: Flow doesn't know that 'buffer' is a valid value
const digest = hash.digest('buffer');
const signature = Buffer.alloc
? Buffer.alloc(digest.length + 1)
: new Buffer(digest.length + 1);