Skip to content

Commit

Permalink
feat: Adding support for safe dotenv compilation.
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
mrsteele committed Aug 4, 2016
1 parent 235f41a commit 47bf7ef
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { parse } from 'dotenv'
import dotenv from 'dotenv-safe'
import fs from 'fs'
import { DefinePlugin } from 'webpack'

class Dotenv {
constructor (options) {
options = options || {}
if (!options.path) options.path = './.env'
this.options = Object.assign({
path: './.env',
safe: false,
sample: './env.example'
}, options)

this.env = {}
this.blueprint = (this.safe) ? this.loadFile(this.options.sample) : this.loadFile(this.options.path)
this.env = this.loadFile(this.options.path)
}

loadFile (file) {
try {
this.env = parse(fs.readFileSync(options.path))
} catch (err) {}
return dotenv.parse(fs.readFileSync(file))
} catch (err) {
return {}
}
}

apply (compiler) {
const plugin = Object.keys(this.env).reduce((definitions, key) => {
const plugin = Object.keys(this.blueprint).reduce((definitions, key) => {
const value = process.env[key] || this.env[key]
if (!value) {
throw new Error(`Missing environment variable: ${key}`)
}
definitions[`process.env.${key}`] = JSON.stringify(value)
return definitions
}, {})
Expand Down

0 comments on commit 47bf7ef

Please sign in to comment.