2015-09-22 16:00:04 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2016-10-22 13:07:01 +00:00
|
|
|
*
|
2017-09-26 19:54:44 +00:00
|
|
|
* @format
|
2016-10-22 13:07:01 +00:00
|
|
|
* @flow
|
2015-09-22 16:00:04 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-09-14 11:20:26 +00:00
|
|
|
const findSymlinkedModules = require('./findSymlinkedModules');
|
2015-09-22 16:00:04 +00:00
|
|
|
const fs = require('fs');
|
2017-07-13 10:30:06 +00:00
|
|
|
const getPolyfills = require('../../rn-get-polyfills');
|
2017-05-03 09:47:38 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-09-22 16:00:04 +00:00
|
|
|
const path = require('path');
|
|
|
|
|
2017-11-29 11:00:49 +00:00
|
|
|
const {Config: MetroConfig} = require('metro');
|
2017-05-03 13:38:40 +00:00
|
|
|
|
2015-09-22 16:00:04 +00:00
|
|
|
const RN_CLI_CONFIG = 'rn-cli.config.js';
|
|
|
|
|
2017-11-29 11:00:49 +00:00
|
|
|
import type {ConfigT as MetroConfigT} from 'metro';
|
2017-05-03 13:38:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration file of the CLI.
|
|
|
|
*/
|
2017-09-15 11:55:29 +00:00
|
|
|
export type ConfigT = MetroConfigT;
|
2017-05-03 13:38:40 +00:00
|
|
|
|
Fix broken default getProjectRoots
Summary:
In <= 0.44, the default implementation of getProjectRoots() came from `local-cli/core/default.config.js`. With changes happening in the CLI and the packager over the course of the last two months, various pieces of this logic (specifically `local-cli/utils/Config.js`) were rewritten, and though default.config.js was still being imported and used in `local-cli/core/index.js`, the default `getProjectRoots()` was being overriden by the defaults specified in `local-cli/utils/Config.js`.
This PR moves the logic from default.config.js into Config.js and index.js, as appropriate. Specifically:
- The `getProjectCommands()`, `getProjectConfig()`, and `getDependencyConfig()` methods, which have traditionally not been part of the rn-cli.config.js spec, are now defined in `local-cli/core/index.js`.
- The `getProjectRoots()` method, which contained logic for properly resolving the _actual_ project root as well as resolving symlinks within that root, has been moved to `local-cli/utils/Config.js`, to match the fact that other default rn-cli.config.js definitions live there.
Closes https://github.com/facebook/react-native/pull/14412
Differential Revision: D5216887
Pulled By: hramos
fbshipit-source-id: 7a3840ecf0ad8ea3f6d7bbd3d54e4f02950c6a32
2017-06-09 08:14:37 +00:00
|
|
|
function getProjectPath() {
|
2017-09-26 19:54:44 +00:00
|
|
|
if (
|
|
|
|
__dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli[\/\\]util$/)
|
|
|
|
) {
|
Fix broken default getProjectRoots
Summary:
In <= 0.44, the default implementation of getProjectRoots() came from `local-cli/core/default.config.js`. With changes happening in the CLI and the packager over the course of the last two months, various pieces of this logic (specifically `local-cli/utils/Config.js`) were rewritten, and though default.config.js was still being imported and used in `local-cli/core/index.js`, the default `getProjectRoots()` was being overriden by the defaults specified in `local-cli/utils/Config.js`.
This PR moves the logic from default.config.js into Config.js and index.js, as appropriate. Specifically:
- The `getProjectCommands()`, `getProjectConfig()`, and `getDependencyConfig()` methods, which have traditionally not been part of the rn-cli.config.js spec, are now defined in `local-cli/core/index.js`.
- The `getProjectRoots()` method, which contained logic for properly resolving the _actual_ project root as well as resolving symlinks within that root, has been moved to `local-cli/utils/Config.js`, to match the fact that other default rn-cli.config.js definitions live there.
Closes https://github.com/facebook/react-native/pull/14412
Differential Revision: D5216887
Pulled By: hramos
fbshipit-source-id: 7a3840ecf0ad8ea3f6d7bbd3d54e4f02950c6a32
2017-06-09 08:14:37 +00:00
|
|
|
// Packager is running from node_modules.
|
|
|
|
// This is the default case for all projects created using 'react-native init'.
|
|
|
|
return path.resolve(__dirname, '../../../..');
|
|
|
|
} else if (__dirname.match(/Pods[\/\\]React[\/\\]packager$/)) {
|
|
|
|
// React Native was installed using CocoaPods.
|
|
|
|
return path.resolve(__dirname, '../../../..');
|
|
|
|
}
|
|
|
|
return path.resolve(__dirname, '../..');
|
|
|
|
}
|
|
|
|
|
2017-09-14 11:20:26 +00:00
|
|
|
const resolveSymlinksForRoots = roots =>
|
|
|
|
roots.reduce(
|
2017-09-26 19:54:44 +00:00
|
|
|
(arr, rootPath) => arr.concat(findSymlinkedModules(rootPath, roots)),
|
|
|
|
[...roots],
|
Fix broken default getProjectRoots
Summary:
In <= 0.44, the default implementation of getProjectRoots() came from `local-cli/core/default.config.js`. With changes happening in the CLI and the packager over the course of the last two months, various pieces of this logic (specifically `local-cli/utils/Config.js`) were rewritten, and though default.config.js was still being imported and used in `local-cli/core/index.js`, the default `getProjectRoots()` was being overriden by the defaults specified in `local-cli/utils/Config.js`.
This PR moves the logic from default.config.js into Config.js and index.js, as appropriate. Specifically:
- The `getProjectCommands()`, `getProjectConfig()`, and `getDependencyConfig()` methods, which have traditionally not been part of the rn-cli.config.js spec, are now defined in `local-cli/core/index.js`.
- The `getProjectRoots()` method, which contained logic for properly resolving the _actual_ project root as well as resolving symlinks within that root, has been moved to `local-cli/utils/Config.js`, to match the fact that other default rn-cli.config.js definitions live there.
Closes https://github.com/facebook/react-native/pull/14412
Differential Revision: D5216887
Pulled By: hramos
fbshipit-source-id: 7a3840ecf0ad8ea3f6d7bbd3d54e4f02950c6a32
2017-06-09 08:14:37 +00:00
|
|
|
);
|
|
|
|
|
2017-09-15 11:55:29 +00:00
|
|
|
const getProjectRoots = () => {
|
|
|
|
const root = process.env.REACT_NATIVE_APP_ROOT;
|
|
|
|
if (root) {
|
|
|
|
return resolveSymlinksForRoots([path.resolve(root)]);
|
|
|
|
}
|
|
|
|
return resolveSymlinksForRoots([getProjectPath()]);
|
|
|
|
};
|
2017-09-14 11:20:26 +00:00
|
|
|
|
2015-09-22 16:00:04 +00:00
|
|
|
/**
|
Merge `rnpm config` with the `cli config`, step 1
Summary:
This is step one on merging the `rnpm` config with the `cli config`. The plan is to remove two sources of truth (rnpm package.json config and rn-cli Javascript config)
Rationale:
As of now, we have `rnpm` config, that used to live in the `local-cli/core/config/index.js` and the `rn-cli` config, living in `default.config.js` and `utils/Config.js`.
This PR moves all the things into one file, called `local-cli/core', simplifies things, enhances types (Config now has better types, added missing properties and fixed descriptions).
One notable addition is that users can now opt-in to override the commands that are loaded at the package level. Previously it was only possible to add a command by writing a plugin. Now, you can just tweak the `rn-cli.config.js`.
This is one of the several improvements I have on my roadmap, with better documentation for the CLI as well.
Closes https://github.com/facebook/react-native/pull/11564
Differential Revision: D4360095
fbshipit-source-id: feaec7c88df63e51cef1f9620c7eedeb738d3d00
2017-01-09 15:49:29 +00:00
|
|
|
* Module capable of getting the configuration out of a given file.
|
2015-10-20 23:58:13 +00:00
|
|
|
*
|
Merge `rnpm config` with the `cli config`, step 1
Summary:
This is step one on merging the `rnpm` config with the `cli config`. The plan is to remove two sources of truth (rnpm package.json config and rn-cli Javascript config)
Rationale:
As of now, we have `rnpm` config, that used to live in the `local-cli/core/config/index.js` and the `rn-cli` config, living in `default.config.js` and `utils/Config.js`.
This PR moves all the things into one file, called `local-cli/core', simplifies things, enhances types (Config now has better types, added missing properties and fixed descriptions).
One notable addition is that users can now opt-in to override the commands that are loaded at the package level. Previously it was only possible to add a command by writing a plugin. Now, you can just tweak the `rn-cli.config.js`.
This is one of the several improvements I have on my roadmap, with better documentation for the CLI as well.
Closes https://github.com/facebook/react-native/pull/11564
Differential Revision: D4360095
fbshipit-source-id: feaec7c88df63e51cef1f9620c7eedeb738d3d00
2017-01-09 15:49:29 +00:00
|
|
|
* The function will return all the default configuration, as specified by the
|
2017-09-15 11:55:29 +00:00
|
|
|
* `DEFAULT` param overriden by those found on `rn-cli.config.js` files, if any. If no
|
Merge `rnpm config` with the `cli config`, step 1
Summary:
This is step one on merging the `rnpm` config with the `cli config`. The plan is to remove two sources of truth (rnpm package.json config and rn-cli Javascript config)
Rationale:
As of now, we have `rnpm` config, that used to live in the `local-cli/core/config/index.js` and the `rn-cli` config, living in `default.config.js` and `utils/Config.js`.
This PR moves all the things into one file, called `local-cli/core', simplifies things, enhances types (Config now has better types, added missing properties and fixed descriptions).
One notable addition is that users can now opt-in to override the commands that are loaded at the package level. Previously it was only possible to add a command by writing a plugin. Now, you can just tweak the `rn-cli.config.js`.
This is one of the several improvements I have on my roadmap, with better documentation for the CLI as well.
Closes https://github.com/facebook/react-native/pull/11564
Differential Revision: D4360095
fbshipit-source-id: feaec7c88df63e51cef1f9620c7eedeb738d3d00
2017-01-09 15:49:29 +00:00
|
|
|
* default config is provided and no configuration can be found in the directory
|
|
|
|
* hierarchy, an error will be thrown.
|
2015-09-22 16:00:04 +00:00
|
|
|
*/
|
|
|
|
const Config = {
|
2017-09-15 11:55:29 +00:00
|
|
|
DEFAULT: ({
|
|
|
|
...MetroConfig.DEFAULT,
|
|
|
|
getProjectRoots,
|
2017-07-13 10:30:06 +00:00
|
|
|
getPolyfills,
|
2017-11-07 15:31:39 +00:00
|
|
|
getModulesRunBeforeMainModule: () => [
|
2017-09-26 19:54:44 +00:00
|
|
|
require.resolve('../../Libraries/Core/InitializeCore'),
|
|
|
|
],
|
2017-06-02 16:23:48 +00:00
|
|
|
}: ConfigT),
|
|
|
|
|
2017-05-03 13:38:40 +00:00
|
|
|
find(startDir: string): ConfigT {
|
2017-08-09 02:03:56 +00:00
|
|
|
return this.findWithPath(startDir).config;
|
2017-05-25 11:23:19 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
findWithPath(startDir: string): {config: ConfigT, projectPath: string} {
|
2017-05-03 09:47:38 +00:00
|
|
|
const configPath = findConfigPath(startDir);
|
|
|
|
invariant(
|
|
|
|
configPath,
|
|
|
|
`Can't find "${RN_CLI_CONFIG}" file in any parent folder of "${startDir}"`,
|
|
|
|
);
|
2017-05-25 11:23:19 +00:00
|
|
|
const projectPath = path.dirname(configPath);
|
2017-09-15 11:55:29 +00:00
|
|
|
return {config: this.load(configPath, startDir), projectPath};
|
2017-05-03 09:47:38 +00:00
|
|
|
},
|
2015-09-22 16:00:04 +00:00
|
|
|
|
2017-05-03 13:38:40 +00:00
|
|
|
findOptional(startDir: string): ConfigT {
|
2017-05-03 09:47:38 +00:00
|
|
|
const configPath = findConfigPath(startDir);
|
2017-09-26 19:54:44 +00:00
|
|
|
return configPath ? this.load(configPath, startDir) : {...Config.DEFAULT};
|
2016-08-12 18:46:04 +00:00
|
|
|
},
|
2015-10-20 23:58:13 +00:00
|
|
|
|
2018-02-02 12:35:11 +00:00
|
|
|
getProjectPath,
|
|
|
|
getProjectRoots,
|
|
|
|
|
2017-09-15 11:55:29 +00:00
|
|
|
load(configFile: string): ConfigT {
|
|
|
|
return MetroConfig.load(configFile, Config.DEFAULT);
|
2016-08-12 18:46:04 +00:00
|
|
|
},
|
2015-09-22 16:00:04 +00:00
|
|
|
};
|
|
|
|
|
2017-05-03 09:47:38 +00:00
|
|
|
function findConfigPath(cwd: string): ?string {
|
|
|
|
const parentDir = findParentDirectory(cwd, RN_CLI_CONFIG);
|
|
|
|
return parentDir ? path.join(parentDir, RN_CLI_CONFIG) : null;
|
|
|
|
}
|
|
|
|
|
2015-09-22 16:00:04 +00:00
|
|
|
// Finds the most near ancestor starting at `currentFullPath` that has
|
|
|
|
// a file named `filename`
|
|
|
|
function findParentDirectory(currentFullPath, filename) {
|
|
|
|
const root = path.parse(currentFullPath).root;
|
2017-09-26 19:54:44 +00:00
|
|
|
const testDir = parts => {
|
2015-09-22 16:00:04 +00:00
|
|
|
if (parts.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fullPath = path.join(root, parts.join(path.sep));
|
|
|
|
|
|
|
|
var exists = fs.existsSync(path.join(fullPath, filename));
|
|
|
|
return exists ? fullPath : testDir(parts.slice(0, -1));
|
|
|
|
};
|
|
|
|
|
2016-03-21 01:08:23 +00:00
|
|
|
return testDir(currentFullPath.substring(root.length).split(path.sep));
|
2015-09-22 16:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Config;
|