From c6c97cbd9d00c89aeb47677cb571205c830f025b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Bigio?= Date: Tue, 20 Oct 2015 16:58:13 -0700 Subject: [PATCH] Introduce default config Reviewed By: frantic Differential Revision: D2561344 fb-gh-sync-id: 651b8a199069f78e1ace2897ba4c0352aab7e3ea --- local-cli/cli.js | 3 +- local-cli/default.config.js | 44 +++++++++++++++++++ local-cli/generator/index.js | 4 -- .../generator/templates/rn-cli.config.js | 20 --------- private-cli/src/util/Config.js | 18 +++++--- 5 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 local-cli/default.config.js delete mode 100644 local-cli/generator/templates/rn-cli.config.js diff --git a/local-cli/cli.js b/local-cli/cli.js index e3bd08eda..44b8cf0d3 100644 --- a/local-cli/cli.js +++ b/local-cli/cli.js @@ -11,6 +11,7 @@ var bundle = require('../private-cli/src/bundle/bundle'); var childProcess = require('child_process'); var Config = require('../private-cli/src/util/Config'); +var defaultConfig = require('./default.config'); var fs = require('fs'); var generate = require('../private-cli/src/generate/generate'); var library = require('../private-cli/src/library/library'); @@ -56,7 +57,7 @@ function run() { return; } - command[0](args, Config.get(__dirname)).done(); + command[0](args, Config.get(__dirname, defaultConfig)).done(); } function generateWrapper(args, config) { diff --git a/local-cli/default.config.js b/local-cli/default.config.js new file mode 100644 index 000000000..2be890764 --- /dev/null +++ b/local-cli/default.config.js @@ -0,0 +1,44 @@ +'use strict'; + +var blacklist = require('../packager/blacklist'); +var path = require('path'); + +/** + * Default configuration for the CLI. + * + * If you need to override any of this functions do so by defining the file + * `rn-cli.config.js` on the root of your project with the functions you need + * to tweak. + */ +var config = { + getProjectRoots() { + if (__dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli$/)) { + // packager is running from node_modules of another project + return [path.resolve(__dirname, '../../..')]; + } else if (__dirname.match(/Pods[\/\\]React[\/\\]packager$/)) { + // packager is running from node_modules of another project + return [path.resolve(__dirname, '../../..')]; + } else { + return [path.resolve(__dirname, '..')]; + } + }, + + /** + * Specify where to look for assets that are referenced using + * `image!`. Asset directories for images referenced using + * `./` don't require any entry in here. + */ + getAssetRoots() { + return []; + }, + + /** + * Returns a regular expression for modules that should be ignored by the + * packager on a given platform. + */ + getBlacklistRE(platform) { + return blacklist(platform); + } +}; + +module.exports = config; diff --git a/local-cli/generator/index.js b/local-cli/generator/index.js index c01649ab1..6a1b6ac00 100644 --- a/local-cli/generator/index.js +++ b/local-cli/generator/index.js @@ -48,10 +48,6 @@ module.exports = yeoman.generators.NamedBase.extend({ { 'Libraries\/react-native\/react-native-interface.js' : 'node_modules/react-native/Libraries/react-native/react-native-interface.js' } ); - this.fs.copy( - this.templatePath('rn-cli.config.js'), - this.destinationPath('rn-cli.config.js') - ); this.fs.copy( this.templatePath('_gitignore'), this.destinationPath('.gitignore') diff --git a/local-cli/generator/templates/rn-cli.config.js b/local-cli/generator/templates/rn-cli.config.js deleted file mode 100644 index 18e2e9269..000000000 --- a/local-cli/generator/templates/rn-cli.config.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -var blacklist = require('./node_modules/react-native/packager/blacklist'); - -var config = { - getProjectRoots() { - return [__dirname]; - }, - - getAssetRoots() { - // speficy where to look for assets - return []; - }, - - getBlacklistRE(platform) { - return blacklist(platform); - } -}; - -module.exports = config; diff --git a/private-cli/src/util/Config.js b/private-cli/src/util/Config.js index c6bfd3bbc..39f1e65c1 100644 --- a/private-cli/src/util/Config.js +++ b/private-cli/src/util/Config.js @@ -16,25 +16,33 @@ let cachedConfig = null; /** * Module capable of getting the configuration that should be used for - * the `rn-cli`. The configuration file is a JS file named `rn-cli.conf.js`. + * the `rn-cli`. The configuration file is a JS file named `rn-cli.config.js`. * It has to be on any parent directory of the cli. + * + * The function will return all the default configuration functions overriden + * by those found on `rn-cli.config.js`, if any. If no default config is + * provided and no configuration can be found in the directory hierarchy an + * error will be thrown. */ const Config = { - get(pwd) { + get(pwd, defaultConfig) { if (cachedConfig) { return cachedConfig; } const parentDir = findParentDirectory(pwd, RN_CLI_CONFIG); - - if (!parentDir) { + if (!parentDir && !defaultConfig) { throw new Error( 'Can\'t find "rn-cli.config.js" file in any parent folder of "' + __dirname + '"' ); } - cachedConfig = require(path.join(parentDir, RN_CLI_CONFIG)); + const config = parentDir + ? require(path.join(parentDir, RN_CLI_CONFIG)) + : {}; + + cachedConfig = Object.assign({}, defaultConfig, config); return cachedConfig; } };