Skip to content

Instantly share code, notes, and snippets.

@Robdel12
Created October 19, 2016 17:51
Show Gist options
  • Save Robdel12/4ed836e2632e09f2b79dcd5e4fa04cd0 to your computer and use it in GitHub Desktop.
Save Robdel12/4ed836e2632e09f2b79dcd5e4fa04cd0 to your computer and use it in GitHub Desktop.
/*
* Adds options to the `ember build` and `ember serve` commands to configure
* which API endpoints will be hit as well as whether to disable mirage or not.
*/
var ServeCommand = require('ember-cli/lib/commands/serve');
var BuildCommand = require('ember-cli/lib/commands/build');
var TestCommand = require('ember-cli/lib/commands/test');
var DeployCommand = require('ember-cli-deploy/lib/commands/deploy');
function addOptionsTo(Command) {
var Definition = Command.run ? Command : Command.prototype;
var availableOptions = Definition.availableOptions;
/**
* Option to enable Mirage:
* $ ember [command] --enable-mirage
* $ ember [command] -em
*/
availableOptions.push({
name: 'enable-mirage',
type: Boolean,
default: false,
aliases: ['em'],
description: "Use Mirage instead of hitting actual API endpoints"
});
/**
* Option to override the API that will be used:
* $ ember serve --api-host https://someplace.else-entirely.com
*/
availableOptions.push({
name: 'api-host',
type: String,
aliases: ['ah'],
description: "Your API host URL",
default: "http://your-api.com"
});
/**
* Option to override the API namespace that will be used:
* $ ember serve -an v1/something
*/
availableOptions.push({
name: 'api-namespace',
type: String,
aliases: ['an'],
description: "Your API host namespace",
default: "v1"
});
var oldRunCommand = Definition.run;
Definition.run = function(options) {
if(options.environment !== "test") {
console.log('Mirage enabled: ' + options.enableMirage);
process.env['ENABLE_MIRAGE'] = options.enableMirage;
}
console.log('API Host: ' + options.apiHost);
process.env.API_HOST = options.apiHost;
console.log('API Namespace: ' + options.apiNamespace);
process.env.API_NAMESPACE = options.apiNamespace;
return oldRunCommand.apply(this, arguments);
};
}
addOptionsTo(BuildCommand);
addOptionsTo(ServeCommand);
addOptionsTo(TestCommand);
addOptionsTo(DeployCommand);
/*jshint node:true*/
// Adds server commands to build & serve commands
// i.e `ember server -em`
require('./ember-cli-option-extensions');
module.exports = {
name: 'api',
config: function(env, config) {
config['ember-cli-mirage'] = {
enabled: env === 'test' || process.env.ENABLE_MIRAGE === 'true'
};
// merge simple auth config
var simpleAuthConfig = config['ember-simple-auth-token'];
var simpleAuthEndpoint = process.env.API_HOST + '/' + process.env.API_NAMESPACE + '/' + config.AUTH_ENDPOINT;
simpleAuthConfig.serverTokenEndpoint = simpleAuthEndpoint + 'token';
simpleAuthConfig.serverTokenRefreshEndpoint = simpleAuthEndpoint + 'refresh';
config.API_HOST = process.env.API_HOST;
config.API_NAMESPACE = process.env.API_NAMESPACE;
return config;
}
};
@Robdel12
Copy link
Author

This lives as an in-repo-addon because when you make this an addon it tries to push the new options onto the addons version of ember-cli and not the apps version of ember-cli. Nested node_modules 👎 So it's pushing the new commands on to a version of ember-cli that isn't running the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment