Skip to content

Commit

Permalink
initial commit to rewrite ember-cli-dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit committed Oct 18, 2017
1 parent ca42cca commit b055555
Show file tree
Hide file tree
Showing 7 changed files with 5,243 additions and 82 deletions.
58 changes: 21 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ 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, put some configuration in your `dotenv.js`. Starting in 0.2.0, *client side keys must be explicitly allowed*:

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

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

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

Expand All @@ -38,10 +34,10 @@ These will be available in your `config/environment.js` file:

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

Expand Down Expand Up @@ -75,39 +71,27 @@ Sometime people may want to use different `.env` file than the one in project ro
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'
}
});
// dotenv.js

return app.toTree();
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();
// 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() {}
};
75 changes: 30 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,44 @@
/* jshint node: true */

'use strict';

const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const existsSync = require('exists-sync');
const EmberApp = require('ember-cli/lib/broccoli/ember-app');

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');
}
init() {
this._super.apply(this, arguments);

if (existsSync(configFilePath) && dotenv.config({path: configFilePath})) {
loadedConfig = dotenv.parse(fs.readFileSync(configFilePath));
} else {
loadedConfig = {};
}
let project = this.project;
let hasOwn = Object.prototype.hasOwnProperty;
let configFactory = path.join(project.root, 'dotenv.js');
let options = {
path: path.join(project.root, '.env'),
clientAllowedKeys: []
};

var app = this.app;
if (!this.app) {
return;
if (existsSync(configFactory)) {
Object.assign(options, require(configFactory)(this.env));
}
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");
}
var allowedKeys = (app.options.dotEnv && (app.options.dotEnv.clientAllowedKeys || app.options.dotEnv.allow) || []);

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

this._config = allowedKeys.reduce((accumulator, key) => {
accumulator[key] = loadedConfig[key];

return config;
return accumulator;
}, {});
}
},
included: function(app){
this.app = app;
this._super.included.apply(this, arguments);

config(env, baseConfig) {
return this._config;
}
};
Loading

0 comments on commit b055555

Please sign in to comment.