Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Modify env file on the fly
Browse files Browse the repository at this point in the history
This closes #52
  • Loading branch information
leo committed May 22, 2016
1 parent ddf4898 commit 51c04d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
32 changes: 29 additions & 3 deletions lib/tasks/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { exec } from 'child_process'
import { log } from '../utils'
import Mongonaut from 'mongonaut'
import Insert from './insert'
import dotenv from 'dotenv'

class Generator {
constructor (answers, targetDir) {
Expand Down Expand Up @@ -124,10 +125,35 @@ class Generator {
}

if (filePath.name == '.env') {
var contents = fs.createReadStream(file),
target = fs.createWriteStream(dest)
var contents = false

try {
contents = fs.readFileSync(file, 'utf8')
} catch (err) {
return log(err)
}

// Generate random session secret
process.env.SESSION_SECRET = Math.random().toString(36).substr(2, 20)

var parsed = dotenv.parse(new Buffer(contents)),
newVariables = {}

for (var property in parsed) {
var fromProcess = process.env[property]

if (fromProcess) {
var prefix = property + '='
contents = contents.replace(prefix + parsed[property], prefix + fromProcess)
}
}

try {
fs.writeFileSync(dest, contents)
} catch (err) {
return log(err)
}

contents.pipe(target)
continue
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"broccoli": "^0.16.9",
"broccoli-sane-watcher": "^1.1.4",
"chalk": "^1.1.3",
"dotenv": "^2.0.0",
"fs-extra": "^0.30.0",
"git-config": "0.0.7",
"inquirer": "^1.0.1",
Expand Down

0 comments on commit 51c04d4

Please sign in to comment.