Skip to content

Commit 078fee1

Browse files
committed
Restructure to be based on ember-cli-deploy-plugin.
Also includes a first pass at the README.
1 parent 8058863 commit 078fee1

File tree

6 files changed

+170
-193
lines changed

6 files changed

+170
-193
lines changed

README.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
1-
# Ember-cli-deploy-gzip
1+
# ember-cli-deploy-gzip
22

3-
This README outlines the details of collaborating on this Ember addon.
3+
NOTE: This plugin targets ember-cli-deploy 0.5.0 and later. 0.5.0 is in development at this time.
4+
5+
This ember-cli-deploy plugin compresses files in-place using gzip compression.
6+
7+
This is helpful to prepare for upload to a asset host that expects files to be
8+
pre-compressed.
49

510
## Installation
611

7-
* `git clone` this repository
8-
* `npm install`
9-
* `bower install`
12+
* `ember install ember-cli-deploy-gzip`
1013

11-
## Running
14+
## ember-cli-deploy Hooks Implemented
1215

13-
* `ember server`
14-
* Visit your app at http://localhost:4200.
16+
* configure
17+
* willUpload
1518

16-
## Running Tests
19+
## Configuration Options
20+
21+
### filePattern
22+
23+
Files matching this pattern will be gzipped.
24+
25+
_Default:_ "\*\*/\*.{js,css,png,gif,jpg,map,xml,txt,svg,eot,ttf,woff,woff2}"
26+
27+
### distDir
28+
29+
Directory where assets have been written to
1730

18-
* `ember test`
19-
* `ember test --server`
31+
_Default:_ the `distDir` property of the deployment context
2032

21-
## Building
33+
### distFiles
2234

23-
* `ember build`
35+
The Array of built assets.
36+
37+
_Default:_ the `distFiles` property of the deployment context
38+
39+
## Prequisites
40+
41+
The default configuration of this plugin expects the deployment context to have `distDir` and `distFiles` properties. These are conveniently created by the [ember-cli-deploy-build](https://github.com/zapnito/ember-cli-deploy-build) plugin so will work out of the box if you are using that plugin.
42+
43+
## Plugins known to work well with this one
44+
45+
[ember-cli-deploy-build](https://github.com/zapnito/ember-cli-deploy-build)
46+
[ember-cli-deploy-s3](https://github.com/zapnito/ember-cli-deploy-s3)
47+
48+
## Running Tests
2449

25-
For more information on using ember-cli, visit [http://www.ember-cli.com/](http://www.ember-cli.com/).
50+
* `npm test`

index.js

Lines changed: 54 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ var fs = require('fs');
66
var path = require('path');
77
var minimatch = require('minimatch');
88

9-
var chalk = require('chalk');
10-
var blue = chalk.blue;
11-
var red = chalk.red;
12-
139
var denodeify = require('rsvp').denodeify;
1410
var renameFile = denodeify(fs.rename);
1511

16-
var validateConfig = require('./lib/utilities/validate-config');
12+
var DeployPluginBase = require('ember-cli-deploy-plugin');
1713

1814
module.exports = {
1915
name: 'ember-cli-deploy-gzip',
@@ -22,88 +18,68 @@ module.exports = {
2218
var zlib = require('zlib');
2319
var fs = require('fs');
2420

25-
function _gzipFileInPlace(ui, distDir, filePath) {
26-
var fullPath = path.join(distDir, filePath);
27-
return new Promise(function(resolve, reject) {
28-
var gzip = zlib.createGzip();
29-
var inp = fs.createReadStream(fullPath);
30-
var out = fs.createWriteStream(fullPath + '.gz');
31-
32-
inp.pipe(gzip).pipe(out);
33-
inp.on('error', function(err){
34-
reject(err);
35-
});
36-
out.on('error', function(err){
37-
reject(err);
38-
});
39-
out.on('finish', function(){
40-
resolve();
41-
});
42-
}).then(function(){
43-
return renameFile(fullPath + '.gz', fullPath);
44-
}).then(function(){
45-
ui.write(blue('| '));
46-
ui.write(blue('- ✔ ' + filePath + '\n'));
47-
return filePath;
48-
});
49-
}
50-
51-
function _gzipFiles(distDir, distFiles, filePattern, ui) {
52-
var filesToGzip = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
53-
return Promise.map(filesToGzip, _gzipFileInPlace.bind(this, ui, distDir));
54-
}
55-
56-
function _beginMessage(ui, indexPath) {
57-
ui.write(blue('| '));
58-
ui.write(blue('- gzipping `' + indexPath + '`\n'));
59-
60-
return Promise.resolve();
61-
}
62-
63-
function _successMessage(ui, count) {
64-
ui.write(blue('| '));
65-
ui.write(blue('- gzipped ' + count + ' files ok\n'));
66-
}
67-
68-
function _errorMessage(ui, error) {
69-
ui.write(blue('| '));
70-
ui.write(red('- ' + error + '`\n'));
71-
72-
return Promise.reject(error);
73-
}
74-
75-
return {
21+
var DeployPlugin = DeployPluginBase.extend({
7622
name: options.name,
77-
78-
configure: function(context) {
79-
var deployment = context.deployment;
80-
var ui = deployment.ui;
81-
var config = deployment.config[this.name] = deployment.config[this.name] || {};
82-
83-
return validateConfig(ui, config)
84-
.then(function() {
85-
ui.write(blue('| '));
86-
ui.writeLine(blue('- config ok'));
87-
});
23+
defaultConfig: {
24+
filePattern: '**/*.{js,css,png,gif,jpg,map,xml,txt,svg,eot,ttf,woff,woff2}',
25+
distDir: function(context){
26+
return context.distDir;
27+
},
28+
distFiles: function(context){
29+
return context.distFiles;
30+
}
8831
},
8932

9033
willUpload: function(context) {
91-
var deployment = context.deployment;
92-
var ui = deployment.ui;
93-
var config = deployment.config[this.name] || {};
34+
var self = this;
9435

95-
var filePattern = config.filePattern;
96-
var distDir = context.distDir;
97-
var distFiles = context.distFiles || [];
36+
var filePattern = this.readConfig('filePattern');
37+
var distDir = this.readConfig('distDir');
38+
var distFiles = this.readConfig('distFiles') || [];
9839

99-
return _beginMessage(ui, filePattern)
100-
.then(_gzipFiles.bind(this, distDir, distFiles, filePattern, ui))
40+
this.log('gzipping `' + filePattern + '`');
41+
return this._gzipFiles(distDir, distFiles, filePattern)
10142
.then(function(gzippedFiles) {
102-
_successMessage(ui, gzippedFiles.length);
43+
self.log('gzipped ' + gzippedFiles.length + ' files ok');
10344
return { gzippedFiles: gzippedFiles };
10445
})
105-
.catch(_errorMessage.bind(this, ui));
46+
.catch(this._errorMessage.bind(this));
47+
},
48+
_gzipFiles: function(distDir, distFiles, filePattern) {
49+
var filesToGzip = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
50+
debugger;
51+
return Promise.map(filesToGzip, this._gzipFileInPlace.bind(this, distDir));
52+
},
53+
_gzipFileInPlace: function(distDir, filePath) {
54+
var self = this;
55+
var fullPath = path.join(distDir, filePath);
56+
return new Promise(function(resolve, reject) {
57+
var gzip = zlib.createGzip();
58+
var inp = fs.createReadStream(fullPath);
59+
var out = fs.createWriteStream(fullPath + '.gz');
60+
61+
inp.pipe(gzip).pipe(out);
62+
inp.on('error', function(err){
63+
reject(err);
64+
});
65+
out.on('error', function(err){
66+
reject(err);
67+
});
68+
out.on('finish', function(){
69+
resolve();
70+
});
71+
}).then(function(){
72+
return renameFile(fullPath + '.gz', fullPath);
73+
}).then(function(){
74+
self.log('✔ ' + filePath);
75+
return filePath;
76+
});
77+
},
78+
_errorMessage: function(error) {
79+
this.log(error, { color: 'red' });
80+
return Promise.reject(error);
10681
}
107-
};
82+
});
83+
return new DeployPlugin();
10884
}
10985
};

lib/utilities/validate-config.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"dependencies": {
4646
"chalk": "^1.0.0",
4747
"core-object": "^1.1.0",
48+
"ember-cli-deploy-plugin": "0.1.1",
4849
"ember-cli-babel": "^5.0.0",
4950
"minimatch": "^2.0.8",
5051
"redis": "^0.12.1",

tests/unit/index-nodetest.js

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ var path = require('path');
77
var rimraf = Promise.denodeify(require('rimraf'));
88

99
describe('gzip plugin', function() {
10-
var subject;
10+
var subject, mockUi, config;
1111

12-
before(function() {
12+
beforeEach(function() {
1313
subject = require('../../index');
14+
mockUi = {
15+
messages: [],
16+
write: function() { },
17+
writeLine: function(message) {
18+
this.messages.push(message);
19+
}
20+
};
1421
});
1522

1623
it('has a name', function() {
@@ -31,23 +38,68 @@ describe('gzip plugin', function() {
3138
});
3239

3340
describe('configure hook', function() {
34-
it('resolves if config is ok', function() {
35-
var plugin = subject.createDeployPlugin({
36-
name: 'gzip'
41+
var plugin, context;
42+
describe('without providing config', function () {
43+
beforeEach(function() {
44+
config = { };
45+
plugin = subject.createDeployPlugin({
46+
name: 'gzip'
47+
});
48+
context = {
49+
ui: mockUi,
50+
config: config
51+
};
52+
plugin.beforeHook(context);
3753
});
54+
it('warns about missing optional config', function() {
55+
plugin.configure(context);
56+
var messages = mockUi.messages.reduce(function(previous, current) {
57+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
58+
previous.push(current);
59+
}
60+
61+
return previous;
62+
}, []);
3863

39-
var context = {
40-
deployment: {
41-
ui: { write: function() {}, writeLine: function() {} },
42-
config: {
43-
gzip: {
44-
filePattern: '**/*.js'
45-
}
64+
assert.equal(messages.length, 3);
65+
});
66+
67+
it('adds default config to the config object', function() {
68+
plugin.configure(context);
69+
assert.isDefined(config.gzip.filePattern);
70+
assert.isDefined(config.gzip.distDir);
71+
assert.isDefined(config.gzip.distFiles);
72+
});
73+
});
74+
describe('with a filePattern, distDir, and distFiles provided', function () {
75+
beforeEach(function() {
76+
config = {
77+
gzip: {
78+
filePattern: '**/*.*',
79+
distDir: 'tmp/dist-deploy',
80+
distFiles: []
81+
}
82+
};
83+
plugin = subject.createDeployPlugin({
84+
name: 'gzip'
85+
});
86+
context = {
87+
ui: mockUi,
88+
config: config
89+
};
90+
plugin.beforeHook(context);
91+
});
92+
it('does not warn about missing optional config', function() {
93+
plugin.configure(context);
94+
var messages = mockUi.messages.reduce(function(previous, current) {
95+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
96+
previous.push(current);
4697
}
47-
}
48-
};
4998

50-
return assert.isFulfilled(plugin.configure.call(plugin, context))
99+
return previous;
100+
}, []);
101+
assert.equal(messages.length, 0);
102+
});
51103
});
52104
});
53105

@@ -66,13 +118,13 @@ describe('gzip plugin', function() {
66118
'assets/foo.js',
67119
'assets/bar.notjs',
68120
],
69-
deployment: {
70-
ui: { write: function() {} },
71-
project: { name: function() { return 'test-project'; } },
72-
config: {
73-
gzip: {
74-
filePattern: '**/*.js'
75-
}
121+
ui: mockUi,
122+
project: { name: function() { return 'test-project'; } },
123+
config: {
124+
gzip: {
125+
filePattern: '**/*.js',
126+
distDir: function(context){ return context.distDir; },
127+
distFiles: function(context){ return context.distFiles; }
76128
}
77129
}
78130
};
@@ -81,14 +133,15 @@ describe('gzip plugin', function() {
81133
if (!fs.existsSync(path.join(context.distDir, 'assets'))) { fs.mkdirSync(path.join(context.distDir, 'assets')); }
82134
fs.writeFileSync(path.join(context.distDir, context.distFiles[0]), 'alert("Hello foo world!");', 'utf8');
83135
fs.writeFileSync(path.join(context.distDir, context.distFiles[1]), 'alert("Hello bar world!");', 'utf8');
136+
plugin.beforeHook(context);
84137
});
85138

86139
afterEach(function(){
87140
return rimraf(context.distDir);
88141
});
89142

90143
it('gzips the matching files', function(done) {
91-
return assert.isFulfilled(plugin.willUpload.call(plugin, context))
144+
return assert.isFulfilled(plugin.willUpload(context))
92145
.then(function(result) {
93146
assert.deepEqual(result, { gzippedFiles: ['assets/foo.js'] });
94147
done();

0 commit comments

Comments
 (0)