Skip to content

Commit 757ab0b

Browse files
skevyFacebook Github Bot 5
authored andcommitted
Add --config option to CLI to allow passing a path to an rn-cli.config.js
Summary: Currently we just try to resolve a rn-cli.config.js file by walking up the tree from node_modules/react-native. In non-standard uses of RN, when your copy of RN may not live within node_modules, it's impossible to use rn-cli.config.js. This PR adds a "config" flag to the cli that let's you pass in a path to rn-cli.config.js. cc ide Closes facebook#7883 Differential Revision: D3382823 Pulled By: bestander fbshipit-source-id: b946f3bb355050fc2fe99273d0e99e441dbed111
1 parent 3f504ec commit 757ab0b

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

local-cli/cliEntry.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const Config = require('./util/Config');
1616
const childProcess = require('child_process');
1717
const Promise = require('promise');
1818
const chalk = require('chalk');
19+
const minimist = require('minimist');
1920
const path = require('path');
2021
const fs = require('fs');
2122
const gracefulFs = require('graceful-fs');
@@ -121,16 +122,38 @@ const addCommand = (command: Command, config: Config) => {
121122
opt.parse || defaultOptParser,
122123
typeof opt.default === 'function' ? opt.default(config) : opt.default,
123124
));
125+
126+
// Placeholder option for --config, which is parsed before any other option,
127+
// but needs to be here to avoid "unknown option" errors when specified
128+
cmd.option('--config [string]', 'Path to the CLI configuration file');
124129
};
125130

131+
function getCliConfig() {
132+
// Use a lightweight option parser to look up the CLI configuration file,
133+
// which we need to set up the parser for the other args and options
134+
let cliArgs = minimist(process.argv.slice(2));
135+
136+
let cwd;
137+
let configPath;
138+
if (cliArgs.config != null) {
139+
cwd = process.cwd();
140+
configPath = cliArgs.config;
141+
} else {
142+
cwd = __dirname;
143+
configPath = Config.findConfigPath(cwd);
144+
}
145+
146+
return Config.get(cwd, defaultConfig, configPath);
147+
}
148+
126149
function run() {
127-
const config = Config.get(__dirname, defaultConfig);
128150
const setupEnvScript = /^win/.test(process.platform)
129151
? 'setup_env.bat'
130152
: 'setup_env.sh';
131153

132154
childProcess.execFileSync(path.join(__dirname, setupEnvScript));
133155

156+
const config = getCliConfig();
134157
commands.forEach(cmd => addCommand(cmd, config));
135158

136159
commander.parse(process.argv);

local-cli/util/Config.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
*/
99
'use strict';
1010

11+
const assert = require('assert');
1112
const fs = require('fs');
1213
const path = require('path');
1314

1415
const RN_CLI_CONFIG = 'rn-cli.config.js';
15-
let cachedConfig = null;
1616

1717
/**
1818
* Module capable of getting the configuration that should be used for
@@ -25,26 +25,37 @@ let cachedConfig = null;
2525
* error will be thrown.
2626
*/
2727
const Config = {
28-
get(cwd, defaultConfig) {
29-
if (cachedConfig) {
30-
return cachedConfig;
31-
}
28+
get(cwd, defaultConfig, pathToConfig) {
29+
let baseConfig;
3230

33-
const parentDir = findParentDirectory(cwd, RN_CLI_CONFIG);
34-
if (!parentDir && !defaultConfig) {
35-
throw new Error(
36-
`Can't find "rn-cli.config.js" file in any parent folder of "${cwd}"`
37-
);
31+
// Handle the legacy code path where pathToConfig is unspecified
32+
if (pathToConfig === undefined) {
33+
const configPath = Config.findConfigPath(cwd);
34+
if (!configPath && !defaultConfig) {
35+
throw new Error(
36+
`Can't find "${RN_CLI_CONFIG}" file in any parent folder of "${cwd}"`
37+
);
38+
}
39+
baseConfig = require(configPath);
40+
} else if (pathToConfig == null) {
41+
assert(defaultConfig, 'Must have a default config if config is missing');
42+
} else {
43+
baseConfig = path.isAbsolute(pathToConfig) ?
44+
require(pathToConfig) :
45+
require(path.join(cwd, pathToConfig));
3846
}
3947

40-
const config = parentDir
41-
? require(path.join(parentDir, RN_CLI_CONFIG))
42-
: {};
48+
return {
49+
...defaultConfig,
50+
...baseConfig,
51+
cwd,
52+
};
53+
},
4354

44-
cachedConfig = Object.assign({}, defaultConfig, config);
45-
cachedConfig.cwd = cwd;
46-
return cachedConfig;
47-
}
55+
findConfigPath(cwd) {
56+
const parentDir = findParentDirectory(cwd, RN_CLI_CONFIG);
57+
return parentDir ? path.join(parentDir, RN_CLI_CONFIG) : null;
58+
},
4859
};
4960

5061
// Finds the most near ancestor starting at `currentFullPath` that has

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
"connect": "^2.8.3",
149149
"core-js": "^2.2.2",
150150
"debug": "^2.2.0",
151+
"denodeify": "^1.2.1",
151152
"event-target-shim": "^1.0.5",
152153
"fbjs": "^0.8.3",
153154
"fbjs-scripts": "^0.7.0",
@@ -163,6 +164,8 @@
163164
"jstransform": "^11.0.3",
164165
"lodash": "^3.10.1",
165166
"mime": "^1.3.4",
167+
"mime-types": "2.1.11",
168+
"minimist": "^1.2.0",
166169
"mkdirp": "^0.5.1",
167170
"module-deps": "^3.9.1",
168171
"node-fetch": "^1.3.3",
@@ -183,17 +186,15 @@
183186
"stacktrace-parser": "^0.1.3",
184187
"temp": "0.8.3",
185188
"uglify-js": "^2.6.2",
189+
"whatwg-fetch": "^1.0.0",
186190
"wordwrap": "^1.0.0",
187191
"worker-farm": "^1.3.1",
188192
"ws": "^1.1.0",
189193
"xcode": "^0.8.9",
190194
"xmldoc": "^0.4.0",
191195
"yargs": "^3.24.0",
192196
"yeoman-environment": "1.5.3",
193-
"yeoman-generator": "0.21.2",
194-
"mime-types": "2.1.11",
195-
"whatwg-fetch": "^1.0.0",
196-
"denodeify": "^1.2.1"
197+
"yeoman-generator": "0.21.2"
197198
},
198199
"devDependencies": {
199200
"babel-eslint": "^6.0.0",

0 commit comments

Comments
 (0)