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

Support custom ENVs #1520

Closed
wants to merge 11 commits into from
13 changes: 13 additions & 0 deletions blueprints/app/files/config/development.js
@@ -0,0 +1,13 @@
/* jshint node: true */

var productionConfig = require('./production.js');

module.exports = function(config) {
productionConfig(config);

config.set('production', false);
config.set('tests', true);


config.set('wrapInEval', true);
};
38 changes: 0 additions & 38 deletions blueprints/app/files/config/environment.js

This file was deleted.

30 changes: 30 additions & 0 deletions blueprints/app/files/config/production.js
@@ -0,0 +1,30 @@
/* jshint node: true */

module.exports = function(config) {
config.set('production', true);
config.set('hinting', false);
config.set('tests', process.env.EMBER_CLI_TEST_COMMAND || false);

config.set('es3Safe', true);
config.set('wrapInEval', false);
config.set('minify', {
css: {
relativeTo: 'app/styles'
},
js: {
mangle: true,
compress: true
}
});

config.set('ENV', {
baseURL: '/',
locationType: 'auto',
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. 'with-controller': true
}
}
});
};
8 changes: 8 additions & 0 deletions blueprints/app/files/config/test.js
@@ -0,0 +1,8 @@
/* jshint node: true */

var productionConfig = require('./production.js');

module.exports = function(config) {
productionConfig(config);
config.set('tests', true);
};
68 changes: 20 additions & 48 deletions lib/broccoli/ember-app.js
Expand Up @@ -49,44 +49,20 @@ function EmberApp(options) {

this.project = options.project || Project.closestSync(process.cwd());

this.env = process.env.EMBER_ENV || 'development';
this.name = options.name || this.project.name();

this.registry = options.registry || p.setupRegistry(this);

var isProduction = this.env === 'production';

this.tests = options.hasOwnProperty('tests') ? options.tests : !isProduction;
this.hinting = options.hasOwnProperty('hinting') ? options.hinting : !isProduction;

this.bowerDirectory = 'bower_components';
this.env = process.env.EMBER_ENV || 'development';

if (fs.existsSync(path.join(this.project.root, '.bowerrc'))) {
this.bowerDirectory = JSON.parse(fs.readFileSync(path.join(this.project.root, '.bowerrc'))).directory;
}
this.config = this.project.config(this.env);

if (process.env.EMBER_CLI_TEST_COMMAND) {
this.tests = true;
}
this.getEnvJSON = function () {
return this.config.get('ENV');
}.bind(this);

this.options = merge(options, {
es3Safe: true,
wrapInEval: !isProduction,
minifyCSS: {
enabled: true,
options: { relativeTo: 'app/styles' }
},
minifyJS: {
enabled: true,
options: {
mangle: true,
compress: true
}
},
loader: this.bowerDirectory + '/loader/loader.js',
trees: {},
jshintrc: {},
getEnvJSON: this.project.require(options.environment || './config/environment'),
vendorFiles: {}
}, defaults);

Expand Down Expand Up @@ -136,10 +112,6 @@ function EmberApp(options) {
vendor: this.unwatchedVendorTrees(),
}, defaults);

this.options.jshintrc = merge(this.options.jshintrc, {
app: this.project.root,
tests: path.join(this.project.root, 'tests'),
}, defaults);

this.importWhitelist = {};
this.legacyFilesToAppend = [];
Expand Down Expand Up @@ -223,7 +195,7 @@ EmberApp.prototype.index = function() {
destDir: '/'
});

return injectENVJson(this.options.getEnvJSON, this.env, index, files);
return injectENVJson(this.getEnvJSON, this.env, index, files);
};

EmberApp.prototype.testIndex = function() {
Expand All @@ -233,7 +205,7 @@ EmberApp.prototype.testIndex = function() {
destDir: '/tests'
});

return injectENVJson(this.options.getEnvJSON, this.env, index, [
return injectENVJson(this.getEnvJSON, this.env, index, [
'tests/index.html'
]);
};
Expand Down Expand Up @@ -337,7 +309,7 @@ EmberApp.prototype.appAndDependencies = function() {
var app = this._processedAppTree();
var templates = this._processedTemplatesTree();

if (this.options.es3Safe) {
if (this.config.isEnabled('es3Safe')) {
app = new ES3SafeFilter(app);
}

Expand All @@ -348,22 +320,22 @@ EmberApp.prototype.appAndDependencies = function() {

var sourceTrees = [ vendor, preprocessedApp, templates ];

if (this.tests) {
if (this.config.isEnabled('tests')) {
var tests = this._processedTestsTree();
var preprocessedTests = preprocessJs(tests, '/tests', this.name, {
registry: this.registry
});

sourceTrees.push(preprocessedTests);

if (this.hinting) {
if (this.config.isEnabled('hinting', 'js')) {
var jshintedApp = jshintTrees(this._appWithoutStylesAndTemplates(), {
jshintrcPath: this.options.jshintrc.app,
jshintrcPath: this.config.get('hinting', 'js', 'app') || this.project.root,
description: 'JSHint - App'
});

var jshintedTests = jshintTrees(tests, {
jshintrcPath: this.options.jshintrc.tests,
jshintrcPath: this.config.get('hinting', 'js', 'tests') || path.join(this.project.root, 'tests'),
description: 'JSHint - Tests'
});

Expand Down Expand Up @@ -418,8 +390,8 @@ EmberApp.prototype.javascript = function() {

var vendorAndApp = mergeTrees([vendor, es6]);

if (this.env === 'production' && this.options.minifyJS.enabled === true) {
var options = this.options.minifyJS.options || {};
if (this.config.isEnabled('minify', 'js')) {
var options = this.config.get('minify', 'js') || {};
return uglifyJavaScript(vendorAndApp, options);
} else {
return vendorAndApp;
Expand Down Expand Up @@ -450,8 +422,8 @@ EmberApp.prototype.styles = function() {
description: 'concatFiles - vendorStyles'
});

if (this.env === 'production' && this.options.minifyCSS.enabled === true) {
var options = this.options.minifyCSS.options || {};
if (this.config.isEnabled('minify', 'css')) {
var options = this.config.get('minify', 'css') || {};
options.registry = this.registry;
processedStyles = preprocessMinifyCss(processedStyles, options);
vendorStyles = preprocessMinifyCss(vendorStyles, options);
Expand Down Expand Up @@ -487,8 +459,8 @@ EmberApp.prototype.testFiles = function() {
destDir: '/'
});

if (this.options.fingerprint && this.options.fingerprint.exclude) {
this.options.fingerprint.exclude.push('testem');
if (this.config.isEnabled('fingerprint')) {
this.config.push('fingerprint', 'exclude', 'testem');
}

var testLoader = pickFiles(this.trees.vendor, {
Expand Down Expand Up @@ -609,7 +581,7 @@ EmberApp.prototype.toArray = function() {
sourceTrees.push(publicFolder);
}

if (this.tests) {
if (this.config.isEnabled('tests')) {
sourceTrees = sourceTrees.concat(this.testIndex(), this.testFiles());
}

Expand All @@ -628,7 +600,7 @@ EmberApp.prototype.toTree = function(additionalTrees) {
function injectENVJson(fn, env, tree, files) {
// TODO: real templating
var envJsonString = function(){
return JSON.stringify(fn(env));
return JSON.stringify(fn());
};

var baseTag = function(){
Expand Down
63 changes: 63 additions & 0 deletions lib/models/config.js
@@ -0,0 +1,63 @@
'use strict';

var isObject = require('lodash-node/modern/objects/isObject');
var isUndefined = require('lodash-node/modern/objects/isUndefined');
var defaults = require('lodash-node/modern/objects/defaults');
var merge = require('lodash-node/modern/objects/merge');


function Config() {
this.options = Object.create(null);
this.groupOptions = Object.create(null);
}

// getter
Config.prototype.get = function(group, key) {
var argc = arguments.length;
var options = this.options;

if (argc === 1) {
// group getter
return this.options[group] || false;
} else if (argc === 2) {
if (options[group] !== undefined && options[group][key] !== undefined) {
return options[group][key];
} else {
return this.get(group);
}
}
};

// setter
Config.prototype.set = function(group) {
var len = arguments.length;
if(len >= 2 && !isObject(arguments[1])) {
this.groupOptions[arguments[0]] = arguments[1];
}

var newOptions;
if (isObject(arguments[1])) {
newOptions = arguments[1];
} else if (isObject(arguments[2])) {
this.set(group, arguments[1]);
newOptions = arguments[2];
}

this.options[group] = merge(newOptions, this.options[group], defaults);
};

Config.prototype.isEnabled = function() {
return !!this.get.apply(this, arguments);
};

Config.prototype.push = function (group, key, option) {
if(isUndefined(this.options[group]) || isUndefined(this.options[group][key])) {
this.options[group] = this.options[group] || {};
this.options[group][key] = [];
}

this.options[group][key].push(option);
};


module.exports = Config;
10 changes: 6 additions & 4 deletions lib/models/project.js
Expand Up @@ -9,6 +9,7 @@ var assign = require('lodash-node/modern/objects/assign');
var DAG = require('../utilities/DAG');
var Command = require('../models/command');
var Addon = require('../models/addon');
var Config = require('./config');

var emberCLIVersion = require('../utilities/ember-cli-version');

Expand Down Expand Up @@ -37,11 +38,12 @@ Project.prototype.config = function(env) {
if (this.pkg['ember-addon'] && this.pkg['ember-addon']['configPath']) {
configPath = this.pkg['ember-addon']['configPath'];
}
if (fs.existsSync(path.join(this.root, configPath, 'environment.js'))) {
return this.require('./' + path.join(configPath, 'environment'))(env);
} else {
return { };

var config = new Config();
if (fs.existsSync(path.join(this.root, configPath, env + '.js'))) {
this.require('./' + path.join(configPath, env))(config);
}
return config;
};

Project.prototype.has = function(file) {
Expand Down
4 changes: 1 addition & 3 deletions tests/fixtures/brocfile-tests/wrap-in-eval/Brocfile.js
Expand Up @@ -5,9 +5,7 @@ var EmberApp = require('ember-cli/lib/broccoli/ember-app');
var app = new EmberApp({
name: require('./package.json').name,

wrapInEval: true,

getEnvJSON: require('./config/environment')
wrapInEval: true
});

module.exports = app.toTree();
6 changes: 3 additions & 3 deletions tests/unit/models/project-test.js
Expand Up @@ -19,7 +19,7 @@ describe('models/project.js', function() {
before(function() {
tmp.setup(projectPath);

touch(projectPath + '/config/environment.js', {
touch(projectPath + '/config/test.js', {
baseURL: '/foo/bar'
});

Expand All @@ -35,8 +35,8 @@ describe('models/project.js', function() {
tmp.teardown(projectPath);
});

it('config() finds and requires config/environment', function() {
project.config('development');
it('config() finds and requires config/test', function() {
project.config('test');
assert.equal(called, true);
});
});
Expand Down