-
Notifications
You must be signed in to change notification settings - Fork 48.3k
/
Copy pathcreateFlowConfigs.js
152 lines (131 loc) · 4.39 KB
/
createFlowConfigs.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const inlinedHostConfigs = require('../shared/inlinedHostConfigs');
const flowVersion = require('../../package.json').devDependencies['flow-bin'];
const configTemplate = fs
.readFileSync(__dirname + '/config/flowconfig')
.toString();
// stores all forks discovered during config generation
const allForks = new Set();
// maps forked file to the base path containing it and it's forks (it's parent)
const forkedFiles = new Map();
function findForks(file) {
const basePath = path.join(file, '..');
const forksPath = path.join(basePath, 'forks');
const forks = fs.readdirSync(path.join('packages', forksPath));
forks.forEach(f => allForks.add('forks/' + f));
forkedFiles.set(file, basePath);
return basePath;
}
function addFork(forks, renderer, file) {
let basePath = forkedFiles.get(file);
if (!basePath) {
basePath = findForks(file);
}
const baseFilename = file.slice(basePath.length + 1);
const parts = renderer.split('-');
while (parts.length) {
const candidate = `forks/${baseFilename}.${parts.join('-')}.js`;
if (allForks.has(candidate)) {
forks.set(candidate, `${baseFilename}$$`);
return;
}
parts.pop();
}
throw new Error(`Cannot find fork for ${file} for renderer ${renderer}`);
}
function writeConfig(
renderer,
rendererInfo,
isServerSupported,
isFlightSupported,
) {
const folder = __dirname + '/' + renderer;
mkdirp.sync(folder);
isFlightSupported =
isFlightSupported === true ||
(isServerSupported && isFlightSupported !== false);
const serverRenderer = isServerSupported ? renderer : 'custom';
const flightRenderer = isFlightSupported ? renderer : 'custom';
const ignoredPaths = [];
inlinedHostConfigs.forEach(otherRenderer => {
if (otherRenderer === rendererInfo) {
return;
}
otherRenderer.paths.forEach(otherPath => {
if (rendererInfo.paths.indexOf(otherPath) !== -1) {
return;
}
ignoredPaths.push(`.*/packages/${otherPath}`);
});
});
const forks = new Map();
addFork(forks, renderer, 'react-reconciler/src/ReactFiberConfig');
addFork(forks, serverRenderer, 'react-server/src/ReactServerStreamConfig');
addFork(forks, serverRenderer, 'react-server/src/ReactFizzConfig');
addFork(forks, flightRenderer, 'react-server/src/ReactFlightServerConfig');
addFork(forks, flightRenderer, 'react-client/src/ReactFlightClientConfig');
forks.set(
'react-devtools-shared/src/config/DevToolsFeatureFlags.default',
'react-devtools-feature-flags',
);
allForks.forEach(fork => {
if (!forks.has(fork)) {
ignoredPaths.push(`.*/packages/.*/${fork}`);
}
});
let moduleMappings = '';
forks.forEach((source, target) => {
moduleMappings += `module.name_mapper='${source.slice(
source.lastIndexOf('/') + 1,
)}' -> '${target}'\n`;
});
const config = configTemplate
.replace('%REACT_RENDERER_FLOW_OPTIONS%', moduleMappings.trim())
.replace('%REACT_RENDERER_FLOW_IGNORES%', ignoredPaths.join('\n'))
.replace('%FLOW_VERSION%', flowVersion);
const disclaimer = `
# ---------------------------------------------------------------#
# NOTE: this file is generated. #
# If you want to edit it, open ./scripts/flow/config/flowconfig. #
# Then run Yarn for changes to take effect. #
# ---------------------------------------------------------------#
`.trim();
const configFile = folder + '/.flowconfig';
let oldConfig;
try {
oldConfig = fs.readFileSync(configFile).toString();
} catch (err) {
oldConfig = null;
}
const newConfig = `
${disclaimer}
${config}
${disclaimer}
`.trim();
if (newConfig !== oldConfig) {
fs.writeFileSync(configFile, newConfig);
console.log(chalk.dim('Wrote a Flow config to ' + configFile));
}
}
// Write multiple configs in different folders
// so that we can run those checks in parallel if we want.
inlinedHostConfigs.forEach(rendererInfo => {
if (rendererInfo.isFlowTyped) {
writeConfig(
rendererInfo.shortName,
rendererInfo,
rendererInfo.isServerSupported,
rendererInfo.isFlightSupported,
);
}
});