-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·43 lines (35 loc) · 976 Bytes
/
index.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
"use strict";
const { constantCase } = require("constant-case");
const readWholeStdin = () => {
process.stdin.resume();
process.stdin.setEncoding("utf8");
let buffer = "";
process.stdin.on("data", (chunk) => {
buffer += chunk;
});
const deferred = new Promise((resolve) => {
process.stdin.on("end", () => {
resolve(buffer);
});
});
return deferred;
};
const writeToStdout = (buffer) => {
process.stdout.write(buffer);
};
const templatifyJson = (buffer, prefix) => {
const json = JSON.parse(buffer);
const elements = []
Object.keys(json).forEach((key) => {
const envexpr = `env "${prefix + constantCase(key)}"`;
const val = json[key];
const template = `{{ if ${envexpr} }}"{{ ${envexpr} | js }}"{{ else }}${JSON.stringify(val)}{{ end }}`;
elements.push(`${JSON.stringify(key)}:${template}`)
});
return `{${elements.join(",")}}`;
};
module.exports = {
readWholeStdin,
writeToStdout,
templatifyJson,
};