-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
481 lines (391 loc) · 15 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* ghost-helm
* https://github.com/masondesu/ghost-helm
*
* Copyright (c) 2014 Mason Stewart
* Licensed under the MIT license.
*/
'use strict';
// Hey!
// _
// ________ ____ ___ __(_)_______ _____
// / ___/ _ \/ __ `/ / / / / ___/ _ \/ ___/
// / / / __/ /_/ / /_/ / / / / __(__ )
// /_/ \___/\__, /\__,_/_/_/ \___/____/
// /_/
var packageJSON = require(process.cwd() + '/package.json'),
gulp = require('gulp'),
gutil = require('gulp-util'),
plumber = require('gulp-plumber'),
exec = require('child_process').exec,
assign = require("lodash.assign"),
Q = require('q'),
autoprefixer = require("gulp-autoprefixer"),
cache = require("gulp-cache"),
csso = require("gulp-csso"),
directoryMap = require('gulp-directory-map'),
filter = require("gulp-filter"),
flatten = require("gulp-flatten"),
gulpIf = require("gulp-if"),
imagemin = require("gulp-imagemin"),
inject = require("gulp-inject"),
jadeEngine = require("jade"),
jade = require("gulp-jade"),
jshint = require("gulp-jshint"),
jshintStylish = require("jshint-stylish"),
livereload = require("gulp-livereload"),
markdown = require("gulp-markdown"),
mocha = require("gulp-mocha"),
plumber = require("gulp-plumber"),
prefix = require('gulp-prefix'),
print = require("gulp-print"),
rimraf = require("gulp-rimraf"),
sass = require("gulp-sass"),
sitemap = require("gulp-sitemap"),
size = require("gulp-size"),
uglify = require("gulp-uglify"),
useref = require("gulp-useref"),
util = require("gulp-util"),
fs = require('fs'),
es = require('event-stream'),
path = require('path'),
// the defaults
defaults = {
distDir: 'dist',
templatesDir: 'app/templates/pages',
templatesBaseDir: 'app/templates',
templatesDistDir: '.tmp',
stylesDir: 'app/styles/main.scss',
stylesDistDir: '.tmp/styles',
jsDir: 'app/scripts/**/*.js',
assetBuildDir: '.tmp/**/*.html',
userefSearchPath: ['app','.tmp'],
imagesDir: ['app/images/**/*', 'app/bower_components/ghost-shield/dist/images/**/*'],
imagesDistDir: 'dist/images',
fontDir: ['app/bower_components/ghost-shield/dist/webfonts/*'],
cname: 'CNAME',
sitemapDir: 'dist/**/*.html',
siteUrl: 'http://theironyard.com',
cleanDir: ['.tmp', 'dist'],
// deployCommand: 'sh deploy.sh',
deployCommand: 'ls',
urlsPath: 'urls.json',
sidebar: '.tmp/sidebar.html',
sidebarTemplate: 'app/templates/includes/sidebar.jade',
markdownLayout: 'app/templates/layouts/markdown-layout.jade',
siteColor: 'grey'
};
module.exports.setup = function(config, outerGulp){
// Overrite defaults with user config properties
config = config || {};
config = assign(defaults, config);
// _ __ __
// _____(_)___/ /__ / /_ ____ ______
// / ___/ / __ / _ \/ __ \/ __ `/ ___/
// (__ ) / /_/ / __/ /_/ / /_/ / /
// /____/_/\__,_/\___/_.___/\__,_/_/
gulp.task('generate-urls', ['sitemap'], function(){
return gulp.src([config.distDir + '/**/*.html', config.distDir + '/**/*.pdf'])
.pipe(print())
.pipe(directoryMap({
filename: config.urlsPath,
prefix: process.env.NODE_ENV === 'production' ? packageJSON.name : ''
}))
.pipe(gulp.dest(config.distDir));
});
gulp.task('sidebar', ['generate-urls'], function(){
// note.. this is dumb. namespacing logic needs to be replaced with a modified version of gulp-prefix
if (process.env.NODE_ENV === 'production')
var formattedJSON = JSON.parse(fs.readFileSync(path.join(config.distDir, config.urlsPath), 'utf8'))[packageJSON.name]
else
var formattedJSON = JSON.parse(fs.readFileSync(path.join(config.distDir, config.urlsPath), 'utf8'))
return gulp.src(config.sidebarTemplate)
.pipe(jade({
basedir: 'app',
data: { urls: formattedJSON }
}))
.pipe(gulp.dest(config.templatesDistDir))
.on('error', gutil.log);
});
gulp.task('inject-sidebar', ['sidebar'], function(){
return gulp.src(config.distDir + '/**/*.html')
// this does not appear to be working
.pipe(inject(gulp.src(config.sidebar), {
name: 'sidebar',
transform: function (filePath, file) {
// return file contents as string
return file.contents.toString('utf8')
}
}))
.pipe(gulp.dest(config.distDir))
.on('error', gutil.log);
});
// __ __ __
// / /____ ____ ___ ____ / /___ _/ /____ _____
// / __/ _ \/ __ `__ \/ __ \/ / __ `/ __/ _ \/ ___/
// / /_/ __/ / / / / / /_/ / / /_/ / /_/ __(__ )
// \__/\___/_/ /_/ /_/ .___/_/\__,_/\__/\___/____/
// /_/
// Let's take all of the jade templates and markdown files, pre-process them,
// and story them in .tmp
gulp.task('jade', ['clean'], function(){
return gulp.src(config.templatesDir + '/**/*.jade')
.pipe(jade({
basedir: config.templatesBaseDir,
pretty: true
}))
.pipe(gulp.dest(config.templatesDistDir));
});
gulp.task('markdown', ['clean'], function(){
return gulp.src(config.templatesDir + '/**/*.md')
.pipe(markdown())
.pipe(es.map(function(file, cb) {
var ghURL = packageJSON.name + '/blob/master' + file.path.replace(process.cwd(), '').replace('.html','.md');
var html = jadeEngine.renderFile(config.markdownLayout, {
basedir: config.templatesBaseDir,
pretty: true,
content: file.contents.toString('utf8'),
siteColor: packageJSON.siteColor ? packageJSON.siteColor : config.siteColor,
path: ghURL
});
file.contents = new Buffer(html);
cb(null, file);
}))
.pipe(gulp.dest(config.templatesDistDir));
})
gulp.task('templates', ['jade', 'markdown'], function(){
return gulp.src(config.templatesDir + '/**/*.html')
.pipe(gulp.dest(config.templatesDistDir));
});
// ______
// ____ ____/ / __/____
// / __ \/ __ / /_/ ___/
// / /_/ / /_/ / __(__ )
// / .___/\__,_/_/ /____/
// /_/
gulp.task('pdfs', ['clean'], function(){
return gulp.src(config.templatesDir + '/**/*.pdf')
.pipe(gulp.dest(config.distDir));
});
// __ __
// _____/ /___ __/ /__ _____
// / ___/ __/ / / / / _ \/ ___/
// (__ ) /_/ /_/ / / __(__ )
// /____/\__/\__, /_/\___/____/
// /____/
// Let's crunch *local* styles (non-ghost-shield)
// in app/styles/main.scss and put them .tmp/styles
gulp.task('styles', ['clean'], function () {
return gulp.src(config.stylesDir)
.pipe(sass({
// gulp-sass was blowing up without the next two line
sourceComments: 'map',
sourceMap: 'sass',
style: 'expanded',
// include boubon (for local styles only)
includePaths: require('node-bourbon').includePaths
}))
.pipe(autoprefixer('last 1 version'))
.pipe(gulp.dest(config.stylesDistDir))
.pipe(size());
});
// _
// (_)____
// / / ___/
// / (__ )
// __/ /____/
// /___/
// Just run everything through JS Hint.
// Don't move the files though. We'll do that later.
gulp.task('scripts', ['clean'], function () {
return gulp.src(config.jsDir)
.pipe(jshint())
.pipe(jshint.reporter(jshintStylish))
.pipe(size());
});
// ____
// __ __________ ________ / __/
// / / / / ___/ _ \______/ ___/ _ \/ /_
// / /_/ (__ ) __/_____/ / / __/ __/
// \__,_/____/\___/ /_/ \___/_/
// This task does a number of things. It's responsible for
// calling useref, which
// * looks at the build directives in the html,
// * concatenates all of the files,
// * replaces the multiple refs with a ref to the build file,
// * and copies the build file to dist
//
// We also use gulp-filter to grab just the JS and just the CSS
// optimize, minify, etc.
gulp.task('use-ref', ['pdfs','templates', 'styles', 'scripts'], function () {
// quick error handler for gulp-plumber
var onError = function (err) {
console.error(err);
throw err;
};
// grab all the html in .tmp
return gulp.src(config.assetBuildDir)
// added gulp-plumber here since you'll often need
// more info on why these stream failed
.pipe(plumber({
errorHandler: onError
}))
// useref all the html, and look in app and .tmp
// for files references in the html
.pipe(useref.assets({
searchPath: config.userefSearchPath
}))
// uglify if it's js, optimize if it's css
.pipe(gulpIf('**/*.js', uglify()))
.pipe(gulpIf('**/*.css', csso()))
// useref requires a call to restore() and useref()
// when you're done
.pipe(useref.restore())
.pipe(useref())
// throw it all into dist
.pipe(gulp.dest(config.distDir))
.pipe(size());
});
// _
// (_)___ ___ ____ _____ ____ _____
// / / __ `__ \/ __ `/ __ `/ _ \/ ___/
// / / / / / / / /_/ / /_/ / __(__ )
// /_/_/ /_/ /_/\__,_/\__, /\___/____/
// /____/
// Look in both local app/images and ghost-shield for images,
// optimize them and put them in dist/images
gulp.task('images', ['cname', 'use-ref', 'fonts'], function () {
return gulp.src(config.imagesDir)
// .pipe(cache(imagemin({
// optimizationLevel: 3,
// progressive: true,
// interlaced: true
// })))
.pipe(gulp.dest(config.imagesDistDir))
.pipe(size());
});
// ____ __
// / __/___ ____ / /______
// / /_/ __ \/ __ \/ __/ ___/
// / __/ /_/ / / / / /_(__ )
// /_/ \____/_/ /_/\__/____/
// take fonts out of ghost-shield and move them to dist
gulp.task('fonts', ['clean'], function () {
return gulp.src(config.fontDir)
.pipe(filter('**/*.{eot,svg,ttf,woff}'))
.pipe(flatten())
.pipe(gulp.dest('dist/styles'))
.pipe(size());
});
// _________ ____ _____ ___ ___
// / ___/ __ \/ __ `/ __ `__ \/ _ \
// / /__/ / / / /_/ / / / / / / __/
// \___/_/ /_/\__,_/_/ /_/ /_/\___/
// Copy the CNAME file over to dist
gulp.task('cname', ['clean'], function () {
return gulp.src(config.cname)
.pipe(gulp.dest(config.distDir))
.pipe(size());
});
// _ __
// _____(_) /____ ____ ___ ____ _____
// / ___/ / __/ _ \______/ __ `__ \/ __ `/ __ \
// (__ ) / /_/ __/_____/ / / / / / /_/ / /_/ /
// /____/_/\__/\___/ /_/ /_/ /_/\__,_/ .___/
// /_/
// After build-step-1 promise is resolved , we can
// safely generate a sitemap and put it in dist
gulp.task('sitemap', ['cname', 'use-ref', 'fonts'], function () {
return gulp.src(config.sitemapDir, {
read: false
}).pipe(sitemap({
siteUrl: config.siteUrl
}))
.pipe(gulp.dest(config.distDir));
});
// __
// _____/ /__ ____ _____
// / ___/ / _ \/ __ `/ __ \
// / /__/ / __/ /_/ / / / /
// \___/_/\___/\__,_/_/ /_/
gulp.task('clean', function () {
return gulp.src(config.cleanDir, { read: false }).pipe(rimraf());
});
// _____ __
// ____ ________ / __(_) __ __ _______/ /____
// / __ \/ ___/ _ \/ /_/ / |/_/_____/ / / / ___/ / ___/
// / /_/ / / / __/ __/ /> </_____/ /_/ / / / (__ )
// / .___/_/ \___/_/ /_/_/|_| \__,_/_/ /_/____/
// /_/
gulp.task('prefix-urls', ['cname', 'use-ref', 'fonts', 'inject-sidebar'], function(){
var prefixUrl = "http://swayze.io/" + packageJSON.name;
return gulp.src(config.distDir + '/**/*.html')
.pipe(prefix(prefixUrl, null, true))
.pipe(gulp.dest(config.distDir));
});
// __ _ __ __
// / /_ __ __(_) /___/ /
// / __ \/ / / / / / __ /
// / /_/ / /_/ / / / /_/ /
// /_.___/\__,_/_/_/\__,_/
gulp.task('build', ['cname', 'use-ref', 'fonts', 'inject-sidebar', 'images', 'prefix-urls']);
// __ __ __
// ____/ /__ ____ / /___ __ ______ ___ ___ ____ / /_
// / __ / _ \/ __ \/ / __ \/ / / / __ `__ \/ _ \/ __ \/ __/
// / /_/ / __/ /_/ / / /_/ / /_/ / / / / / / __/ / / / /_
// \__,_/\___/ .___/_/\____/\__, /_/ /_/ /_/\___/_/ /_/\__/
// /_/ /____/
// this task relies on the build-step-2 task having run, which will
// have triggered all other relevant build tasks. It takes everything
// in dist and commits it to gh-pages and pushes.
//
// deploy script from: https://github.com/X1011/git-directory-deploy
gulp.task('deploy', ['build'], function() {
var deployPromise = Q.defer();
exec(config.deployCommand, function(err){
deployPromise.resolve();
});
return deployPromise.promise;
});
// __ __ __
// / /___ _________ _/ / ____/ /__ _ __
// / / __ \/ ___/ __ `/ / / __ / _ \ | / /
// / / /_/ / /__/ /_/ / / / /_/ / __/ |/ /
// /_/\____/\___/\__,_/_/ \__,_/\___/|___/
// Mostly the same ol' same ol' local dev tasks you know and love.
gulp.task('connect', function () {
var connect = require('connect');
var app = connect()
.use(connect.static('app'))
// look in ghost shield too! XD
.use(connect.static('app/bower_components/ghost-shield/dist'))
.use(connect.static('dist'))
.use(connect.directory('app'));
require('http').createServer(app)
.listen(9000)
.on('listening', function () {
console.log('Started connect web server on http://0.0.0.0:9000');
});
});
gulp.task('serve', ['connect', 'styles', 'inject-sidebar'], function () {
require('opn')('http://0.0.0.0:9000');
});
gulp.task('watch', ['connect', 'serve'], function () {
var server = livereload();
// watch for changes
gulp.watch([
'.tmp/**/*.html',
'app/*.html',
'.tmp/styles/**/*.*',
'app/scripts/**/*.js',
'app/images/**/*'
]).on('change', function (file) {
server.changed(file.path);
});
gulp.watch('app/templates/**/*', ['inject-sidebar']);
gulp.watch('app/styles/**/*.scss', ['styles']);
gulp.watch('app/scripts/**/*.js', ['scripts']);
gulp.watch('app/images/**/*', ['images']);
});
outerGulp.tasks = assign({}, gulp.tasks, outerGulp.tasks);
}