-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
127 lines (115 loc) · 4.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*eslint-env node*/
'use strict';
var RSVP = require('rsvp');
var fs = require('fs');
var path = require('path');
var minimatch = require('minimatch');
var denodeify = require('rsvp').denodeify;
var renameFile = denodeify(fs.rename);
var DeployPluginBase = require('ember-cli-deploy-plugin');
module.exports = {
name: 'ember-cli-deploy-gzip',
createDeployPlugin: function(options) {
var fs = require('fs');
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
filePattern: '**/*.{js,css,json,ico,map,xml,txt,svg,eot,ttf}',
ignorePattern: null,
zopfli: false,
keep: false,
distDir: function(context){
return context.distDir;
},
distFiles: function(context){
return context.distFiles;
}
},
configure: function(context) {
this._super.configure.call(this, context);
if (this.readConfig('zopfli')) {
this.log("Using zopfli for compression", { verbose: true });
this.gzipLibrary = this.project.require('node-zopfli');
} else {
this.gzipLibrary = require('zlib');
}
},
willUpload: function(/* context */) {
var self = this;
var filePattern = this.readConfig('filePattern');
var ignorePattern = this.readConfig('ignorePattern');
var distDir = this.readConfig('distDir');
var distFiles = this.readConfig('distFiles') || [];
var keep = this.readConfig('keep');
this.log('gzipping `' + filePattern + '`', { verbose: true });
this.log('ignoring `' + ignorePattern + '`', { verbose: true });
return this._gzipFiles(distDir, distFiles, filePattern, ignorePattern, keep)
.then(function(gzippedFiles) {
self.log('gzipped ' + gzippedFiles.length + ' files ok', { verbose: true });
if (keep) {
self.log('keep is enabled, added gzipped files to `context.distFiles`', { verbose: true });
return {
distFiles: [].concat(gzippedFiles), // needs to be a copy
gzippedFiles: gzippedFiles
};
}
return { gzippedFiles: gzippedFiles };
})
.catch(this._errorMessage.bind(this));
},
_gzipFiles: function(distDir, distFiles, filePattern, ignorePattern, keep) {
var filesToGzip = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
if (ignorePattern != null) {
filesToGzip = filesToGzip.filter(function(path){
return !minimatch(path, ignorePattern, { matchBase: true });
});
}
return RSVP.map(filesToGzip, this._gzipFile.bind(this, distDir, keep));
},
_gzipFile: function(distDir, keep, filePath) {
var self = this;
var fullPath = path.join(distDir, filePath);
var outFilePath = fullPath + '.gz';
return new RSVP.Promise(function(resolve, reject) {
var gzip = self.gzipLibrary.createGzip({ format: 'gzip' });
var inp = fs.createReadStream(fullPath);
var out = fs.createWriteStream(outFilePath);
inp.pipe(gzip).pipe(out);
inp.on('error', function(err){
reject(err);
});
out.on('error', function(err){
reject(err);
});
out.on('finish', function(){
// set mtime of gz file to mtime of original
fs.stat(fullPath, function(err, stats) {
if (err) resolve(); // ignore errors
else {
fs.utimes(outFilePath, new Date(), stats.mtime, function () {
resolve();
});
}
});
});
}).then(function(){
if(!keep) {
return renameFile(fullPath + '.gz', fullPath).then(function() {
return filePath;
});
} else {
return filePath + '.gz';
}
}).then(function(outFilePath){
self.log('✔ ' + outFilePath, { verbose: true });
return outFilePath;
});
},
_errorMessage: function(error) {
this.log(error, { color: 'red' });
return RSVP.reject(error);
}
});
return new DeployPlugin();
}
};