-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
330 lines (271 loc) · 7.79 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
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var connect = require('gulp-connect');
var csso = require('gulp-csso');
var frontmatter = require('front-matter');
var fs = require('fs');
var ghpages = require('gh-pages');
var gjade = require('gulp-jade');
var glob = require('glob');
var gutil = require('gulp-util');
var ignore = require('gulp-ignore');
var jade = require('jade');
var langmap = require('langmap');
var less = require('gulp-less');
var marked = require('marked');
var opn = require('opn');
var path = require('path');
var plumber = require('gulp-plumber');
var rename = require('gulp-rename');
var rimraf = require('gulp-rimraf');
var source = require('vinyl-source-stream');
var through = require('through');
var uglify = require('gulp-uglify');
var watchify = require('watchify');
var browserify = require('browserify');
var pkg = require('./package.json');
var isDist = process.argv.indexOf('serve') === -1;
var bundler = watchify(browserify(path.resolve(__dirname, 'src/scripts/main.js'), watchify.args));
bundler.transform('babelify', {
experimental: true
});
bundler.transform('debowerify');
// Utilities
var jadeUtils = {
getSections: getSections,
getArticles: getArticles,
getArticle: getArticle,
getArticleTranslations: getArticleTranslations,
getLocaleArticles: getLocaleArticles,
languages: langmap
};
var ARTICLES_CACHE = null;
function getArticles() {
if (ARTICLES_CACHE) return ARTICLES_CACHE;
return ARTICLES_CACHE =
glob.sync('journal/**/*.md')
.map(function (filepath) {
return getArticle(filepath, path.resolve('journal'));
})
.sort(function (a, b) {
return -(a.slug.localeCompare(b.slug));
});
}
function getLocaleArticles(locale) {
var languageId = locale.split('-')[0];
var articles = getArticles();
var index = {};
return articles.reduce(function (memo, article) {
var group = index[ article.id ];
var append = false;
if (group == null) {
append = true;
group = index[ article.id ] = {
id: article.id,
translations: []
};
}
if ((article.locale === locale) || (article.locale === languageId)) {
group.default = article;
}
else {
group.translations.push(article);
}
return append ? memo.concat([ group ]) : memo;
}, []);
}
function getArticleTranslations(translations, article) {
if (translations.indexOf(article) < 0) {
translations = translations.concat([ article ]);
}
return translations.sort(function (a, b) {
return a.locale.localeCompare(b.locale);
})
}
function getArticle(filepath, basepath, contents) {
contents || (contents = fs.readFileSync(filepath, 'utf-8'));
var matter = frontmatter(contents);
var data = matter.attributes;
var body = matter.body;
data.id = getSlug(data.slug || path.relative(basepath, filepath));
data.author = getPerson(data.author);
data.translator = getPerson(data.translator);
data.locale = data.locale || 'en-US';
data.languageId = data.locale.split('-')[0];
data.language = langmap[ data.languageId ] || langmap[ data.locale ];
data.slug = getSlug(data.id, data.locale);
data.body = marked(body);
return data;
}
function getPerson(id) {
if (!id) {
return null;
}
var person = getJSON(path.resolve('journal/authors/', id + '.json'));
person.id = id;
return person;
}
function getJSON(filename) {
return JSON.parse(fs.readFileSync(filename, 'utf-8'));
}
function getSections(name) {
name = String(name || '');
return [
{
slug: '/',
name: 'Manifesto'
},
{
slug: '/journal/',
name: 'Journal'
}
].map(function (section) {
section.active = (section.name.toLowerCase() === name.toLowerCase()) ? 'active' : '';
return section;
});
}
var slugRE = /^(\d\d\d\d)[-\/\\](\d\d)[-\/\\](\d\d)[-\/\\]([^\.]*)/i;
function getSlug(slug, locale) {
var match = String(slug).match(slugRE);
if (match) {
slug = match.slice(1, 5).join('/');
}
if (locale && (locale !== 'en-US') && (locale !== 'en')) {
slug = [ slug, locale ].join('/');
}
return slug;
}
// Tasks
function runJS() {
return bundler.bundle()
.pipe(isDist ? through() : plumber())
.pipe(source('bundle.js'))
.pipe(gulp.dest('dist/build'))
.pipe(connect.reload());
}
gulp.task('js', runJS);
gulp.task('journal', function() {
return gulp.src('journal/**/*.md')
.pipe(through(function (file) {
var article = getArticle(file.path, file.base, file.contents.toString('utf-8'));
var translations = null;
getLocaleArticles(article.locale).forEach(function (group) {
if (group.id === article.id) {
translations = group.translations;
}
});
var content = jade.renderFile('src/templates/journal_article.jade', {
article: article,
translations: translations,
utils: jadeUtils
});
file.path = path.resolve(file.base, article.slug + '/index.html');
file.contents = new Buffer(content);
this.queue(file);
}))
.pipe(gulp.dest('dist/journal'))
.pipe(connect.reload());
});
gulp.task('html', ['clean:html', 'journal'], function() {
return gulp.src('src/*.jade')
.pipe(ignore.exclude('_*'))
.pipe(isDist ? through() : plumber())
.pipe(gjade({
jade: jade,
pretty: true,
locals: {
utils: jadeUtils
}
}))
.pipe(through(function(file) {
if (file.path === path.resolve('src/index.html')) {
file.path = path.resolve('src');
}
this.queue(file);
}))
// Hack!
.pipe(rename({ extname: '/index.html' }))
.pipe(gulp.dest('dist'))
.pipe(connect.reload());
});
gulp.task('css', ['clean:css'], function() {
return gulp.src('src/styles/*.less')
.pipe(isDist ? through() : plumber(function () {}))
.pipe(less({
// Allow CSS to be imported from node_modules and bower_components
'paths': ['./node_modules', './bower_components']
}))
.pipe(autoprefixer('last 2 versions', {
map: false
}))
.pipe(isDist ? csso() : through())
.pipe(rename(function (path) {
path.extname = '.css';
}))
.pipe(gulp.dest('dist/build'))
.pipe(connect.reload());
});
gulp.task('images', ['clean:images'], function() {
return gulp.src('src/images/**/*')
.pipe(gulp.dest('dist/images'))
.pipe(connect.reload());
});
gulp.task('cname', function() {
return gulp.src('CNAME')
.pipe(gulp.dest('dist'));
});
gulp.task('favicons', function() {
return gulp.src('src/favicons/*')
.pipe(gulp.dest('dist'));
});
gulp.task('clean', function() {
return gulp.src('dist')
.pipe(rimraf());
});
gulp.task('clean:html', function() {
ARTICLES_CACHE = null;
return gulp.src('dist/index.html')
.pipe(rimraf());
});
gulp.task('clean:js', function() {
return gulp.src('dist/build/build.js')
.pipe(rimraf());
});
gulp.task('clean:css', function() {
return gulp.src('dist/build/build.css')
.pipe(rimraf());
});
gulp.task('clean:images', function() {
return gulp.src('dist/images')
.pipe(rimraf());
});
gulp.task('connect', ['build'], function(done) {
connect.server({
port: 4444,
root: 'dist',
livereload: true
});
opn('http://localhost:4444', done);
});
gulp.task('watch', function() {
gulp.watch('journal/**/*.json', ['html']);
gulp.watch('journal/**/*.md', ['html']);
gulp.watch('src/**/*.jade', ['html']);
gulp.watch('src/styles/**/*.less', ['css']);
gulp.watch('src/images/**/*', ['images']);
bundler.on('update', function() {
runJS()
});
});
gulp.task('deploy', ['build'], function(done) {
bundler.close();
ghpages.publish(path.join(__dirname, 'dist'), {
logger: gutil.log,
branch: 'master'
}, done);
});
gulp.task('build', ['js', 'html', 'css', 'images', 'favicons', 'cname']);
gulp.task('serve', ['connect', 'watch']);
gulp.task('default', ['build'], function() {
bundler.close();
});