forked from mdn/serviceworker-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
232 lines (206 loc) · 6.49 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
var gulp = require('gulp');
var fs = require('fs');
var plugins = require('gulp-load-plugins')();
var del = require('del');
var glob = require('glob');
var path = require('path');
var marked = require('marked');
var swig = require('swig');
var through2 = require('through2');
var promisify = require('es6-promisify');
var mergeStream = require('merge-stream');
var parseRecipes = require('./parseRecipes.js');
var renderFile = promisify(swig.renderFile);
var fsWriteFile = promisify(fs.writeFile);
var fsReadFile = promisify(fs.readFile);
var ignore = ['!./dist', '!./dist', '!./dist/**', '!./node_modules', '!./node_modules/**'];
var recipeSlugs = glob.sync('./!(dist|node_modules|src|_recipe_template)/').map(function toBase(dir) {
return path.basename(dir);
});
var srcRecipes = recipeSlugs.map(function makePath(name) {
return './' + name + '/**';
});
var recipes = parseRecipes(recipeSlugs).sort(function(recipeA, recipeB) {
if (recipeA.category === recipeB.category) {
return recipeA.difficulty < recipeB.difficulty ? -1 : 1;
}
if (recipeA.category < recipeB.category) {
return -1;
}
return 1;
});
var template = (function() {
function renderContent(content, options) {
var newOptions = options || {};
return renderFile('./src/tpl/layout.html', {
content: content,
package: require('./package.json'),
recipes: recipes,
ghBase: 'https://github.com/digitarald/serviceworker-cookbook/blob/master/',
currentRecipe: newOptions.currentRecipe,
});
}
/**
* Write content into the layout file.
* @param {string} outputFile - filename
* @param {string} content - put in the main body, NOT ESCAPED
* @param {Object} options -
* 'currentRecipe' - if the page needs the recipe's header
* @returns {Promise}
*/
function writeFile(outputFile, content, options) {
return renderContent(content, options)
.then(function(output) {
return fsWriteFile(outputFile, output);
});
}
/**
* Same as writeFile but works with streams
* @returns Stream transform.
*/
function transform(options) {
return through2.obj(function(file, enc, next) {
var self = this;
renderContent(file.contents.toString('utf8'), options)
.then(function(output) {
file.contents = new Buffer(output);
self.push(file);
next();
});
});
}
return {
transform: transform,
writeFile: writeFile
};
})();
gulp.task('clean', function clean(done) {
del(['./dist']).then(function delThen() {
fs.mkdir('./dist', function() {
done();
});
});
});
gulp.task('lint', function lint() {
return gulp
.src(['./*.js', './*/*.js'].concat(ignore))
.pipe(plugins.eslint())
.pipe(plugins.eslint.format())
.pipe(plugins.eslint.failAfterError());
});
gulp.task('build:docs', ['clean'], function buildDocs() {
var streams = recipes.map(function(recipe) {
return gulp
.src(recipe.srcs.map(function(src) {
return recipe.slug + '/' + src.filename;
}), {
base: './'
})
.pipe(plugins.docco({
template: './src/tpl/docco/docco.jst'
}))
.pipe(template.transform({ currentRecipe: recipe }))
.pipe(plugins.rename(function(renamePath) {
renamePath.dirname = '';
renamePath.basename = recipe.slug + '_' + renamePath.basename + '_doc';
}))
.pipe(gulp.dest('./dist/'));
});
return mergeStream.apply(null, streams);
});
gulp.task('build:index', ['clean'], function buildIndex() {
var intros = recipes.map(function(recipe) {
var title = recipe.name;
var summary = marked(recipe.summary);
return {
slug: recipe.slug,
title: title,
summary: summary,
};
});
return renderFile('./src/tpl/index.html', { recipes: intros })
.then(function(output) {
return template.writeFile('./dist/index.html', output);
});
});
gulp.task('build:intros', ['clean'], function() {
var renderer = new marked.Renderer();
renderer.link = function(href, title, text) {
var link = '<a href="'+ href +'" target="_blank"';
if (title) {
link += ' title="' + title + '"';
}
return link + '>' + text + '</a>';
}
return Promise.all(recipes.map(function(recipe) {
return fsReadFile(recipe.slug + '/README.md', 'utf8')
.then(function(readme) {
return renderFile('./src/tpl/intro.html', { markdown: marked(readme, { renderer: renderer }) });
})
.then(function(content) {
return template.writeFile('./dist/' + recipe.slug + '.html', content, { currentRecipe: recipe });
});
}));
});
gulp.task('build:demos', ['clean'], function() {
return Promise.all(recipes.map(function(recipe) {
return renderFile('./src/tpl/demo.html', { recipe: recipe })
.then(function(output) {
return template.writeFile('./dist/' + recipe.slug + '_demo.html', output, { currentRecipe: recipe });
});
}));
});
gulp.task('build:recipes', ['clean'], function() {
return gulp
.src(srcRecipes, {
base: './'
})
.pipe(gulp.dest('./dist'));
});
gulp.task('build:css', ['clean'], function() {
return gulp
.src([
'src/css/foundation.normalize.css',
'src/css/foundation.css',
'src/css/foundation-icons.css',
'src/css/style.css',
'src/css/docco.css'
])
.pipe(plugins.concat('bundle.css'))
.pipe(gulp.dest('./dist'));
});
gulp.task('build:js', ['clean'], function() {
return gulp
.src('src/js/*.js')
.pipe(plugins.concat('bundle.js'))
.pipe(gulp.dest('./dist'));
});
gulp.task('build:tabzilla', ['clean'], function() {
return gulp
.src('node_modules/mozilla-tabzilla/**/*.{css,png}')
.pipe(gulp.dest('dist/tabzilla'));
});
gulp.task('build:favicon', ['clean'], function() {
return gulp
.src('favicon.ico')
.pipe(gulp.dest('dist'));
});
// Start express server after building site
gulp.task('start-server', ['build'], function startServer(cb) {
require('./server.js').ready.then(cb);
});
// Watch files and reloads BrowserSync
gulp.task('watch', ['start-server'], function serve() {
var browserSync = require('browser-sync').create();
browserSync.init(null, {
proxy: 'http://localhost:3003',
files: './*/*.js',
open: false
});
return gulp.watch(['./*/*'].concat(ignore), ['build-dev'], browserSync.reload);
});
gulp.task('test', ['lint']);
// Minimal build tasks for speedy development
gulp.task('build-dev', ['build:recipes', 'test']);
// Full build for publishing
gulp.task('build', ['build:index', 'build:intros', 'build:demos', 'build:recipes', 'build:docs', 'build:css', 'build:js', 'build:tabzilla', 'build:favicon']);