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 https://github.com/facebook/react-native/pull/7883

Differential Revision: D3382823

Pulled By: bestander

fbshipit-source-id: b946f3bb355050fc2fe99273d0e99e441dbed111
This commit is contained in:
Adam Miskiewicz
2016-08-12 11:46:04 -07:00
committed by Facebook Github Bot 5
parent 3f504ec147
commit 757ab0b936
3 changed files with 58 additions and 23 deletions

View File

@@ -8,11 +8,11 @@
*/
'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const RN_CLI_CONFIG = 'rn-cli.config.js';
let cachedConfig = null;
/**
* Module capable of getting the configuration that should be used for
@@ -25,26 +25,37 @@ let cachedConfig = null;
* error will be thrown.
*/
const Config = {
get(cwd, defaultConfig) {
if (cachedConfig) {
return cachedConfig;
get(cwd, defaultConfig, pathToConfig) {
let baseConfig;
// Handle the legacy code path where pathToConfig is unspecified
if (pathToConfig === undefined) {
const configPath = Config.findConfigPath(cwd);
if (!configPath && !defaultConfig) {
throw new Error(
`Can't find "${RN_CLI_CONFIG}" file in any parent folder of "${cwd}"`
);
}
baseConfig = require(configPath);
} else if (pathToConfig == null) {
assert(defaultConfig, 'Must have a default config if config is missing');
} else {
baseConfig = path.isAbsolute(pathToConfig) ?
require(pathToConfig) :
require(path.join(cwd, pathToConfig));
}
return {
...defaultConfig,
...baseConfig,
cwd,
};
},
findConfigPath(cwd) {
const parentDir = findParentDirectory(cwd, RN_CLI_CONFIG);
if (!parentDir && !defaultConfig) {
throw new Error(
`Can't find "rn-cli.config.js" file in any parent folder of "${cwd}"`
);
}
const config = parentDir
? require(path.join(parentDir, RN_CLI_CONFIG))
: {};
cachedConfig = Object.assign({}, defaultConfig, config);
cachedConfig.cwd = cwd;
return cachedConfig;
}
return parentDir ? path.join(parentDir, RN_CLI_CONFIG) : null;
},
};
// Finds the most near ancestor starting at `currentFullPath` that has