Closed brianjhanson closed 7 years ago
We kind of have that in /gulp/config.js
its just not really intended to be outside of version control, and most of the things in it you wouldn't want to change from setup to setup. I get what you're saying.
I just implemented https://www.npmjs.com/package/dotenv on a node project earlier today, was super easy, and worked great.
Can we use our existing .env
file with that?!
@brianjhanson that was my first thought too, and then the more I thought about it the more it seemed like, "sure why not". It's just a text file, and it's up to libraries (called dotenv
in the case of both PHP and Node) to pull the contents of that file in and do something with it.
@brianjhanson in the specific case of the browserync proxy
value, we usually already have that value in our .env
file as APP_SITE_URL
!!!!
😮
Looks like this is as simple as
let path = require ('path');
let fs = require('fs');
// Get our .env file
let envVars = fs.readFileSync(path.resolve(__dirname, "../.env"), "utf8");
// Create an empty object to store our variables
let env = {};
// Turn our .env file into an object.
envVars.replace(/(\w+)="(.+)"/g, function($0, $1, $2) { env[$1] = $2 });
That will turn our .env
file into an object. Then we can set proxy = env.APP_SITE_URL.
Using this on Belgravia with no ill effects, but just documenting here so I don't forget about it
A more robust regex (because it's possible to not specify quotes):
envVars.replace(/(\w+)="?([^"\n]+)"?/g, function($0, $1, $2) { env[$1] = $2 });
Could we put this behind a module or something? e.g. var env = require('./utils/get_env')
Otherwise would definitely get gross having all of this boilerplate code in even one of our gulp files, let alone multiple files.
Alternatively, as mentioned before, this module is dead simple to use: https://www.npmjs.com/package/dotenv
I vote for using dotenv! Thanks @cmalven.
I feel like we should make a
.env
-y thing for our gulp config so we can set things like proxys and credentials for things like AWS in there without putting them into the repo.