Skip to content

Commit

Permalink
Use require.resolve to resolve package path (#378)
Browse files Browse the repository at this point in the history
* Use `require.resolve` to resolve package path

* resolveNodeModule -> resolveNodeModuleDir
  • Loading branch information
janicduplessis authored and grabbou committed May 8, 2019
1 parent 093ce51 commit c3ed10c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
12 changes: 12 additions & 0 deletions packages/cli/src/tools/config/__mocks__/resolveNodeModuleDir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @flow
*/

const path = require('path');

export default function resolveNodeModuleDir(
root: string,
packageName: string,
): string {
return path.join(root, 'node_modules', packageName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
getTempDirectory,
} from '../../../../../../jest/helpers';

jest.mock('../resolveNodeModuleDir');

beforeEach(() => {
cleanup(DIR);
jest.resetModules();
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/tools/config/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {

import {logger} from '@react-native-community/cli-tools';

jest.mock('../resolveNodeModuleDir');

const DIR = getTempDirectory('resolve_config_path_test');

// Removes string from all key/values within an object
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/tools/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {type ConfigT} from 'types';

import assign from '../assign';
import merge from '../merge';
import resolveNodeModuleDir from './resolveNodeModuleDir';
/**
* Built-in platforms
*/
Expand All @@ -34,10 +35,10 @@ function loadConfig(projectRoot: string = process.cwd()): ConfigT {

const finalConfig = findDependencies(projectRoot).reduce(
(acc: ConfigT, dependencyName) => {
const root = path.join(projectRoot, 'node_modules', dependencyName);

let root;
let config;
try {
root = resolveNodeModuleDir(projectRoot, dependencyName);
config =
readLegacyDependencyConfigFromDisk(root) ||
readDependencyConfigFromDisk(root);
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/src/tools/config/resolveNodeModuleDir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @flow
*/
import path from 'path';

/**
* Finds a path inside `node_modules`
*/
export default function resolveNodeModuleDir(
root: string,
packageName: string,
): string {
return path.dirname(
// $FlowIssue: Wrong `require.resolve` type definition
require.resolve(path.join(packageName, 'package.json'), {
paths: [root],
}),
);
}
10 changes: 3 additions & 7 deletions packages/cli/src/tools/config/resolveReactNativePath.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
/**
* @flow
*/
import path from 'path';
import {CLIError} from '@react-native-community/cli-tools';

import resolveNodeModuleDir from './resolveNodeModuleDir';

/**
* Finds path to React Native inside `node_modules` or throws
* an error otherwise.
*/
export default function resolveReactNativePath(root: string) {
try {
return path.dirname(
// $FlowIssue: Wrong `require.resolve` type definition
require.resolve('react-native/package.json', {
paths: [root],
}),
);
return resolveNodeModuleDir(root, 'react-native');
} catch (_ignored) {
throw new CLIError(`
Unable to find React Native files. Make sure "react-native" module is installed
Expand Down

0 comments on commit c3ed10c

Please sign in to comment.