Skip to content

Commit

Permalink
Merge pull request #84 from gfmartinez/allow-nonstart-for-specified-e…
Browse files Browse the repository at this point in the history
…nvironment

Add ability to shut off dotenv invocation
  • Loading branch information
jasonmit authored May 19, 2020
2 parents d5d94c0 + 233568a commit dd6cb1b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function(env) {
clientAllowedKeys: ['DROPBOX_KEY'],
// Fail build when there is missing any of clientAllowedKeys environment variables.
// By default false.
failOnMissingKey: false,
failOnMissingKey: false
};
};
```
Expand Down Expand Up @@ -106,7 +106,7 @@ when it runs in browser.

### Multiple Environments

Sometime people may want to use different `.env` file than the one in project root.
Sometimes people may want to use different `.env` file than the one in project root.
This can be configured as below:

```js
Expand Down Expand Up @@ -135,6 +135,24 @@ module.exports = function(env) {
With the above, if you run `ember build --environment production`, the file
`./path/to/.env-production` will be used instead.

### Environment Specific Use

Sometimes people may want to only initiate the dotenv file in one or more environments (e.g. not in production)
This can be configured as below:

```js
// config/dotenv.js
module.exports = function(env) {
return {
enabled: env !== 'production' // default is TRUE for any environment
};
};
```

When `enabled` is set to `false`, the dotenv protocol will not be used at all.
This is great for quieting errors and side issues when deploying with a service like Heroku,
where you can use environment variables set within the service and avoid storing a .env file in your repo.

## Compatibility

This addon supports the Ember 2.x series, but it is also backwards-compatible down to Ember-CLI 0.1.2 and Ember 1.7.0.
Expand Down
29 changes: 18 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
clientAllowedKeys: [],
fastbootAllowedKeys: [],
failOnMissingKey: false,
enabled: true
};

if (fs.existsSync(configFactory)) {
Expand All @@ -32,16 +33,18 @@ module.exports = {
this._config = options;
}

let loadedConfig = dotenv.config({path: options.path});
this._envConfig = loadedConfig.parsed;
if (this._config.enabled) {
let loadedConfig = dotenv.config({path: options.path});
this._envConfig = loadedConfig.parsed;

// It might happen that environment config is missing or corrupted
if (loadedConfig.error) {
let loadingErrMsg = `[ember-cli-dotenv]: ${loadedConfig.error.message}`;
if (options.failOnMissingKey) {
throw new Error(loadingErrMsg);
} else {
console.warn(loadingErrMsg); // eslint-disable-line no-console
// It might happen that environment config is missing or corrupted
if (loadedConfig.error) {
let loadingErrMsg = `[ember-cli-dotenv]: ${loadedConfig.error.message}`;
if (options.failOnMissingKey) {
throw new Error(loadingErrMsg);
} else {
console.warn(loadingErrMsg); // eslint-disable-line no-console
}
}
}
},
Expand All @@ -68,9 +71,13 @@ module.exports = {
},

config() {
let allowedKeys = this._config.clientAllowedKeys || [];
if (this._config.enabled) {
let allowedKeys = this._config.clientAllowedKeys || [];

return this._pickConfigKeys(allowedKeys);
}

return this._pickConfigKeys(allowedKeys);
return {};
},

/**
Expand Down

0 comments on commit dd6cb1b

Please sign in to comment.