-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeConfFile.ts
69 lines (44 loc) · 1.74 KB
/
writeConfFile.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
68
69
import * as fs from 'fs';
import {resolveConf} from './resolveConf';
import {getEnvArguments} from './getEnvArguments';
import {loadConfFromFiles} from './loadConfFromFiles';
import {mergeConfs} from './mergeConfs';
import {getConfDir} from './getConfDir';
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const indent = (depth: number) => Array(depth * 4).fill(' ').join('');
export async function writeConfFile(): Promise<void> {
const env = getEnvArguments();
const confSources = loadConfFromFiles(env);
const mergedConf = mergeConfs(confSources);
const defaultConfig = await resolveConf(mergedConf, {});
const filepath = `${getConfDir()}/Conf.d.ts`;
const ts = `
import {Conf} from "lambdaconf";
declare module "lambdaconf" {
export interface Conf = {
${props(defaultConfig)}
}
}
`;
console.log(`lambdaconf: writing ${filepath}`);
fs.writeFileSync(filepath, ts);
}
export function props(obj: {[key: string]: any}, depth: number = 3): string {
return Object.keys(obj).map((key) => {
switch (typeof obj[key]) {
case 'object':
if (Array.isArray(obj[key])) {
const type = typeof obj[key][0];
return `${indent(depth)}'${key}': ${type === 'undefined' ? 'any' : type}[]`;
} else {
return `${indent(depth)}'${key}': {\n${props(obj[key], depth + 1)}\n${indent(depth)}}`;
}
case 'boolean':
return `${indent(depth)}'${key}': boolean`;
case 'number':
return `${indent(depth)}'${key}': number`;
default:
return `${indent(depth)}'${key}': string`;
}
}).join('\n');
}