Skip to content

Commit 165151c

Browse files
committed
Merge pull request #12 from dschmidt/keep
Add keep option with tests
2 parents 4997c4c + 7977f42 commit 165151c

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ If set to `true`, you will need to `npm install node-zopfli --save-dev` in your
7878

7979
*Default:* `false`
8080

81+
### keep
82+
83+
Keep original file and write compressed data to `originalFile.gz`
84+
85+
*Default:* `false`
86+
8187
## Prequisites
8288

8389
The following properties are expected to be present on the deployment `context` object:
@@ -97,8 +103,3 @@ The following properties are expected to be present on the deployment `context`
97103
[1]: http://ember-cli.github.io/ember-cli-deploy/plugins "Plugin Documentation"
98104
[2]: https://github.com/zapnito/ember-cli-deploy-build "ember-cli-deploy-build"
99105
[3]: https://github.com/zapnito/ember-cli-deploy-s3 "ember-cli-deploy-s3"
100-
101-
## TODO
102-
103-
Some deployment flows require side-by-side deployment of compressed and uncompressed
104-
assets. A PR to add this option would be welcome.

index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
defaultConfig: {
2323
filePattern: '**/*.{js,css,png,gif,ico,jpg,map,xml,txt,svg,swf,eot,ttf,woff,woff2}',
2424
zopfli: false,
25+
keep: false,
2526
distDir: function(context){
2627
return context.distDir;
2728
},
@@ -46,26 +47,28 @@ module.exports = {
4647
var filePattern = this.readConfig('filePattern');
4748
var distDir = this.readConfig('distDir');
4849
var distFiles = this.readConfig('distFiles') || [];
50+
var keep = this.readConfig('keep');
4951

5052
this.log('gzipping `' + filePattern + '`');
51-
return this._gzipFiles(distDir, distFiles, filePattern)
53+
return this._gzipFiles(distDir, distFiles, filePattern, keep)
5254
.then(function(gzippedFiles) {
5355
self.log('gzipped ' + gzippedFiles.length + ' files ok');
5456
return { gzippedFiles: gzippedFiles };
5557
})
5658
.catch(this._errorMessage.bind(this));
5759
},
58-
_gzipFiles: function(distDir, distFiles, filePattern, gzipLibrary) {
60+
_gzipFiles: function(distDir, distFiles, filePattern, keep) {
5961
var filesToGzip = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
60-
return Promise.map(filesToGzip, this._gzipFileInPlace.bind(this, distDir));
62+
return Promise.map(filesToGzip, this._gzipFile.bind(this, distDir, keep));
6163
},
62-
_gzipFileInPlace: function(distDir, filePath) {
64+
_gzipFile: function(distDir, keep, filePath) {
6365
var self = this;
6466
var fullPath = path.join(distDir, filePath);
67+
var outFilePath = fullPath + '.gz';
6568
return new Promise(function(resolve, reject) {
6669
var gzip = self.gzipLibrary.createGzip({ format: 'gzip' });
6770
var inp = fs.createReadStream(fullPath);
68-
var out = fs.createWriteStream(fullPath + '.gz');
71+
var out = fs.createWriteStream(outFilePath);
6972

7073
inp.pipe(gzip).pipe(out);
7174
inp.on('error', function(err){
@@ -78,10 +81,17 @@ module.exports = {
7881
resolve();
7982
});
8083
}).then(function(){
81-
return renameFile(fullPath + '.gz', fullPath);
82-
}).then(function(){
83-
self.log('✔ ' + filePath);
84-
return filePath;
84+
if(!keep) {
85+
return renameFile(fullPath + '.gz', fullPath).then(function() {
86+
return filePath;
87+
});
88+
} else {
89+
return filePath + '.gz';
90+
}
91+
}).then(function(outFilePath){
92+
self.log('✔ ' + outFilePath);
93+
94+
return outFilePath;
8595
});
8696
},
8797
_errorMessage: function(error) {

tests/unit/index-nodetest.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('gzip plugin', function() {
6161
return previous;
6262
}, []);
6363

64-
assert.equal(messages.length, 4);
64+
assert.equal(messages.length, 5);
6565
});
6666

6767
it('adds default config to the config object', function() {
@@ -78,6 +78,7 @@ describe('gzip plugin', function() {
7878
gzip: {
7979
filePattern: '**/*.*',
8080
zopfli: false,
81+
keep: false,
8182
distDir: 'tmp/dist-deploy',
8283
distFiles: []
8384
}
@@ -152,5 +153,16 @@ describe('gzip plugin', function() {
152153
done(reason);
153154
});
154155
});
156+
157+
it('gzips the matching files with .gz suffix when keep is enabled', function(done) {
158+
context.config.gzip.keep = true;
159+
return assert.isFulfilled(plugin.willUpload(context))
160+
.then(function(result) {
161+
assert.deepEqual(result, { gzippedFiles: ['assets/foo.js.gz'] });
162+
done();
163+
}).catch(function(reason){
164+
done(reason);
165+
});
166+
});
155167
});
156168
});

0 commit comments

Comments
 (0)