Skip to content

Commit

Permalink
Add support for .env for storing private data
Browse files Browse the repository at this point in the history
Create .env file from template if it doesn't exist
Import items from .env using dotenv - good for storing private data
  • Loading branch information
edwardhorsford committed Apr 28, 2017
1 parent cc5ae41 commit 4186bbe
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
.sass-cache
.DS_Store
.start.pid
Expand Down
20 changes: 20 additions & 0 deletions lib/template.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# =========================================
# WARNING: Don't commit this file to git
# =========================================
#
# Use this file for storing private data - things like API keys and admin credentials
#
# Storing data:
#
# EXAMPLE_API_KEY=123456789
# JSON={"foo": "bar"}
#
# More examples here: https://www.npmjs.com/package/dotenv
#
# Use the data in your app:
#
# var key = process.env.EXAMPLE_API_KEY

# =========================================
# INSERT YOUR DATA HERE:

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"body-parser": "^1.14.1",
"browser-sync": "^2.11.1",
"cross-spawn": "^5.0.0",
"dotenv": "^4.0.0",
"express": "4.13.3",
"express-session": "^1.13.0",
"express-writer": "0.0.4",
Expand Down
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config()
var crypto = require('crypto')
var path = require('path')
var express = require('express')
Expand Down
12 changes: 11 additions & 1 deletion start.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
var path = require('path')
var fs = require('fs')

if (!fs.existsSync(path.join(__dirname, '/node_modules'))) {
// Check if node_modules folder exists
const nodeModulesExists = fs.existsSync(path.join(__dirname, '/node_modules'))
if (!nodeModulesExists) {
console.error('ERROR: Node module folder missing. Try running `npm install`')
process.exit(0)
}

// Create template .env file if it doesn't exist
const envExists = fs.existsSync(path.join(__dirname, '/.env'))
if (!envExists) {
console.log('Creating template .env file')
fs.createReadStream(path.join(__dirname, '/lib/template.env'))
.pipe(fs.createWriteStream(path.join(__dirname, '/.env')))
}

// run gulp

var spawn = require('cross-spawn')
Expand Down

0 comments on commit 4186bbe

Please sign in to comment.