forked from mint-metrics/mojito-js-delivery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
235 lines (210 loc) · 7.54 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
231
232
233
234
235
const gulp = require('gulp'),
uglify = require('gulp-uglify'),
del = require('del'),
concat = require('gulp-concat'),
awspublish = require('gulp-awspublish'),
yaml = require('gulp-yaml'),
addsrc = require('gulp-add-src'),
modularBuild = require('./scripts/modular-build'),
fs = require('fs'),
through = require('through2'),
zlib = require('zlib'),
babel = require('gulp-babel'),
newCli = require('./scripts/cli-new'),
setCli = require('./scripts/cli-set'),
ssaCli = require('./scripts/cli-ssa'),
runTests = require('./scripts/run-tests')
parallelize = require('concurrent-transform');
// Check whether config exists & create it if not.
for (const file of ['config.js', 'lib/shared-code.js'])
{
!fs.existsSync(file) &&
fs.existsSync(file.replace('.js', '.example.js')) &&
fs.copyFileSync(file.replace('.js', '.example.js'), file);
}
const config = require('./config');
// parsing extra arguments from process.argv
const getCLIArgs = (defaultNull) =>
{
let argList = process.argv;
let args = {}, i, opt, thisOpt, curOpt;
for (i = 0; i < argList.length; i++)
{
thisOpt = argList[i].trim();
opt = thisOpt.replace(/^\-+/, '');
if (opt === thisOpt)
{
// argument value
if (curOpt)
{
args[curOpt] = opt;
}
curOpt = null;
}
else
{
// argument name
curOpt = opt;
args[curOpt] = defaultNull?null:true;
}
}
return args;
};
function test()
{
return runTests();
}
function build()
{
del(['dist/assets/js']);
let containerName = config.containerName;
let modularResult = {liveList: [], stagingList: [], divertList: [], inactive: 0};
// is there a nuke.lock file?
const fileName = 'nuke.lock';
if (fs.existsSync(fileName))
{
// generate the container without test objects
return (
gulp.src('license.txt')
.pipe(addsrc.append(['lib/mojito.js', 'lib/shared-code.js']))
.pipe(concat(containerName + '.pretty.js'))
.pipe(babel({presets: [['@babel/preset-env', { "modules": false, exclude:['@babel/plugin-transform-typeof-symbol'] }]]}))
.pipe(gulp.dest('dist/assets/js'))
.pipe(uglify())
.pipe(addsrc.prepend(['license.txt']))
.pipe(concat(containerName + '.js'))
.pipe(gulp.dest('dist/assets/js'))
.pipe(through.obj(function(file, enc, callback)
{
let gzippedSize = ((zlib.gzipSync(file.contents, {level: 9}).length)/1024).toFixed(2) + ' KB',
colorCyan = '\x1b[36m',
colorReset = '\x1b[0m';
setTimeout(function(){
console.log(
`%s${colorCyan}%s${colorReset}%s`,
'Mojito container built (', gzippedSize, ')');
});
callback(null, file);
}))
);
}
return (
gulp.src('lib/waves/**/config.yml')
.pipe(yaml())
.pipe(modularBuild(modularResult))
.pipe(concat('test-objects.js'))
.pipe(addsrc.append(['lib/waves/**/test-object.js']))
.pipe(addsrc.prepend(['license.txt', 'lib/mojito.js', 'lib/shared-code.js']))
.pipe(concat(containerName + '.pretty.js'))
.pipe(babel({presets: [['@babel/preset-env', { "modules": false, exclude:['@babel/plugin-transform-typeof-symbol'] }]]}))
.pipe(gulp.dest('dist/assets/js'))
.pipe(uglify())
.pipe(addsrc.prepend(['license.txt']))
.pipe(concat(containerName + '.js'))
.pipe(gulp.dest('dist/assets/js'))
.pipe(through.obj(function(file, enc, callback)
{
let gzippedSize = ((zlib.gzipSync(file.contents, {level: 9}).length)/1024).toFixed(2) + ' KB',
activeTestCount = modularResult.liveList.length + modularResult.divertList.length + modularResult.stagingList.length,
colorCyan = '\x1b[36m',
colorReset = '\x1b[0m';
// use setTimeout to let built result messages display after gulp 'Finished...' message
setTimeout(function(){
if (activeTestCount)
{
console.log(
`%s${colorCyan}%s${colorReset}%s${colorCyan}%s${colorReset}%s`,
'Mojito container built with ', activeTestCount, ' tests (', gzippedSize, '):');
if (modularResult.liveList.length)
{
console.log(
`%s${colorCyan}%s${colorReset}%s`,
' Live (', modularResult.liveList.length, ') - ' + modularResult.liveList.join(' '));
}
if (modularResult.stagingList.length)
{
console.log(
`%s${colorCyan}%s${colorReset}%s`,
' Staging (', modularResult.stagingList.length, ') - ' + modularResult.stagingList.join(' '));
}
if (modularResult.divertList.length)
{
console.log(
`%s${colorCyan}%s${colorReset}%s`,
' Diverted (', modularResult.divertList.length, ') - ' + modularResult.divertList.join(' '));
}
}
else
{
console.log(
`%s${colorCyan}%s${colorReset}%s`,
'Mojito container built (', gzippedSize, ')');
}
if (modularResult.inactive)
{
console.log(
`%s${colorCyan}%s${colorReset}%s`,
' Inactive (', modularResult.inactive, ')');
}
});
callback(null, file);
}))
);
}
function publish()
{
let args = getCLIArgs();
// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property
let publisherOptions = {
region: config.s3Region,
params: {}
};
// production or development? default is development
if (args.production)
{
publisherOptions.params.Bucket = config.s3BucketPRD;
}
else
{
publisherOptions.params.Bucket = config.s3BucketDev;
}
// if accessKeyId and secretAccessKey was passed as command line parameter
if (args.awsk && args.awss)
{
publisherOptions.accessKeyId = args.awsk;
publisherOptions.secretAccessKey = args.awss;
}
let publisher = awspublish.create(publisherOptions);
// define custom headers
let headers = {
"Cache-Control": "max-age=300, public,must-revalidate,s-maxage=300",
"Content-Encoding": "gzip"
};
return (
gulp.src("dist/assets/js/*.js")
.pipe(parallelize(awspublish.gzip(), 5))
.pipe(parallelize(publisher.publish(headers, { force: true }), 5))
.pipe(awspublish.reporter())
);
}
function cliNew(cb)
{
newCli(getCLIArgs(true), cb);
}
function cliSet(cb)
{
setCli(getCLIArgs(true), cb);
}
function cliSSA(cb)
{
ssaCli(getCLIArgs(true), cb);
}
exports.test = test;
exports.build = build;
exports.scripts = build;
exports.publish = publish;
exports.default = build;
exports.new = cliNew;
exports.set = cliSet;
exports.ssa = cliSSA;
exports.refresh = gulp.series([build, publish]);