Skip to content

Commit

Permalink
Merge 8a71e8a into 5766abb
Browse files Browse the repository at this point in the history
  • Loading branch information
martypenner committed Jul 18, 2015
2 parents 5766abb + 8a71e8a commit 2f038f5
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ module.exports = Command.extend({
aliases: ['b'],

availableOptions: [
{ name: 'environment', type: String, default: 'development', aliases: ['e',{'dev' : 'development'}, {'prod' : 'production'}] },
{ name: 'environment', type: String, default: 'development', aliases: ['e', {'dev' : 'development'}, {'prod' : 'production'}] },
{ name: 'output-path', type: path, default: 'dist/', aliases: ['o'] },
{ name: 'watch', type: Boolean, default: false, aliases: ['w'] },
{ name: 'watcher', type: String }
{ name: 'watcher', type: String },
{ name: 'suppress-sizes', type: Boolean, default: false }
],

run: function(commandOptions) {
Expand All @@ -22,7 +23,18 @@ module.exports = Command.extend({
analytics: this.analytics,
project: this.project
});
return buildTask.run(commandOptions);

return buildTask.run(commandOptions)
.then(function () {
var ShowAssetSizesTask = this.tasks.ShowAssetSizes;
var show = new ShowAssetSizesTask({
ui: this.ui
});

return show.run({
outputPath: commandOptions.outputPath
});
}.bind(this));
},

taskFor: function(options) {
Expand Down
20 changes: 20 additions & 0 deletions lib/commands/show-asset-sizes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var path = require('path');
var Command = require('../models/command');

module.exports = Command.extend({
name: 'show-asset-sizes',
description: 'Show asset file sizes.',

availableOptions: [
{ name: 'output-path', type: path, default: 'dist/' }
],

run: function(commandOptions) {
var task = new this.tasks.ShowAssetSizes({
ui: this.ui
});
return task.run(commandOptions);
}
});
44 changes: 44 additions & 0 deletions lib/tasks/show-asset-sizes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

var chalk = require('chalk');
var Task = require('../models/task');
var fs = require('fs');
var path = require('path');
var filesize = require('filesize');
var glob = require('glob');
var Promise = require('../ext/promise');

module.exports = Task.extend({
run: function (options) {
var ui = this.ui;
var outputPath = options.outputPath + 'assets';
var globOptions = {nodir: true, nocase: true};

return Promise.resolve()
.then(function () {
var files = glob.sync(outputPath + '/*.{css,js}', globOptions);

if (!files.length) {
throw new Error('No asset files found in the asset path provided: ' + options.outputPath);
}

ui.writeLine(chalk.green('File sizes:'));

files
// Skip test files
.filter(function (file) {
var filename = path.basename(file);
return !/test-(loader|support)/i.test(filename);
})
.forEach(function (file) {
var filename = path.basename(file);
var stat = fs.statSync(file);

ui.writeLine(chalk.blue(filename + ': ') + chalk.white(filesize(stat.size)));
});
})
.catch(function (err) {
ui.writeLine(chalk.red(err));
});
}
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"exists-sync": "0.0.3",
"exit": "^0.1.2",
"express": "^4.12.3",
"filesize": "^3.1.2",
"findup": "0.1.5",
"findup-sync": "^0.2.1",
"fs-extra": "0.22.1",
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/cli/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ describe('Unit: CLI', function() {

var options = build.calledWith[0][0];
expect(options.watch).to.equal(false, 'expected the default watch flag to be false');
expect(options.suppressSizes).to.equal(false, 'expected the default silent flag to be false');
});
});

Expand All @@ -426,6 +427,15 @@ describe('Unit: CLI', function() {
});
});

it('ember ' + command + ' --suppress-sizes', function () {
var build = stubRun('build');

return ember([command, '--suppress-sizes']).then(function () {
var options = build.calledWith[0][0];
expect(options.suppressSizes).to.equal(true, 'expected the suppressSizes flag to be true');
});
});

['production', 'development', 'baz'].forEach(function(env){
it('ember ' + command + ' --environment ' + env, function() {
var build = stubRun('build');
Expand Down
78 changes: 78 additions & 0 deletions tests/unit/commands/show-asset-sizes-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

var expect = require('chai').expect;
var stub = require('../../helpers/stub').stub;
var commandOptions = require('../../factories/command-options');
var Task = require('../../../lib/models/task');

describe('show asset sizes command', function () {
var ShowCommand;
var tasks;
var options;
var taskInstance;

before(function () {
ShowCommand = require('../../../lib/commands/show-asset-sizes');
});

beforeEach(function () {
tasks = {
ShowAssetSizes: Task.extend({
init: function () {
taskInstance = this;
}
})
};

options = commandOptions({
tasks: tasks,
settings: {}
});

stub(tasks.ShowAssetSizes.prototype, 'run');
});

after(function () {
ShowCommand = null;
taskInstance = null;
});

afterEach(function () {
tasks.ShowAssetSizes.prototype.run.restore();
});

it('has correct default value for output path', function () {
return new ShowCommand(options).validateAndRun().then(function () {
var run = tasks.ShowAssetSizes.prototype.run;
var ops = run.calledWith[0][0];

expect(run.called).to.equal(1, 'expected run to be called once');
expect(ops.outputPath).to.equal('dist/', 'has correct output path option when not set');
});
});

it('has correct options', function() {
return new ShowCommand(options)
.validateAndRun(['--output-path', 'some/path'])
.then(function () {
var run = tasks.ShowAssetSizes.prototype.run;
var ops = run.calledWith[0][0];

expect(run.called).to.equal(1, 'expected run to be called once');
expect(ops.outputPath).to.equal(process.cwd() + '/some/path', 'has correct asset path');
});
});

it('displays an error when no files are found', function () {
return new ShowCommand(options)
.validateAndRun()
.then(function () {
var run = tasks.ShowAssetSizes.prototype.run;

expect(run.called).to.equal(1, 'expected run to be called once');
})
.catch(function (error) {
expect(error.message).to.equal('No asset files found in the asset path you provided: dist/assets');
});
});
});

0 comments on commit 2f038f5

Please sign in to comment.