Skip to content

Commit 6980dbb

Browse files
Alex Huntfacebook-github-bot
authored andcommitted
Refactor CLI command entry points (facebook#39075)
Summary: Pull Request resolved: facebook#39075 Small refactor: reorganise command entry points and types into a consistent pattern. Changelog: [Internal] Differential Revision: https://internalfb.com/D48433284 fbshipit-source-id: ecf9ee271a3c0f4ee085a9af6ce6c05499434a92
1 parent 09a7b67 commit 6980dbb

File tree

8 files changed

+182
-202
lines changed

8 files changed

+182
-202
lines changed

packages/community-cli-plugin/src/commands/bundle/buildBundle.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,57 @@
1212
import type {Config} from '@react-native-community/cli-types';
1313
import type {RequestOptions} from 'metro/src/shared/types.flow';
1414
import type {ConfigT} from 'metro-config';
15-
import type {CommandLineArgs} from './bundleCommandLineArgs';
1615

1716
import Server from 'metro/src/Server';
18-
const outputBundle = require('metro/src/shared/output/bundle');
17+
import metroBundle from 'metro/src/shared/output/bundle';
18+
import metroRamBundle from 'metro/src/shared/output/RamBundle';
1919
import path from 'path';
2020
import chalk from 'chalk';
2121
import saveAssets from './saveAssets';
22-
import {default as loadMetroConfig} from '../../utils/loadMetroConfig';
22+
import loadMetroConfig from '../../utils/loadMetroConfig';
2323
import {logger} from '@react-native-community/cli-tools';
2424

25+
export type BundleCommandArgs = {
26+
assetsDest?: string,
27+
assetCatalogDest?: string,
28+
entryFile: string,
29+
resetCache: boolean,
30+
resetGlobalCache: boolean,
31+
transformer?: string,
32+
minify?: boolean,
33+
config?: string,
34+
platform: string,
35+
dev: boolean,
36+
bundleOutput: string,
37+
bundleEncoding?: 'utf8' | 'utf16le' | 'ascii',
38+
maxWorkers?: number,
39+
sourcemapOutput?: string,
40+
sourcemapSourcesRoot?: string,
41+
sourcemapUseAbsolutePath: boolean,
42+
verbose: boolean,
43+
unstableTransformProfile: string,
44+
indexedRamBundle?: boolean,
45+
};
46+
2547
async function buildBundle(
26-
args: CommandLineArgs,
48+
_argv: Array<string>,
2749
ctx: Config,
28-
output: typeof outputBundle = outputBundle,
50+
args: BundleCommandArgs,
51+
bundleImpl: typeof metroBundle | typeof metroRamBundle = metroBundle,
2952
): Promise<void> {
3053
const config = await loadMetroConfig(ctx, {
3154
maxWorkers: args.maxWorkers,
3255
resetCache: args.resetCache,
3356
config: args.config,
3457
});
3558

36-
return buildBundleWithConfig(args, config, output);
59+
return buildBundleWithConfig(args, config, bundleImpl);
3760
}
3861

3962
async function buildBundleWithConfig(
40-
args: CommandLineArgs,
63+
args: BundleCommandArgs,
4164
config: ConfigT,
42-
output: typeof outputBundle = outputBundle,
65+
bundleImpl: typeof metroBundle | typeof metroRamBundle = metroBundle,
4366
): Promise<void> {
4467
if (config.resolver.platforms.indexOf(args.platform) === -1) {
4568
logger.error(
@@ -80,11 +103,13 @@ async function buildBundleWithConfig(
80103
const server = new Server(config);
81104

82105
try {
83-
const bundle = await output.build(server, requestOpts);
106+
const bundle = await bundleImpl.build(server, requestOpts);
84107

85108
// $FlowIgnore[class-object-subtyping]
86109
// $FlowIgnore[incompatible-call]
87-
await output.save(bundle, args, logger.info);
110+
// $FlowIgnore[prop-missing]
111+
// $FlowIgnore[incompatible-exact]
112+
await bundleImpl.save(bundle, args, logger.info);
88113

89114
// Save the assets of the bundle
90115
const outputAssets = await server.getAssets({

packages/community-cli-plugin/src/commands/bundle/bundle.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/community-cli-plugin/src/commands/bundle/bundleCommandLineArgs.js

Lines changed: 0 additions & 129 deletions
This file was deleted.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
import type {Command} from '@react-native-community/cli-types';
13+
14+
import path from 'path';
15+
import buildBundle from './buildBundle';
16+
17+
export type {BundleCommandArgs} from './buildBundle';
18+
19+
const bundleCommand: Command = {
20+
name: 'bundle',
21+
description: 'Build the bundle for the provided JavaScript entry file.',
22+
func: buildBundle,
23+
options: [
24+
{
25+
name: '--entry-file <path>',
26+
description:
27+
'Path to the root JS file, either absolute or relative to JS root',
28+
},
29+
{
30+
name: '--platform <string>',
31+
description: 'Either "ios" or "android"',
32+
default: 'ios',
33+
},
34+
{
35+
name: '--transformer <string>',
36+
description: 'Specify a custom transformer to be used',
37+
},
38+
{
39+
name: '--dev [boolean]',
40+
description: 'If false, warnings are disabled and the bundle is minified',
41+
parse: (val: string): boolean => val !== 'false',
42+
default: true,
43+
},
44+
{
45+
name: '--minify [boolean]',
46+
description:
47+
'Allows overriding whether bundle is minified. This defaults to ' +
48+
'false if dev is true, and true if dev is false. Disabling minification ' +
49+
'can be useful for speeding up production builds for testing purposes.',
50+
parse: (val: string): boolean => val !== 'false',
51+
},
52+
{
53+
name: '--bundle-output <string>',
54+
description:
55+
'File name where to store the resulting bundle, ex. /tmp/groups.bundle',
56+
},
57+
{
58+
name: '--bundle-encoding <string>',
59+
description:
60+
'Encoding the bundle should be written in (https://nodejs.org/api/buffer.html#buffer_buffer).',
61+
default: 'utf8',
62+
},
63+
{
64+
name: '--max-workers <number>',
65+
description:
66+
'Specifies the maximum number of workers the worker-pool ' +
67+
'will spawn for transforming files. This defaults to the number of the ' +
68+
'cores available on your machine.',
69+
parse: (workers: string): number => Number(workers),
70+
},
71+
{
72+
name: '--sourcemap-output <string>',
73+
description:
74+
'File name where to store the sourcemap file for resulting bundle, ex. /tmp/groups.map',
75+
},
76+
{
77+
name: '--sourcemap-sources-root <string>',
78+
description:
79+
"Path to make sourcemap's sources entries relative to, ex. /root/dir",
80+
},
81+
{
82+
name: '--sourcemap-use-absolute-path',
83+
description: 'Report SourceMapURL using its full path',
84+
default: false,
85+
},
86+
{
87+
name: '--assets-dest <string>',
88+
description:
89+
'Directory name where to store assets referenced in the bundle',
90+
},
91+
{
92+
name: '--unstable-transform-profile <string>',
93+
description:
94+
'Experimental, transform JS for a specific JS engine. Currently supported: hermes, hermes-canary, default',
95+
default: 'default',
96+
},
97+
{
98+
name: '--asset-catalog-dest [string]',
99+
description: 'Path where to create an iOS Asset Catalog for images',
100+
},
101+
{
102+
name: '--reset-cache',
103+
description: 'Removes cached files',
104+
default: false,
105+
},
106+
{
107+
name: '--read-global-cache',
108+
description:
109+
'Try to fetch transformed JS code from the global cache, if configured.',
110+
default: false,
111+
},
112+
{
113+
name: '--config <string>',
114+
description: 'Path to the CLI configuration file',
115+
parse: (val: string): string => path.resolve(val),
116+
},
117+
],
118+
};
119+
120+
export default bundleCommand;

0 commit comments

Comments
 (0)