Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/community-cli-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"@react-native/asset-utils": "0.87.0-main",
"@react-native/dev-middleware": "0.87.0-main",
"commander": "^12.0.0",
"debug": "^4.4.0",
"invariant": "^2.2.4",
"metro": "^0.87.0",
Expand Down
51 changes: 49 additions & 2 deletions packages/community-cli-plugin/src/commands/bundle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
* @format
*/

import type {Command} from '@react-native-community/cli-types';
import type {Command as CommunityCommand} from '@react-native-community/cli-types';

import buildBundle from './buildBundle';
import {Command} from 'commander';
import path from 'node:path';

export type {BundleCommandArgs} from './buildBundle';

const bundleCommand: Command = {
type CommandOption = Readonly<NonNullable<CommunityCommand['options']>[number]>;

type BundleCommandParser = {
parser: Command,
baseHelpInformation: string,
};

const bundleCommand: CommunityCommand = {
name: 'bundle',
description: 'Build the bundle for the provided JavaScript entry file.',
func: buildBundle,
Expand Down Expand Up @@ -123,4 +131,43 @@ const bundleCommand: Command = {
],
};

function addOptions(
command: Command,
options: ReadonlyArray<CommandOption>,
): void {
for (const option of options) {
const description = option.description ?? '';
const defaultValue =
typeof option.default === 'function' ? undefined : option.default;

if (option.parse != null) {
command.option(option.name, description, option.parse, defaultValue);
} else if (
typeof defaultValue === 'string' ||
typeof defaultValue === 'boolean' ||
Array.isArray(defaultValue)
) {
command.option(option.name, description, defaultValue);
} else {
command.option(option.name, description);
}
}
}

export function unstable_createBundleCommandParser(
additionalOptions: ReadonlyArray<CommandOption> = [],
): BundleCommandParser {
const parser = new Command()
.name(bundleCommand.name)
.description(bundleCommand.description ?? '')
.helpOption('--help', 'Display help for command')
.allowUnknownOption();

addOptions(parser, bundleCommand.options ?? []);
const baseHelpInformation = parser.helpInformation();
addOptions(parser, additionalOptions);

return {parser, baseHelpInformation};
}

export default bundleCommand;
5 changes: 4 additions & 1 deletion packages/community-cli-plugin/src/index.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
* @format
*/

export {default as bundleCommand} from './commands/bundle';
export {
default as bundleCommand,
unstable_createBundleCommandParser,
} from './commands/bundle';
export {default as startCommand} from './commands/start';

export {unstable_buildBundleWithConfig} from './commands/bundle/buildBundle';
1 change: 0 additions & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
"ansi-regex": "^5.0.0",
"babel-plugin-syntax-hermes-parser": "0.37.0",
"base64-js": "^1.5.1",
"commander": "^12.0.0",
"flow-enums-runtime": "^0.0.6",
"hermes-compiler": "0.0.0",
"invariant": "^2.2.4",
Expand Down
120 changes: 63 additions & 57 deletions packages/react-native/scripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,79 @@

'use strict';

const {bundleCommand: bc} = require('@react-native/community-cli-plugin');
const commander = require('commander');
const {
bundleCommand: bundleCommandBase,
unstable_createBundleCommandParser,
} = require('@react-native/community-cli-plugin');
const {execSync} = require('node:child_process');
const {readFileSync} = require('node:fs');
const path = require('node:path');

// Commander 12.0.0 changes from the global to named export
// $FlowFixMe[signature-verification-failure]
const program = commander.program ?? commander;
/*::
type BundleOptions = {
configCmd?: string,
loadConfig?: string,
...
};
*/

program.version(
JSON.parse(
readFileSync(path.resolve(__dirname, '..', 'package.json'), 'utf8'),
).version,
);
const baseOptions = [
{
name: '--config-cmd <string>',
description: 'Command to generate a JSON project config',
default: 'npx react-native config',
},
{
name: '--load-config <string>',
description: 'JSON project config',
},
];

program
.name(bc.name)
.description(bc.description ?? '')
.option(
'--config-cmd <string>',
'Command to generate a JSON project config',
'npx react-native config',
)
.option('--load-config <string>', 'JSON project config')
.option('--verbose', 'Additional logs', () => true, false)
.allowUnknownOption()
.action(async function handleAction() {
let config = null;
let options = program
.opts /*::<{
configCmd?: string,
loadConfig?: string,
verbose: boolean,
...
}>*/
();
if (options.loadConfig != null) {
config = JSON.parse(
options.loadConfig.replace(/^\W*'/, '').replace(/'\W*$/, ''),
);
} else if (options.configCmd != null) {
config = JSON.parse(
execSync(options.configCmd.trim(), {encoding: 'utf8'}),
);
}
const {
parser: bundleCommandParser,
baseHelpInformation: bundleCommandBaseHelp,
} = unstable_createBundleCommandParser(baseOptions);

if (config == null) {
throw new Error('No config provided');
}
function formatOptions(
options /*: ReadonlyArray<Readonly<{description?: string, name: string, ...}>> */,
) {
return options
.map(option => ` ${option.name.padEnd(35)} ${option.description ?? ''}`)
.join('\n');
}

function printHelp() {
console.log(`${bundleCommandBaseHelp}
Additional options:
${formatOptions(baseOptions)}
`);
}

async function main(argv /*: ReadonlyArray<string> */ = process.argv.slice(2)) {
if (argv.includes('--help')) {
printHelp();
return;
}

await bc.func(program.args, config, options);
});
bundleCommandParser.parse(argv, {from: 'user'});
const options = bundleCommandParser
.opts /*::<BundleOptions>*/
();

if (bc.options != null) {
for (const o of bc.options) {
program.option(
o.name,
o.description ?? '',
o.parse ?? (value => value),
o.default,
let config = null;
if (options.loadConfig != null) {
config = JSON.parse(
options.loadConfig.replace(/^\W*'/, '').replace(/'\W*$/, ''),
);
} else if (options.configCmd != null) {
config = JSON.parse(execSync(options.configCmd.trim(), {encoding: 'utf8'}));
}

if (config == null) {
throw new Error('No config provided');
}

await bundleCommandBase.func(bundleCommandParser.args, config, options);
}

if (require.main === module) {
program.parse(process.argv);
void main();
}

module.exports = program;
Loading