-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGruntfile.js
432 lines (388 loc) · 14.9 KB
/
Gruntfile.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
module.exports = function(grunt) {
var fs = require('fs');
var _ = require('lodash');
var awsConfig = {
accessKey: false,
secretKey: false,
sessionToken: '',
region: false,
s3Bucket: false
};
// Breaks out the keys and also gives us a nice reference of what
// needs to be in here without being able to be changed.
var awsConfigKeys = _.keys(awsConfig);
// Take inputs for the awsConfig, prefixed with 'aws.'.
// For example accessKey can be inputted with '--aws.accessKey=accessKey'
_.each(awsConfigKeys, function(awsConfigKey){
var optionKey = 'aws.' + awsConfigKey;
var optionValue = grunt.option(optionKey);
if (optionValue) {
// Use the original key to update it.
awsConfig[awsConfigKey] = optionValue;
}
});
grunt.file.move = function(from,to) {
grunt.log.writeln('Moved '+from.cyan+' to '+to.cyan);
grunt.file.copy(from,to);
grunt.file.delete(from);
};
grunt.loadNpmTasks('grunt-continue');
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-version');
grunt.loadNpmTasks('grunt-aws');
grunt.loadNpmTasks('grunt-fastly');
grunt.loadNpmTasks('grunt-cloudflare-purge');
grunt.loadNpmTasks('grunt-git');
grunt.initConfig({
aws: awsConfig,
version: {
eloqua: {
options: {
prefix: '@version *',
pkg: grunt.file.readJSON('src/eloqua/version.json')
},
src: ['dist/plugins/eloqua/*.js']
},
soundjs: {
options: {
prefix: '@version *',
pkg: grunt.file.readJSON('src/soundjs/version.json')
},
src: ['dist/plugins/soundjs/*.js']
},
highlander: {
options: {
prefix: '@version *',
pkg: grunt.file.readJSON('src/highlander/version.json')
},
src: ['dist/plugins/highlander/*.js']
},
"marketo-munchkin": {
options: {
prefix: '@version *',
pkg: grunt.file.readJSON('src/marketo-munchkin/version.json')
},
src: ['dist/plugins/marketo-munchkin/*.js']
}
},
watch: {
js : {
files: ['./src/**/*.js'],
tasks: ['compileJS'],
options: {
spawn: false,
debounceDelay: 250,
interrupt: true
}
}
},
requirejs: {
compile_eloqua_plugin: {
options: {
baseUrl: './src/eloqua',
include: ['eloqua'],
optimize: 'none',
out: './dist/plugins/eloqua/main.js',
skipSemiColonInsertion: true,
paths: {
elq: "//img.en25.com/i/elqCfg.min",
CerosSDK: "//sdk.ceros.com/standalone-player-sdk-v3"
}
}
},
compile_marketo_munchkin_plugin: {
options: {
baseUrl: './src/marketo-munchkin',
include: ['marketo-munchkin'],
optimize: 'none',
out: './dist/plugins/marketo-munchkin/main.js',
skipSemiColonInsertion: true,
paths: {
CerosSDK: "//sdk.ceros.com/standalone-player-sdk-v3"
}
}
},
compile_soundjs_plugin: {
options: {
baseUrl: './src/soundjs',
include: ['soundjs'],
optimize: 'none',
out: './dist/plugins/soundjs/main.js',
skipSemiColonInsertion: true,
shim: {
CreateJS: {
exports: 'createjs'
}
},
paths: {
CreateJS: "https://code.createjs.com/soundjs-0.6.2.min",
CerosSDK: "//sdk.ceros.com/standalone-player-sdk-v3"
}
}
},
compile_highlander_plugin: {
options: {
baseUrl: './src/highlander',
include: ['highlander'],
optimize: 'none',
out: './dist/plugins/highlander/main.js',
skipSemiColonInsertion: true,
paths: {
CerosSDK: "//sdk.ceros.com/standalone-player-sdk-v3"
}
}
}
},
md5: {
release: {
src:['dist/plugins/**/*.*']
}
},
s3: {
releaseNonGzip: {
options: {
accessKeyId: "<%= aws.accessKey %>",
secretAccessKey: "<%= aws.secretKey %>",
sessionToken: "<%= aws.sessionToken %>",
assumeRole: true,
region: "<%= aws.region %>",
bucket: "<%= aws.s3Bucket %>",
access: 'public-read',
// This flag auto gzips all files it uploads,
// since we handle it ourselves we disable it.
gzip: false,
cache: false, // Always upload, even if it's the same etag
headers: {
CacheControl: 86400 * 30 // 1 month
}
},
cwd: "./dist/plugins/",
src: ["**/*.*"]
}
},
gittag: {
tagEloqua: {
options: {
cwd: './',
verbose: true,
tag: 'eloqua-' + String(grunt.file.readJSON('src/eloqua/version.json').version)
}
},
tagHighlander: {
options: {
cwd: './',
verbose: true,
tag: 'highlander-' + String(grunt.file.readJSON('src/highlander/version.json').version)
}
},
tagSoundJs: {
options: {
cwd: './',
verbose: true,
tag: 'soundjs-' + String(grunt.file.readJSON('src/soundjs/version.json').version)
}
},
tagMarketoMunchkin: {
options: {
cwd: './',
verbose: true,
tag: 'marketo-munchkin-' + String(grunt.file.readJSON('src/marketo-munchkin/version.json').version)
}
}
},
gitpush: {
pushReleaseTag: {
options: {
cwd: './',
verbose: true,
tags: true
}
}
},
fastly: {
options: {
key: grunt.option('fastly.key')
},
plugins: {
options: {
urls: [],
host: grunt.option('fastly.host')
}
}
},
cloudflare_purge: {
options: {
email: grunt.option('cloudflare.email'),
apiKey: grunt.option('cloudflare.apiKey')
},
playersdk: {
options: {
zone: grunt.option('cloudflare.zone')
}
}
}
});
/**
* This is kind of a hack, because we want to dynamically invalidate fastly based on the files we
* have created in a directory, we have to set them AFTER they are created. This updates the config
* in order to invalidate the correct files.
*/
grunt.registerTask('updateFastlyOptions', 'Update the Fastly Inputs', function() {
var fastly = grunt.config.get('fastly');
/**
* This reads all the files we are releasing (both major and specific versions)
* We technically only need to invalidate the major version since there was a file there before
* but since this is easy to do and may help if someone ever mistags, we invalidate all.
*/
fastly.plugins.options.urls = fs.readdirSync('./dist/').map(function(value, key, array) {
return '/' + value;
});
grunt.log.writeln('Purging cache for host ' + fastly.plugins.options.host);
grunt.log.writeln('Purging cache for urls ' + fastly.plugins.options.urls);
// Set the config back.
grunt.config.set('fastly', fastly);
});
grunt.registerTask('clean','Cleans build artifacts',function(){
grunt.file.delete('dist/');
grunt.log.writeln('Removed '+'dist/'.green);
});
grunt.registerTask('rename','Renames build artifacts to include version number',function(){
// If pushing to develop or a feature branch, codeship sends in the branch name so that builds can
// be pushed to S3 with the branch name included to assist multi-branch development
var branchFileModifier = '';
if(grunt.option('branch')) {
branchFileModifier = '-' + grunt.option('branch').replace('/', '-');
}
var plugins = ['eloqua', 'marketo-munchkin', 'soundjs', 'highlander'];
_.each(plugins, function(type){
var version = String(grunt.file.readJSON('src/' + type +'/version.json').version);
var versionArray = version.split('.');
if (versionArray.length < 3) {
grunt.fail.warn('Version of ' + type + ' should be specified in Major.Minor.Patch (1.0.0)');
}
var majorVersion = versionArray[0];
/**
* Output files for specific Version (when version = 1.3.0)
* These should never overwrite files in our s3 bucket, always should be unique.
* Code changes require update to version.
*
* plugins/eloqua/main-1.0.0.js
* plugins/eloqua/main-1.0.0.gz.js
*/
grunt.file.move('dist/plugins/' + type + '/main.js', 'dist/plugins/' + type + '/main' + branchFileModifier + '-' + version + '.js');
/**
* Output files for Major Version (when version = 1.3.0 -- majorVersion = 1)
* These replace the existing major release version. The concept is that customers
* can use v1 and we can update it with non-breaking changes without them having to update their
* source.
*
* plugins/eloqua/main-v1.js
* plugins/eloqua/main-v1.gz.js
*/
grunt.file.copy('dist/plugins/' + type + '/main' + branchFileModifier + '-' + version + '.js', 'dist/plugins/' + type + '/main' + branchFileModifier + '-v' + majorVersion + '.js');
});
});
grunt.registerMultiTask('md5', 'Generates MD5 for build artifacts', function() {
_.each(this.files, function(file) {
if (typeof file.src === 'undefined') {
grunt.fail.warn('Files object doesn\'t exist');
}
var srcFiles = grunt.file.expand(file.src);
_.each(srcFiles, function(srcFile) {
try {
var srcCode = grunt.file.read(srcFile);
var md5 = require('crypto').createHash('md5').update(srcCode).digest('hex');
grunt.log.writeln(srcFile+' '+md5.green);
} catch(err) {
grunt.log.error(err);
grunt.fail.warn('Fail to generate an MD5 file name');
}
});
});
});
/**
* Checks to make sure the AWS Credentials have been passed in.
* This allows us to check right away to make sure we can complete this task.
*/
grunt.registerTask('awsInputCheck', 'Checks to make sure we have the correct AWS Inputs', function() {
var aws = grunt.config.get('aws');
var hasAllKeys = true;
// Loop through the array of values here and make sure they are all set.
_.each(awsConfigKeys, function(key) {
// Optional key
if (key === 'sessionToken') {
return;
}
if (!awsConfig[key]) {
grunt.log.error('Missing input [' + key + '] Please use --aws.' + key + '=<value>');
hasAllKeys = false;
}
});
if (!hasAllKeys) {
grunt.fail.warn('Missing required AWS Inputs');
}
});
grunt.registerTask('compileJS',
"Resolves all requirejs dependencies and compiles javascript into file for the embedded and standalone SDKs",
[
'requirejs',
'version'
]
);
grunt.registerTask('dev', ['watch:js']);
grunt.registerTask('default',['test']);
grunt.registerTask('test',[
'clean',
'compileJS'
]);
// Should only be run via CodeShip.
// Does everything the regular 'release' task does EXCEPT tagging the repo
// Codeship should be setup to push this to a dev bucket
grunt.registerTask('dev-release',[
// Make sure we have the s3 credentials needed to release this.
'awsInputCheck',
// Remove working files and created files.
'clean',
// Version and create the files in the /dist folder
'compileJS',
'rename',
// Show the md5 of the files
'md5',
// Push to S3
's3:releaseNonGzip',
// Invalidate the Files on Fastly
'updateFastlyOptions',
'fastly',
'cloudflare_purge'
]);
// Should only be run via CodeShip.
grunt.registerTask('release',[
// Make sure we have the s3 credentials needed to release this.
'awsInputCheck',
// Remove working files and created files.
'clean',
// Version and create the files in the /dist folder
'compileJS',
'rename',
// Show the md5 of the files
'md5',
// Turn on continue-on-failure for the creation of git tags.
// Because we are independently versioning the plugins, it is
// expected that some tag creation might fail.
'continue:on',
// Create the tag from the version. Will fail if already exists.
'gittag',
// Turn off continue-on-failure
'continue:off',
// Push the tags to github. Git will do nothing if the tag already exists.
'gitpush:pushReleaseTag',
// Push to S3
's3:releaseNonGzip',
// Invalidate the Files on Fastly
'updateFastlyOptions',
'fastly',
'cloudflare_purge'
]);
};