Skip to content

Commit

Permalink
Use default options from .ember-cli file
Browse files Browse the repository at this point in the history
  • Loading branch information
joefiorini committed Apr 2, 2014
1 parent 24517ce commit dfac84f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
50 changes: 35 additions & 15 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ var merge = require('lodash-node/modern/objects/merge');
var path = require('path');
var pkg = require('../package.json');
var ui = require('./ui');
var fs = require('fs');
var denodeify = RSVP.denodeify;
var readFile = denodeify(fs.readFile);
var sequence = require('./utilities/sequence');

RSVP.on('error', function(error) {
ui.write(chalk.red(String(error)) + '\n');
Expand Down Expand Up @@ -53,32 +57,41 @@ function loadInsight() {
});
}

function readDefaults() {
var defaultsPath = path.join(process.cwd(), '.ember-cli');
return readFile(defaultsPath).then(function(content) {
return content.toString().split(' ');
}).catch(function() { });
}


Cli.run = function run (argv, ui, insight) {
var commands = require('../lib/commands/commands');
var permission;

ui = ui || require('./ui');
insight = insight || loadInsight();

var cli = new Cli(argv, commands, ui, insight);
var permission = insight.askPermission();

permission = insight.askPermission();

return permission.then(function() {
return cli.run();
});
return sequence([
permission,
readDefaults,
cli.run.bind(cli)
]);
};

function collectArgs(args, options) {
return JSON.stringify(args.concat([options]));
}

function setupEnvironment(command, options) {
if (typeof command.getEnv === 'function') {
process.env.BROCCOLI_ENV = command.getEnv(options) || 'development';
}
}

Cli.prototype.runCurrentCommand = function() {
Cli.prototype.runCurrentCommand = function(defaults) {
var cmd = this.cmd;
cmd = commandAliases[cmd] ? commandAliases[cmd] : cmd;
var action = this.commands[cmd];
Expand All @@ -94,9 +107,16 @@ Cli.prototype.runCurrentCommand = function() {
action = this.commands.help;
}

var opts = nopt(action.types, action.shorthands, this.argv);
var parseOpts = nopt.bind(null, action.types, action.shorthands);
var opts = parseOpts(this.argv);

if(defaults !== undefined) {
defaults = nopt(action.types, action.shorthands, defaults, 0);
} else {
defaults = {};
}

var options = merge({}, opts, {
var options = merge({}, opts, defaults, {
appRoot: process.cwd(),
cliRoot: path.resolve(path.join(__dirname, '..'))
});
Expand All @@ -116,7 +136,7 @@ Cli.prototype.runCurrentCommand = function() {

};

Cli.prototype.run = function() {
Cli.prototype.run = function(defaults) {
var opts = this.opts;
var cli = this;

Expand All @@ -128,10 +148,10 @@ Cli.prototype.run = function() {
}

return new Promise(function(resolve) {
resolve(cli.runCurrentCommand());
resolve(cli.runCurrentCommand(defaults));
}).catch(function(err) {
// Log it if it's just a string. Else if it's a real error, throw.
if (typeof err === 'string') { cli.ui.write(err); }
else { throw err; }
});
// Log it if it's just a string. Else if it's a real error, throw.
if (typeof err === 'string') { cli.ui.write(err); }
else { throw err; }
});
};
16 changes: 14 additions & 2 deletions tests/unit/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var commands;
var insight;
var argv;
// helper to similate running the CLI
function ember(args) {
function ember(args, defaults) {
var argv;

if (args) {
Expand All @@ -23,7 +23,7 @@ function ember(args) {
argv = baseArgs;
}

return new Cli(argv, commands, ui, insight).run();
return new Cli(argv, commands, ui, insight).run(defaults);
}

function stubCommand(name) {
Expand Down Expand Up @@ -310,6 +310,18 @@ describe('Unit: CLI', function(){
assert.equal(help.called, 0, 'expected the help command to be run');
});

describe('default options config file', function() {
it('reads default options from .ember-cli file', function() {
var defaults = ['--output', process.cwd()];
var build = stubCommand('build');

ember(['build'], defaults);

var options = build.calledWith[0][0].options;
assert.equal(options.output, process.cwd());
});
});

describe('analytics tracking', function() {

var track;
Expand Down

0 comments on commit dfac84f

Please sign in to comment.