forked from bem-site/bem.info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
244 lines (207 loc) · 7.22 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
'use strict';
var path = require('path'),
fs = require('fs'),
cp = require('child_process'),
_ = require('lodash'),
Q = require('q'),
enb = require('enb'),
rimraf = require('rimraf'),
gulp = require('gulp'),
batch = require('gulp-batch'),
browserSync = require('browser-sync'),
csscomb = require('gulp-csscomb');
const LANGUAGES = ['en', 'ru'];
const CACHE_DIRS = LANGUAGES.reduce((prev, language) => {
prev[language] = './.cache/gorshochek-cache-' + language;
return prev;
}, {});
const DATA_DIR_PREFIX = './.cache/gorshochek-data-';
const DATA_DIRS = LANGUAGES.reduce((prev, language) => {
prev[language] = DATA_DIR_PREFIX + language;
return prev;
}, {});
const STATIC = './static';
const OUTPUT = './output';
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 removeFolder(folder) {
return Q.denodeify(rimraf)(folder);
}
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: DATA_DIRS[lang],
destination: OUTPUT_DIRS[lang],
destinationRoot: OUTPUT + (process.env.YENV === 'production' ? '/bem.info/static' : ''),
langs: LANGUAGES,
lang,
DEBUG: process.env.DEBUG,
YENV: process.env.YENV
}
});
}
// Подготовка директорий output-*
gulp.task('copy-misc-to-output', () => {
rimraf.sync(OUTPUT);
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 => {
return gulp.src(path.join(STATIC, '{favicon.ico,robots.txt}'))
.pipe(gulp.dest(OUTPUT_DIRS[lang]));
}));
});
// Сборка данных
function data() {
return Q.all(LANGUAGES.map(lang => {
return runSubProcess('./lib/data-builder.js', {
cwd: process.cwd(),
encoding: 'utf-8',
env: {
GORSHOCHEK_CACHE_FOLDER: CACHE_DIRS[lang],
modelPath: `./content/model-hybrid.${lang}.json`,
host: `http://${lang}.bem.info`,
dest: DATA_DIRS[lang],
root: process.env.YENV === 'production' ? '' : '/bem.info/' + lang,
token: process.env.TOKEN,
DEBUG: process.env.DEBUG
}
});
}));
}
gulp.task('data-clean', () => Q.all(_.values(DATA_DIRS).map(removeFolder)));
gulp.task('data-cache-clean', () => Q.all(_.values(CACHE_DIRS).map(removeFolder)));
gulp.task('data', data);
gulp.task('data-rebuild', () => gulp.series('data-clean', 'data-cache-clean', 'data'));
// Шаблонизация данных
gulp.task('enb-make', enb.make);
function copyStatic() {
return Q.all(LANGUAGES.map(lang => {
var files = BUNDLES.map(bundle => {
return 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(DATA_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(DATA_DIRS[lang], '/*.{gif,png,jpg,svg,svgz}'))
.pipe(gulp.dest(OUTPUT_DIRS[lang]));
})));
gulp.task('compile-pages', gulp.series(
'enb-make',
'copy-static',
'copy-static-images',
'copy-sitemap-xml',
'build-html'
));
// Наблюдатель
gulp.task('watch', () => {
gulp.watch(['content/**/*'], batch((event, done) => {
data();
done();
}));
gulp.watch(['blocks/**/*'], batch((event, done) => {
enb.make();
copyStatic();
done();
//TODO: gulp.series does not work :(
// gulp.series('enb-make', 'copy-static', 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]));
gulp.watch(bemtree.concat(bemhtml, [
path.join(DATA_DIR_PREFIX + '*', bundle, '**'),
path.join(DATA_DIR_PREFIX + '*', bundle + '.js')
]),
batch((event, done) => {
bemhtml.forEach(pathToBemhtml => delete require.cache[pathToBemhtml]);
bemtree.forEach(pathToBemtree => delete require.cache[pathToBemtree]);
LANGUAGES.forEach(lang => compilePages(lang, bundle));
done();
}
));
});
});
gulp.task('browser-sync', () => {
browserSync.create().init({
files: OUTPUT + '/**',
server: { baseDir: OUTPUT },
port: 8008,
open: false,
online: false,
logLevel: 'silent',
notify: false,
ui: false,
middleware: function(req, res, next) {
if (req.url.match(/svgd/)) {
res.setHeader('Content-Type', 'image/svg+xml');
res.setHeader('Content-Encoding', 'deflate')
}
next();
}
});
});
gulp.task('libs-build', function() {
// require('bem-lib-site-generator').data(path.resolve('path/to/library'));
});
gulp.task('csscomb', function() {
return gulp.src('blocks/**/*.css', { base: './' })
.pipe(csscomb())
.pipe(gulp.dest('./'));
});
gulp.task('default', gulp.series(
'copy-misc-to-output',
'data',
'enb-make',
'build-html',
'copy-static',
'copy-static-images',
'copy-sitemap-xml',
'csscomb',
gulp.parallel('watch', 'browser-sync')
));