Skip to content

Commit 39e0750

Browse files
Adam Miskiewiczhramos
authored andcommitted
Fix broken getProjectRoots default in local-cli
1 parent 1668f90 commit 39e0750

2 files changed

Lines changed: 92 additions & 3 deletions

File tree

local-cli/core/index.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010
*/
1111
'use strict';
1212

13+
const android = require('./android');
1314
const Config = require('../util/Config');
15+
const findPlugins = require('./findPlugins');
16+
const findAssets = require('./findAssets');
17+
const ios = require('./ios');
18+
const windows = require('./windows');
19+
const wrapCommands = require('./wrapCommands');
1420

15-
const defaultConfig = require('./default.config');
21+
const flatten = require('lodash').flatten;
1622
const minimist = require('minimist');
1723
const path = require('path');
1824

@@ -35,6 +41,61 @@ export type RNConfig = {
3541
getDependencyConfig(pkgName: string): Object,
3642
};
3743

44+
const getRNPMConfig = (folder) =>
45+
// $FlowFixMe non-literal require
46+
require(path.join(folder, './package.json')).rnpm || {};
47+
48+
const attachPackage = (command, pkg) => Array.isArray(command)
49+
? command.map(cmd => attachPackage(cmd, pkg))
50+
: { ...command, pkg };
51+
52+
const defaultRNConfig = {
53+
getProjectCommands(): Array<CommandT> {
54+
const appRoot = process.cwd();
55+
const plugins = findPlugins([appRoot])
56+
.map(pathToCommands => {
57+
const name = pathToCommands.split(path.sep)[0];
58+
59+
return attachPackage(
60+
// $FlowFixMe non-literal require
61+
require(path.join(appRoot, 'node_modules', pathToCommands)),
62+
// $FlowFixMe non-literal require
63+
require(path.join(appRoot, 'node_modules', name, 'package.json'))
64+
);
65+
});
66+
67+
return flatten(plugins);
68+
},
69+
70+
getProjectConfig(): Object {
71+
const folder = process.cwd();
72+
const rnpm = getRNPMConfig(folder);
73+
74+
return Object.assign({}, rnpm, {
75+
ios: ios.projectConfig(folder, rnpm.ios || {}),
76+
android: android.projectConfig(folder, rnpm.android || {}),
77+
windows: windows.projectConfig(folder, rnpm.windows || {}),
78+
assets: findAssets(folder, rnpm.assets),
79+
});
80+
},
81+
82+
getDependencyConfig(packageName: string) {
83+
const folder = path.join(process.cwd(), 'node_modules', packageName);
84+
const rnpm = getRNPMConfig(
85+
path.join(process.cwd(), 'node_modules', packageName)
86+
);
87+
88+
return Object.assign({}, rnpm, {
89+
ios: ios.dependencyConfig(folder, rnpm.ios || {}),
90+
android: android.dependencyConfig(folder, rnpm.android || {}),
91+
windows: windows.dependencyConfig(folder, rnpm.windows || {}),
92+
assets: findAssets(folder, rnpm.assets),
93+
commands: wrapCommands(rnpm.commands),
94+
params: rnpm.params || [],
95+
});
96+
},
97+
};
98+
3899
/**
39100
* Loads the CLI configuration
40101
*/
@@ -44,7 +105,7 @@ function getCliConfig(): RNConfig {
44105
? Config.loadFile(path.resolve(__dirname, cliArgs.config))
45106
: Config.findOptional(__dirname);
46107

47-
return {...defaultConfig, ...config};
108+
return {...defaultRNConfig, ...config};
48109
}
49110

50111
module.exports = getCliConfig();

local-cli/util/Config.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
'use strict';
1212

13+
const findSymlinksPaths = require('./findSymlinksPaths');
14+
1315
const blacklist = require('../../packager/blacklist');
1416
const fs = require('fs');
1517
const invariant = require('fbjs/lib/invariant');
@@ -93,12 +95,38 @@ export type ConfigT = {
9395
transformVariants: () => TransformVariants,
9496
};
9597

98+
function getProjectPath() {
99+
if (__dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli[\/\\]util$/)) {
100+
// Packager is running from node_modules.
101+
// This is the default case for all projects created using 'react-native init'.
102+
return path.resolve(__dirname, '../../../..');
103+
} else if (__dirname.match(/Pods[\/\\]React[\/\\]packager$/)) {
104+
// React Native was installed using CocoaPods.
105+
return path.resolve(__dirname, '../../../..');
106+
}
107+
return path.resolve(__dirname, '../..');
108+
}
109+
110+
const resolveSymlink = (roots) =>
111+
roots.concat(
112+
findSymlinksPaths(
113+
path.join(getProjectPath(), 'node_modules'),
114+
roots
115+
)
116+
);
117+
96118
const defaultConfig: ConfigT = {
97119
extraNodeModules: Object.create(null),
98120
getAssetExts: () => [],
99121
getBlacklistRE: () => blacklist(),
100122
getPlatforms: () => [],
101-
getProjectRoots: () => [process.cwd()],
123+
getProjectRoots: () => {
124+
const root = process.env.REACT_NATIVE_APP_ROOT;
125+
if (root) {
126+
return resolveSymlink([path.resolve(root)]);
127+
}
128+
return resolveSymlink([getProjectPath()]);
129+
},
102130
getProvidesModuleNodeModules: () => providesModuleNodeModules.slice(),
103131
getSourceExts: () => [],
104132
getTransformModulePath: () => path.resolve(__dirname, '../../packager/transformer'),

0 commit comments

Comments
 (0)