From 3ba08041eb500af75a5db9575b4396224cfeeb97 Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Thu, 21 Dec 2023 18:10:51 +0100 Subject: [PATCH 1/4] feat: allow overriding commands --- .../cli-config/src/__tests__/__snapshots__/index-test.ts.snap | 4 ++-- packages/cli-config/src/loadConfig.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap b/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap index 453523b62..e154d602b 100644 --- a/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap +++ b/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap @@ -35,11 +35,11 @@ exports[`should load commands from "react-native-foo" and "react-native-bar" pac Array [ Object { "func": [Function], - "name": "foo-command", + "name": "bar-command", }, Object { "func": [Function], - "name": "bar-command", + "name": "foo-command", }, ] `; diff --git a/packages/cli-config/src/loadConfig.ts b/packages/cli-config/src/loadConfig.ts index ffd3533de..59b1dfc4d 100644 --- a/packages/cli-config/src/loadConfig.ts +++ b/packages/cli-config/src/loadConfig.ts @@ -143,7 +143,7 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config { ); }, }), - commands: [...acc.commands, ...config.commands], + commands: [...config.commands, ...acc.commands], platforms: { ...acc.platforms, ...config.platforms, From 53ad723da82a8e47e0b94797a74d7112c3ce12af Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Mon, 8 Jan 2024 23:37:03 +0100 Subject: [PATCH 2/4] chore: remove duplicates --- .../__snapshots__/index-test.ts.snap | 15 ++++++ .../cli-config/src/__tests__/index-test.ts | 53 +++++++++++++++++++ packages/cli-config/src/loadConfig.ts | 16 +++++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap b/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap index e154d602b..16f97343c 100644 --- a/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap +++ b/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap @@ -1,5 +1,14 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`command specified in root config should overwrite command in "react-native-foo" and "react-native-bar" packages 1`] = ` +Array [ + Object { + "description": "Custom option", + "name": "--option", + }, +] +`; + exports[`should apply build types from dependency config 1`] = ` Object { "name": "react-native-test", @@ -40,6 +49,12 @@ Array [ Object { "func": [Function], "name": "foo-command", + "options": Array [ + Object { + "description": "Custom option", + "name": "--option", + }, + ], }, ] `; diff --git a/packages/cli-config/src/__tests__/index-test.ts b/packages/cli-config/src/__tests__/index-test.ts index 11bc73aaf..d82528a62 100644 --- a/packages/cli-config/src/__tests__/index-test.ts +++ b/packages/cli-config/src/__tests__/index-test.ts @@ -128,6 +128,59 @@ test('should read a config of a dependency and use it to load other settings', ( ).toMatchSnapshot(); }); +test('command specified in root config should overwrite command in "react-native-foo" and "react-native-bar" packages', () => { + DIR = getTempDirectory('config_test_packages'); + writeFiles(DIR, { + 'node_modules/react-native-foo/package.json': '{}', + 'node_modules/react-native-foo/react-native.config.js': `module.exports = { + commands: [ + { + name: 'foo-command', + func: () => console.log('foo') + } + ] + }`, + 'node_modules/react-native-bar/package.json': '{}', + 'node_modules/react-native-bar/react-native.config.js': `module.exports = { + commands: [ + { + name: 'bar-command', + func: () => console.log('bar') + } + ] + }`, + 'package.json': `{ + "dependencies": { + "react-native-foo": "0.0.1", + "react-native-bar": "0.0.1" + } + }`, + 'react-native.config.js': `module.exports = { + reactNativePath: '.', + commands: [ + { + name: 'foo-command', + func: () => { + console.log('overridden foo command'); + }, + options: [ + { + name: '--option', + description: 'Custom option', + }, + ], + }, + ], + };`, + }); + const {commands} = loadConfig(DIR); + const commandsNames = commands.map(({name}) => name); + const commandIndex = commandsNames.indexOf('foo-command'); + + expect(commands[commandIndex].options).not.toBeNull(); + expect(commands[commandIndex].options).toMatchSnapshot(); +}); + test('should merge project configuration with default values', () => { DIR = getTempDirectory('config_test_merge'); writeFiles(DIR, { diff --git a/packages/cli-config/src/loadConfig.ts b/packages/cli-config/src/loadConfig.ts index 59b1dfc4d..0e8d6bf9c 100644 --- a/packages/cli-config/src/loadConfig.ts +++ b/packages/cli-config/src/loadConfig.ts @@ -5,6 +5,7 @@ import { DependencyConfig, UserConfig, Config, + Command, } from '@react-native-community/cli-types'; import { findProjectRoot, @@ -72,6 +73,16 @@ function getReactNativeVersion(reactNativePath: string) { return 'unknown'; } +const removeDuplicateCommands = (commands: Command[]) => { + const uniqueCommandsMap = new Map(); + + commands.forEach((command) => { + uniqueCommandsMap.set(command.name, command); + }); + + return Array.from(uniqueCommandsMap.values()); +}; + /** * Loads CLI configuration */ @@ -143,7 +154,10 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config { ); }, }), - commands: [...config.commands, ...acc.commands], + commands: removeDuplicateCommands([ + ...config.commands, + ...acc.commands, + ]), platforms: { ...acc.platforms, ...config.platforms, From 14e3c2933d9dfee2eb35a03f3fce7afdf649ee6d Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Tue, 9 Jan 2024 18:01:26 +0100 Subject: [PATCH 3/4] chore: update snaphosts --- .../__tests__/__snapshots__/index-test.ts.snap | 16 ++++++++++------ packages/cli-config/src/__tests__/index-test.ts | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap b/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap index 16f97343c..7e0d70fd5 100644 --- a/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap +++ b/packages/cli-config/src/__tests__/__snapshots__/index-test.ts.snap @@ -1,12 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`command specified in root config should overwrite command in "react-native-foo" and "react-native-bar" packages 1`] = ` -Array [ - Object { - "description": "Custom option", - "name": "--option", - }, -] +Object { + "func": [Function], + "name": "foo-command", + "options": Array [ + Object { + "description": "Custom option", + "name": "--option", + }, + ], +} `; exports[`should apply build types from dependency config 1`] = ` diff --git a/packages/cli-config/src/__tests__/index-test.ts b/packages/cli-config/src/__tests__/index-test.ts index d82528a62..612255e7b 100644 --- a/packages/cli-config/src/__tests__/index-test.ts +++ b/packages/cli-config/src/__tests__/index-test.ts @@ -178,7 +178,7 @@ test('command specified in root config should overwrite command in "react-native const commandIndex = commandsNames.indexOf('foo-command'); expect(commands[commandIndex].options).not.toBeNull(); - expect(commands[commandIndex].options).toMatchSnapshot(); + expect(commands[commandIndex]).toMatchSnapshot(); }); test('should merge project configuration with default values', () => { From 7cca2f553777027d553c52d238aa832d35f25722 Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Mon, 15 Jan 2024 10:50:51 +0100 Subject: [PATCH 4/4] docs: add note about overriding config --- docs/plugins.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/plugins.md b/docs/plugins.md index cec16598a..072e2096d 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -27,6 +27,8 @@ At the startup, React Native CLI reads configuration from all dependencies liste At the end, an array of commands concatenated from all plugins is passed on to the CLI to be loaded after built-in commands. +Also, you can provide your own configuration by creating a `react-native.config.js` file in the root of the project, which overrides the configuration from plugins. + > See [`healthChecks`](./healthChecks.md) for information on how plugins can provide additional health checks for `npx react-native doctor`. ## Command interface