-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
352 lines (304 loc) · 9.03 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
/* jshint camelcase: false */
'use strict';
/** npm dependencies */
var argv = require('yargs')
.default('port', 4000)
.default('tag', null)
.default('version', null)
.default('type', null)
.argv;
var child_process = require('child_process');
var del = require('del');
var Dgeni = require('dgeni');
var exec = require('child_process').exec;
var karma = require('karma').server;
var merge = require('merge-stream');
var path = require('path');
var runSequence = require('run-sequence');
var url = require('url');
/** Gulp dependencies */
var gulp = require('gulp');
var $g = require('gulp-load-plugins')();
/** Local dependencies */
var buildConfig = require('./config/build.config.js');
function noop() {}
/** Arguments */
var VERSION = argv.version || buildConfig.version;
/** Build configuration */
var config = {
banner: ['/*!',
' * ' + buildConfig.name,
' * ' + buildConfig.repository,
' * @license ' + buildConfig.pkg.license,
' * v' + VERSION,
' * ' + (new Date()).toISOString(),
' */',
''].join('\n'),
paths: {
clean: ['dist', '.tmp'],
js: ['src/js/**/*.js'], // '.tmp/templates/templates.js'],
test: ['test/*.spec.js'],
less: {
main: 'src/less/main.less',
watch: ['src/less/**/*.less']
},
templates: 'src/template/**/*.html',
docs: 'docs/assets/**/*',
docs_watch: 'docs/**',
// Directories for compiled, distributed files
dist: {
js: 'dist/js',
css: 'dist/css'
},
// Locations for temporary files to use while building
tmp: {
docs: '.tmp/docs',
e2e: ['.tmp/docs/ptore2e/**/*.js', '.tmp/docs/examples/**/*.js'],
templates: '.tmp/templates'
}
}
};
/** Gulp tasks */
// Clean
gulp.task('clean', function (cb) {
del(config.paths.clean, cb);
});
// Check gulpfile with jshint
gulp.task('gulpfile', function () {
return gulp.src('gulpfile.js')
// TODO: this is too strict
// .pipe($g.jshint('.jshintrc'))
// .pipe($g.jshint.reporter('jshint-stylish'))
// .pipe($g.jshint.reporter('fail'));
});
function templates() {
return gulp.src(config.paths.templates)
.pipe($g.cached('templates'))
.pipe($g.minifyHtml({
empty: true,
spare: true,
quotes: true
}))
.pipe($g.remember('templates'))
.pipe($g.ngHtml2js({
moduleName: buildConfig.moduleName
//prefix: buildConfig.moduleName + "/"
}))
.pipe($g.concat('templates.js'));
}
gulp.task('templates', function () {
return templates()
.pipe(
gulp.dest(config.paths.tmp.templates)
);
});
// TODO: need progeny() here for after templates change?
gulp.task('js', function () {
var js = gulp.src(config.paths.js)
.pipe($g.plumber())
.pipe($g.cached('js'))
.pipe($g.jshint())
// .pipe($g.jshint.reporter('fail'))
.pipe($g.jscs())
.on('error', noop)
.pipe($g.jscsStylish.combineWithHintResults())
.pipe($g.jshint.reporter('jshint-stylish'))
.pipe($g.ngAnnotate());
return merge(js, templates())
.pipe($g.remember('js'))
// Re-order files so templates come last.
// They will fail to find the module if they're first
.pipe($g.order([
'src/js/plugin.module.js',
'src/js/**/*.js',
'src/template/**/*.js'
], { base: '.' }))
.pipe($g.concat(buildConfig.distFileName + '.js')) // angular.confirm.bootstrap
.pipe($g.header(config.banner))
.pipe(gulp.dest('dist/js'))
.pipe($g.sourcemaps.init())
.pipe($g.uglify({
compress: {
negate_iife: false
}
}))
.pipe($g.rename(buildConfig.distFileName + '.min.js'))
.pipe($g.header(config.banner))
.pipe($g.sourcemaps.write('./'))
.pipe($g.size({ title: 'js' }))
.pipe(
gulp.dest(config.paths.dist.js)
)
.pipe($g.connect.reload());
});
gulp.task('less', function () {
// Process the less files
return gulp.src(config.paths.less.main)
.pipe($g.cached('less'))
.pipe($g.progeny())
.pipe($g.less())
.pipe($g.autoprefixer())
.pipe($g.rename(buildConfig.distFileName + '.css'))
.pipe($g.header(config.banner))
.pipe(
gulp.dest(config.paths.dist.css)
)
// Minify
.pipe($g.sourcemaps.init())
.pipe($g.minifyCss())
.pipe($g.rename(buildConfig.name.distFileName + '.min.css'))
.pipe($g.sourcemaps.write('./'))
.pipe($g.size({ title: 'css' }))
.pipe(
gulp.dest(config.paths.dist.css)
)
.pipe($g.connect.reload());
});
gulp.task('test', function (done) {
karma.start({
configFile: __dirname + '/config/karma.conf.js',
singleRun: true
}, done);
});
gulp.task('webdriver_update', $g.protractor.webdriver_update);
gulp.task('webdriver_standalone', ['webdriver_update'], function (cb) {
var child = child_process.spawn(path.normalize('./node_modules/.bin/webdriver-manager.cmd'), ['start'], {
stdio: 'pipe'
});
child.once('close', cb);
child.on('error', cb);
child.stderr.on('data', function(data) {
var sentinal = 'Started org.openqa.jetty.jetty.Server'
if (data.toString().indexOf(sentinal) !== -1) {
cb(null, child)
}
});
// child.stdout.on('data', function(data) {
// var sentinal = 'Started org.openqa.jetty.jetty.Server'
// if (data.toString().indexOf(sentinal) != -1) {
// cb(null, child)
// }
// });
});
gulp.task('connect', function () {
return $g.connect.server({
root: ['.tmp/docs', '.'],
port: 8000
});
});
gulp.task('protractor', ['webdriver_standalone', 'connect'], function () {
return gulp.src(config.paths.tmp.e2e)
.pipe($g.protractor.protractor({
configFile: 'config/protractor.conf.js',
args: ['--baseUrl', 'http://127.0.0.1:8000']
}))
.on('error', function(e) { throw e });
});
gulp.task('docs', function () {
gulp.src(config.paths.docs, { base: 'docs/assets' })
.pipe(gulp.dest(config.paths.tmp.docs))
.pipe($g.connect.reload());
var dgeni = new Dgeni([require('./docs/config')]);
dgeni.generate()
.then($g.connect.reload);
});
/*-- Build and watch tasks --*/
gulp.task('watch', ['build'], function (done) { // TODO?: 'auto-reload-gulp'
gulp.watch(config.paths.templates, ['js', 'docs']);
gulp.watch(config.paths.js, ['js', 'docs']);
gulp.watch(config.paths.docs_watch, ['docs']);
gulp.watch(config.paths.less.watch, ['less']);
// Start karma
karma.start({
singleRun:false,
autoWatch:true,
configFile: __dirname + '/config/karma.conf.js',
browsers : argv.browsers ? argv.browsers.trim().split(',') : ['PhantomJS'],
}, done);
// Fire up connect server
$g.connect.server({
root: ['.tmp/docs', '.'],
port: argv.port,
livereload: true
});
});
gulp.task('build', function (cb) {
runSequence(
'clean',
'gulpfile',
['less', 'js'],
cb
);
});
gulp.task('bump', function () {
if (!argv.version && !argv.type) {
$g.util.log($g.util.colors.red('You must supply either a version or bump type with the --version or --type arguments'));
return;
}
var opts = {};
if (argv.version) { opts.version = argv.version; }
else if (argv.type) { opts.type = argv.type; }
return gulp.src([
'./package.json',
'./bower.json'
])
.pipe($g.bump(opts))
.pipe(gulp.dest('./'));
});
gulp.task('pre-publish', ['build'], function (cb) {
var pub = merge(
gulp.src('dist/**', { base: '.' }),
gulp.src('.tmp/docs/**', { base: '.tmp/docs' }),
gulp.src('bower_components/**', { base: '.' })
)
.pipe(gulp.dest('.tmp/publish'));
var bower = gulp.src('bower.json')
.pipe($g.jsonTransform(function(data) {
return {
name: data.name,
version: data.version,
authors: data.authors,
description: data.description,
keywords: data.keywords,
license: data.license,
dependencies: data.dependencies
};
}, 2))
.pipe(gulp.dest('.tmp/publish'));
return merge(pub, bower);
});
gulp.task('publish', ['pre-publish'], function (cb) {
var ghpages = require('gh-pages');
var opts = {
// Use the tag argument as a tag, if it's not present use the current package version
tag: argv.tag ? argv.tag : buildConfig.version,
user: {
name: 'Brian Hann',
email: 'emailc0bra@gmail.com'
}
};
$g.util.log($g.util.colors.green('Publising version ' + opts.tag));
// Check if tag exists
exec('git rev-parse ' + opts.tag, function (error, stdout, stderr) {
if (!error) {
$g.util.log($g.util.colors.red('Tag ' + opts.tag + ' already exists'));
return cb(error);
}
else {
// Use encrypted environment variable to set github token
if (process.env.TRAVIS) {
// Make sure token environment variables exists
if (!process.env.GITHUB_TOKEN) {
var err = 'No github token for pushing to gh-pages';
$g.util.log($g.util.colors.red(err));
return cb(err);
}
var parsed = url.parse(buildConfig.pkg.repository.url);
parsed.auth = encodeURIComponent(process.env.GITHUB_TOKEN);
opts.repo = url.format(parsed);
}
ghpages.publish(path.join(__dirname, '.tmp/publish'), opts, cb);
}
});
});
gulp.task('default', ['build']);