forked from ionic-team/ionic-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
351 lines (311 loc) · 10.8 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserSync = require('browser-sync');
var cache = require('gulp-cache');
var concat = require('gulp-concat');
var cp = require('child_process');
var footer = require('gulp-footer');
var header = require('gulp-header');
var minifyCss = require('gulp-minify-css');
var ngmin = require('gulp-ngmin');
var pagespeed = require('psi');
var pkg = require('./package.json');
var prefix = require('gulp-autoprefixer');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var shell = require('gulp-shell');
var uglify = require('gulp-uglify');
var messages = {
jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};
var AUTOPREFIXER_BROWSERS = [
'ie >= 9',
'ie_mob >= 9',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
var closureStart =
'/*!\n' +
' * Ionic IO\n' +
' * Copyright ' + new Date().getYear() + ' Drifty Co. http://drifty.com/\n' +
' */\n' +
'(function() {\n';
var closureEnd = '\n})();\n';
var version = pkg.version;
gulp.task('styles:v2', function() {
// For best performance, don't add Sass partials to `gulp.src`
return gulp.src('_scss/v2.scss')
// .pipe($.sourcemaps.init())
.pipe(sass({
precision: 10,
onError: console.error.bind(console, 'Sass error:')
}))
.pipe($.autoprefixer({browsers: AUTOPREFIXER_BROWSERS}))
//
// .pipe($.sourcemaps.write())
.pipe(gulp.dest('./css/'))
.pipe(gulp.dest('./_site/css/'))
// .pipe($.sourcemaps.write('./css/'))
// Concatenate and minify styles
.pipe(minifyCss({
keepSpecialComments: 0
}))
.pipe(rename({extname: '.min.css'}))
.pipe(gulp.dest('./css/'))
.pipe(gulp.dest('./_site/css/'))
.pipe($.size({title: 'styles'}));
});
gulp.task('styles:v1', function(done) {
return gulp.src('scss/**/*.scss')
.pipe(sass({onError: browserSync.notify}))
.pipe($.autoprefixer({browsers: AUTOPREFIXER_BROWSERS}))
.pipe(gulp.dest('./css/'))
.pipe(gulp.dest('./_site/css/'))
.pipe(minifyCss({
keepSpecialComments: 0
}))
.pipe(rename({extname: '.min.css'}))
.pipe(gulp.dest('./css/'))
.pipe(gulp.dest('./_site/css/'));
});
// Optimize images
gulp.task('images', function() {
return gulp.src('_img/**/*')
.pipe(cache($.imagemin({
progressive: true,
interlaced: true
})))
.pipe(gulp.dest('img'))
.pipe(gulp.dest('_site/img'))
.pipe($.size({title: 'images'}));
});
// compress and concat JS
gulp.task('js', function() {
return gulp.src(['_js/**/*.js'])
.pipe(concat('ionic-docs.js'))
.pipe(header(closureStart))
.pipe(footer(closureEnd))
.pipe(gulp.dest('js'))
.pipe(gulp.dest('_site/js'))
.pipe(uglify())
.pipe(rename({extname: '.min.js'}))
.pipe(gulp.dest('js'))
.pipe(gulp.dest('_site/js'))
.pipe($.size({title: 'js'}));
});
/**
* Build the Jekyll Site
*/
gulp.task('jekyll-build', [], function(done) {
browserSync.notify(messages.jekyllBuild);
return cp.spawn('jekyll',
['build', '-I', '--config', '_config_development.yml'],
{stdio: 'inherit'})
.on('close', done);
});
/**
* Run Generate linkchecker page
*/
gulp.task('linkchecker', ['build'],
shell.task('_scripts/linkchecker.sh',{verbose: true})
);
/**
* Rebuild Jekyll & do page reload
*/
gulp.task('jekyll-rebuild', ['jekyll-build'], function() {
browserSync.reload();
});
/**
* Wait for jekyll-build, then launch the Server
*/
gulp.task('server', ['server:ionicons','server:stylesv1', 'server:stylesv2', 'images',
'jekyll-build'
], function() {
browserSync({
server: {
baseDir: '_site'
}
});
});
gulp.task('server:ionicons', ['ionicons'], function() {
browserSync.reload();
});
gulp.task('server:stylesv1', ['styles:v1'], function() {
browserSync.reload();
});
gulp.task('server:stylesv2', ['styles:v2'], function() {
browserSync.reload();
});
gulp.task('server:jekyll', ['jekyll-build'], function() {
browserSync.reload();
});
gulp.task('server:images', ['images'], function() {
browserSync.reload();
});
gulp.task('server:js', ['js'], function() {
browserSync.reload();
});
gulp.task('watch', ['server'], function() {
gulp.watch('scss/**.scss', ['server:stylesv1']);
gulp.watch(['_scss/*.scss', '_scss/docs/*.scss', '_scss/pages/*.scss'],
['server:stylesv2']);
gulp.watch(['_img/*', '_img/*/*'], ['server:images']);
gulp.watch(['_js/**/*.js', 'submit-issue/*/*.js'], ['server:js']);
gulp.watch(['*.html', 'submit-issue/*.html', 'getting-started/*.html',
'_layouts/*', '_layouts/*/*', '_posts/*', '_includes/**/*',
'docs/**/*.{md,html,js,css}', '!docs/v2/2*', '!docs/v2/nightly',
'!docs/1.*', 'dist/preview-app/www/**/*'
], ['server:jekyll']);
});
gulp.task('cli-docs', function() {
try {
var fs = require('fs');
var cliTasks = JSON.stringify(require('../ionic-cli/lib/tasks/cliTasks'));
fs.writeFileSync('_data/cliData.json', cliTasks);
} catch (e) {
console.error('Cannot find module cliTasks, please make sure the ionic-' +
'cli repo is cloned locally. ionic-site and ionic-cli ' +
'should be sibling directories.');
}
});
gulp.task('docs.index', function() {
var lunr = require('lunr');
var gutil = require('gulp-util');
var es = require('event-stream');
var yaml = require('js-yaml');
var htmlparser = require('htmlparser2');
var mkdirp = require('mkdirp');
var fs = require('fs');
var idx = lunr(function() {
this.field('path');
this.field('title', {boost: 10});
this.field('body');
this.ref('id');
});
var ref = {};
var refId = 0;
function addToIndex(path, title, layout, body) {
// Add the data to the indexer and ref object
idx.add({'path': path, 'body': body, 'title': title, id: refId});
ref[refId] = {'p': path, 't': title, 'l': layout};
refId++;
}
var docPath = 'docs/v2';
gutil.log('Reading docs from', gutil.colors.cyan(docPath));
return gulp.src([
docPath + '/{api,cli,components,faq,getting-started,native,resources,theming,utils}/**/*.{md,html,markdown}',
docPath + '/index.md',
])
.pipe(es.map(function(file, callback) {
//docs for gulp file objects: https://github.com/wearefractal/vinyl
var contents = file.contents.toString(); //was buffer
// Grab relative path from ionic-site root
var relpath = file.path.replace(RegExp('^.*?' + docPath.replace('/docs/v2', '') + '/'), '');
// Read out the yaml portion of the Jekyll file
var yamlStartIndex = contents.indexOf('---');
if (yamlStartIndex === -1) {
return callback();
}
// read Jekyll's page yaml variables at the top of the file
var yamlEndIndex = contents.indexOf('---', yamlStartIndex+3); //starting from start
var yamlRaw = contents.substring(yamlStartIndex+3, yamlEndIndex);
var pageData = yaml.safeLoad(yamlRaw);
if(!pageData.title || !pageData.layout) {
return callback();
}
// manually set to not be searchable, or for a blog post, manually set to be searchable
if(pageData.searchable === false || (pageData.layout == 'post' && pageData.searchable !== true)) {
return callback();
}
// clean up some content so code variables are searchable too
contents = contents.substring(yamlEndIndex+3);
contents = contents.replace(/<code?>/gi, '');
contents = contents.replace(/<\/code>/gi, '');
contents = contents.replace(/<code?></gi, '');
contents = contents.replace(/><\/code>/gi, '');
contents = contents.replace(/`</gi, '');
contents = contents.replace(/>`/gi, '');
// create a clean path to the URL
var path = '/' + relpath.replace('index.md', '')
.replace('index.html', '')
.replace('.md', '.html')
.replace('.markdown', '.html');
if(pageData.layout == 'post') {
path = '/blog/' + path.substring(19).replace('.html', '/');
}
var parser;
if(pageData.search_sections === true) {
// each section within the content should be its own search result
var section = { body: '', title: '' };
var isTitleOpen = false;
parser = new htmlparser.Parser({
ontext: function(text){
if(isTitleOpen) {
section.title += text; // get the title of this section
} else {
section.body += text.replace(/{%.*%}/, '', 'g'); // Ignore any Jekyll expressions
}
},
onopentag: function(name, attrs) {
if(name == 'section' && attrs.id) {
// start building new section data
section = { body: '', path: path + '#' + attrs.id, title: '' };
} else if( (name == 'h1' || name == 'h2' || name == 'h3') && attrs.class == 'title') {
isTitleOpen = true; // the next text will be this sections title
}
},
onclosetag: function(name) {
if(name == 'section') {
// section closed, index this section then clear it out
addToIndex(section.path, section.title, pageData.layout, section.body);
section = { body: '', title: '' };
} else if( (name == 'h1' || name == 'h2' || name == 'h3') && isTitleOpen) {
isTitleOpen = false;
}
}
});
parser.write(contents);
parser.end();
} else {
// index the entire page
var body = '';
parser = new htmlparser.Parser({
ontext: function(text){
body += text.replace(/{%.*%}/, '', 'g'); // Ignore any Jekyll expressions
}
});
parser.write(contents);
parser.end();
addToIndex(path, pageData.title, pageData.layout, body);
}
callback();
})).on('end', function() {
// Write out as one json file
mkdirp.sync(docPath + '/data');
fs.writeFileSync(
docPath + '/data/index.json',
JSON.stringify({'ref': ref, 'index': idx.toJSON()})
);
});
});
gulp.task('ionicons', function() {
gulp.src('node_modules/ionicons/dist/css/ionicons.min.css')
.pipe(gulp.dest('./css/v2-demos/ionicons/'));
gulp.src('node_modules/ionicons/dist/fonts/*{eot,svg,ttf,woff}')
.pipe(gulp.dest('./css/v2-demos/fonts/'));
gulp.src('node_modules/ionicons/dist/data/ionicons.json')
.pipe(rename('site_data.json'))
.pipe(gulp.dest('./docs/v2/resources/ionicons/'));
gulp.src('node_modules/ionicons/dist/data/mode-icons.json')
.pipe(gulp.dest('./docs/v2/resources/ionicons/data/'));
gulp.src('node_modules/ionicons/dist/data/logo-icons.json')
.pipe(rename('generic-icons.json'))
.pipe(gulp.dest('./docs/v2/resources/ionicons/data/'));
});
gulp.task('build', ['ionicons', 'cli-docs', 'styles:v1', 'styles:v2', 'jekyll-build', 'images', 'js']);
gulp.task('default', ['build']);