This repository has been archived by the owner on Apr 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
367 lines (307 loc) · 10.4 KB
/
gulpfile.babel.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
import gulp from 'gulp';
import git from 'gulp-git';
import bump from 'gulp-bump';
import webpack from 'webpack';
import path from 'path';
import rename from 'gulp-rename';
import template from 'gulp-template';
import yargs from 'yargs';
import gutil from 'gulp-util';
import serve from 'browser-sync';
import del from 'del';
import rp from 'request-promise';
import tag_version from 'gulp-tag-version';
import protractorLib from 'gulp-protractor';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import colorsSupported from 'supports-color';
import historyApiFallback from 'connect-history-api-fallback';
import awspublish from 'gulp-awspublish';
import cloudFront from 'gulp-cloudfront-invalidate-aws-publish';
let root = 'client';
const protractor = protractorLib.protractor;
let commonAwsConf = {
buildSrc: {
index: [ './dist/index.html' ],
allButIndex: [
'./dist/**/*',
'!./dist/index.html'
]
},
headers: {
index: {
'Cache-Control': 'no-cache, no-store, must-revalidate'
},
allButIndex: {
'Cache-Control': 'max-age=31536000, no-transform, public'
},
},
region: 'eu-west-2',
};
let awsConf = {
UAT: {
deployUrl: 'https://test-program.potentialife.com',
s3: {
buildSrc: commonAwsConf.buildSrc,
keys: {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
region: commonAwsConf.region,
params: {
Bucket: 'test.program.potentialife.com'
}
},
headers: commonAwsConf.headers
},
cloudFront: {
distribution: 'E3N0OB6AFVAPFJ', // Cloudfront distribution ID
accessKeyId: process.env.S3_ACCESS_KEY_ID, // Optional AWS Access Key ID
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY, // Optional AWS Secret Access Key
wait: true, // Whether to wait until invalidation is completed (default: false)
indexRootPath: true // Invalidate index.html root paths (`foo/index.html` and `foo/`) (default: false)
}
},
PROD: {
deployUrl: 'https://program.potentialife.com',
s3: {
buildSrc: commonAwsConf.buildSrc,
keys: {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
region: commonAwsConf.region,
params: {
Bucket: 'program.potentialife.com'
}
},
headers: commonAwsConf.headers
},
cloudFront: {
distribution: 'ELTZC5FGWC0Q', // Cloudfront distribution ID
accessKeyId: process.env.S3_ACCESS_KEY_ID, // Optional AWS Access Key ID
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY, // Optional AWS Secret Access Key
wait: true, // Whether to wait until invalidation is completed (default: false)
indexRootPath: true // Invalidate index.html root paths (`foo/index.html` and `foo/`) (default: false)
}
}
};
// helper method for resolving paths
let resolveToApp = (glob = '') => {
return path.join(root, 'app', glob); // app/{glob}
};
let resolveToComponents = (glob = '') => {
return path.join(root, 'app/components', glob); // app/components/{glob}
};
// map of all paths
let paths = {
js: resolveToComponents('**/*!(.spec.js).js'), // exclude spec files
scss: resolveToApp('**/*.scss'), // stylesheets
html: [
resolveToApp('**/*.html'),
path.join(root, 'index.html')
],
entry: [
'@babel/polyfill',
path.join(__dirname, root, 'app/appMockBackEnd.js')
],
output: root,
blankTemplates: path.join(__dirname, 'generator', 'component/**/*.**'),
dest: path.join(__dirname, 'dist')
};
gulp.task('clean', (cb) => {
del([paths.dest]).then(function (paths) {
gutil.log("[clean]", paths);
cb();
})
});
// use webpack.config.js to build modules
gulp.task('webpack', gulp.series('clean', (cb) => {
// Phase is used by Bugsnag to know if the error happened on UAT (default) or PROD
let phase = yargs.argv.phase || 'UAT';
const config = require('./webpack.dist.config')(phase);
config.entry.app = paths.entry;
webpack(config, (err, stats) => {
if(err) {
throw new gutil.PluginError("webpack", err);
}
gutil.log("[webpack]", stats.toString({
colors: colorsSupported,
chunks: false,
errorDetails: true
}));
cb();
});
}));
gulp.task('e2e_test', () => {
let baseUrl = yargs.argv.baseUrl || 'http://127.0.0.1:3000/';
gutil.log(`Launch e2e Test on: ${baseUrl}`);
let launchE2ETesting = () => {
return gulp.src([
'./e2eTesting/Cycle1_module1.spec.js',
'./e2eTesting/Cycle1_module2.spec.js'
])
.pipe(protractor({
configFile: "./protractor.conf.js",
args: ['--baseUrl', baseUrl]
}))
.on('error', (e) => {
throw e
});
};
if ( baseUrl !== 'http://127.0.0.1:3000/' ) {
let options = {
uri: 'https://apipl.ciprianspiridon.com/tonio-user',
headers: {
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
return rp(options)
.then( () => {
gutil.log('User successfully reseted to Cycle 1 - Module 1 - Step 2');
return launchE2ETesting();
})
.catch( (err) => {
gutil.log('Error resetting User to Cycle 1 - Module 1 - Step 2 / Abort E2E testing');
// API call failed...
return err;
});
}
else {
return launchE2ETesting();
}
});
gulp.task('serve', (done) => {
const config = require('./webpack.dev.config')();
config.entry.app = [
// this modules required to make HRM working
// it responsible for all this webpack magic
'webpack-hot-middleware/client?reload=true',
// application entry point
].concat(paths.entry);
const compiler = webpack(config);
serve({
port: process.env.PORT || 3000,
open: true,
server: {baseDir: root},
middleware: [
historyApiFallback(),
webpackDevMiddleware(compiler, {
stats: {
colors: colorsSupported,
chunks: false,
modules: false
},
publicPath: config.output.publicPath
}),
webpackHotMiddleware(compiler, {
log: () => {},
heartbeat: 2000
})
]
});
done();
});
gulp.task('watch', gulp.series('serve'));
gulp.task('component', () => {
const cap = (val) => {
return val.charAt(0).toUpperCase() + val.slice(1);
};
const name = yargs.argv.name;
const parentPath = yargs.argv.parent || '';
const destPath = path.join(resolveToComponents(), parentPath, name);
let reDetectUpperCase = new RegExp('[A-Z]', 'g');
let convertUppercase = (matchSubString, offset, wholeString) => {
let returnString = '';
if ( offset > 0 ) {
// add '-' only if it is not the first letter in the word
returnString += '-';
}
returnString += matchSubString.toLowerCase()
return returnString;
};
return gulp.src(paths.blankTemplates)
.pipe(template({
name: name,
dashCaseName: name.replace(reDetectUpperCase, convertUppercase),
upCaseName: cap(name)
}))
.pipe(rename((path) => {
path.basename = path.basename.replace('temp', name);
}))
.pipe(gulp.dest(destPath));
});
gulp.task('tagRepo', () => {
gutil.log('create a git Tag');
return gulp.src(['./package.json'])
.pipe(tag_version());
});
// Push tag to current branch
gulp.task('pushTag', gulp.series('tagRepo', (done) => {
gutil.log('push a git Tag');
git.push('origin', '', {args: ' --tags'}, function(err) { if (err) throw err;});
done();
}));
gulp.task('bumpVersionNumber', () => {
return gulp.src('./package.json')
.pipe(bump({type:'patch'}))
.pipe(gulp.dest('./'))
.pipe(git.add())
.pipe(git.commit('[skip ci] - Bump and tag package.json version'));
});
gulp.task('pushVersionNumber', gulp.series('bumpVersionNumber', (done) => {
gutil.log('gitPush will Increment package.json version, commit and push');
git.push('origin', '', {args: ''}, function(err) { if (err) throw err;});
done();
}));
gulp.task('default', gulp.series('watch'));
gulp.task('deployAllButIndex', gulp.series( () => {
let phase = yargs.argv.phase || 'UAT';
let conf = awsConf[ phase ];
let publisher = awspublish.create( conf.s3.keys );
gutil.log( `Deploy dist(all but index.html) folder into ${phase} S3-Bucket: ${conf.s3.keys.params.Bucket}` );
gutil.log( 'Headers to be added to the files: ', conf.s3.headers.allButIndex );
return gulp.src( conf.s3.buildSrc.allButIndex )
.pipe( awspublish.gzip( { ext: '' } ) )
.pipe( publisher.publish( conf.s3.headers.allButIndex ) )
.pipe( publisher.sync() )
.pipe( publisher.cache() )
.pipe( awspublish.reporter() );
}));
gulp.task('deployIndex', gulp.series( 'deployAllButIndex', () => {
let phase = yargs.argv.phase || 'UAT';
let conf = awsConf[ phase ];
let publisher = awspublish.create( conf.s3.keys );
gutil.log( `Deploy dist/index.html file into ${phase} S3-Bucket: ${conf.s3.keys.params.Bucket}` );
gutil.log( 'Headers to be added to index.html: ', conf.s3.headers.index );
gutil.log( 'Invalidate Files on CloudFront With Distribution ID=', conf.cloudFront.distribution );
return gulp.src( conf.s3.buildSrc.index )
.pipe( awspublish.gzip( { ext: '' } ) )
.pipe( publisher.publish( conf.s3.headers.index ) )
.pipe( cloudFront( conf.cloudFront ) )
.pipe( publisher.cache() )
.pipe( awspublish.reporter() );
}));
gulp.task('deploy', gulp.series( 'deployIndex', (done) => {
let phase = yargs.argv.phase || 'UAT';
let versionNumber = require( './package.json' ).version;
let slack = require('gulp-slack')({
url: 'https://hooks.slack.com/services/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',
channel: '#deployments',
user: 'Frankie Program',
icon_emoji: ':shipit:'
});
slack( `PROGRAM: Deployment on ${phase} - v${versionNumber}: ${awsConf[ phase ].deployUrl}` );
done();
}));
gulp.task('notifyTicketsChanel', (done) => {
let phase = yargs.argv.phase || 'UAT';
gutil.log('phase=', phase);
let slack = require('gulp-slack')({
url: 'https://hooks.slack.com/services/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',
channel: '#tickets', // Optional
user: 'Automating Deployment', // Optional
icon_emoji: ':potentialife:' // Optional
});
slack(`A new version of the Front End has been pushed on ${phase}, be aware for incoming tickets`);
done();
});