Skip to content

Commit

Permalink
✨ Add dotenv file hierarchy (#200)
Browse files Browse the repository at this point in the history
* ✨ Add dotenv file hierarchy

* ✨ Allow disabling dotenv files

* 🔖 v3.8.0
  • Loading branch information
Wil Wilsman committed May 27, 2020
1 parent e6b1f77 commit d8df115
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "percy-client",
"version": "3.7.0",
"version": "3.8.0",
"description": "JavaScript API client library for Percy (https://percy.io).",
"main": "dist/main.js",
"scripts": {
Expand Down
22 changes: 22 additions & 0 deletions src/dotenv.js
@@ -0,0 +1,22 @@
const dotenv = require('dotenv');

// mimic dotenv-rails file hierarchy
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
export function config() {
let {NODE_ENV: env, PERCY_DISABLE_DOTENV: disable} = process.env;

// don't load dotenv files when disabled
if (disable) return;

let paths = [
env && `.env.${env}.local`,
// .env.local is not loaded in test environments
env === 'test' ? null : '.env.local',
env && `.env.${env}`,
'.env',
].filter(Boolean);

for (let path of paths) {
dotenv.config({path});
}
}
2 changes: 1 addition & 1 deletion src/main.js
Expand Up @@ -9,7 +9,7 @@ const PromisePool = require('es6-promise-pool');
const regeneratorRuntime = require('regenerator-runtime'); // eslint-disable-line no-unused-vars
const fs = require('fs');

require('dotenv').config();
require('./dotenv').config();

const RETRY_ERROR_CODES = ['ECONNRESET', 'ECONNREFUSED', 'EPIPE', 'EHOSTUNREACH', 'EAI_AGAIN'];
const JSON_API_CONTENT_TYPE = 'application/vnd.api+json';
Expand Down

0 comments on commit d8df115

Please sign in to comment.