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
|
|
|
*
|
|
|
|
* @flow
|
2015-09-22 16:00:04 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-08-12 18:46:04 +00:00
|
|
|
const assert = require('assert');
|
2015-09-22 16:00:04 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const RN_CLI_CONFIG = 'rn-cli.config.js';
|
|
|
|
|
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
|
|
|
// TODO: @bestander & @grabbou - get rid when internal tests are fixed
|
|
|
|
export type { ConfigT } from '../core';
|
2016-10-22 13:07:01 +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
|
|
|
|
* `defaultConfig` param overriden by those found on `rn-cli.config.js` files, if any. If no
|
|
|
|
* 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 = {
|
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
|
|
|
get<T>(
|
2016-10-22 13:07:01 +00:00
|
|
|
cwd: string,
|
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
|
|
|
defaultConfig?: T | null,
|
2016-10-22 13:07:01 +00:00
|
|
|
pathToConfig?: string | null,
|
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
|
|
|
): T {
|
2016-08-12 18:46:04 +00:00
|
|
|
let baseConfig;
|
2015-09-22 16:00:04 +00:00
|
|
|
|
2016-08-12 18:46:04 +00:00
|
|
|
// 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}"`
|
|
|
|
);
|
|
|
|
}
|
2016-10-22 13:07:01 +00:00
|
|
|
// $FlowFixMe nope
|
2016-08-12 18:46:04 +00:00
|
|
|
baseConfig = require(configPath);
|
|
|
|
} else if (pathToConfig == null) {
|
|
|
|
assert(defaultConfig, 'Must have a default config if config is missing');
|
|
|
|
} else {
|
|
|
|
baseConfig = path.isAbsolute(pathToConfig) ?
|
2016-10-22 13:07:01 +00:00
|
|
|
// $FlowFixMe nope
|
2016-08-12 18:46:04 +00:00
|
|
|
require(pathToConfig) :
|
2016-10-22 13:07:01 +00:00
|
|
|
// $FlowFixMe nope
|
2016-08-12 18:46:04 +00:00
|
|
|
require(path.join(cwd, pathToConfig));
|
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
|
|
|
// $FlowFixMe we return `at least` T + extras
|
2016-08-12 18:46:04 +00:00
|
|
|
return {
|
|
|
|
...defaultConfig,
|
|
|
|
...baseConfig,
|
|
|
|
cwd,
|
|
|
|
};
|
|
|
|
},
|
2015-10-20 23:58:13 +00:00
|
|
|
|
2016-10-22 13:07:01 +00:00
|
|
|
findConfigPath(cwd: string): ?string {
|
2016-08-12 18:46:04 +00:00
|
|
|
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;
|
|
|
|
const testDir = (parts) => {
|
|
|
|
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;
|