Skip to content

Commit

Permalink
Introduce default config
Browse files Browse the repository at this point in the history
Reviewed By: frantic

Differential Revision: D2561344

fb-gh-sync-id: 651b8a199069f78e1ace2897ba4c0352aab7e3ea
  • Loading branch information
martinbigio authored and facebook-github-bot-3 committed Oct 21, 2015
1 parent 8e7cfcd commit c6c97cb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
3 changes: 2 additions & 1 deletion local-cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions local-cli/default.config.js
Original file line number Diff line number Diff line change
@@ -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!<image_name>`. Asset directories for images referenced using
* `./<image.extension>` 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;
4 changes: 0 additions & 4 deletions local-cli/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
20 changes: 0 additions & 20 deletions local-cli/generator/templates/rn-cli.config.js

This file was deleted.

18 changes: 13 additions & 5 deletions private-cli/src/util/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down

0 comments on commit c6c97cb

Please sign in to comment.