-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
238 lines (217 loc) · 6.96 KB
/
gulpfile.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const babel = require("gulp-babel");
const compass = require("gulp-compass");
const concat = require("gulp-concat");
const gulpif = require('gulp-if');
const gulp = require("gulp");
const cleanCss = require("gulp-clean-css");
const uglify = require('gulp-uglify');
const util = require('gulp-util');
const stripJsonComments = require('gulp-strip-json-comments');
const _ = require('lodash');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const fs = require('fs');
const glob = require("glob");
const path = require('path');
const md5File = require('md5-file');
const mkdirp = require('mkdirp');
const distJs = 'dist/js';
const distCss = 'dist/css';
const env = util.env.env || 'development';
const isProduction = env === 'production';
console.log('Using environment: ' + env);
mkdirp.sync(distJs);
mkdirp.sync(`${distCss}/fancybox`);
const paths = (() => {
const vendorScripts = [
'jquery',
'lodash',
'popper',
'bootstrap',
'moment',
'moment-fi',
'fancybox/jquery.fancybox',
'select2.full',
'invert-color',
// TODO: Separate place for highcharts & d3
'd3',
'd3.layout.cloud',
'highcharts/highcharts',
'highcharts/highcharts-more'
//'highcharts/themes/gray'
].map(function(prefix) {
prefix = 'src/js/vendor/' + prefix;
return isProduction ? prefix + '.min.js' : prefix + '.js';
});
console.log('Using vendor scripts: ' + vendorScripts.join(' '));
const vendorCss = [
'bootstrap',
'select2'
].map(name => `src/css/vendor/${name}.css`);
const siteJs = [
//'highcharts.screenshot-theme',
'highcharts.theme',
'main',
'util',
'twitter-user-graph'
].map(file => `src/js/${file}.js`);
return {
siteJs,
resources: 'src/resources/**/*',
customJs: 'src/js/custom/**/*',
vendorJs: vendorScripts,
vendorCss : vendorCss,
siteSass: 'src/sass/**/*.scss'
};
})();
gulp.task('clean', () => {
return glob("./dist/*", {}, (er, items) => {
items.forEach(file => {
exec('rm -r ' + file, function (err, stdout, stderr ) {
if (!err) console.log(`Removed ${file}`)
});
})
});
});
gulp.task('processContent', () => {
const isImage = name => /\.(gif|jpg|png)$/.test(name);
const containsWhitespace = str => /.*\s.*/.test(str);
const startTime = Date.now();
const sizeConversions = [
['thumb', `convert -strip -resize "200x200^" -gravity Center -crop 200x200+0+0 +repage`],
['crop', `convert -strip -resize "420x250^" -gravity Center -crop 420x250+0+0 +repage`],
['aside', `convert -strip -resize "700x500>"`],
['listing', `convert -strip -resize "800x600>" -modulate 120,80`],
['large', `convert -strip -resize "1920x1000>"`]
];
return glob("./content/**/*", {nodir: true}, (error, files) => {
const images = _.filter(files, isImage);
let resizeCount = 0;
_.forEach(images, img => {
const target = img.replace(/\/content\//, '/data/');
const targetDir = path.parse(target).dir;
const filename = path.basename(target);
const ext = path.extname(filename); // e.g. `.jpg`
const basename = path.basename(filename, ext);
const resizeTargets = _.map(sizeConversions, ([size, convert]) => {
const targetFile = `${targetDir}/${basename}.${size}${ext}`;
return {
targetFile,
resizeCommand: `${convert} ${img} ${targetFile}`,
optimizeCommand:
ext === '.jpg' ? `jpegoptim ${targetFile}` :
ext === '.png' ? `optipng -quiet ${targetFile}` :
null
};
});
const allTargetsExist = (() => {
const targetFiles =_.map(resizeTargets, 'targetFile').concat([target]);
if (_.some(targetFiles, containsWhitespace)) {
console.error(`Cannot process file: ${img}`);
process.exit(1);
}
return _.every(targetFiles, f => {
try {
fs.statSync(f);
return true;
} catch (err) {
return false;
}
}
);
})();
if (allTargetsExist && md5File.sync(img) === md5File.sync(target)) {
// Do nothing
} else {
mkdirp.sync(targetDir);
const commands = [
`cp ${img} ${target}`,
..._.map(resizeTargets, 'resizeCommand'),
..._.compact(_.map(resizeTargets, 'optimizeCommand')),
];
_.forEach(commands, cmd => {
execSync(cmd, function (err, stdout, stderr) {
console.log(err);
console.log(stdout);
console.log(stderr);
if (err) {
process.exit(1);
}
});
});
util.log(`Resized ${img}`);
resizeCount += 1;
}
});
util.log(`Resized ${resizeCount} / ${_.size(images)} content images in ${Date.now() - startTime} ms`);
});
});
gulp.task('compass', () => {
return gulp.src(paths.siteSass).
pipe(compass({
// config_file: 'public/config.rb'
environment : 'production',
style : isProduction ? 'compact' : 'expanded',
images_dir : 'img',
image : 'img',
css : 'dist/css',
javascript : 'src/js',
sass : 'src/sass',
relative : false
})).
pipe(gulp.dest(distCss));
});
gulp.task('vendorCss', () => (
gulp.src(paths.vendorCss).
pipe(cleanCss({compatibility: ''})).
pipe(concat('vendor.css')).
pipe(gulp.dest(distCss))
));
gulp.task('vendorCssAssets', () => (
gulp.src('./src/css/vendor/fancybox/**/*').
pipe(gulp.dest(`${distCss}/fancybox`))
));
gulp.task('siteJs', () => {
return gulp.src(paths.siteJs).
pipe(babel()).
pipe(gulpif(isProduction, uglify({
mangle: true,
compress: { drop_console: true }
}))).
pipe(concat('site.js')).
pipe(gulp.dest(distJs));
});
gulp.task('resources', () => (
gulp.
src(paths.resources).
pipe(
gulpif(
f => path.extname(f.path) === '.json',
stripJsonComments({whitespace: false})
)
).
pipe(gulp.dest('dist/resources'))
));
gulp.task('customJs', () => {
return gulp.src(paths.customJs).
pipe(babel()).
pipe(gulpif(isProduction, uglify({
mangle: true,
compress: { drop_console: true }
}))).
pipe(gulp.dest(`${distJs}/custom/`));
});
gulp.task('vendorJs', () => {
return gulp.src(paths.vendorJs).
pipe(concat('vendor.js')).
pipe(gulp.dest(distJs));
});
gulp.task('fileWatch', () => {
gulp.watch(paths.siteJs, ['siteJs']);
gulp.watch(paths.resources, ['resources']);
gulp.watch(paths.customJs, ['customJs']);
gulp.watch(paths.siteSass, ['compass']);
});
gulp.task('pipeline', ['siteJs', 'resources', 'customJs', 'vendorJs', 'vendorCss', 'vendorCssAssets', 'compass']);
gulp.task('default', ['pipeline']);
gulp.task('watch', ['pipeline', 'fileWatch']);