-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
194 lines (158 loc) · 6.67 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var tap = require('gulp-tap');
var _ = require('lodash');
var chalk = require('chalk');
var gm = require('gm');
var when = require('when');
var nodefn = require('when/node');
var path = require('path');
var fs = require('fs');
var fileActions = {
'\\.(jpe?g|png|gif)$': {
maxSize: 1000000,
image: true,
maxWidth: 2048,
maxHeight: 1536,
},
'\\.svg$': { maxSize: 1000000 },
'\\.(mp4|m4v|ogv)': { maxSize: 10000000 },
'\\.(mp3|ogg|aac|m4a)': { maxSize: 2000000},
'.*' : { fileNames: true }
};
function parseNumber(str) {
try {
return parseFloat(str.match(/\((.*)\)/)[1]);
} catch (ex) {
return NaN;
}
}
module.exports = function fileSizeChecker(lfa) {
function warn(file, msg) {
var err = new Error(msg);
err.fileName = file.history[0];
err.nameLess = true;
lfa.logWarning(err);
}
var fixes = [];
var videoRegexp = /^video\/(.*)\.(mp4|webm|ogv)$/;
var audioRegexp = /^audio\/(.*)\.(mp3|ogg)$/;
function renameAsset(assetsPath, from, to) {
var relativeFromPath = path.relative(assetsPath, from);
var relativeToPath = path.relative(assetsPath, to);
var escapedRelativeFromPath = relativeFromPath.replace(/(\/|\.)/g, '\\\\$1');
var escapedRelativeToPath = relativeToPath.replace(/(\/)/g, '\\\\$1');
var textPath = path.join(lfa.config.projectPath, 'text');
var stylesPath = path.join(lfa.config.projectPath, 'styles');
var jsPath = path.join(lfa.config.projectPath, 'js');
fixes.push('find "' + textPath + '" -type f \\( -name "*.jade" \\) -exec sed -i "" "s/' + escapedRelativeFromPath + '/' + escapedRelativeToPath + '/g" {} \\;');
fixes.push('find "' + stylesPath + '" -type f \\( -name "*.styl" -or -name "*.css" \\) -exec sed -i "" "s/' + escapedRelativeFromPath + '/' + escapedRelativeToPath + '/g" {} \\;');
fixes.push('find "' + jsPath + '" -type f \\( -name "*.js" -or -name "*.jsx" -or -name "*.json" \\) -exec sed -i "" "s/' + escapedRelativeFromPath + '/' + escapedRelativeToPath + '/g" {} \\;');
_.each([videoRegexp, audioRegexp], function (regexp) {
var match = relativeFromPath.match(regexp);
if (match) {
var relativeFromPath_ = match[1];
var relativeToPath_ = relativeToPath.match(regexp)[1];
var escapedRelativeFromPath_ = relativeFromPath_.replace(/(\/|\.)/g, '\\\\$1');
var escapedRelativeToPath_ = relativeToPath_.replace(/(\/)/g, '\\\\$1');
fixes.push('find "' + textPath + '" -type f \\( -name "*.jade" \\) -exec sed -i "" "s/' + escapedRelativeFromPath_ + '/' + escapedRelativeToPath_ + '/g" {} \\;');
}
});
}
function applyFix(file, fix) {
if (!fix) { return; }
var fileName = file.history[0];
var assetsPath = path.join(lfa.config.projectPath, 'assets');
if (fileName.indexOf(assetsPath) !== 0) { return; }
if (fix === 'pngcrush') {
fixes.push('pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB "' + fileName + '" "' + fileName + '.crushed.png"');
fixes.push('mv "' + fileName + '.crushed.png" "' + fileName + '"');
return;
}
if (fix === 'tojpg') {
var jpegPath = fileName.replace(/\.png$/, '.jpg');
fixes.push('gm convert "' + fileName + '" "' + jpegPath + '"');
fixes.push('rm "' + fileName + '"');
renameAsset(assetsPath, fileName, jpegPath);
return;
}
if (fix === 'rename') {
var relative = path.relative(assetsPath, fileName);
var newRelative = relative.replace(/ /g, '_').toLowerCase();
var newName = path.resolve(assetsPath, newRelative);
if (path.dirname(fileName) !== path.dirname(newName)) {
fixes.push('mkdir -p "' + path.dirname(newName) + '"');
}
fixes.push('mv "' + fileName + '" "' + newName + '"');
renameAsset(assetsPath, fileName, newName);
return;
}
}
lfa.task('assets:pre-write:file-size-checker', function (stream) {
if (lfa.currentCompile.debug) {
return stream;
}
var assetsPath = path.join(lfa.config.projectPath, 'assets');
var promises = [];
var returnStream = stream.pipe(tap(function (file) {
if (!file.stat.isFile()) {
return file;
}
var actions = lfa.config.package.fileSizeCheckerActions || fileActions;
promises = promises.concat(_.map(actions, function (options, regexp) {
if (!new RegExp(regexp).test(file.path)) {
return;
}
var fix = null;
var tooBig = false;
if (options.maxSize && file.stat.size >= options.maxSize) {
tooBig = true;
warn(file, 'File size (' + file.stat.size + ') bigger than ' + options.maxSize);
}
if (options.fileNames && /[ A-Z]/.test(path.relative(assetsPath, file.history[0]))) {
warn(file, 'Spaces or upper-case letters in file name');
fix = 'rename';
}
return when.try(function () {
if (!options.image) { return; }
var image = gm(file.history[0]);
return nodefn.call(image.identify.bind(image))
.then(function (data) {
if (tooBig && data.format === 'PNG') {
fix = 'pngcrush';
}
var size = data.size;
if ((options.maxWidth && size.width > options.maxWidth) ||
(options.maxHeight && size.height > options.maxHeight)) {
warn(file, 'Image dimensions too big: ' + size.width + 'x' + size.height + ' bigger than ' + options.maxWidth + 'x' + options.maxHeight);
}
if ((options.minWidth && size.width < options.minWidth) ||
(options.minHeight && size.height < options.minHeight)) {
warn(file, 'Image dimensions too small: ' + size.width + 'x' + size.height + ' smaller than ' + options.minWidth + 'x' + options.minHeight);
}
var chStats = data['Channel Statistics'] || {};
if (data.format === 'PNG' && (!chStats.Opacity || parseNumber(chStats.Opacity.Minimum) === 1)) {
fix = 'tojpg';
warn(file, 'Opaque image saved as PNG');
}
})
.catch(function (err) {
warn(file, 'Can\'t open image:\n' + err.message);
});
}).then(function () {
applyFix(file, fix);
});
}));
return file;
}));
returnStream.on('end', function () {
when.all(promises).then(function () {
if (fixes.length) {
lfa.logWarning('Saving a script with potential fixes in .lfa/build/fix.sh');
fixes.splice(0, 0, '#!/bin/bash');
var data = fixes.join('\n');
fs.writeFile(path.join(lfa.config.projectPath, '.lfa' ,'build', 'fix.sh'), data);
}
});
});
return returnStream;
});
};