forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
471 lines (421 loc) · 14 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
var gulp = require('gulp');
var pkg = require('./package.json');
var semver = require('semver');
var through = require('through');
var argv = require('minimist')(process.argv.slice(2));
var _ = require('lodash');
var buildConfig = require('./config/build.config.js');
var changelog = require('conventional-changelog');
var connect = require('connect');
var dgeni = require('dgeni');
var es = require('event-stream');
var htmlparser = require('htmlparser2');
var irc = require('ircb');
var lunr = require('lunr');
var marked = require('marked');
var mkdirp = require('mkdirp');
var twitter = require('node-twitter-api');
var yaml = require('js-yaml');
var http = require('http');
var cp = require('child_process');
var fs = require('fs');
var concat = require('gulp-concat');
var footer = require('gulp-footer');
var gulpif = require('gulp-if');
var header = require('gulp-header');
var jshint = require('gulp-jshint');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var stripDebug = require('gulp-strip-debug');
var template = require('gulp-template');
var uglify = require('gulp-uglify');
var gutil = require('gulp-util');
var banner = _.template(buildConfig.banner, { pkg: pkg });
var IS_RELEASE_BUILD = !!argv.release;
if (IS_RELEASE_BUILD) {
gutil.log(
gutil.colors.red('--release:'),
'Building release version (minified, debugs stripped)...'
);
}
gulp.task('default', ['build']);
gulp.task('build', ['bundle', 'sass']);
gulp.task('docs', function(done) {
var docVersion = argv['doc-version'];
if (docVersion != 'nightly' && !semver.valid(docVersion)) {
console.log('Usage: gulp docs --doc-version=(nightly|versionName)');
return process.exit(1);
}
process.env.DOC_VERSION = docVersion;
return dgeni(__dirname + '/docs/docs.config.js').generateDocs().then(function() {
gutil.log('Docs for', gutil.colors.cyan(docVersion), 'generated!');
});
});
var IS_WATCH = false;
gulp.task('watch', ['build'], function() {
IS_WATCH = true;
gulp.watch('js/**/*.js', ['bundle']);
gulp.watch('scss/**/*.scss', ['sass']);
});
gulp.task('changelog', function(done) {
var file = argv.standalone ? '' : __dirname + '/CHANGELOG.md';
var subtitle = argv.subtitle || '"' + pkg.codename + '"';
var toHtml = !!argv.html;
var dest = argv.dest || 'CHANGELOG.md';
var from = argv.from;
changelog({
repository: 'https://github.com/driftyco/ionic',
version: pkg.version,
subtitle: subtitle,
file: file,
from: from
}, function(err, data) {
if (err) return done(err);
if (toHtml) {
data = marked(data, {
gfm: true
});
}
fs.writeFileSync(dest, data);
done();
});
});
gulp.task('bundle', [
'scripts',
'scripts-ng',
'vendor',
'version',
], function() {
IS_RELEASE_BUILD && gulp.src(buildConfig.ionicBundleFiles.map(function(src) {
return src.replace(/.js$/, '.min.js');
}))
.pipe(header(buildConfig.bundleBanner))
.pipe(concat('ionic.bundle.min.js'))
.pipe(gulp.dest(buildConfig.distJs));
return gulp.src(buildConfig.ionicBundleFiles)
.pipe(header(buildConfig.bundleBanner))
.pipe(concat('ionic.bundle.js'))
.pipe(gulp.dest(buildConfig.distJs));
});
gulp.task('jshint', function() {
return gulp.src(['js/**/*.js', 'test/**/*.js'])
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('jshint-stylish'));
});
gulp.task('ddescribe-iit', function() {
return gulp.src(['test/**/*.js', 'js/**/*.js'])
.pipe(notContains([
'ddescribe',
'iit',
'xit',
'xdescribe'
]));
});
gulp.task('vendor', function() {
return gulp.src(buildConfig.vendorFiles, {
cwd: 'config/lib/',
base: 'config/lib/'
})
.pipe(gulp.dest(buildConfig.dist));
});
gulp.task('scripts', function() {
return gulp.src(buildConfig.ionicFiles)
.pipe(gulpif(IS_RELEASE_BUILD, stripDebug()))
.pipe(template({ pkg: pkg }))
.pipe(concat('ionic.js'))
.pipe(header(buildConfig.closureStart))
.pipe(footer(buildConfig.closureEnd))
.pipe(header(banner))
.pipe(gulp.dest(buildConfig.distJs))
.pipe(gulpif(IS_RELEASE_BUILD, uglify()))
.pipe(rename({ extname: '.min.js' }))
.pipe(header(banner))
.pipe(gulp.dest(buildConfig.distJs));
});
gulp.task('scripts-ng', function() {
return gulp.src(buildConfig.angularIonicFiles)
.pipe(gulpif(IS_RELEASE_BUILD, stripDebug()))
.pipe(concat('ionic-angular.js'))
.pipe(header(buildConfig.closureStart))
.pipe(footer(buildConfig.closureEnd))
.pipe(header(banner))
.pipe(gulp.dest(buildConfig.distJs))
.pipe(gulpif(IS_RELEASE_BUILD, uglify()))
.pipe(rename({ extname: '.min.js' }))
.pipe(header(banner))
.pipe(gulp.dest(buildConfig.distJs));
});
gulp.task('sass', function(done) {
gulp.src('scss/ionic.scss')
.pipe(header(banner))
.pipe(sass({
onError: function(err) {
//If we're watching, don't exit on error
if (IS_WATCH) {
console.log(gutil.colors.red(err));
} else {
done(err);
}
}
}))
.pipe(concat('ionic.css'))
.pipe(gulp.dest(buildConfig.distCss))
// .pipe(gulpif(IS_RELEASE_BUILD, minifyCss()))
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest(buildConfig.distCss))
.on('end', done);
});
gulp.task('version', function() {
var d = new Date();
var date = d.toISOString().substring(0,10);
var time = pad(d.getUTCHours()) +
':' + pad(d.getUTCMinutes()) +
':' + pad(d.getUTCSeconds());
return gulp.src('config/version.template.json')
.pipe(template({
pkg: pkg,
date: date,
time: time
}))
.pipe(rename('version.json'))
.pipe(gulp.dest('dist'));
});
gulp.task('release-tweet', function(done) {
var oauth = {
consumerKey: process.env.TWITTER_CONSUMER_KEY,
consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN,
accessTokenSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET
};
var client = new twitter(oauth);
client.statuses(
'update',
{ status: buildConfig.releaseMessage() },
oauth.accessToken,
oauth.accessTokenSecret,
done
);
});
gulp.task('release-irc', function(done) {
var client = irc({
host: 'irc.freenode.net',
secure: true,
nick: 'ionitron',
username: 'ionitron',
realName: 'ionitron',
channels: ['#ionic']
}, function() {
client.say('#ionic', buildConfig.releaseMessage(), function() {
client.quit('', done);
});
});
});
gulp.task('docs-index', function() {
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++;
}
return gulp.src([
'tmp/ionic-site/docs/{components,guide,api,overview}/**/*.{md,html,markdown}',
'tmp/ionic-site/docs/index.html',
'tmp/ionic-site/getting-started/index.html',
'tmp/ionic-site/tutorials/**/*.{md,html,markdown}',
'tmp/ionic-site/_posts/**/*.{md,html,markdown}'
])
.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(/^.*?tmp\/ionic-site\//, '');
// 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('tmp/ionic-site/data');
fs.writeFileSync(
'tmp/ionic-site/data/index.json',
JSON.stringify({'ref': ref, 'index': idx.toJSON()})
);
});
});
gulp.task('sauce-connect', sauceConnect);
gulp.task('cloudtest', ['protractor-sauce'], function(cb) {
sauceDisconnect(cb);
});
gulp.task('karma', function(cb) {
return karma(cb, [__dirname + '/config/karma.conf.js', '--single-run=true']);
});
gulp.task('karma-watch', function(cb) {
return karma(cb, [__dirname + '/config/karma.conf.js']);
});
var connectServer;
gulp.task('connect-server', function() {
var app = connect().use(connect.static(__dirname));
connectServer = http.createServer(app).listen(8765);
});
gulp.task('protractor', ['connect-server'], function(cb) {
return protractor(cb, ['config/protractor.conf.js']);
});
gulp.task('protractor-sauce', ['sauce-connect', 'connect-server'], function(cb) {
return protractor(cb, ['config/protractor-sauce.conf.js']);
});
function karma(cb, args) {
if (argv.browsers) {
args.push('--browsers='+argv.browsers.trim());
}
if (argv.reporters) {
args.push('--reporters='+argv.reporters.trim());
}
cp.spawn('node', [
__dirname + '/node_modules/karma/bin/karma',
'start'
].concat(args), { stdio: 'inherit' })
.on('exit', function(code) {
if (code) return cb('Karma test(s) failed. Exit code: ' + code);
cb();
});
}
function pad(n) {
if (n<10) { return '0' + n; }
return n;
}
function protractor(cb, args) {
cp.spawn('protractor', args, { stdio: 'inherit' })
.on('exit', function(code) {
connectServer && connectServer.close();
if (code) return cb('Protector test(s) failed. Exit code: ' + code);
cb();
});
}
var sauceInstance;
function sauceConnect(cb) {
require('sauce-connect-launcher')({
username: process.env.SAUCE_USER,
accessKey: process.env.SAUCE_KEY,
verbose: true,
tunnelIdentifier: process.env.TRAVIS_BUILD_NUMBER
}, function(err, instance) {
if (err) return cb('Failed to launch sauce connect!');
sauceInstance = instance;
cb();
});
}
function sauceDisconnect(cb) {
if (sauceInstance) {
return sauceInstance.close(cb);
}
cb();
}
function notContains(disallowed) {
disallowed = disallowed || [];
return through(function(file) {
var error;
var contents = file.contents.toString();
disallowed.forEach(function(str) {
var idx = disallowedIndex(contents, str);
if (idx !== -1) {
error = error || file.path + ' contains ' + str + ' on line ' +
contents.substring(0, idx, str).split('\n').length + '!';
}
});
if (error) {
throw new Error(error);
} else {
this.emit('data', file);
}
});
function disallowedIndex(content, disallowedString) {
var notFunctionName = '[^A-Za-z0-9$_]';
var regex = new RegExp('(^|' + notFunctionName + ')(' + disallowedString + ')' + notFunctionName + '*\\(', 'gm');
var match = regex.exec(content);
// Return the match accounting for the first submatch length.
return match !== null ? match.index + match[1].length : -1;
}
}