-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |