-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
executable file
·69 lines (59 loc) · 1.45 KB
/
index.mjs
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
#!/usr/bin/env node
"use strict";
import commandLineArgs from "command-line-args";
import fs from "fs/promises";
async function main() {
const options = commandLineArgs([
{
name: "env",
alias: "e",
type: String,
multiple: true,
defaultValue: [],
},
{
name: "yaml",
alias: "y",
type: String,
},
{
name: "out",
alias: "o",
type: String,
defaultValue: "out.yml",
},
]);
const envs = await Promise.all(
options.env.map(async (filename) => {
console.log(filename);
const contents = await fs.readFile(filename);
const file = contents.toString();
const env = {};
file.replace(/(\w+)=(.+)/g, function ($0, $1, $2) {
env[$1] = $2;
});
return env;
})
);
const env = envs.reduce((acc, cur) => ({ ...acc, ...cur }), {});
const ymlBuffer = await fs.readFile(options.yaml);
const yml = ymlBuffer
.toString()
.replace(/\$\{(\w+)(?::-?(.*))?}/g, ($0, $1, $2) => {
let envValue = env[$1];
if (!envValue) {
if (!$2) {
console.log(
`Error interpreting ${$1} - Neither provided with a default value or covered by env - using default value`
);
return "";
}
envValue = $2;
}
return envValue;
});
await fs.writeFile(options.out, yml);
}
await main()
.then(() => console.log("\nCompleted!"))
.catch((e) => console.error(e));