This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.babel.js
218 lines (192 loc) · 6.26 KB
/
gulpfile.babel.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
import buildbranch from 'buildbranch';
import rimraf from 'rimraf';
import each from 'each-done';
import express from 'express';
import fs, { outputFile as output } from 'fs-extra';
import { html } from 'commonmark-helpers';
import numbers from 'typographic-numbers';
import numd from 'numd';
import RSS from 'rss';
import { merge, pipe, prop, head, splitEvery } from 'ramda';
import sequence from 'run-sequence';
import renderTweet from 'tweet.md';
import autoprefixer from 'autoprefixer';
import pcssImport from 'postcss-import';
import pcssInitial from 'postcss-initial';
import webpack from 'webpack';
import gulp, { dest, src, start as _start, task as _task } from 'gulp';
import gulpJade from 'gulp-jade';
import rename from 'gulp-rename';
import watch from 'gulp-watch';
import { log, PluginError } from 'gulp-util';
import jimp from 'gulp-jimp';
import postcss from 'gulp-postcss';
import articleData from 'article-data';
import getStats from './stats.js';
import underhood from './.underhoodrc.json';
import webpackConfig from './webpack.config.babel.js';
import authorRender from './helpers/author-render';
import bust from './helpers/bust';
import lastUpdated from './helpers/last-updated';
import authors from './dump';
const latestInfo = (head(authors) || {}).info;
const start = _start.bind(gulp);
const task = _task.bind(gulp);
const jadeDefaults = {
pretty: true,
locals: {
site: underhood.site,
latestInfo,
numbers: input => numbers(input, { locale: 'ru' }),
people: numd('человек', 'человека', 'человек'),
},
};
const getOptions = (opts = {}) =>
Object.assign({}, jadeDefaults, opts, {
locals: Object.assign({}, jadeDefaults.locals, opts.locals),
});
const jade = opts => gulpJade(getOptions(opts));
const firstTweet = pipe(prop('tweets'), head);
const render = pipe(renderTweet, html);
/**
* MAIN TASKS
*/
task('index', ['css'], () => {
const authorsToPost = authors.filter(author => author.post !== false);
return src('layouts/index.jade')
.pipe(jade({
locals: {
title: `Сайт @${underhood.underhood}`,
desc: underhood.underhoodDesc,
underhood,
currentAuthor: head(authors),
authors: splitEvery(3, authorsToPost),
helpers: { bust, firstTweet, render },
},
}))
.pipe(rename({ basename: 'index' }))
.pipe(dest('dist'));
});
task('stats', ['css'], () =>
src('layouts/stats.jade')
.pipe(jade({
locals: {
title: `Статистика @${underhood.underhood}`,
url: 'stats/',
desc: underhood.underhoodDesc,
lastUpdated,
underhood,
stats: getStats(authors),
helpers: { bust },
},
}))
.pipe(rename({ dirname: 'stats' }))
.pipe(rename({ basename: 'index' }))
.pipe(dest('dist')));
task('md-pages', ['css'], done => {
each([
{ name: 'about', title: 'О проекте' },
{ name: 'authoring', title: 'Авторам' },
{ name: 'instruction', title: 'Инструкция' },
], item => {
const page = fs.readFileSync(`./pages/${item.name}.md`, { encoding: 'utf8' });
const article = articleData(page, 'D MMMM YYYY', 'en'); // TODO change to 'ru' after moment/moment#2634 will be published
return src('layouts/article.jade')
.pipe(jade({
locals: merge(article, {
title: item.title,
url: item.name + '/',
underhood,
helpers: { bust },
}),
}))
.pipe(rename({ dirname: item.name }))
.pipe(rename({ basename: 'index' }))
.pipe(dest('dist'));
}, done);
});
task('rss', done => {
const feed = new RSS(underhood.site);
const authorsToPost = authors.filter(author => author.post !== false);
authorsToPost.forEach(author => {
feed.item({
title: author.username,
description: render(firstTweet(author)),
url: `https://jsunderhood.ru/${author.username}/`,
date: firstTweet(author).created_at,
});
});
output('dist/rss.xml', feed.xml({ indent: true }), done);
});
task('authors', ['css'], done => {
const authorsToPost = authors.filter(author => author.post !== false);
each(authorsToPost, author => {
return src('./layouts/author.jade')
.pipe(jade({
pretty: true,
locals: {
title: `Неделя @${author.username} в @${underhood.underhood}`,
author, underhood,
helpers: { authorRender, bust },
},
}))
.pipe(rename({ dirname: author.username }))
.pipe(rename({ basename: 'index' }))
.pipe(dest('dist'));
}, done);
});
task('userpics', () =>
src('dump/images/*-image*')
.pipe(jimp({ resize: { width: 96, height: 96 }}))
.pipe(dest('dist/images')));
task('current-userpic', () =>
head(authors) && src(`dump/images/${head(authors).username}-image*`)
.pipe(jimp({ resize: { width: 192, height: 192 }}))
.pipe(rename('current-image'))
.pipe(dest('dist/images')));
task('current-banner', () =>
head(authors) && src(`dump/images/${head(authors).username}-banner*`)
.pipe(rename('current-banner'))
.pipe(dest('dist/images')));
task('current-media', ['current-userpic', 'current-banner']);
task('css', () =>
src('css/styles.css')
.pipe(postcss([
pcssImport,
pcssInitial,
autoprefixer,
]))
.pipe(dest('dist/css')));
task('js', done => {
webpack(webpackConfig, err => {
if (err) throw new PluginError('webpack', err);
done();
});
});
task('static', () =>
src([
'static/**',
'static/.**',
'node_modules/bootstrap/dist/**',
]).pipe(dest('dist')));
task('server', () => {
const app = express();
app.use(express.static('dist'));
app.listen(4000);
log('Server is running on http://localhost:4000');
});
/**
* FLOW
*/
task('clean', done => rimraf('dist', done));
task('html', ['stats', 'authors', 'index', 'rss', 'md-pages']);
task('build', done => sequence( 'html', 'css', 'js', 'stats', 'static', 'userpics', 'current-media', done));
task('default', done => sequence('clean', 'watch', done));
task('watch', ['server', 'build'], () => {
watch(['**/*.jade'], () => start('html'));
watch(['css/**/*.css'], () => start('css'));
watch('js/**/*.js', () => start('js'));
watch('static/**', () => start('static'));
});
task('deploy', ['build'], done =>
buildbranch({ branch: 'gh-pages', folder: 'dist' }, done));