-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
32 lines (26 loc) · 966 Bytes
/
main.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const { parseEnvFile } = require('./envParser')
const { generateTypeDeclarations } = require('./typeGenerator')
const { getArgumentValue } = require('./cliHelper')
try {
const args = process.argv.slice(2)
const envFilePath = getArgumentValue(args, '--env-file') || (fs.existsSync('.env') ? '.env' : null)
const outputFilePath = getArgumentValue(args, '--output') || 'process-env.d.ts'
if (!envFilePath) {
throw new Error('No .env file found and no --env-file argument provided.')
}
const dryRun = args.includes('--dry-run')
const envVars = parseEnvFile(path.resolve(envFilePath))
const output = generateTypeDeclarations(envVars)
if (dryRun) {
console.log(output)
} else {
fs.writeFileSync(outputFilePath, output, 'utf-8')
console.log(`Generated ${outputFilePath}`)
}
} catch (error) {
console.error(`Error: ${error.message}`)
process.exit(1)
}