Skip to content

Commit dfac84f

Browse files
committed
Use default options from .ember-cli file
1 parent 24517ce commit dfac84f

2 files changed

Lines changed: 49 additions & 17 deletions

File tree

lib/cli.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var merge = require('lodash-node/modern/objects/merge');
99
var path = require('path');
1010
var pkg = require('../package.json');
1111
var ui = require('./ui');
12+
var fs = require('fs');
13+
var denodeify = RSVP.denodeify;
14+
var readFile = denodeify(fs.readFile);
15+
var sequence = require('./utilities/sequence');
1216

1317
RSVP.on('error', function(error) {
1418
ui.write(chalk.red(String(error)) + '\n');
@@ -53,32 +57,41 @@ function loadInsight() {
5357
});
5458
}
5559

60+
function readDefaults() {
61+
var defaultsPath = path.join(process.cwd(), '.ember-cli');
62+
return readFile(defaultsPath).then(function(content) {
63+
return content.toString().split(' ');
64+
}).catch(function() { });
65+
}
66+
67+
5668
Cli.run = function run (argv, ui, insight) {
5769
var commands = require('../lib/commands/commands');
58-
var permission;
5970

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

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

65-
permission = insight.askPermission();
66-
67-
return permission.then(function() {
68-
return cli.run();
69-
});
77+
return sequence([
78+
permission,
79+
readDefaults,
80+
cli.run.bind(cli)
81+
]);
7082
};
7183

7284
function collectArgs(args, options) {
7385
return JSON.stringify(args.concat([options]));
7486
}
87+
7588
function setupEnvironment(command, options) {
7689
if (typeof command.getEnv === 'function') {
7790
process.env.BROCCOLI_ENV = command.getEnv(options) || 'development';
7891
}
7992
}
8093

81-
Cli.prototype.runCurrentCommand = function() {
94+
Cli.prototype.runCurrentCommand = function(defaults) {
8295
var cmd = this.cmd;
8396
cmd = commandAliases[cmd] ? commandAliases[cmd] : cmd;
8497
var action = this.commands[cmd];
@@ -94,9 +107,16 @@ Cli.prototype.runCurrentCommand = function() {
94107
action = this.commands.help;
95108
}
96109

97-
var opts = nopt(action.types, action.shorthands, this.argv);
110+
var parseOpts = nopt.bind(null, action.types, action.shorthands);
111+
var opts = parseOpts(this.argv);
112+
113+
if(defaults !== undefined) {
114+
defaults = nopt(action.types, action.shorthands, defaults, 0);
115+
} else {
116+
defaults = {};
117+
}
98118

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

117137
};
118138

119-
Cli.prototype.run = function() {
139+
Cli.prototype.run = function(defaults) {
120140
var opts = this.opts;
121141
var cli = this;
122142

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

130150
return new Promise(function(resolve) {
131-
resolve(cli.runCurrentCommand());
151+
resolve(cli.runCurrentCommand(defaults));
132152
}).catch(function(err) {
133-
// Log it if it's just a string. Else if it's a real error, throw.
134-
if (typeof err === 'string') { cli.ui.write(err); }
135-
else { throw err; }
136-
});
153+
// Log it if it's just a string. Else if it's a real error, throw.
154+
if (typeof err === 'string') { cli.ui.write(err); }
155+
else { throw err; }
156+
});
137157
};

tests/unit/cli-test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var commands;
1414
var insight;
1515
var argv;
1616
// helper to similate running the CLI
17-
function ember(args) {
17+
function ember(args, defaults) {
1818
var argv;
1919

2020
if (args) {
@@ -23,7 +23,7 @@ function ember(args) {
2323
argv = baseArgs;
2424
}
2525

26-
return new Cli(argv, commands, ui, insight).run();
26+
return new Cli(argv, commands, ui, insight).run(defaults);
2727
}
2828

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

313+
describe('default options config file', function() {
314+
it('reads default options from .ember-cli file', function() {
315+
var defaults = ['--output', process.cwd()];
316+
var build = stubCommand('build');
317+
318+
ember(['build'], defaults);
319+
320+
var options = build.calledWith[0][0].options;
321+
assert.equal(options.output, process.cwd());
322+
});
323+
});
324+
313325
describe('analytics tracking', function() {
314326

315327
var track;

0 commit comments

Comments
 (0)