Skip to content

Commit

Permalink
Add --config option to CLI to allow passing a path to an `rn-cli.co…
Browse files Browse the repository at this point in the history
…nfig.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 #7883

Differential Revision: D3382823

Pulled By: bestander

fbshipit-source-id: b946f3bb355050fc2fe99273d0e99e441dbed111
  • Loading branch information
skevy authored and Facebook Github Bot 5 committed Aug 12, 2016
1 parent 3f504ec commit 757ab0b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
25 changes: 24 additions & 1 deletion local-cli/cliEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const Config = require('./util/Config');
const childProcess = require('child_process');
const Promise = require('promise');
const chalk = require('chalk');
const minimist = require('minimist');
const path = require('path');
const fs = require('fs');
const gracefulFs = require('graceful-fs');
Expand Down Expand Up @@ -121,16 +122,38 @@ const addCommand = (command: Command, config: Config) => {
opt.parse || defaultOptParser,
typeof opt.default === 'function' ? opt.default(config) : opt.default,
));

// Placeholder option for --config, which is parsed before any other option,
// but needs to be here to avoid "unknown option" errors when specified
cmd.option('--config [string]', 'Path to the CLI configuration file');
};

function getCliConfig() {
// Use a lightweight option parser to look up the CLI configuration file,
// which we need to set up the parser for the other args and options
let cliArgs = minimist(process.argv.slice(2));

let cwd;
let configPath;
if (cliArgs.config != null) {
cwd = process.cwd();
configPath = cliArgs.config;
} else {
cwd = __dirname;
configPath = Config.findConfigPath(cwd);
}

return Config.get(cwd, defaultConfig, configPath);
}

function run() {
const config = Config.get(__dirname, defaultConfig);
const setupEnvScript = /^win/.test(process.platform)
? 'setup_env.bat'
: 'setup_env.sh';

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

const config = getCliConfig();
commands.forEach(cmd => addCommand(cmd, config));

commander.parse(process.argv);
Expand Down
45 changes: 28 additions & 17 deletions local-cli/util/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

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}"`
);
// 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));
}

const config = parentDir
? require(path.join(parentDir, RN_CLI_CONFIG))
: {};
return {
...defaultConfig,
...baseConfig,
cwd,
};
},

cachedConfig = Object.assign({}, defaultConfig, config);
cachedConfig.cwd = cwd;
return cachedConfig;
}
findConfigPath(cwd) {
const parentDir = findParentDirectory(cwd, RN_CLI_CONFIG);
return parentDir ? path.join(parentDir, RN_CLI_CONFIG) : null;
},
};

// Finds the most near ancestor starting at `currentFullPath` that has
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"connect": "^2.8.3",
"core-js": "^2.2.2",
"debug": "^2.2.0",
"denodeify": "^1.2.1",
"event-target-shim": "^1.0.5",
"fbjs": "^0.8.3",
"fbjs-scripts": "^0.7.0",
Expand All @@ -163,6 +164,8 @@
"jstransform": "^11.0.3",
"lodash": "^3.10.1",
"mime": "^1.3.4",
"mime-types": "2.1.11",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"module-deps": "^3.9.1",
"node-fetch": "^1.3.3",
Expand All @@ -183,17 +186,15 @@
"stacktrace-parser": "^0.1.3",
"temp": "0.8.3",
"uglify-js": "^2.6.2",
"whatwg-fetch": "^1.0.0",
"wordwrap": "^1.0.0",
"worker-farm": "^1.3.1",
"ws": "^1.1.0",
"xcode": "^0.8.9",
"xmldoc": "^0.4.0",
"yargs": "^3.24.0",
"yeoman-environment": "1.5.3",
"yeoman-generator": "0.21.2",
"mime-types": "2.1.11",
"whatwg-fetch": "^1.0.0",
"denodeify": "^1.2.1"
"yeoman-generator": "0.21.2"
},
"devDependencies": {
"babel-eslint": "^6.0.0",
Expand Down

0 comments on commit 757ab0b

Please sign in to comment.