forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.js
585 lines (532 loc) · 15.2 KB
/
flags.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
'use strict';
const babel = require('@babel/register');
const {transformSync} = require('@babel/core');
const Module = require('module');
const path = require('path');
const fs = require('fs');
babel({
plugins: ['@babel/plugin-transform-modules-commonjs'],
});
const yargs = require('yargs');
const argv = yargs
.parserConfiguration({
// Important: This option tells yargs to move all other options not
// specified here into the `_` key. We use this to send all of the
// Jest options that we don't use through to Jest (like --watch).
'unknown-options-as-args': true,
})
.wrap(yargs.terminalWidth())
.options({
csv: {
alias: 'c',
describe: 'output cvs.',
requiresArg: false,
type: 'boolean',
default: false,
},
diff: {
alias: 'd',
describe: 'output diff of two or more flags.',
requiresArg: false,
type: 'array',
choices: [
'www',
'www-modern',
'rn',
'rn-fb',
'rn-next',
'canary',
'next',
'experimental',
null,
],
default: null,
},
sort: {
alias: 's',
describe: 'sort diff by one or more flags.',
requiresArg: false,
type: 'string',
default: 'flag',
choices: [
'flag',
'www',
'www-modern',
'rn',
'rn-fb',
'rn-next',
'canary',
'next',
'experimental',
],
},
cleanup: {
describe: 'output flags by cleanup category.',
requiresArg: false,
type: 'boolean',
default: false,
},
}).argv;
// Load ReactFeatureFlags with __NEXT_MAJOR__ replaced with 'next'.
// We need to do string replace, since the __NEXT_MAJOR__ is assigned to __EXPERIMENTAL__.
function getReactFeatureFlagsMajor() {
const virtualName = 'ReactFeatureFlagsMajor.js';
const file = fs.readFileSync(
path.join(__dirname, '../../packages/shared/ReactFeatureFlags.js'),
'utf8'
);
const fileContent = transformSync(
file.replace(
'const __NEXT_MAJOR__ = __EXPERIMENTAL__;',
'const __NEXT_MAJOR__ = "next";'
),
{
plugins: ['@babel/plugin-transform-modules-commonjs'],
}
).code;
const parent = module.parent;
const m = new Module(virtualName, parent);
m.filename = virtualName;
m._compile(fileContent, virtualName);
return m.exports;
}
// Load RN ReactFeatureFlags with __NEXT_RN_MAJOR__ replaced with 'next'.
// We need to do string replace, since the __NEXT_RN_MAJOR__ is assigned to false.
function getReactNativeFeatureFlagsMajor() {
const virtualName = 'ReactNativeFeatureFlagsMajor.js';
const file = fs.readFileSync(
path.join(
__dirname,
'../../packages/shared/forks/ReactFeatureFlags.native-oss.js'
),
'utf8'
);
const fileContent = transformSync(
file
.replace(
'const __NEXT_RN_MAJOR__ = true;',
'const __NEXT_RN_MAJOR__ = "next";'
)
.replace(
'const __TODO_NEXT_RN_MAJOR__ = false;',
'const __TODO_NEXT_RN_MAJOR__ = "next-todo";'
),
{
plugins: ['@babel/plugin-transform-modules-commonjs'],
}
).code;
const parent = module.parent;
const m = new Module(virtualName, parent);
m.filename = virtualName;
m._compile(fileContent, virtualName);
return m.exports;
}
// The RN and www Feature flag files import files that don't exist.
// Mock the imports with the dynamic flag values.
function mockDynamicallyFeatureFlags() {
// Mock the ReactNativeInternalFeatureFlags and ReactFeatureFlags modules
const DynamicFeatureFlagsWWW = require('../../packages/shared/forks/ReactFeatureFlags.www-dynamic.js');
const DynamicFeatureFlagsNative = require('../../packages/shared/forks/ReactFeatureFlags.native-fb-dynamic.js');
const originalLoad = Module._load;
Module._load = function (request, parent) {
if (request === 'ReactNativeInternalFeatureFlags') {
return DynamicFeatureFlagsNative;
} else if (request === 'ReactFeatureFlags') {
return DynamicFeatureFlagsWWW;
}
return originalLoad.apply(this, arguments);
};
}
// Set the globals to string values to output them to the table.
global.__VARIANT__ = 'gk';
global.__PROFILE__ = 'profile';
global.__DEV__ = 'dev';
global.__EXPERIMENTAL__ = 'experimental';
// Load all the feature flag files.
mockDynamicallyFeatureFlags();
const ReactFeatureFlags = require('../../packages/shared/ReactFeatureFlags.js');
const ReactFeatureFlagsWWW = require('../../packages/shared/forks/ReactFeatureFlags.www.js');
const ReactFeatureFlagsNativeFB = require('../../packages/shared/forks/ReactFeatureFlags.native-fb.js');
const ReactFeatureFlagsMajor = getReactFeatureFlagsMajor();
const ReactNativeFeatureFlagsMajor = getReactNativeFeatureFlagsMajor();
const allFlagsUniqueFlags = Array.from(
new Set([
...Object.keys(ReactFeatureFlags),
...Object.keys(ReactFeatureFlagsWWW),
...Object.keys(ReactFeatureFlagsNativeFB),
])
).sort();
// These functions are the rules for what each value means in each channel.
function getNextMajorFlagValue(flag) {
const value = ReactFeatureFlagsMajor[flag];
if (value === true || value === 'next') {
return '✅';
} else if (value === false || value === null || value === 'experimental') {
return '❌';
} else if (value === 'profile') {
return '📊';
} else if (value === 'dev') {
return '💻';
} else if (typeof value === 'number') {
return value;
} else {
throw new Error(`Unexpected OSS Stable value ${value} for flag ${flag}`);
}
}
function getOSSCanaryFlagValue(flag) {
const value = ReactFeatureFlags[flag];
if (value === true) {
return '✅';
} else if (
value === false ||
value === null ||
value === 'experimental' ||
value === 'next'
) {
return '❌';
} else if (value === 'profile') {
return '📊';
} else if (value === 'dev') {
return '💻';
} else if (typeof value === 'number') {
return value;
} else {
throw new Error(`Unexpected OSS Canary value ${value} for flag ${flag}`);
}
}
function getOSSExperimentalFlagValue(flag) {
const value = ReactFeatureFlags[flag];
if (value === true || value === 'experimental') {
return '✅';
} else if (value === false || value === null || value === 'next') {
return '❌';
} else if (value === 'profile') {
return '📊';
} else if (value === 'dev') {
return '💻';
} else if (typeof value === 'number') {
return value;
} else {
throw new Error(
`Unexpected OSS Experimental value ${value} for flag ${flag}`
);
}
}
function getWWWModernFlagValue(flag) {
const value = ReactFeatureFlagsWWW[flag];
if (value === true || value === 'experimental') {
return '✅';
} else if (value === false || value === null || value === 'next') {
return '❌';
} else if (value === 'profile') {
return '📊';
} else if (value === 'dev') {
return '💻';
} else if (value === 'gk') {
return '🧪';
} else if (typeof value === 'number') {
return value;
} else {
throw new Error(`Unexpected WWW Modern value ${value} for flag ${flag}`);
}
}
function getWWWClassicFlagValue(flag) {
const value = ReactFeatureFlagsWWW[flag];
if (value === true) {
return '✅';
} else if (
value === false ||
value === null ||
value === 'experimental' ||
value === 'next'
) {
return '❌';
} else if (value === 'profile') {
return '📊';
} else if (value === 'dev') {
return '💻';
} else if (value === 'gk') {
return '🧪';
} else if (typeof value === 'number') {
return value;
} else {
throw new Error(`Unexpected WWW Classic value ${value} for flag ${flag}`);
}
}
function getRNNextMajorFlagValue(flag) {
const value = ReactNativeFeatureFlagsMajor[flag];
if (value === true || value === 'next') {
return '✅';
} else if (value === 'next-todo') {
return '📋';
} else if (value === false || value === null || value === 'experimental') {
return '❌';
} else if (value === 'profile') {
return '📊';
} else if (value === 'dev') {
return '💻';
} else if (value === 'gk') {
return '🧪';
} else if (typeof value === 'number') {
return value;
} else {
throw new Error(`Unexpected RN OSS value ${value} for flag ${flag}`);
}
}
function getRNOSSFlagValue(flag) {
const value = ReactNativeFeatureFlagsMajor[flag];
if (value === true) {
return '✅';
} else if (
value === false ||
value === null ||
value === 'experimental' ||
value === 'next' ||
value === 'next-todo'
) {
return '❌';
} else if (value === 'profile') {
return '📊';
} else if (value === 'dev') {
return '💻';
} else if (value === 'gk') {
return '🧪';
} else if (typeof value === 'number') {
return value;
} else {
throw new Error(`Unexpected RN OSS value ${value} for flag ${flag}`);
}
}
function getRNFBFlagValue(flag) {
const value = ReactFeatureFlagsNativeFB[flag];
if (value === true) {
return '✅';
} else if (
value === false ||
value === null ||
value === 'experimental' ||
value === 'next'
) {
return '❌';
} else if (value === 'profile') {
return '📊';
} else if (value === 'dev') {
return '💻';
} else if (value === 'gk') {
return '🧪';
} else if (typeof value === 'number') {
return value;
} else {
throw new Error(`Unexpected RN FB value ${value} for flag ${flag}`);
}
}
function argToHeader(arg) {
switch (arg) {
case 'www':
return 'WWW Classic';
case 'www-modern':
return 'WWW Modern';
case 'rn':
return 'RN OSS';
case 'rn-fb':
return 'RN FB';
case 'rn-next':
return 'RN Next Major';
case 'canary':
return 'OSS Canary';
case 'next':
return 'OSS Next Major';
case 'experimental':
return 'OSS Experimental';
default:
return arg;
}
}
const FLAG_CONFIG = {
'OSS Next Major': getNextMajorFlagValue,
'OSS Canary': getOSSCanaryFlagValue,
'OSS Experimental': getOSSExperimentalFlagValue,
'WWW Classic': getWWWClassicFlagValue,
'WWW Modern': getWWWModernFlagValue,
'RN FB': getRNFBFlagValue,
'RN OSS': getRNOSSFlagValue,
'RN Next Major': getRNNextMajorFlagValue,
};
const FLAG_COLUMNS = Object.keys(FLAG_CONFIG);
const INTERNAL_VARIANTS = ['WWW Classic', 'WWW Modern', 'RN FB'];
const OSS_VARIANTS = [
'OSS Next Major',
'OSS Canary',
'OSS Experimental',
'RN OSS',
'RN Next Major',
];
// Build the table with the value for each flag.
function buildTable(filterFn) {
const isDiff = argv.diff != null && argv.diff.length > 1;
const table = {};
const filteredFlags = filterFn
? allFlagsUniqueFlags.filter(filterFn)
: allFlagsUniqueFlags;
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const flag of filteredFlags) {
const values = FLAG_COLUMNS.reduce((acc, key) => {
acc[key] = FLAG_CONFIG[key](flag);
return acc;
}, {});
if (!isDiff) {
table[flag] = values;
continue;
}
const subset = argv.diff.map(argToHeader).reduce((acc, key) => {
if (key in values) {
acc[key] = values[key];
}
return acc;
}, {});
if (new Set(Object.values(subset)).size !== 1) {
table[flag] = subset;
}
}
// Sort the table
let sorted = table;
if (isDiff || argv.sort) {
const sortChannel = argToHeader(isDiff ? argv.diff[0] : argv.sort);
const sortBy =
sortChannel === 'flag'
? ([flagA], [flagB]) => {
return flagA.localeCompare(flagB);
}
: ([, rowA], [, rowB]) => {
return rowB[sortChannel]
.toString()
.localeCompare(rowA[sortChannel]);
};
sorted = Object.fromEntries(Object.entries(table).sort(sortBy));
}
return sorted;
}
function formatTable(tableData) {
// left align the flag names.
const maxLength = Math.max(
...Object.keys(tableData).map(item => item.length)
);
const padded = {};
Object.keys(tableData).forEach(key => {
const newKey = key.padEnd(maxLength, ' ');
padded[newKey] = tableData[key];
});
return padded;
}
if (argv.csv) {
const table = buildTable();
const csvRows = [
`Flag name, ${FLAG_COLUMNS.join(', ')}`,
...Object.keys(table).map(flag => {
const row = table[flag];
return `${flag}, ${FLAG_COLUMNS.map(col => row[col]).join(', ')}`;
}),
];
fs.writeFile('./flags.csv', csvRows.join('\n'), function (err) {
if (err) {
return console.log(err);
}
console.log('The file was saved to ./flags.csv');
});
}
if (argv.cleanup) {
const allPassingFlags = [];
const allFailingFlags = [];
const needsShippedExperimentFlags = [];
const earlyExperimentationFlags = [];
const internalOnlyFlags = [];
const diffedFlagColumns =
argv.diff[0] != null ? argv.diff.map(argToHeader) : FLAG_COLUMNS;
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const flag of allFlagsUniqueFlags) {
const values = diffedFlagColumns.reduce((acc, key) => {
acc[key] = FLAG_CONFIG[key](flag);
return acc;
}, {});
const uniqueValues = new Set(Object.values(values));
if (
uniqueValues.size === 1 &&
(uniqueValues.has('✅') ||
typeof uniqueValues.values().next().value === 'number')
) {
allPassingFlags.push(flag);
}
if (uniqueValues.size === 1 && uniqueValues.has('❌')) {
allFailingFlags.push(flag);
}
const internalVariantValues = INTERNAL_VARIANTS.filter(value =>
diffedFlagColumns.includes(value)
).map(v => values[v]);
const ossVariantValues = OSS_VARIANTS.filter(value =>
diffedFlagColumns.includes(value)
).map(v => values[v]);
if (
internalVariantValues.some(v => v === '✅') &&
ossVariantValues.every(v => v === '❌')
) {
internalOnlyFlags.push(flag);
}
if (
internalVariantValues.some(v => v === '🧪') &&
(ossVariantValues.every(v => v === '❌') ||
(ossVariantValues.some(v => v === '❌') &&
values['OSS Experimental'] === '✅'))
) {
earlyExperimentationFlags.push(flag);
}
if (
internalVariantValues.some(v => v === '🧪' || v === '❌') &&
ossVariantValues.every(v => v === '✅')
) {
needsShippedExperimentFlags.push(flag);
}
}
if (allPassingFlags.length > 0) {
console.log('ALL VARIANTS PASS (✅)');
console.table(
formatTable(buildTable(flag => allPassingFlags.includes(flag)))
);
}
if (allFailingFlags.length > 0) {
console.log('ALL VARIANTS FAIL (❌)');
console.table(
formatTable(buildTable(flag => allFailingFlags.includes(flag)))
);
}
if (internalOnlyFlags.length > 0) {
console.log('INTERNAL ONLY (✅)');
console.table(
formatTable(buildTable(flag => internalOnlyFlags.includes(flag)))
);
}
if (earlyExperimentationFlags.length > 0) {
console.log('WAITING ON RESULTS (🧪)');
console.table(
formatTable(buildTable(flag => earlyExperimentationFlags.includes(flag)))
);
}
if (needsShippedExperimentFlags.length > 0) {
console.log('WAITING ON ROLLOUT (🧪)');
console.table(
formatTable(
buildTable(flag => needsShippedExperimentFlags.includes(flag))
)
);
}
} else {
console.table(formatTable(buildTable()));
}
console.log(`
Legend:
✅ On
❌ Off
💻 DEV
📋 TODO
📊 Profiling
🧪 Experiment
`);