Fix symlink resolving for examples in the repo

Summary:
Motivation:
Few days ago gaearon [filed an issue](https://github.com/facebook/react-native/issues/12406) that examples in the react-native repo doesn't work after the [recent changes in local-cli](bce6ece5f6). This PR fixes reported bug.

**Test plan (required)**
- No UI changes
- [x] Run UIExplorer from XCode (no package.json in the folder) and check if packager/application runs correctly

cc davidaurelio satya164 grabbou
Closes https://github.com/facebook/react-native/pull/12435

Differential Revision: D4657370

Pulled By: ericvicenti

fbshipit-source-id: 72ee4b96cae37c7ed2794ed4490ce7b4fbbd66c3
This commit is contained in:
Alexey Kureev 2017-03-05 17:17:23 -08:00 committed by Facebook Github Bot
parent 5353d39172
commit c97c1e5516

View File

@ -12,21 +12,29 @@
const path = require('path'); const path = require('path');
const flatten = require('lodash').flatten; const flatten = require('lodash').flatten;
const blacklist = require('../../packager/blacklist'); const blacklist = require('../../packager/blacklist');
const android = require('./android'); const android = require('./android');
const findAssets = require('./findAssets'); const findAssets = require('./findAssets');
const ios = require('./ios'); const ios = require('./ios');
const windows = require('./windows'); const windows = require('./windows');
const wrapCommands = require('./wrapCommands'); const wrapCommands = require('./wrapCommands');
const findPlugins = require('./findPlugins'); const findPlugins = require('./findPlugins');
const findSymlinksPaths = require('../util/findSymlinksPaths'); const findSymlinksPaths = require('../util/findSymlinksPaths');
const NODE_MODULES = path.resolve(__dirname, '..', '..', '..');
import type {ConfigT} from './index'; import type {ConfigT} from './index';
function getProjectPath() {
if (__dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli[\/\\]core$/)) {
// Packager is running from node_modules.
// This is the default case for all projects created using 'react-native init'.
return path.resolve(__dirname, '../../../..');
} else if (__dirname.match(/Pods[\/\\]React[\/\\]packager$/)) {
// React Native was installed using CocoaPods.
return path.resolve(__dirname, '../../../..');
}
return path.resolve(__dirname, '../..');
}
const getRNPMConfig = (folder) => const getRNPMConfig = (folder) =>
// $FlowFixMe non-literal require // $FlowFixMe non-literal require
require(path.join(folder, './package.json')).rnpm || {}; require(path.join(folder, './package.json')).rnpm || {};
@ -35,8 +43,13 @@ const attachPackage = (command, pkg) => Array.isArray(command)
? command.map(cmd => attachPackage(cmd, pkg)) ? command.map(cmd => attachPackage(cmd, pkg))
: { ...command, pkg }; : { ...command, pkg };
const addSymlinkToRoots = (roots) => const resolveSymlink = (roots) =>
roots.concat(findSymlinksPaths(NODE_MODULES, roots)); roots.concat(
findSymlinksPaths(
path.join(getProjectPath(), 'node_modules'),
roots
)
);
/** /**
* Default configuration for the CLI. * Default configuration for the CLI.
@ -103,21 +116,10 @@ const config: ConfigT = {
getProjectRoots() { getProjectRoots() {
const root = process.env.REACT_NATIVE_APP_ROOT; const root = process.env.REACT_NATIVE_APP_ROOT;
if (root) { if (root) {
return addSymlinkToRoots([path.resolve(root)]); return resolveSymlink([path.resolve(root)]);
} }
var roots; return resolveSymlink([getProjectPath()]);
if (__dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli[\/\\]core$/)) {
// Packager is running from node_modules.
// This is the default case for all projects created using 'react-native init'.
roots = [path.resolve(__dirname, '../../../..')];
} else if (__dirname.match(/Pods[\/\\]React[\/\\]packager$/)) {
// React Native was installed using CocoaPods.
roots = [path.resolve(__dirname, '../../../..')];
} else {
roots = [path.resolve(__dirname, '../..')];
}
return addSymlinkToRoots(roots);
}, },
}; };