import colors from 'colors' import * as dotenv from 'dotenv' import fs from 'fs' import { IndexState } from '../types' import { errorMessage, isBoolean } from './helpers' dotenv.config() export const CreateEnvFile = async (state: IndexState) => { console.info(colors.green('Creating Env File...')) const envFile = fs.readFileSync('./.env.example', 'utf8') const lines = envFile.split('\n') let fileContents = '' const prefix: string = state.args[1] lines.forEach((lineText) => { if ((lineText.split('')[0] !== '#') && (lineText !== '')) { const lineSpit = lineText.split('=') const key = lineSpit[0] if (!process.env[key]) { errorMessage(key) process.exit(1) } let value: string = process.env[key]! if (prefix) { value = `${process.env[`${prefix}_${key}`]}` } if (!isBoolean(value) && Number.isNaN(Number(value))) { fileContents += `${key}="${value}"\n` } else { fileContents += `${key}=${value}\n` } } else { // Ignore the line. It's a comment or blank line } }) fs.writeFileSync('./.env', `${fileContents.trim()}\n`) console.info(colors.green('.env file complete.')) return fileContents } export default CreateEnvFile