-
Notifications
You must be signed in to change notification settings - Fork 10
/
gulpfile.js
230 lines (196 loc) · 7.16 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
'use strict';
/*global require*/
var _ = require('lodash');
var del = require('del');
var path = require('path');
var runSequence = require('run-sequence');
var gulp = require('gulp');
var harmonize = require('harmonize');
var awspublish = require('gulp-awspublish');
var rename = require('gulp-rename');
var parallelize = require('concurrent-transform');
var gutil = require('gulp-util');
var deploy = require('gulp-gh-pages');
var cloudfront = require('gulp-invalidate-cloudfront');
var notifier = require('node-notifier');
var merge = require('merge-stream');
var ngrok = require('ngrok');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
// Get our Config.
var config = require('./config');
var webpackConfig = require('./webpack.config');
harmonize();
var notify = function(message){
notifier.notify({title: config.displayName+" Gulp",message:message});
};
// Setup a Ngrok server
var ngrokServe = function(subdomain){
var options = { port: config.serverPort };
var env = process.env;
if (env.NGROK_AUTHTOKEN) {
options.authtoken = env.NGROK_AUTHTOKEN;
if(env.NGROK_SUBDOMAIN || subdomain){
options.subdomain = (env.NGROK_SUBDOMAIN || subdomain).replace(/-/g,'');
}
}
ngrok.connect(options, function (error, url) {
if (error) {
throw new gutil.PluginError('ship:server', error);
}
url = url.replace('https', 'http');
notify({message:"Ngrok Started on "+url});
gutil.log('[ship:server]', url);
});
}
gulp.task('default', ['server']);
gulp.task('serve', ['server']);
gulp.task('clean', function(callback) { del(['./'+config.outputFolder+'/**/*']).then(function(){callback()}); });
gulp.task('server', function(callback) { runSequence('clean', 'copy-files:watch', 'webpack:server', callback); });
gulp.task('build', function(callback) { runSequence('clean', 'copy-files', 'webpack:build', callback); });
gulp.task('deploy', function(callback) { runSequence('build', 'publish:sha', callback); });
gulp.task('deploy:release', function(callback) { runSequence('build', 'publish:release', callback); });
var notify = function(message){
notifier.notify({title: config.displayName+" Gulp", message: message});
};
var handleError = function(err, taskName){
if(err){
notify(taskName+" Error: "+ err);
throw new gutil.PluginError("webpack:build", err);
}
};
// Copy static files from the source to the destination
var copyFiles = function(callback){
_.map(config.files, function(dest, src){
gulp.src(src).pipe(gulp.dest(dest));
});
notify("Vendors Updated");
if (_.isFunction(callback)){
callback();
}
};
gulp.task("copy-files", copyFiles);
gulp.task("copy-files:watch", function(){
copyFiles();
gulp.watch(_.keys(config.files), copyFiles);
});
//Production Build.
//Minified, clean code. No demo keys inside.
//demo.html WILL NOT WORK with this build.
//
//Webpack handles CSS/SCSS, JS, and HTML files.
var afterBuild = function(err, stats){
if (!!err) {throw new gutil.PluginError("webpack:build", err); }
var jsonStats = stats.toJson();
if (jsonStats.errors.length > 0) {
return new gutil.PluginError("webpack:build", JSON.stringify(jsonStats.errors));
}
if (jsonStats.warnings.length > 0) {
new gutil.PluginError("webpack:build", JSON.stringify(jsonStats.warnings));
}
gutil.log("[webpack:build]", stats.toString({colors: true}));
notify("App Built");
};
gulp.task("webpack:build", function(callback) {
// Then, use Webpack to bundle all JS and html files to the destination folder
notify("Building App");
webpack(_.values(webpackConfig.production), function(err, stats) {
afterBuild(err, stats);
webpack(_.values(webpackConfig.debug), function(err, stats){
afterBuild(err, stats);
callback();
});
});
});
// Dev Build
// Create the webpack compiler here for caching and performance.
var devCompiler = webpack(webpackConfig.development.browser);
// Build a Dev version of the project. Launched once on startup so we can have eveything copied.
gulp.task("webpack:build:dev", function(callback) {
// run webpack with Dev profile.
// Embeds the Hull config keys, and the necessary stuff to make demo.html work
devCompiler.run(function(err, stats) {
if (err){
throw new gutil.PluginError("webpack:build:dev", err);
}
var jsonStats = stats.toJson();
if(jsonStats.errors.length > 0){
return new gutil.PluginError("webpack:build:dev", JSON.stringify(jsonStats.errors));
}
if(jsonStats.warnings.length > 0){
new gutil.PluginError("webpack:build:dev", JSON.stringify(jsonStats.warnings));
}
gutil.log("[webpack:build:dev]", stats.toString({colors: true}));
notify({message: "Webpack Updated"});
callback();
});
});
// Launch webpack dev server.
gulp.task("webpack:server", function() {
var taskName = "webpack:server";
new WebpackDevServer(devCompiler, {
disableHostCheck: true,
contentBase: config.outputFolder,
publicPath: config.assetsFolder+"/",
hot: config.hotReload,
stats: {colors: true }
}).listen(config.serverPort, function(err) {
handleError(err, taskName);
// Dump the preview URL in the console, and open Chrome when launched for convenience.
notify({message: "Dev Server Started"});
var url = webpackConfig.development.browser.output.publicPath+"webpack-dev-server/";
ngrokServe(config.libName)
gutil.log("["+taskName+"]", url);
});
});
var publish = function(versions){
var aws = config.aws
var publisher = awspublish.create(aws.config);
var files = path.join(config.outputFolder, config.assetsFolder, "*")
var streams = [];
var cloudfrontInvalidations = []
for (var i = 0; i < versions.length; i++) {
var version = versions[i];
if(version){
console.log('Deploying to ',version);
var plain = gulp.src(files)
.pipe(rename(function(p){
p.dirname += '/'+version;
console.log('Publishing '+path.join(p.dirname,p.basename+p.extname))
}))
var gzip = gulp.src(files)
.pipe(rename(function(p){
p.dirname += '/'+version;
console.log('Publishing '+path.join(p.dirname,p.basename+p.extname))
}))
.pipe(awspublish.gzip(aws.gzip))
cloudfrontInvalidations.push('/'+version+'/*');
streams.push(plain);
streams.push(gzip);
}
};
var invalidationBatch = {
CallerReference: new Date().toString(),
Paths:{
Quantity:cloudfrontInvalidations.length,
Items:cloudfrontInvalidations
}
}
return merge.apply(merge,streams)
.pipe(parallelize(publisher.publish(aws.publish.headers,aws.publish.options)))
.pipe(publisher.cache())
.pipe(cloudfront(invalidationBatch, aws.cloudfront))
.pipe(awspublish.reporter())
}
// Deploys to S3
gulp.task('publish:sha',function(){
var SHA1 = process.env.CIRCLE_SHA1;
if( !SHA1 ){ return; }
return publish([SHA1]);
});
gulp.task('publish:release',function(){
var SHA1 = process.env.CIRCLE_SHA1;
var RELEASE = config.pkg.version;
if( !SHA1 || !RELEASE ){ return; }
return publish([SHA1,RELEASE]);
});