-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathknown-feature-flag-constants.ts
More file actions
87 lines (80 loc) · 2.63 KB
/
known-feature-flag-constants.ts
File metadata and controls
87 lines (80 loc) · 2.63 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
/**
* Known Feature Flag Constants — maps constant names to resolved flag strings.
* Used by check-feature-flag-registry.ts for bracket-access like `remoteFeatureFlags[CONSTANT]`.
*
* Enum values are imported directly. UI constants are resolved from source files
* (importing them would pull in browser APIs). Add new constants here when the
* CI job reports an "unresolved constant" error.
*/
import * as fs from 'fs';
import * as path from 'path';
import { FeatureFlagNames } from '../../shared/lib/feature-flags';
/** Auto-populated from the FeatureFlagNames enum. Key = `FeatureFlagNames.Member`. */
const DIRECT_IMPORTS: Record<string, string> = Object.fromEntries(
Object.entries(FeatureFlagNames).map(([k, v]) => [`FeatureFlagNames.${k}`, v]),
);
/**
* Constants that must be resolved by reading their source file (because
* importing them would pull in browser-only dependencies).
*/
const FILE_SOURCES: Array<{
key: string;
file: string;
exportName: string;
}> = [
{
key: 'ASSETS_UNIFY_STATE_FLAG',
file: 'shared/lib/assets-unify-state/remote-feature-flag.ts',
exportName: 'ASSETS_UNIFY_STATE_FLAG',
},
{
key: 'STATE_1_FLAG',
file: 'ui/selectors/multichain-accounts/feature-flags.ts',
exportName: 'STATE_1_FLAG',
},
{
key: 'STATE_2_FLAG',
file: 'ui/selectors/multichain-accounts/feature-flags.ts',
exportName: 'STATE_2_FLAG',
},
{
key: 'MERKL_FEATURE_FLAG_KEY',
file: 'ui/components/app/musd/constants.ts',
exportName: 'MERKL_FEATURE_FLAG_KEY',
},
];
/**
* Reads a source file and extracts the string value of an exported constant.
* Matches patterns like: `export const NAME = 'value';`
*/
function resolveConstantFromFile(
filePath: string,
constantName: string,
): string | undefined {
try {
const fullPath = path.resolve(filePath);
const content = fs.readFileSync(fullPath, 'utf-8');
const escaped = constantName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp(
`export\\s+const\\s+${escaped}(?:\\s*:[^=]+)?\\s*=\\s*(?:'([^']+)'|"([^"]+)"|` + '`([^`]+)`)',
);
const match = re.exec(content);
return match?.[1] ?? match?.[2] ?? match?.[3];
} catch {
return undefined;
}
}
/**
* Builds and returns the complete mapping of constant expressions to
* their resolved flag name strings.
*/
export function buildKnownFlagConstants(): Record<string, string> {
const constants: Record<string, string> = { ...DIRECT_IMPORTS };
for (const { key, file, exportName } of FILE_SOURCES) {
const resolved = resolveConstantFromFile(file, exportName);
if (resolved) {
constants[key] = resolved;
}
}
return constants;
}