This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
179 lines (156 loc) · 5.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
// main dependencies
var gulp = require('gulp'),
gulpif = require('gulp-if'),
plumber = require('gulp-plumber'),
concat = require('gulp-concat'),
gutil = require('gulp-util'),
bust = require('gulp-buster'),
runSequence = require('run-sequence');
// styling dependencies
var sass = require('gulp-sass'),
cssmin = require('gulp-cssmin'),
rename = require('gulp-rename');
// code dependencies
var browserify = require('browserify'),
babelify = require('babelify'),
watchify = require('watchify'),
source = require('vinyl-source-stream');
// docker
var Docker = require('dockerode'),
spawn = require('child_process').spawn;
// check if set to build for production
var production = process.env.NODE_ENV === 'production';
// Webbhook operation
var request = require('request'),
moment = require('moment');
// compile sass files
gulp.task('styles', function() {
return gulp.src('frontend/app/stylesheets/*.sass')
.pipe(plumber())
.pipe(sass())
// .pipe(gulpif(production, cssmin()))
// .pipe(gulpif(production, rename({'suffix': '.min'})))
.pipe(gulp.dest('public/css'));
});
// watch and recompile styles on the fly
gulp.task('styles-watch', function() {
gulp.watch('frontend/app/stylesheets/**/*.sass', ['styles']);
});
/**
* 3rd party libraries used on front-end
* We compile them separately to skip babelify process
*/
var dependencies = [
'alt',
'react',
'react-dom',
'react-router',
'underscore',
'jsonwebtoken',
'react-select'
];
/**
* Gather all the dependencies into one vendor file
* - get all the dependencies
* - pack them into one file
* - convert browserify text stream to vinyl for gulp to pipe them further
* - OPTIONAL: add uglify for prod
* - move to public
*/
gulp.task('browserify-vendor', function() {
return browserify()
.require(dependencies)
.bundle()
.pipe(source('vendor.bundle.js'))
.pipe(gulp.dest('public/js'));
});
/**
* Babelify and concat the source code of our React app
* - mark dependencies to be an extarnal file (skip them)
* - transform the rest with babelify
* - pack them into one file
* - convert browserify text stream to vinyl for gulp to pipe them further
* - OPTIONAL: add uglify for prod
* - move to public
*/
gulp.task('browserify', ['browserify-vendor'], function() {
return browserify('frontend/app/main.js')
.external(dependencies)
.transform(babelify, {'presets': ['es2015', 'react']})
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest('public/js'));
});
/**
* Babelify and concat the source code of our React app while watching for changes
*/
gulp.task('browserify-watch', ['browserify-vendor'], function() {
// build a bundler to run our task continiously with watchify
var bundler = watchify(browserify('frontend/app/main.js', watchify.args));
bundler.external(dependencies);
bundler.transform(babelify, {'presets': ['es2015', 'react']});
bundler.on('update', rebundle);
return rebundle();
function rebundle() {
var start = Date.now();
return bundler.bundle()
.on('error', function(err) {
gutil.log(gutil.colors.red(err.toString()));
})
.on('end', function() {
gutil.log(gutil.colors.green('Finished rebuilding in', (Date.now() - start + 'ms.')));
})
.pipe(source('bundle.js'))
.pipe(gulp.dest('public/js'));
}
});
gulp.task('cache-bust', function() {
return gulp.src(['public/js/*.js', 'public/css/*.css'])
.pipe(bust())
.pipe(gulp.dest('.'));
});
// configure default tasks
gulp.task('default', ['styles', 'browserify-watch', 'styles-watch']);
gulp.task('build', function(cb) {
runSequence(['styles', 'browserify'],
'cache-bust',
cb);
});
// simple service function
function execCommand(command, args, cb) {
var ctx = spawn(command, args);
ctx.stdout.on('data', function(data) {
process.stdout.write(data);
});
ctx.stderr.on('data', function(data) {
process.stderr.write(data);
});
ctx.on('close', function(code) {
if(cb) {cb(code === 0 ? null : code);}
});
}
// Docker build
var docker = new Docker(),
imageName = 'orels1/red-portal',
containerName = 'red-portal';
gulp.task('docker-build', function(cb) {
execCommand('docker', ['build', '-t', imageName, '.'], cb);
});
gulp.task('docker-push', function(cb) {
execCommand('docker', ['push', imageName], cb);
});
gulp.task('kubernetes-update', function(cb) {
execCommand('kubectl', ['rolling-update', containerName, '--image=' + imageName, '--image-pull-policy=Always'], cb);
});
gulp.task('kubernetes-proxy', function(cb) {
execCommand('kubectl', ['proxy'], cb);
});
gulp.task('deploy', ['build'], function(cb) {
execCommand('docker', ['build', '-t', imageName, '.'], function() {
execCommand('docker', ['push', imageName], function() {
execCommand('kubectl', ['rolling-update', containerName, '--image=' + imageName, '--image-pull-policy=Always'], function(){
cb();
});
});
});
});