forked from philipwalton/solved-by-flexbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
259 lines (212 loc) · 6.59 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
require('shelljs/global');
var argv = require('yargs').argv;
var assign = require('object-assign');
var babelify = require('babelify');
var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var connect = require('connect');
var cssnext = require('gulp-cssnext');
var del = require('del');
var frontMatter = require('front-matter');
var gulp = require('gulp');
var gulpIf = require('gulp-if');
var gutil = require('gulp-util');
var he = require('he');
var hljs = require('highlight.js');
var htmlmin = require('gulp-htmlmin');
var jshint = require('gulp-jshint');
var nunjucks = require('nunjucks');
var path = require('path');
var plumber = require('gulp-plumber');
var Remarkable = require('remarkable');
var rename = require('gulp-rename');
var serveStatic = require('serve-static');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var through = require('through2');
var uglify = require('gulp-uglify');
/**
* The output directory for all the built files.
*/
const DEST = './build';
/**
* The name of the Github repo.
*/
const REPO = 'solved-by-flexbox';
/**
* Truthy if NODE_ENV isn't 'dev'
*/
const PROD = process.env.NODE_ENV !== 'dev';
nunjucks.configure('templates', { autoescape: false });
function streamError(err) {
gutil.beep();
gutil.log(err instanceof gutil.PluginError ? err.toString() : err.stack);
}
function extractFrontMatter(options) {
var files = [];
var site = assign({demos: []}, options);
return through.obj(
function transform(file, enc, done) {
var contents = file.contents.toString();
var yaml = frontMatter(contents);
if (yaml.attributes) {
var slug = path.basename(file.path, path.extname(file.path));
file.contents = new Buffer(yaml.body);
file.data = {
site: site,
page: assign({slug: slug}, yaml.attributes)
};
if (file.path.indexOf('demos') > -1) {
site.demos.push(file.data.page);
}
}
files.push(file);
done();
},
function flush(done) {
files.forEach(function(file) { this.push(file); }.bind(this));
done();
}
)
}
function renderMarkdown() {
var markdown = new Remarkable({
html: true,
typographer: true,
highlight: function (code, lang) {
// Unescape to avoid double escaping.
code = he.unescape(code);
return lang ? hljs.highlight(lang, code).value : he.escape(code);
}
});
return through.obj(function (file, enc, cb) {
try {
if (path.extname(file.path) == '.md') {
file.contents = new Buffer(markdown.render(file.contents.toString()));
}
this.push(file);
}
catch (err) {
this.emit('error', new gutil.PluginError('renderMarkdown', err, {
fileName: file.path
}));
}
cb();
});
}
function renderTemplate() {
return through.obj(function (file, enc, cb) {
try {
// Render the file's content to the page.content template property.
var content = file.contents.toString();
file.data.page.content = nunjucks.renderString(content, file.data);
// Then render the page in its template.
var template = file.data.page.template;
file.contents = new Buffer(nunjucks.render(template, file.data));
this.push(file);
}
catch (err) {
this.emit('error', new gutil.PluginError('renderTemplate', err, {
fileName: file.path
}));
}
cb();
});
}
gulp.task('pages', function() {
var baseData = require('./config.json');
var overrides = {
baseUrl: PROD ? '/' + REPO + '/' : '/',
env: PROD ? 'prod' : 'dev'
};
var siteData = assign(baseData, overrides);
return gulp.src(['*.html', './demos/**/*'], {base: process.cwd()})
.pipe(plumber({errorHandler: streamError}))
.pipe(extractFrontMatter(siteData))
.pipe(renderMarkdown())
.pipe(renderTemplate())
.pipe(rename(function(path) {
if (path.basename != 'index' && path.basename != '404') {
path.dirname += '/' + path.basename;
path.basename = 'index';
path.extname = '.html';
}
}))
.pipe(htmlmin({
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
minifyJS: true,
minifyCSS: true
}))
.pipe(gulp.dest(DEST));
});
gulp.task('images', function() {
return gulp.src('./assets/images/**/*')
.pipe(gulp.dest(path.join(DEST, 'images')));
});
gulp.task('css', function() {
return gulp.src('./assets/css/main.css')
.pipe(plumber({errorHandler: streamError}))
.pipe(cssnext({compress: true}))
.pipe(gulp.dest(DEST));
});
gulp.task('lint', function() {
return gulp.src('./assets/javascript/**/*.js')
.pipe(plumber({errorHandler: streamError}))
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(gulpIf(PROD, jshint.reporter('fail')))
});
gulp.task('javascript', ['lint'], function() {
return browserify('./assets/javascript/main.js', {debug: true})
.transform(babelify)
.bundle()
.on('error', streamError)
.pipe(source('main.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(gulpIf(PROD, uglify()))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(DEST));
});
gulp.task('clean', function(done) {
del(DEST, done);
});
gulp.task('default', ['css', 'images', 'javascript', 'pages']);
gulp.task('serve', ['default'], function() {
var port = argv.port || argv.p || 4000;
connect().use(serveStatic(DEST)).listen(port);
gulp.watch('./assets/css/**/*.css', ['css']);
gulp.watch('./assets/images/*', ['images']);
gulp.watch('./assets/javascript/*', ['javascript']);
gulp.watch(['*.html', './demos/*', './templates/*'], ['pages']);
});
gulp.task('release', ['default'], function() {
// Create a tempory directory and
// checkout the existing gh-pages branch.
rm('-rf', '_tmp');
mkdir('_tmp');
cd('_tmp');
exec('git init');
exec('git remote add origin [email protected]:philipwalton/' + REPO + '.git');
exec('git pull origin gh-pages');
// Delete all the existing files and add
// the new ones from the build directory.
rm('-rf', './*');
cp('-rf', path.join('..', DEST, '/'), './');
exec('git add -A');
// Commit and push the changes to
// the gh-pages branch.
exec('git commit -m "Deploy site."');
exec('git branch -m gh-pages');
exec('git push origin gh-pages');
// Clean up.
cd('..');
rm('-rf', '_tmp');
rm('-rf', DEST);
});