Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit for ember-cli 2.15.0 support #33

Merged
merged 4 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure how we want to deal with this, are you planning to land your fastboot work soon or should I defer the README changes until after we version? @SergeAstapov

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do actually have a branch with FastBoot support, but it appeared there is a small issue in ember-cli-fastboot that blocks this addon to have FastBoot support, see ember-fastboot/ember-cli-fastboot#543

To not block people from using this addon I think we can

  • merge this PR
  • merge PR with ember-cli update
  • fix tests - I can do this tomorrow
  • update readme and release 2.0
  • add FastBoot support
  • release 2.1

What about such plan?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonmit I'll take care of ember-cli upgrade (need to resolve conflict) and failing tests so we would be able to release version 2.0 shortly


* `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