-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
294 lines (259 loc) · 8.09 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
/*global -$: true] */
'use strict';
require('colors');
var path = require('path');
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var gulpsmith = require('gulpsmith');
var _ = require('lodash');
var through = require('through2');
var lazypipe = require('lazypipe');
var del = require('del');
var notifier = require('node-notifier');
var async = require('async');
var respokeStyle = require('respoke-style');
var navigation = require('metalsmith-navigation');
var renderJade = require('./lib/metalsmith/render-jade');
var insertExamples = require('./lib/metalsmith/insert-examples');
var argv = require('yargs')
.default({
dist: false,
host: '127.0.0.1',
port: '2003'
})
.alias({
d: 'dist',
h: 'host',
p: 'port'
})
.argv;
var paths = {
templates: path.join(__dirname, '/templates'),
examples: path.join(__dirname, '/examples'),
output: path.join(__dirname, '/public'),
source: path.join(__dirname, '/src'),
scripts: path.join(__dirname, '/js'),
sass: path.join(__dirname, '/scss'),
root: __dirname
};
var navConfig = {
sidebar: {
sortBy: 'menuOrder',
sortByNameFirst: true,
breadcrumbProperty: 'breadcrumbPath',
filterProperty: 'showInMenu',
filterValue: 'true',
mergeMatchingFilesAndDirs: true
}
};
// default values shown
var navSettings = {
navListProperty: 'navs'
};
function cleanBuildDir(done) {
del(paths.output, done);
}
gulp.task('default', ['serve']);
gulp.task('deploy', ['build'], function deployTask(done) {
var options = {};
gulp.src(paths.output + '/**/*')
.pipe($.ghPages(options))
.on('end', function cleanUpDeploy() {
cleanBuildDir(done);
});
});
function buildSite(callback) {
var filterMarkdown = $.filter('**/*.md');
var distPipe = lazypipe()
.pipe(function buildDistPipe() {
return $.if('*.html', $.minifyHtml());
});
gulp.src(paths.source + '/**/*')
.pipe(filterMarkdown)
.pipe($.frontMatter({
property: 'data',
remove: true
}))
.pipe(through.obj(function sidebarDataTransform(file, enc, callback) {
if (!file.data) {
callback();
}
// copy nav metadata for metalsmith-navigation plugin
file.showInMenu = file.data.showInMenu;
file.menuOrder = file.data.menuOrder;
callback(null, file);
}))
.pipe($.marked({
gfm: true,
tables: true
}))
.pipe(through.obj(function checkAndApplyAlternateExt(file, enc, callback) {
if (file.data.extname) {
var currentExt = path.extname(file.path);
file.path = file.path.replace(currentExt, file.data.extname);
}
callback(null, file);
}))
.pipe(gulpsmith()
.use(navigation(navConfig, navSettings))
.use(renderJade(paths, argv.dist))
.use(insertExamples(paths, argv.dist))
)
.pipe(filterMarkdown.restore())
.pipe($.if(argv.dist, distPipe()))
.pipe(gulp.dest(paths.output))
.on('end', function buildSiteCallback() {
callback();
});
}
function buildSass(callback) {
var cssOutput = path.join(paths.output, 'css');
gulp.src(path.join(paths.sass, '**/*'))
.pipe($.if(!argv.dist, $.sourcemaps.init()))
.pipe($.sass({
includePaths: respokeStyle.includeStylePaths(),
errLogToConsole: true
}))
.pipe($.if(argv.dist, $.minifyCss()))
.pipe($.if(!argv.dist, $.sourcemaps.write()))
.pipe(gulp.dest(cssOutput))
.on('end', function buildSassCallback() {
callback();
});
}
function buildScripts(callback) {
var scriptsOutput = paths.output + '/js';
gulp.src(paths.scripts + '/**/*.js')
.pipe($.if(argv.dist, $.uglify()))
.pipe(gulp.dest(scriptsOutput))
.on('end', function buildScriptsCallback() {
callback();
});
}
function copySharedAssets(callback) {
var filterJS = $.filter('**/*.js');
var filterCSS = $.filter('**/*.css');
gulp.src(path.join(respokeStyle.paths.assets, '**/*'))
.pipe(filterJS)
.pipe($.if(argv.dist, $.uglify()))
.pipe(filterJS.restore())
.pipe(filterCSS)
.pipe($.if(argv.dist, $.minifyCss()))
.pipe(filterCSS.restore())
.pipe(gulp.dest(paths.output))
.on('end', function copyAssetsCallback() {
callback();
});
}
gulp.task('build', ['clean'], function buildTask(done) {
async.parallel([
buildSite,
buildSass,
buildScripts,
copySharedAssets
], done);
});
gulp.task('build:site', function (done) {
buildSite(done);
});
gulp.task('build:assets', [
'build:assets:scripts',
'build:assets:sass',
'build:assets:respoke-style'
]);
gulp.task('build:assets:sass', function (done) {
buildSass(done);
});
gulp.task('build:assets:scripts', function (done) {
buildScripts(done);
});
gulp.task('build:assets:respoke-style', function (done) {
copySharedAssets(done);
});
gulp.task('clean', function cleanupTask(done) {
cleanBuildDir(done);
});
gulp.task('lint', function lintTask() {
return gulp.src([
__dirname + 'lib/**/*.js',
paths.source + '/**/*.js',
__filename
])
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.jscs());
});
gulp.task('webserver', function webserverTask() {
return gulp.src(paths.output)
.pipe($.webserver({
host: argv.host,
port: argv.port,
livereload: true
}));
});
gulp.task('serve', ['build', 'watch', 'webserver']);
gulp.task('watch', function watchTask(done) {
var options = {
emitOnGlob: false
};
var watcherNotify = function watcherNotify(message) {
notifier.notify({
title: 'Gulp watcher',
message: message
});
};
$.watch(
[
paths.templates + '/**/*',
paths.source + '/**/*',
path.join(respokeStyle.paths.templates, '**/*'),
path.join(respokeStyle.paths.assets, '**/*')
],
options,
function siteWatch(files, cb) {
watcherNotify('Starting build:site');
gulp.start('build:site', function siteBuildCallback() {
watcherNotify('Finished build:site');
watcherNotify('Starting build:assets:respoke-style');
gulp.start('build:assets:respoke-style', function syncSharedAssetsCallback() {
watcherNotify('Finished build:assets:respoke-style');
cb();
});
});
});
$.watch(
[
paths.sass + '/**/*',
path.join(respokeStyle.paths.styles, '**/*')
],
options,
function sassWatch(files, cb) {
watcherNotify('Starting build:assets:sass');
gulp.start('build:assets:sass', function sassBuildCallback() {
watcherNotify('Finished build:assets:sass');
cb();
});
});
$.watch(
paths.scripts + '/**/*', options,
function scriptsWatch(files, cb) {
watcherNotify('Starting build:assets:scripts');
gulp.start('build:assets:scripts', function scriptsBuildCallback() {
watcherNotify('Finished build:assets:scripts');
cb();
});
});
done();
});
gulp.task('example-runner', function (done) {
var exampleRunner = require('./lib/example-runner')(paths);
exampleRunner.run(function runnerFinished(error, runnersOutput) {
if (error) {
console.log(error.toString().red);
}
if (runnersOutput) {
console.log(runnersOutput.join('\n').green);
}
done();
});
});