-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
372 lines (303 loc) · 9.62 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// ===================================================
// Settin'
// ===================================================
var gulp = require('gulp'),
gulpLoadPlugins = require('gulp-load-plugins'),
$ = gulpLoadPlugins({
rename: {
'gulp-minify-css' : 'mincss',
'gulp-minify-html' : 'minhtml',
'gulp-gh-pages' : 'ghPages',
'gulp-foreach' : 'foreach',
'gulp-mocha' : 'mocha',
'gulp-if' : 'if'
}
}),
assemble = require('assemble'),
del = require('del'),
merge = require('merge-stream'),
basename = require('path').basename,
extname = require('path').extname;
$.exec = require('child_process').exec;
$.fs = require('fs');
// ===================================================
// Configin'
// ===================================================
var env_flag = false;
var asset_dir = {
site: 'site',
templates : 'templates',
data: 'data',
dist: 'dist',
js: 'js',
css: 'css',
sass: 'src'
};
var path = {
site: asset_dir.site,
data: asset_dir.data,
templates: asset_dir.site + '/' + asset_dir.templates,
dist: asset_dir.dist,
js: asset_dir.site + '/' + asset_dir.js,
css: asset_dir.site + '/' + asset_dir.css,
sass: asset_dir.site + '/' + asset_dir.css + '/' + asset_dir.sass
};
var glob = {
html: path.site + '/*.html',
css: path.css + '/*.css',
sass: path.sass + '/**/*.scss',
js: path.js + '/src/**/*.js',
jslibs : path.js + '/lib/**/*.js',
layouts: path.templates + '/layouts/*.{md,hbs}',
pages: path.templates + '/pages/**/*.{md,hbs}',
includes: path.templates + '/includes/**/*.{md,hbs}',
data: path.data + '/**/*.{json,yaml}',
rootData: ['site.yaml', 'package.json']
};
// ===================================================
// Developin'
// ===================================================
gulp.task('serve', ['assemble'], function() {
$.connect.server({
root: [path.site],
port: 5000,
livereload: true,
middleware: function(connect) {
return [
connect().use(connect.query())
];
}
});
$.exec('open http://localhost:5000');
});
// ===================================================
// Previewin'
// ===================================================
gulp.task('preview', function() {
$.connect.server({
root: [path.dist],
port: 5001
});
$.exec('open http://localhost:5001');
});
// ===================================================
// Testin'
// ===================================================
gulp.task('mocha', function () {
return gulp.src('test/*.js', {read: false})
.pipe($.mocha({ reporter: 'nyan' }));
});
// ===================================================
// Stylin'
// ===================================================
gulp.task('sass', function() {
var stream = gulp.src(glob.sass)
.pipe($.sass())
.pipe($.autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest(path.css))
.pipe($.connect.reload());
return stream;
});
// ===================================================
// Templatin'
// ===================================================
// Pull #3. Also see https://github.com/assemble/assemble/issues/715
// create a `categories` object to keep categories in (e.g. 'clients')
// categories: {
// clients: {
// "polyon": { ... }
// }
// };
assemble.set('categories', {});
/**
Populate categories with pages that specify the categories they belong to.
When the onLoad middleware runs for a single file, it looks at the file's front-matter (file.data) to see if it contains a categories property. This property can be a string or an array of strings. If it exists, then the middleware updates the categories object for each category in the array. In the case of polyon.hbs, there is only 1 category called client, so the categories object becomes:
categories: {
clients: {
"polyon": { ... }
}
};
*/
assemble.onLoad(/\.hbs/, function(file, next) {
// if the file doesn't have a data object or
// doesn't contain `categories` in it's
// front-matter, move on.
if (!file.data || !file.data.categories) {
return next();
}
// use the default `renameKey` function to store
// pages on the `categories` object
var renameKey = assemble.option('renameKey');
// get the categories object
var categories = assemble.get('categories');
// figure out which categories this file belongs to
var cats = file.data.categories;
cats = Array.isArray(cats) ? cats : [cats];
// add this file's data (file object) to each of
// it's catogories
cats.forEach(function(cat) {
categories[cat] = categories[cat] || [];
categories[cat][renameKey(file.path)] = file;
});
// done
next();
});
/**
* Handlebars helper to iterate over an object of pages for a specific category
*
* ```
* {{#category "clients"}}
* <li>{{data.summary}}</li>
* {{/category}}
* ```
*/
assemble.helper('category', function(category, options) {
var pages = this.app.get('categories.' + category);
if (!pages) {
return '';
}
return Object.keys(pages).map(function(page) {
// this renders the block between `{{#category}}` and `{{category}}` passing the
// entire page object as the context.
// If you only want to use the page's front-matter, then change this to something like
// return options.fn(pages[page].data);
return options.fn(pages[page]).toLowerCase();
}).join('\n');
});
/**
* Load data onto assemble cache.
* This loads data from `glob.data` and `glob.rootData`.
* When loading `glob.rootData`, use a custom namespace function
* to return `pkg` for `package.json`.
*
* After all data is loaded, process the data to resolve templates
* in values.
* @doowb PR: https://github.com/grayghostvisuals/grayghostvisuals/pull/5
*/
function loadData() {
assemble.data(glob.data);
assemble.data(assemble.plasma(glob.rootData, {namespace: function (fp) {
var name = basename(fp, extname(fp));
if (name === 'package') return 'pkg';
return name;
}}));
assemble.data(assemble.process(assemble.data()));
}
// Placing assemble setups inside the task allows
// live reloading/monitoring for files changes.
gulp.task('assemble', function() {
assemble.option('production', env_flag);
assemble.option('layout', 'default');
assemble.layouts(glob.layouts);
assemble.partials(glob.includes);
loadData();
var stream = assemble.src(glob.pages)
.pipe($.extname())
.pipe(assemble.dest(path.site))
.pipe($.connect.reload());
return stream;
});
// ===================================================
// Optimizin'
// ===================================================
gulp.task('svgstore', function() {
return gulp
.src(path.site + '/img/icons/linear/*.svg')
.pipe($.svgmin({
plugins: [{
removeDoctype: true
}]
}))
.pipe($.svgstore())
.pipe($.cheerio(function($) {
$('svg').attr('style', 'display:none');
}))
.pipe(gulp.dest(path.templates + '/includes/atoms/svg-sprite.svg'));
});
// ===================================================
// Minifyin'
// ===================================================
gulp.task('cssmin', ['sass'], function() {
var stream = gulp.src(glob.css)
.pipe($.mincss({ keepBreaks:true }))
.pipe(gulp.dest(path.css));
return stream;
});
// ===================================================
// Buildin'
// ===================================================
/*
* foreach is because usemin 0.3.11 won't manipulate
* multiple files as an array.
*/
gulp.task('usemin', ['assemble', 'cssmin'], function() {
return gulp.src(glob.html)
.pipe($.foreach(function(stream, file) {
return stream
.pipe($.usemin({
assetsDir: path.site,
css: [ $.rev() ],
html: [ $.minhtml({ empty: true }) ],
js: [ $.uglify(), $.rev() ]
}))
.pipe(gulp.dest(path.dist));
}));
});
// ===================================================
// Duplicatin'
// ===================================================
0
gulp.task('copy', ['usemin'], function() {
return merge(
gulp.src([path.site + '/{img,bower_components,js/lib}/**/*'])
.pipe(gulp.dest(path.dist)),
gulp.src([
'webhook.php',
path.site + '/*.{ico,png,txt}',
path.site + '/.htaccess',
]).pipe(gulp.dest(path.dist))
);
});
// ===================================================
// Releasin'
// ===================================================
gulp.task('deploy', function() {
return gulp.src([path.dist + '/**/*', path.dist + '/.htaccess' ])
.pipe($.ghPages(
$.if(env_flag === false,
{ branch: 'staging' },
{ branch: 'production' })
));
});
// ===================================================
// Cleanin'
// ===================================================
gulp.task('clean', function(cb) {
del([
'dist',
glob.css,
path.site + '/client',
glob.html
], cb);
});
// ===================================================
// Monitorin'
// ===================================================
gulp.task('watch', function() {
gulp.watch([
glob.sass
], ['sass']);
gulp.watch([
glob.includes,
glob.pages,
glob.layouts
], ['assemble']);
});
// ===================================================
// Taskin'
// ===================================================
gulp.task('build', [ 'copy','usemin' ]);
gulp.task('default', [ 'sass','assemble','serve','watch' ]);