Skip to content

Commit 19c4f7e

Browse files
huntiefacebook-github-bot
authored andcommittedJul 19, 2022
Enable yargs strict, add 'start' alias for serve command
Summary: Fixes command validation in Metro CLI. - Enables `yargs.strict()`, which prevents Metro from exiting silently if called with a nonexistent subcommand or option. - Adds `start` as an alias for `serve` command — matching RN CLI. - Simplifies `yargs.command` calls in index module by aligning `desc` key name to match yargs `ModuleObject` type. Changelog: **[Breaking]** Enable strict CLI validation when passed an invalid subcommand or argument **[Feature]** Add `start` alias for `serve` CLI command Reviewed By: motiz88 Differential Revision: D37922367 fbshipit-source-id: 54ba4f714d82ad3d825236d2982c6f4be02b07e5
1 parent c389f0b commit 19c4f7e

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed
 

‎packages/metro/src/cli-utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ exports.watchFile = async function (
2727

2828
exports.makeAsyncCommand =
2929
(
30-
command: (argv: YargArguments) => Promise<mixed>,
30+
command: (argv: YargArguments) => Promise<void>,
3131
): ((argv: YargArguments) => void) =>
3232
(argv: YargArguments) => {
3333
Promise.resolve(command(argv)).catch(error => {

‎packages/metro/src/commands/build.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import type {RunBuildOptions} from '../index';
1414
import type {YargArguments} from 'metro-config/src/configTypes.flow';
1515
import typeof Yargs from 'yargs';
16+
import type {ModuleObject} from 'yargs';
1617

1718
const {makeAsyncCommand} = require('../cli-utils');
1819
const TerminalReporter = require('../lib/TerminalReporter');
@@ -22,16 +23,12 @@ const {Terminal} = require('metro-core');
2223
const term = new Terminal(process.stdout);
2324
const updateReporter = new TerminalReporter(term);
2425

25-
module.exports = (): ({
26-
builder: (yargs: Yargs) => void,
27-
command: string,
28-
description: string,
29-
handler: (argv: YargArguments) => void,
30-
}) => ({
26+
module.exports = (): {
27+
...ModuleObject,
28+
handler: Function,
29+
} => ({
3130
command: 'build <entry>',
32-
33-
description:
34-
'Generates a JavaScript bundle containing the specified entrypoint and its descendants',
31+
desc: 'Generates a JavaScript bundle containing the specified entrypoint and its descendants',
3532

3633
builder: (yargs: Yargs): void => {
3734
yargs.option('project-roots', {

‎packages/metro/src/commands/dependencies.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'use strict';
1111

1212
import typeof Yargs from 'yargs';
13+
import type {ModuleObject} from 'yargs';
1314

1415
const {makeAsyncCommand} = require('../cli-utils');
1516
const Server = require('../Server');
@@ -67,9 +68,9 @@ async function dependencies(args: any, config: any) {
6768
: Promise.resolve();
6869
}
6970

70-
module.exports = () => ({
71+
module.exports = (): ModuleObject => ({
7172
command: 'get-dependencies',
72-
description: 'List dependencies',
73+
desc: 'List dependencies',
7374
builder: (yargs: Yargs) => {
7475
yargs.option('entry-file', {
7576
type: 'string',

‎packages/metro/src/commands/serve.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@
1313
import type {RunServerOptions} from '../index';
1414
import type {YargArguments} from 'metro-config/src/configTypes.flow';
1515
import typeof Yargs from 'yargs';
16+
import type {ModuleObject} from 'yargs';
1617

1718
const {makeAsyncCommand, watchFile} = require('../cli-utils');
1819
const {loadConfig, resolveConfig} = require('metro-config');
1920
const {promisify} = require('util');
2021

21-
module.exports = (): ({
22-
builder: (yargs: Yargs) => void,
23-
command: $TEMPORARY$string<'serve'>,
24-
description: string,
25-
handler: (argv: YargArguments) => void,
26-
}) => ({
22+
module.exports = (): {
23+
...ModuleObject,
24+
handler: Function,
25+
} => ({
2726
command: 'serve',
28-
29-
description: 'Starts Metro on the given port, building bundles on the fly',
27+
aliases: ['start'],
28+
desc: 'Starts Metro on the given port, building bundles on the fly',
3029

3130
builder: (yargs: Yargs): void => {
3231
yargs.option('project-roots', {

‎packages/metro/src/index.flow.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -453,18 +453,18 @@ exports.attachMetroCli = function (
453453
// $FlowFixMe[prop-missing]
454454
= {},
455455
): Yargs {
456+
yargs.strict();
457+
456458
if (build) {
457-
const {command, description, builder, handler} = makeBuildCommand();
458-
yargs.command(command, description, builder, handler);
459+
yargs.command(makeBuildCommand());
459460
}
460461
if (serve) {
461-
const {command, description, builder, handler} = makeServeCommand();
462-
yargs.command(command, description, builder, handler);
462+
yargs.command(makeServeCommand());
463463
}
464464
if (dependencies) {
465-
const {command, description, builder, handler} = makeDependenciesCommand();
466-
yargs.command(command, description, builder, handler);
465+
yargs.command(makeDependenciesCommand());
467466
}
467+
468468
return yargs;
469469
};
470470

0 commit comments

Comments
 (0)
Please sign in to comment.