Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow overriding commands from react-native.config.js #2229

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// 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`] = `
Object {
"func": [Function],
"name": "foo-command",
"options": Array [
Object {
"description": "Custom option",
"name": "--option",
},
],
}
`;

exports[`should apply build types from dependency config 1`] = `
Object {
"name": "react-native-test",
Expand Down Expand Up @@ -35,11 +48,17 @@ 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",
"options": Array [
Object {
"description": "Custom option",
"name": "--option",
},
],
},
]
`;
Expand Down
53 changes: 53 additions & 0 deletions packages/cli-config/src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]).toMatchSnapshot();
});

test('should merge project configuration with default values', () => {
DIR = getTempDirectory('config_test_merge');
writeFiles(DIR, {
Expand Down
16 changes: 15 additions & 1 deletion packages/cli-config/src/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DependencyConfig,
UserConfig,
Config,
Command,
} from '@react-native-community/cli-types';
import {
findProjectRoot,
Expand Down Expand Up @@ -72,6 +73,16 @@ function getReactNativeVersion(reactNativePath: string) {
return 'unknown';
}

const removeDuplicateCommands = <T extends boolean>(commands: Command<T>[]) => {
const uniqueCommandsMap = new Map();

commands.forEach((command) => {
uniqueCommandsMap.set(command.name, command);
});

return Array.from(uniqueCommandsMap.values());
};

/**
* Loads CLI configuration
*/
Expand Down Expand Up @@ -143,7 +154,10 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
);
},
}),
commands: [...acc.commands, ...config.commands],
commands: removeDuplicateCommands([
...config.commands,
...acc.commands,
]),
platforms: {
...acc.platforms,
...config.platforms,
Expand Down