-
Notifications
You must be signed in to change notification settings - Fork 19
/
gulpfile.js
134 lines (111 loc) · 4.67 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
var gulp = require('gulp');
var cheerio = require('gulp-cheerio');
var file = require('gulp-file');
var jsonEditor = require('gulp-json-editor');
var path = require('path');
var paths = {
sampleAssertionPaths: [
'site-reactelement/assertions/ReactElement/index.html',
'site-renderedreactelement/assertions/RenderedReactElement/index.html',
'site-testrenderer/assertions/ReactTestRenderer/index.html'
],
allAssertions: [
'site-reactelement/assertions/ReactElement/**/*.html',
'site-renderedreactelement/assertions/RenderedReactElement/**/*.html',
'site-testrenderer/assertions/ReactTestRenderer/**/*.html'
],
otherDocs: [
'site-reactelement/**/*',
'!site-reactelement/assertions/**/*.html'
],
searchIndex: [
'site-reactelement/searchIndex.json',
'site-renderedreactelement/searchIndex.json',
'site-testrenderer/searchIndex.json'
],
output: 'site-build'
};
var navs = {};
var fullNavigation = '';
gulp.task('extract-navs', function () {
return gulp.src(paths.sampleAssertionPaths)
.pipe(cheerio(function ($, file) {
$('#assertion-menu li.active').attr('class', '');
$('#assertion-menu h3.active').attr('class', '');
var assertionSubjectPath = path.basename(path.dirname(file.path));
$('#assertion-menu h3 a').each(function () {
var anchor = $(this);
anchor.attr('href', path.join('../..', assertionSubjectPath));
});
$('#assertion-menu li a').each(function () {
var anchor = $(this);
anchor.attr('href', path.join('../..', assertionSubjectPath, path.basename(anchor.attr('href'))));
});
navs[file.path.substr(file.cwd.length + 1).split('/')[0]] = $('#assertion-menu').html();
}));
});
gulp.task('merge-navs', ['extract-navs'], function (callback) {
fullNavigation = navs['site-reactelement'] + navs['site-renderedreactelement'] + navs['site-testrenderer'];
callback();
});
gulp.task('update-all-navs', ['merge-navs'], function () {
return gulp.src(paths.allAssertions)
.pipe(cheerio(function ($, file) {
$('#assertion-menu').html(fullNavigation);
var assertionSubjectPath = path.basename(path.dirname(path.dirname(file.path)));
var subjectPrefix;
if (assertionSubjectPath === 'assertions') {
// This is the index.html at the root of the subject directory (e.g. ReactElement/index.html)
// Need to remove one '../' from each link
// Update the assertionSubjectPath so it's correct for the 'active' class update just below
assertionSubjectPath = path.basename(path.dirname(file.path));
$('#assertion-menu li a').each(function () {
var anchor = $(this);
anchor.attr('href', anchor.attr('href').substr(3));
});
// Update the links for the groups
$('#assertion-menu h3 a').each(function () {
var anchor = $(this);
anchor.attr('href', anchor.attr('href').substr(3));
});
subjectPrefix = '../';
} else {
// This is a normal assertion page, need to remove the href from the active <a> element,
// and set the 'active' class on the parent li
var assertionName = path.basename(path.dirname(file.path));
var currentAssertion = $('#assertion-menu').find('li a[href="../../' + assertionSubjectPath + '/' + assertionName + '"]');
currentAssertion.attr('href', '');
currentAssertion.parent().attr('class', 'active');
subjectPrefix = '../../';
}
// Update the assertion subject group to be 'active'
$('#assertion-menu').find('h3 a').each(function () {
var anchor = $(this);
if (anchor.text() === assertionSubjectPath) {
anchor.parent().attr('class', 'active');
anchor.attr('href', '');
}
});
file.base = path.join(file.cwd, file.path.substr(file.cwd.length + 1).split('/')[0]);
}))
.pipe(gulp.dest(paths.output));
});
gulp.task('copy-rest', function () {
return gulp.src(paths.otherDocs)
.pipe(gulp.dest(paths.output));
});
var mergedSearch = [];
gulp.task('merge-search', function () {
return gulp.src(paths.searchIndex)
.pipe(jsonEditor(function (json) {
mergedSearch = mergedSearch.concat(json);
return json;
}))
.pipe(gulp.dest(paths.output)); // jsonEditor seems to have issues if we don't pipe the output to a writer
// This output will be overwritten when we 'output-search' completes
});
gulp.task('output-search', ['merge-search'], function () {
return file('searchIndex.json', JSON.stringify(mergedSearch, null, 2), { src: true })
.pipe(gulp.dest(paths.output));
});
gulp.task('default', ['update-all-navs', 'copy-rest', 'output-search']);