-
Notifications
You must be signed in to change notification settings - Fork 20
/
gulpfile.js
322 lines (261 loc) · 9.98 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
'use strict';
const path = require('path');
const fs = require('fs');
const fsExtra = require('fs-extra');
const cp = require('child_process');
const Q = require('q');
const enb = require('enb');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');
const gulp = require('gulp');
const batch = require('gulp-batch');
const watch = require('gulp-watch');
const browserSync = require('browser-sync');
const csscomb = require('gulp-csscomb');
const genNginxHostConf = require('./tools/generate-nginx-host-conf');
const prepareModel = require('./lib/prepare-model');
const fsHelpers = require('./node_modules/bem-lib-site-view/lib/fs-helpers');
const docFeedbackHandlers = require('doc-feedback-handlers');
const env = process.env;
const browserSyncPort = env.PORT || 8008;
const model = require(env.PATH_TO_MODEL || './content/model.js');
const LANGUAGES = env.LANGUAGES ? env.LANGUAGES.split(',') : ['en', 'ru'];
const CACHE = './.cache';
const STATIC = './static';
const OUTPUT = './output';
const CACHE_DIR_PREFIX = CACHE + '/gorshochek-cache-';
const CACHE_DIRS = LANGUAGES.reduce((prev, language) => {
prev[language] = CACHE + '/gorshochek-cache-' + language;
return prev;
}, {});
const OUTPUT_ROOT = OUTPUT + '/bem.info/';
const OUTPUT_DIRS = LANGUAGES.reduce((prev, language) => {
prev[language] = OUTPUT_ROOT + language;
return prev;
}, {});
const BUNDLES_DIR = 'bundles';
const BUNDLES = fs.readdirSync(BUNDLES_DIR);
const BEMTREE = BUNDLES.reduce((prev, bundle) => {
LANGUAGES.forEach(lang => {
prev[lang] || (prev[lang] = {});
prev[lang][bundle] = path.join(BUNDLES_DIR, bundle, bundle + '.' + lang + '.bemtree.js');
});
return prev;
}, {});
const BEMHTML = BUNDLES.reduce((prev, bundle) => {
LANGUAGES.forEach(lang => {
prev[lang] || (prev[lang] = {});
prev[lang][bundle] = path.join(BUNDLES_DIR, bundle, bundle + '.' + lang + '.bemhtml.js');
});
return prev;
}, {});
function runSubProcess(file, options) {
const defer = Q.defer();
const proc = cp.fork(file, options);
proc.on('error', (error) => defer.reject(error));
proc.on('close', () => defer.resolve());
proc.on('exit', () => defer.resolve());
return defer.promise;
}
function compilePages(lang, bundle) {
return runSubProcess('./lib/template.js', {
cwd: process.cwd(),
encoding: 'utf-8',
env: {
GORSHOCHEK_CACHE_FOLDER: CACHE_DIRS[lang],
bemtree: BEMTREE[lang][bundle],
bemhtml: BEMHTML[lang][bundle],
bundle,
static: STATIC,
source: CACHE_DIRS[lang],
destination: OUTPUT_DIRS[lang],
destinationRoot: OUTPUT + (env.YENV === 'production' ? '/bem.info/static' : ''),
langs: LANGUAGES,
sites: env.SITES ? env.SITES.split(',') : ['methodology', 'technologies', 'toolbox', 'libraries', 'tutorials'],
lang,
DEBUG: env.DEBUG,
YENV: env.YENV
}
});
}
// Подготовка директорий output-*
gulp.task('prepare-output', () => new Promise(resolve =>
fsExtra.remove(OUTPUT, () =>
fsExtra.copy(path.join(CACHE, OUTPUT), OUTPUT, () => resolve())
)
));
gulp.task('prepare-static', () => {
return Q.all(
gulp.src(path.join(STATIC, '{index.html,robots.txt,.nojekyll}'))
.pipe(gulp.dest(OUTPUT))
.pipe(gulp.dest(OUTPUT_ROOT)),
LANGUAGES.map(lang => gulp.src(path.join(STATIC, '{favicon.ico,robots.txt,.well-known/*,og_image/*}'))
.pipe(gulp.dest(OUTPUT_DIRS[lang])))
);
});
// Сборка данных
function data() {
rimraf.sync(CACHE);
mkdirp.sync(CACHE);
return Q.all(LANGUAGES.map(lang => {
const modelWithRedirects = model.concat(require('./content/redirects'));
const preparedModel = prepareModel(modelWithRedirects, lang);
const modelPath = path.join(CACHE, `model.${lang}.json`);
fs.writeFileSync(modelPath, JSON.stringify(preparedModel.model, null, 2));
if (preparedModel.redirects && preparedModel.redirects.length) {
const redirectsDir = path.join(CACHE, OUTPUT_ROOT);
const redirectsNginxPath = path.join(redirectsDir, `bem_info_redirects_${lang}.conf`);
mkdirp.sync(redirectsDir);
fs.writeFileSync(redirectsNginxPath, genNginxHostConf(preparedModel.redirects || []));
}
return runSubProcess('./lib/data-builder.js', {
cwd: process.cwd(),
encoding: 'utf-8',
env: {
GORSHOCHEK_CACHE_FOLDER: CACHE_DIRS[lang],
modelPath: modelPath,
host: `https://${lang}.bem.info`,
dest: CACHE_DIRS[lang],
root: env.YENV === 'production' ? '' : '/bem.info/' + lang,
token: env.TOKEN,
DEBUG: env.DEBUG,
githubHosts: env.GITHUB_HOSTS
}
});
})).then(() => fsHelpers.touch(path.join(CACHE, '.inited')));
}
gulp.task('data', data);
gulp.task('is-data-exists', () => {
return fsHelpers.exists(path.join(CACHE, '.inited')).then(doesExists => {
if (doesExists) {
return Promise.resolve();
}
throw Error('Data is not initialized in ' + CACHE + ', run `gulp data`');
});
});
// Шаблонизация данных
gulp.task('enb-make', enb.make);
function copyStatic() {
return Q.all(LANGUAGES.map(lang => {
const files = BUNDLES.map(bundle =>
path.join(BUNDLES_DIR, bundle, bundle + '*.min.*'));
files.push(path.join(OUTPUT_ROOT, 'static', '*'));
return gulp.src(files).pipe(gulp.dest(OUTPUT_DIRS[lang]));
}));
}
gulp.task('copy-static', copyStatic);
gulp.task('copy-sitemap-xml', () => Q.all(LANGUAGES.map(lang => {
return gulp.src(path.join(CACHE_DIRS[lang], 'sitemap.xml'))
.pipe(gulp.dest(path.join(OUTPUT_DIRS[lang])));
})));
gulp.task('build-html', () => Q.all(LANGUAGES.map(lang => {
return Q.all(BUNDLES.map(compilePages.bind(null, lang)));
})));
gulp.task('copy-static-images', () => Q.all(LANGUAGES.map(lang => {
// FIXME: use '/static/*' then https://github.com/bem-site/gorshochek/issues/49 would be resolved
return gulp.src(path.join(CACHE_DIRS[lang], '/*.{gif,png,jpg,svg,svgz,svgd}'))
.pipe(gulp.dest(OUTPUT_DIRS[lang]));
})));
gulp.task('compile-pages', gulp.series(
'enb-make',
'prepare-static',
'copy-static',
'copy-static-images',
'copy-sitemap-xml',
'build-html'
));
// Наблюдатель
gulp.task('watch', () => {
watch(['content/**/*'], batch((event, done) => {
data().then(done);
}));
watch(['blocks/**/*'], batch((event, done) => {
enb.make().then(() => copyStatic().then(done));
}));
// compile pages then bemtree/bemhtml bundle or data changes
BUNDLES.forEach(bundle => {
var cwd = process.cwd(),
bemtree = LANGUAGES.map(lang => path.join(cwd, BEMTREE[lang][bundle])),
bemhtml = LANGUAGES.map(lang => path.join(cwd, BEMHTML[lang][bundle]));
watch(bemtree.concat(bemhtml, [
path.join(CACHE_DIR_PREFIX + '*', bundle, '**'),
path.join(CACHE_DIR_PREFIX + '*', bundle + '.js')
]),
batch((event, done) => {
bemhtml.forEach(pathToBemhtml => delete require.cache[pathToBemhtml]);
bemtree.forEach(pathToBemtree => delete require.cache[pathToBemtree]);
Q.all(LANGUAGES.map(lang => compilePages(lang, bundle))).then(done);
}
));
});
});
var got = require('got'),
qs = require('querystring');
gulp.task('browser-sync', () => {
const docFeedbackHandlersPort = 8090;
let config;
try {
config = require('./secret-config');
} catch (err) {
console.log('warn: no config with DB credentials was found');
console.log('warn: feedback server will not be started');
}
const isFeedbackEnabled = !!config;
function runBrowserSync() {
console.log('Starting browser-sync on', browserSyncPort);
return browserSync.create().init({
files: OUTPUT + '/**',
server: { baseDir: OUTPUT },
port: browserSyncPort,
open: false,
online: false,
logLevel: 'silent',
notify: false,
ui: false,
middleware: middleware
});
}
function middleware(req, res, next) {
if (isFeedbackEnabled && req.url.includes('/doc-feedback/')) {
var backendUrl = 'http://localhost:' + docFeedbackHandlersPort + req.url.substr(req.url.indexOf('/doc-feedback/'));
if (req.method.toLowerCase() === 'get') {
return got.stream(backendUrl)
.pipe(res);
} else {
var body = '';
req
.on('data', chunk => {
body += chunk;
})
.on('end', () => {
got.post(backendUrl, { query: qs.parse(body) })
.then(() => res.end('ok'))
.catch(console.error);
});
return;
}
}
if (req.url.match(/svgd/)) {
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Content-Encoding', 'deflate')
}
next();
}
return isFeedbackEnabled ? docFeedbackHandlers(config).then(app => {
app.listen(docFeedbackHandlersPort, runBrowserSync);
}) : runBrowserSync(isFeedbackEnabled);
});
gulp.task('csscomb', function() {
return gulp.src('blocks/**/*.css', { base: './' })
.pipe(csscomb())
.pipe(gulp.dest('./'));
});
gulp.task('build', gulp.series(
'is-data-exists',
gulp.parallel('prepare-output', 'prepare-static', 'copy-static-images', 'enb-make'),
gulp.parallel('build-html', 'copy-static', 'copy-sitemap-xml')
));
gulp.task('default', gulp.series(
'build',
gulp.parallel('watch', 'browser-sync')
));