Skip to content

Commit

Permalink
Merge pull request #33 from jasonmit/u/jasonmit/2.0.0
Browse files Browse the repository at this point in the history
Initial commit for ember-cli 2.15.0 support
  • Loading branch information
SergeAstapov authored Oct 25, 2017
2 parents 36f82a2 + 1f5c86c commit f464ddf
Show file tree
Hide file tree
Showing 10 changed files with 5,268 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
language: node_js
node_js:
- "0.12"
- "4.5.0"

sudo: false

Expand Down
77 changes: 33 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
[![Build Status](https://travis-ci.org/fivetanley/ember-cli-dotenv.svg?branch=master)](https://travis-ci.org/fivetanley/ember-cli-dotenv)
[![npm version](https://badge.fury.io/js/ember-cli-dotenv.svg)](https://badge.fury.io/js/ember-cli-dotenv)
[![Ember Observer Score](https://emberobserver.com/badges/ember-cli-dotenv.svg)](http://emberobserver.com/addons/ember-cli-dotenv)

# Ember CLI Dotenv

# Installation

`ember install ember-cli-dotenv`

# Upgrading to 2.0.0

* `ember install ember-cli-dotenv@^2.0.0`
* open `dotenv.js` and `ember-cli-build.js`
* Move/convert the `dotEnv` application options from `ember-cli-build.js` to the function declared within `dotenv.js`
* NOTE: if your `path` is dynamic see [Multiple Environments](https://github.com/fivetanley/ember-cli-dotenv#multiple-environments)

# What is Ember CLI Dotenv?

This addon allows you to write environment variables in a `.env` file and
Expand All @@ -19,31 +27,26 @@ file in the root of your repository:
DROPBOX_KEY=YOURKEYGOESHERE
```

Next, put some configuration in your `ember-cli-build.js`. Starting in 0.2.0, *client side keys must be explicitly allowed*:
Next, configure `dotenv.js`.

```javascript
// ember-cli-build.js

module.exports = function(defaults) {
var app = new EmberApp(defaults, {
dotEnv: {
clientAllowedKeys: ['DROPBOX_KEY']
}
});

return app.toTree();
```js
// dotenv.js
module.exports = function(env) {
return {
clientAllowedKeys: ['DROPBOX_KEY']
};
};
```

*All keys in `.env` are currently injected into node’s `process.env`.*
These will be available in your `config/environment.js` file:

```javascript
```js
// config/environment.js
module.exports = function(environment){
module.exports = function(environment) {
return {
MY_OTHER_KEY: process.env.MY_OTHER_KEY
}
};
};
```

Expand All @@ -59,7 +62,7 @@ be exposed publicly via ember's `<meta name="app/config/environment">` tag.**
then, you can access the environment variables anywhere in your app like
you usually would.

```javascript
```js
import ENV from "my-app/config/environment";

console.log(ENV.DROPBOX_KEY); // logs YOURKEYGOESHERE
Expand All @@ -76,40 +79,26 @@ ruby implementation.
Sometime people may want to use different `.env` file than the one in project root.
This can be configured as below:

```javascript
// ember-cli-build.js

module.exports = function(defaults) {
var app = new EmberApp(defaults, {
dotEnv: {
clientAllowedKeys: ['DROPBOX_KEY'],
path: './path/to/.env'
}
});

return app.toTree();
```js
// dotenv.js
module.exports = function(env) {
return {
clientAllowedKeys: ['DROPBOX_KEY'],
path: './path/to/.env'
};
};
```

In addition, you may also customize for different environments:


```javascript
// ember-cli-build.js

module.exports = function(defaults) {
var app = new EmberApp(defaults, {
dotEnv: {
clientAllowedKeys: ['DROPBOX_KEY'],
path: {
development: './path/to/.env',
test: './path/to/.env.test',
production: './path/to/.env.production'
}
}
});

return app.toTree();
```js
// dotenv.js
module.exports = function(env) {
return {
clientAllowedKeys: ['DROPBOX_KEY'],
path: `./path/to/.env-${env}`
};
};
```

Expand Down
Empty file removed addon/.gitkeep
Empty file.
Empty file removed app/.gitkeep
Empty file.
12 changes: 12 additions & 0 deletions blueprints/ember-cli-dotenv/files/dotenv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-env node */

'use strict';

const path = require('path');

module.exports = function(/* env */) {
return {
clientAllowedKeys: ['DROPBOX_KEY'],
path: path.join(__dirname, '.env')
}
}
6 changes: 6 additions & 0 deletions blueprints/ember-cli-dotenv/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
description: 'Setup ember-cli-dotenv',
normalizeEntityName() {}
};
86 changes: 43 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
/* jshint node: true */

'use strict';

const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const parseArgs = require('minimist');
const existsSync = require('exists-sync');

module.exports = {
name: 'ember-cli-dotenv',
config: function(environment){
var path = require('path');
var fs = require('fs');
var dotenv = require('dotenv');
var existsSync = require('exists-sync');
var project = this.project;
var loadedConfig;
var config = {};
var hasOwn = Object.prototype.hasOwnProperty;

var configFilePath,
dotEnvPath = this.app && this.app.options.dotEnv && this.app.options.dotEnv.path;

if (dotEnvPath) {
// path is defined
if (typeof dotEnvPath === 'string') {
configFilePath = dotEnvPath;
} else {
if (dotEnvPath[environment]) {
configFilePath = dotEnvPath[environment];
}
}
}

if (!configFilePath) {
configFilePath = path.join(project.root, '.env');
/**
* NOTE: dotenv needs to be invoked before the app config is materialized
* so that the process.env's are set appropriately. Previously this was done
* within the `config` hook. As of 2.15.x, that is too late in the process
* and needed to be moved into `init`.
*/
init() {
this._super.init && this._super.init.apply(this, arguments);
let root = this.project.root;
let configFactory = path.join(root, 'dotenv.js');
let options = {
path: path.join(root, '.env'),
clientAllowedKeys: []
};

if (existsSync(configFactory)) {
Object.assign(options, require(configFactory)(this._resolveEnvironment()));
}

if (existsSync(configFilePath) && dotenv.config({path: configFilePath})) {
loadedConfig = dotenv.parse(fs.readFileSync(configFilePath));
} else {
loadedConfig = {};
}
if (existsSync(options.path) && dotenv.config({ path: options.path })) {
let loadedConfig = dotenv.parse(fs.readFileSync(options.path));
let allowedKeys = options.clientAllowedKeys || [];

var app = this.app;
if (!this.app) {
return;
this._config = allowedKeys.reduce((accumulator, key) => {
accumulator[key] = loadedConfig[key];

return accumulator;
}, {});
}
if (app.options.dotEnv && hasOwn.call(app.options.dotEnv, 'allow')){
console.warn("[EMBER-CLI-DOTENV] app.options.allow has been deprecated. Please use clientAllowedKeys instead. Support will be removed in the next major version");
},

_resolveEnvironment() {
if (process.env.EMBER_ENV) {
return process.env.EMBER_ENV;
}
var allowedKeys = (app.options.dotEnv && (app.options.dotEnv.clientAllowedKeys || app.options.dotEnv.allow) || []);

allowedKeys.forEach(function(key){
config[key] = loadedConfig[key];
});
let args = parseArgs(process.argv);
let env = args.e || args.env || args.environment;

return config;
return env || 'development';
},
included: function(app){
this.app = app;
this._super.included.apply(this, arguments);

config() {
return this._config;
}
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"repository": "https://github.com/fivetanley/ember-cli-dotenv",
"engines": {
"node": ">= 0.10.0"
"node": "^4.5 || 6.* || >= 7.*"
},
"author": "Stanley Stuart",
"license": "MIT",
Expand All @@ -37,7 +37,8 @@
],
"dependencies": {
"dotenv": "^1.0.0",
"exists-sync": "0.0.3"
"exists-sync": "0.0.3",
"minimist": "^1.2.0"
},
"ember-addon": {
"configPath": "tests/dummy/config"
Expand Down
Empty file removed vendor/.gitkeep
Empty file.
Loading

0 comments on commit f464ddf

Please sign in to comment.