-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
44 lines (37 loc) · 1.24 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
#!/usr/bin/env node
// biome-ignore lint/style/useNodejsImportProtocol: <explanation>
import fs from "fs"
import dotenv from "dotenv"
import { getArg, getArgs } from "./utils/getArgs"
const inputArgs = getArgs(["--input", "-input", "--i", "-i"])
const outputArgs = getArgs(["--output", "-output", "--o", "-o"])
const templateArg = getArg(["--template", "-template", "--t", "-t"])
const envPaths = inputArgs ?? [".env"]
const outputPaths = outputArgs ?? ["env.d.ts"]
const template =
templateArg ??
`${process.argv[1]!.replace(".bin", "").replace(
/\/gen-env-dts\/.*/g,
"/gen-env-dts"
)}/template.d.ts`
const templateString = fs.readFileSync(template, "utf-8")
for (const index in envPaths) {
// .env file path
const envPath = envPaths[index]!
// ['VAR1', 'VAR2', ...]
const envKeys = Object.keys(dotenv.config({ path: envPath })?.parsed ?? {})
// generate env.d.ts string
const dtsString = templateString.replace(
/(\s*?)TEMPLATE_REPLACE/,
envKeys.map((k) => `$1${k}: string`).join("")
)
// get output file path
const outputPath =
outputPaths[index] ??
(() => {
const output = envPath.startsWith(".") ? envPath.slice(1) : envPath
return `${output}.d.ts`
})()
// write env.d.ts file
fs.writeFileSync(outputPath, dtsString)
}