-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
304 lines (270 loc) · 8.05 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
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
"use strict";
const gulp = require("gulp");
const ts = require("gulp-typescript");
const tsProject = ts.createProject("tsconfig.json");
const sourcemaps = require('gulp-sourcemaps');
const clean = require('gulp-clean');
const env = require("gulp-env");
const tslint = require("gulp-tslint");
const jsonfile = require('jsonfile');
const run = require('gulp-run');
// const jasmine = require('gulp-jasmine');
const istanbul = require('gulp-istanbul');
const mocha = require('gulp-mocha');
const nodemon = require('gulp-nodemon');
const runSequence = require('run-sequence');
const replace = require('gulp-replace');
const shell = require('gulp-shell')
const remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
const path = require('path');
if (typeof process.env.DOCURL == "undefined") {
process.env.DOCURL = "https://matchmyroute-backend.appspot.com";
}
function getOption (name) {
let i = process.argv.indexOf('--' + name)
let result
if (i > -1) {
result = process.argv[i + 1]
}
return result
}
gulp.task("info", () => {
return gulp.src('./conf/info.json')
.pipe(populateWithEnvVariables())
.pipe(gulp.dest('./build/static'));
});
gulp.task("copy-swagger", () => {
return gulp.src('./node_modules/swagger-ui/dist/**/*')
.pipe(gulp.dest('./build/static'))
});
gulp.task("swagger-ui", ["copy-swagger"], () => {
return gulp.src('./node_modules/swagger-ui/dist/index.html')
.pipe(replace('http://petstore.swagger.io/v2/swagger.json', process.env.DOCURL + '/swagger.json'))
.pipe(gulp.dest('./build/static'))
});
function populateWithEnvVariables() {
let variables = ["CIRCLE_BUILD_NUM"];
function transform(file, cb) {
// read and modify file contents
let conf = JSON.parse(file.contents);
for (let varIndex in variables) {
let varName = variables[varIndex];
conf[varName] = process.env[varName];
}
file.contents = new Buffer(JSON.stringify(conf, null, ' '));
// if there was some error, just pass as the first parameter here
cb(null, file);
}
return require('event-stream').map(transform);
}
gulp.task("default", ["build"]);
function getBaseEnvVars() {
return {
PROCESS_TYPE: "web",
WITH_SERVICES: true,
NODE_PATH: ".",
PGDATABASE: "matchMyRoute",
STORAGE_BASE_URL: "https://storage.googleapis.com"
};
};
function getDevelopmentEnvVars() {
return Object.assign(getBaseEnvVars(),
{
DB_CONNECTION_PATH: "localhost",
PGPORT: 5432,
DOCURL: "http://localhost:8080",
STATIC_DIR: "build/static",
NODE_ENV: "development",
GOOGLE_APPLICATION_CREDENTIALS: "conf/storage-keyfile.json",
STORAGE_BUCKET: "matchmyroute-dev",
});
};
function getProductionEnvVars() {
return Object.assign(getBaseEnvVars(),
{
DB_CONNECTION_PATH: "/cloudsql/matchmyroute-backend:us-east1:matchmyroute-database",
PGPORT: 5432,
DOCURL: "https://matchmyroute-backend.appspot.com",
STATIC_DIR: "static",
NODE_ENV: "production",
PGUSER: "postgres",
PGPASSWORD: "aUZw[:Gw38H&>Jf2hUwd",
GOOGLE_APPLICATION_CREDENTIALS: "conf/storage-keyfile.json",
STORAGE_BUCKET: "matchmyroute-prod"
});
};
function getStagingEnvVars() {
return Object.assign(getBaseEnvVars(),
{
DB_CONNECTION_PATH: "127.0.0.1",
PGPORT: 5432,
NODE_ENV: "staging",
PGUSER: "testuser",
PGPASSWORD: "test",
GOOGLE_APPLICATION_CREDENTIALS: "conf/storage-keyfile.json",
STORAGE_BUCKET: "matchmyroute-prod"
});
};
// Format an object of {key:value} as key: value with the given indentation
function formatYaml(obj, indentation) {
const indent = Array((indentation||0)+1).join(' ');
return Object.keys(obj).map(function(key) {
return indent + key + ': ' + obj[key];
}).join('\n')
}
gulp.task("set-env-vars", function() {
switch(process.env.NODE_ENV) {
case "development":
env.set(getDevelopmentEnvVars());
break;
case "production":
env.set(getProductionEnvVars());
break;
case "staging":
env.set(getStagingEnvVars());
break;
default:
env.set(getDevelopmentEnvVars());
break;
}
});
gulp.task("tslint", () => {
return tsProject.src()
.pipe(tslint({
formatter: "verbose",
configuration: "tslint.json",
}))
.pipe(tslint.report({
emitError: process.argv[2] === 'serve' ? false: true,
}))
});
gulp.task("typescript", () => {
return tsProject.src()
.pipe(sourcemaps.init())
.pipe(tsProject()).js
.pipe(sourcemaps.write())
.pipe(gulp.dest(tsProject.options.outDir));
});
gulp.task("clean", () => {
return gulp.src(["build","coverage"])
.pipe(clean());
});
gulp.task("pre-test", ["typescript", "set-env-vars"], () => {
return gulp.src(["build/**/*.js", "!build/_*", "!build/**/**[sS]pec.js", "!build/static/**/*.js",])
.pipe(istanbul({includeUntested: true}))
.pipe(istanbul.hookRequire());
});
gulp.task("unittest", ["pre-test"], () => {
return gulp.src(["build/**/*[sS]pec.js"])
.pipe(mocha({
verbose: true,
includeStackTrace: true,
timeout: 5000,
// debugBrk: true,
// debug: true,
// inspect: true
}))
.on('error', (err) => {
console.log("error: ", err);
if (process.argv[2] !== 'serve') {
process.exit(1);
}
})
.pipe(istanbul.writeReports({
dir: "./coverage/unit",
reporters: ["json"]
}));
});
gulp.task("e2etest", ["pre-test"], () => {
return gulp.src("build/**/*.e2e.js")
.pipe(mocha({
verbose: true,
includeStackTrace: true,
timeout: 20000
}))
.on('error', () => {
if (process.argv[2] !== 'serve') {
console.error('End to end tests failed');
process.exit(1);
}
})
.pipe(istanbul.writeReports({
dir: "./coverage/e2e",
reporters: ["json"]
}));
});
gulp.task("test", ["build", "pre-test"], (cb) => {
// runSequence(["remap-istanbul-unit", "tslint"], 'remap-istanbul-e2e', cb);
runSequence(["remap-istanbul-unit", "tslint"], cb);
});
gulp.task("copy-conf", () => {
gulp.src(['conf/*'])
.pipe(gulp.dest('build/conf/'));
});
gulp.task("build", ["typescript", "swagger-ui", "info", "copy-conf", "set-env-vars"], () => {
gulp.src('package.json')
.pipe(gulp.dest('build'));
gulp.src('conf/app.yaml')
.pipe(replace(' GULP_INSERT: "HERE"', formatYaml(getProductionEnvVars(),2) ))
.pipe(gulp.dest('build'));
});
gulp.task("serve", ["build"], () => {
// configure nodemon
nodemon({
script: getOption('script'),
ext: 'ts',
tasks: ["test"],
});
});
gulp.task('debug', ["build"], () => {
// configure nodemon
const envVars = {
NODE_ENV: 'development',
PROCESS_TYPE: 'web',
WITH_SERVICES: getOption('with_services') || true
}
nodemon({
exec: 'node --inspect-brk',
ext: 'ts',
script: getOption('script'),
verbose: true,
env: envVars
}).on('start', ['']);
});
gulp.task('remap-istanbul-e2e', ["e2etest"], function () {
return gulp.src('./coverage/e2e/coverage-final.json')
.pipe(remapIstanbul({
reports: {
'html': "./coverage/e2e/html-report",
"text": null,
"json": "./coverage/e2e/coverage-remapped.json"
}
}))
.pipe(gulp.dest('./coverage/e2e'));
});
gulp.task('remap-istanbul-unit', ["unittest"], function () {
return gulp.src('./coverage/unit/coverage-final.json')
.pipe(remapIstanbul({
reports: {
'html': "./coverage/unit/html-report",
"text": null,
"json": "./coverage/unit/coverage-remapped.json"
}
}))
.pipe(gulp.dest('./coverage/unit'));
});
gulp.task("dredd", ["serve"], function () {
// shell doesn't need the source piped to it but this way we can attach 'error' and 'end' events...
return gulp.src('.')
.pipe(shell([
'sleep 10',
'npm run dredd'
]))
.once('error', (err) => {
console.log('[gulp] error:', err)
process.exit(1)
})
.once('end', () => {
process.exit()
})
})