Skip to content

Commit

Permalink
Give RNPM the ability to look for plugins in @scoped modules (#21082)
Browse files Browse the repository at this point in the history
Summary:
This PR gives RNPM the ability to look for plugins in `scoped` modules.

The regexes for finding RNPM plugins will match these hypothetical examples:

 * `rnpm-plugin-foo`
 * `org/rnpm-plugin-foo`

The regexes for finding React Native plugins will match these hypothetical examples:

 * `react-native-foo`
 * `org/react-native-foo`
 * `The controller you requested could not be found./module` (will be useful in the slimmening)
 * `The controller you requested could not be found./module`

RNPM plugins will be able to benefit from this immediately, but React Native plugins will run into this Metro issue currently:

facebook/metro#241
Pull Request resolved: #21082

Differential Revision: D9809094

Pulled By: hramos

fbshipit-source-id: 4b0694ad4119b37dd5664af52c48e48ebe4d7404
  • Loading branch information
empyrical authored and facebook-github-bot committed Sep 13, 2018
1 parent 7f1fcb6 commit 4b106be
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
15 changes: 15 additions & 0 deletions local-cli/core/__tests__/findPlugins.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,19 @@ describe('findPlugins', () => {
}));
expect(findPlugins([ROOT]).commands).toHaveLength(1);
});

it('returns plugins in scoped modules', () => {
jest.mock(pjsonPath, () => ({
dependencies: {
'@org/rnpm-plugin-test': '*',
'@org/react-native-test': '*',
'@react-native/test': '*',
'@react-native-org/test': '*',
},
}));

expect(findPlugins([ROOT])).toHaveProperty('commands');
expect(findPlugins([ROOT])).toHaveProperty('platforms');
expect(findPlugins([ROOT]).commands[0]).toBe('@org/rnpm-plugin-test');
});
});
13 changes: 11 additions & 2 deletions local-cli/core/findPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ const union = require('lodash').union;
const uniq = require('lodash').uniq;
const flatten = require('lodash').flatten;

const RNPM_PLUGIN_PATTERNS = [/^rnpm-plugin-/, /^@(.*)\/rnpm-plugin-/];

const REACT_NATIVE_PLUGIN_PATTERNS = [
/^react-native-/,
/^@(.*)\/react-native-/,
/^@react-native(.*)\/(?!rnpm-plugin-)/,
];

/**
* Filter dependencies by name pattern
* @param {String} dependency Name of the dependency
* @return {Boolean} If dependency is a rnpm plugin
*/
const isRNPMPlugin = dependency => dependency.indexOf('rnpm-plugin-') === 0;
const isRNPMPlugin = dependency =>
RNPM_PLUGIN_PATTERNS.some(pattern => pattern.test(dependency));
const isReactNativePlugin = dependency =>
dependency.indexOf('react-native-') === 0;
REACT_NATIVE_PLUGIN_PATTERNS.some(pattern => pattern.test(dependency));

const readPackage = folder => {
try {
Expand Down
8 changes: 7 additions & 1 deletion local-cli/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ async function getCliConfig(): Promise<RNConfig> {
*/
function getProjectCommands(): Array<CommandT> {
const commands = plugins.commands.map(pathToCommands => {
const name = pathToCommands.split(path.sep)[0];
const name =
pathToCommands[0] === '@'
? pathToCommands
.split(path.sep)
.slice(0, 2)
.join(path.sep)
: pathToCommands.split(path.sep)[0];

return attachPackage(
require(path.join(appRoot, 'node_modules', pathToCommands)),
Expand Down

0 comments on commit 4b106be

Please sign in to comment.