-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (53 loc) · 1.63 KB
/
index.ts
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
#!/usr/bin/env node
const {readFileSync, writeFileSync} = require('fs');
const ENV_FILES: string[] = process.argv.slice(2);
const KEY_GROUP_INDEX = 1;
const VALUE_GROUP_INDEX = 2;
type AggregatedValues = {
[key: string]: string[];
};
/**
* Generates TypeScript types declaration for env files
*
* Inspired by https://dev.to/osamaq/react-native-generating-typescript-types-for-environment-variables-141d
* and https://regex101.com/r/cbm5Tp/1 (from https://github.com/luggit/react-native-config)
*/
const generate = (): void => {
// READ ALL ENV FILES
const envs = ENV_FILES.map(envFile =>
readFileSync(envFile, {
encoding: 'ascii',
}),
);
const aggregatedValues: AggregatedValues = {};
// RETRIEVE KEY AND VALUE FOR EACH ENV FILE
envs.forEach(env => {
const matches = [...env.matchAll(/^(.*?)= *(.*?)$/gm)];
for (let match of matches) {
const key = match[KEY_GROUP_INDEX];
const value = match[VALUE_GROUP_INDEX];
const previousValues = aggregatedValues[key] ?? [];
// APPEND THE CURRENT VALUE TO EXISTING ONES IF EXISTS
aggregatedValues[key] = previousValues.includes(value)
? previousValues
: [...previousValues, value];
}
});
// FORMAT THE TYPE VALUES
const joined = Object.entries(aggregatedValues)
.map(
([key, values]) =>
`${key}: ${values.map(value => `'${value}'`).join(' | ')}`,
)
.join('\n ');
const typeDeclaration = `declare module 'react-native-config' {
interface Env {
${joined};
}
const Config: Env;
export default Config;
}
`;
writeFileSync('env.d.ts', typeDeclaration, 'utf8');
};
generate();