-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgulpfile.js
More file actions
318 lines (283 loc) · 9.52 KB
/
gulpfile.js
File metadata and controls
318 lines (283 loc) · 9.52 KB
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
'use strict';
var gulp = require('gulp'),
bump = require('gulp-bump'),
debug = require('gulp-debug'),
ccompiler = require('gulp-closure-compiler'),
gjslint = require('gulp-gjslint'),
gutil = require('gulp-util'),
jsonedit = require('gulp-json-editor'),
rename = require('gulp-rename'),
minimist = require('minimist'),
del = require('del'),
extend = require('node.extend'),
sequence = require('run-sequence'),
path = require('path'),
karma = require('karma').server,
webserver = require('gulp-webserver'),
protractor = require('gulp-protractor').protractor,
webdriverStandalone = require('gulp-protractor').webdriver_standalone,
webdriverUpdate = require('gulp-protractor').webdriver_update,
coveralls = require('gulp-coveralls'),
allowedLevels = ['major', 'minor', 'patch', 'prerelease'],
allowedEnvironments = ['production', 'development'],
knownArgs = {
'boolean': ['banner'],
'string': ['env', 'level'],
'default': {
env: process.env.NODE_ENV || allowedEnvironments[0],
banner: false,
level: allowedLevels[2]
},
'alias': { e: 'env', b: 'banner' }
};
/**
* @typedef {Object} GulpArguments
* @property {!boolean} banner
* @property {!string} env
* @property {!string} level
*/
var GulpArguments;
/** @type {GulpArguments} */
var args = minimist(process.argv.slice(2), knownArgs),
bannerHelp = { options: {} },
environmentsHelp = { options: {} },
levelsHelp = { options: {} };
var getLevel = function() {
if (allowedLevels.indexOf(args.level) === -1) {
args.level = knownArgs.default.level;
}
return args.level;
};
var getEnv = function() {
if (allowedEnvironments.indexOf(args.env) === -1) {
args.env = knownArgs.default.env;
}
return args.env;
};
var isProduction = function() {
return getEnv() === allowedEnvironments[0];
};
levelsHelp.options['level=' + allowedLevels.join('|')] = 'Version level to increment';
environmentsHelp.options['env=' + allowedEnvironments.join('|')] = 'Kind of build to perform, defaults to production';
/**
* @type {string}
*/
bannerHelp.options.banner = 'Prepend banner to the built file';
/**
* @typedef {*} PackageJson
* @property {!string} name
* @property {!string} description
* @property {!string} license
* @property {!string} homepage
* @property {!string} version
* @property {!string} main
* @property {{source: !string, dist: !string, example: !string, unit: !string, e2e: !string}} directories
* @property {{name: !string, email: !string}} author
*/
var PackageJson;
/** @type {PackageJson} */
var bundle = require('./package.json'),
banner = [
'/**',
' * Copyright (c) ' + new Date().getFullYear() + ', ' + bundle.author.name + ' <' + bundle.author.email + '>',
' * ' + bundle.main + ' - ' + bundle.description,
' * @version ' + bundle.version,
' * @link ' + bundle.homepage,
' * @license ' + bundle.license,
' */'
].join('\n');
/**
* @type {!string}
*/
bundle.main = bundle.main.split('.').slice(0, -2).join('.');
gulp = gulp = require('gulp-help')(gulp, {
description: 'Display this help text'
});
/**
* @type {{
* compiler: string,
* prefixes: {dev: string},
* output: {minified: string, sourcemap: string},
* externs: string[],
* sources: *[],
* release: string
* }}
*/
var settings = {
compiler: 'bower_components/closure-compiler/compiler.jar',
prefixes: {
dev: 'dev.'
},
output: {
minified: /** @type {string} */ (bundle.main + '.min.js'),
sourcemap: /** @type {string} */ (bundle.main + '.min.js.map')
},
externs: [
'bower_components/closure-angularjs-externs/index.js',
'bower_components/closure-angularjs-q_templated-externs/index.js',
'bower_components/closure-angularjs-http-promise_templated-externs/index.js'
],
sources: [
'bower_components/closure-library/closure/goog/base.js',
bundle.directories.source + '/*.js'
],
release: /** @type {string} */ (bundle.directories.release)
};
gulp.task('lint', 'Lint JS source files', [], function() {
var lintOptions = {
flags: [
'--flagfile=gjslint.conf'
]
};
return gulp.src(settings.sources)
.pipe(debug({ title: 'Lint' }))
.pipe(gjslint(lintOptions))
.pipe(gjslint.reporter('console'), { fail: true });
});
gulp.task('compile', false, [], function() {
var flags = {
compilation_level: 'ADVANCED_OPTIMIZATIONS',
language_in: 'ECMASCRIPT3',
angular_pass: true,
formatting: 'SINGLE_QUOTES',
externs: settings.externs,
generate_exports: true,
manage_closure_dependencies: true,
output_wrapper: (args.banner ? banner + '\n' : '') + '(function(){%output%})();',
define: [
'leodido.constants.DEBUG=' + (isProduction() ? 'false' : 'true')
],
warning_level: 'VERBOSE'
};
var dest = bundle.directories.dist;
if (!isProduction()) {
var sourcemap = settings.prefixes.dev + settings.output.sourcemap;
flags.create_source_map = path.join(dest, sourcemap);
flags.output_wrapper += '\n//# sourceMappingURL=' + sourcemap;
}
return gulp.src(settings.sources)
.pipe(ccompiler({
compilerPath: settings.compiler,
fileName: path.join(dest, (isProduction() ? '' : settings.prefixes.dev) + settings.output.minified),
compilerFlags: flags
}))
.pipe(gulp.dest('./'));
});
// Fix the source array paths of sourcemap file
gulp.task('fix-sourcemap', false, ['compile'], function() {
var file = path.join(bundle.directories.dist, settings.prefixes.dev + settings.output.sourcemap);
return isProduction() ?
true :
gutil.log('Writing', file + '.fix') &&
gulp.src(file)
.pipe(jsonedit(function(json) {
var root = path.relative(bundle.directories.dist, './');
json['sources'] = json['sources'].map(function(o) {
return path.join(root, o);
});
json['sources'].forEach(function(src) {
gutil.log('Updated sourcemap source path:', src);
});
return json;
}))
.pipe(rename(file + '.fix'))
.pipe(gulp.dest('./'));
});
gulp.task('del-sourcemap', false, ['fix-sourcemap'], function() {
var file = path.join(bundle.directories.dist, settings.prefixes.dev + settings.output.sourcemap);
return isProduction() ?
true :
gutil.log('Deleted', path.relative('./', del.sync([file])[0]));
});
gulp.task('upd-sourcemap', false, ['del-sourcemap'], function() {
var file = path.join(bundle.directories.dist, settings.prefixes.dev + settings.output.sourcemap);
return isProduction() ?
true :
gutil.log('Copying', file + '.fix', 'to', file) &&
gulp.src([file + '.fix'])
.pipe(rename(file))
.pipe(gulp.dest('./'));
});
gulp.task('dist', false, ['upd-sourcemap'], function() {
var file = path.join(bundle.directories.dist, settings.prefixes.dev + settings.output.sourcemap);
return isProduction() ?
true :
gutil.log('Deleted', path.relative('./', del.sync([file + '.fix'])[0]));
});
gulp.task('build', 'Build the library', [], function(cb) {
sequence(['clean', 'lint'], 'dist', cb);
}, {
options: extend(bannerHelp.options, environmentsHelp.options)
});
gulp.task('clean', 'Clean build directory', function(cb) {
var hello = [
path.join(bundle.directories.dist, (isProduction() ? '' : settings.prefixes.dev) + settings.output.minified)
];
if (!isProduction()) {
hello.push(path.join(bundle.directories.dist, settings.prefixes.dev + settings.output.sourcemap));
}
hello.forEach(function(filepath) {
gutil.log('Deleting', filepath);
});
del(hello, cb);
}, environmentsHelp);
gulp.task('version', 'Print the library version', [], function() {
return gutil.log('Library', gutil.colors.magenta(bundle.name) + ',', gutil.colors.magenta(bundle.version));
});
gulp.task('bump', 'Bump version up for a new release', function() {
return gulp.src(['./bower.json', 'package.json'])
.pipe(bump({ type: getLevel() }))
.pipe(gulp.dest('./'));
}, levelsHelp);
gulp.task('karma', 'Run karma tests', [], function(done) {
karma.start(
{
configFile: __dirname + '/karma.conf.js',
singleRun: true
},
done
);
});
var stream;
gulp.task('connect', false, [], function() {
stream = gulp.src(__dirname)
.pipe(webserver({
port: bundle.server.port,
directoryListing: true
}));
});
// Update/install webdriver
gulp.task('webdriver:update', false, [], webdriverUpdate);
// Run webdriver standalone server indefinitely. Usually not required.
gulp.task('webdriver:standalone', false, ['webdriver:update'], webdriverStandalone);
gulp.task('protractor', 'Run protractor E2E tests', ['connect', 'webdriver:update'], function() {
gulp.src(bundle.directories.e2e + '/**.scenario.js')
.pipe((protractor({
configFile: __dirname + '/protractor.conf.js'
})).on('error', function(e) {
throw e;
})).on('end', function() {
stream.emit('kill');
});
});
gulp.task('coveralls', false, [], function() {
return gulp.src('coverage/lcov.info')
.pipe(coveralls());
});
gulp.task('check:lcov', false, [], function(cb) {
var parse = require('lcov-parse');
parse('coverage/lcov.info', function(error, data) {
if (!error) {
data.forEach(function(obj) {
var file = obj['file'];
delete obj['file'];
gutil.log('Coverage info for', file);
gutil.log(JSON.stringify(obj, 2, null));
});
} else {
gutil.log(gutil.colors.red('Error'), error);
}
cb();
});
});
gulp.task('default', false, ['help']);