Config.js
3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* 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.
*
* @format
* @flow
*/
'use strict';
const findSymlinkedModules = require('./findSymlinkedModules');
const fs = require('fs');
const getPolyfills = require('../../rn-get-polyfills');
const invariant = require('fbjs/lib/invariant');
const path = require('path');
const {Config: MetroConfig, createBlacklist} = require('metro');
const RN_CLI_CONFIG = 'rn-cli.config.js';
import type {ConfigT as MetroConfigT} from 'metro';
/**
* Configuration file of the CLI.
*/
export type ConfigT = MetroConfigT;
function getProjectPath() {
if (
__dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli[\/\\]util$/)
) {
// 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, '../..');
}
const resolveSymlinksForRoots = roots =>
roots.reduce(
(arr, rootPath) => arr.concat(findSymlinkedModules(rootPath, roots)),
[...roots],
);
const getProjectRoots = () => {
const root = process.env.REACT_NATIVE_APP_ROOT;
if (root) {
return resolveSymlinksForRoots([path.resolve(root)]);
}
return resolveSymlinksForRoots([getProjectPath()]);
};
const getBlacklistRE = () => {
return createBlacklist([/.*\/__fixtures__\/.*/]);
};
/**
* Module capable of getting the configuration out of a given file.
*
* The function will return all the default configuration, as specified by the
* `DEFAULT` 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.
*/
const Config = {
DEFAULT: ({
...MetroConfig.DEFAULT,
getBlacklistRE,
getProjectRoots,
getPolyfills,
getModulesRunBeforeMainModule: () => [
require.resolve('../../Libraries/Core/InitializeCore'),
],
}: ConfigT),
find(startDir: string): ConfigT {
return this.findWithPath(startDir).config;
},
findWithPath(startDir: string): {config: ConfigT, projectPath: string} {
const configPath = findConfigPath(startDir);
invariant(
configPath,
`Can't find "${RN_CLI_CONFIG}" file in any parent folder of "${startDir}"`,
);
const projectPath = path.dirname(configPath);
return {config: this.load(configPath, startDir), projectPath};
},
findOptional(startDir: string): ConfigT {
const configPath = findConfigPath(startDir);
return configPath ? this.load(configPath, startDir) : {...Config.DEFAULT};
},
getProjectPath,
getProjectRoots,
load(configFile: string): ConfigT {
return MetroConfig.load(configFile, Config.DEFAULT);
},
};
function findConfigPath(cwd: string): ?string {
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
// 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));
};
return testDir(currentFullPath.substring(root.length).split(path.sep));
}
module.exports = Config;