-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
70 lines (61 loc) · 1.75 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
68
69
70
import mustache from "mustache";
import * as path from "path";
import { promises as fs } from "fs";
const { writeFile, mkdir } = fs;
export interface ConfigData {
[key: string]: boolean | string | number;
}
export interface Options {
srcDir: string;
moduleName: string;
}
const defaultOptions = {
srcDir: "src",
moduleName: "BuildConfig"
};
export async function createConfigFile(
configuration: ConfigData,
options: Options = defaultOptions
) {
const fileOptions = { ...defaultOptions, ...options };
const config = buildConfigList(configuration);
const outFile = path.join(
fileOptions.srcDir,
fileOptions.moduleName.replaceAll(".", "/") + ".elm"
);
const outDir = path.dirname(outFile);
const output = mustache.render(template, {
config: config,
file: fileOptions
});
await mkdir(outDir, { recursive: true });
await writeFile(outFile, output);
}
function buildConfigList(configuration: ConfigData) {
return Object.entries(configuration)
.map(([key, value]) => {
let configType = typeof value;
if (configType == "boolean") {
return { key: key, elmType: "Bool", value: value ? "True" : "False " };
} else if (configType == "string") {
return { key: key, elmType: "String", value: `"${value}"` };
} else if (configType == "number") {
const elmType = Number.isInteger(value) ? "Int" : "Float";
return { key: key, elmType: elmType, value: value };
} else {
throw new Error(
`Unsupported Elm config type '${configType}' @ '${key}'`
);
}
})
.filter(item => {
return item !== null;
});
}
const template = `
module {{file.moduleName}} exposing (..)
{{#config}}
{{key}}: {{elmType}}
{{key}} = {{&value}}
{{/config}}
`.trim();