forked from synergistic-silobusters/flintandsteel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
304 lines (261 loc) · 8.13 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
/* global __dirname */
/* global process */
var gulp = require('gulp'),
gutil = require('gulp-util'),
inject = require('gulp-inject'),
os = require('os'),
del = require('del'),
chalk = require('chalk'),
jshint = require('gulp-jshint'),
jscs = require('gulp-jscs'),
stylish = require('jshint-stylish-ex'),
nodemon = require('gulp-nodemon'),
KarmaServer = require('karma').Server,
spawn = require('child_process').spawn,
mkdirs = require('mkdirs'),
angularFilesort = require('gulp-angular-filesort'),
naturalSort = require('gulp-natural-sort');
var paths = {
js: [
'src/**/*.js',
'!src/dist/**/*.js',
'!src/lib/**/*.js',
'!src/**/*.spec.js',
'!src/**/*.mock.js',
'!src/**/*.conf.js',
'!src/**/*.e2e.js'
]
};
var commandBuilder = function(command) {
"use strict";
var cmd = {};
var cmdArr = command.split(' ');
cmd.exec = cmdArr.shift();
cmd.args = cmdArr;
return cmd;
};
var runCommand = function(command, description, cb) {
"use strict";
if (typeof command.exec === 'undefined') {
command = commandBuilder(command);
}
var child = spawn(command.exec, command.args);
child.stdout.on('data', function(data) {
process.stdout.write(data);
});
child.stderr.on('data', function(data) {
process.stdout.write(chalk.red(data));
});
child.on('exit', function(exitCode) {
console.log(chalk.yellow(description + " exited with " + exitCode));
cb(exitCode);
});
return child;
};
gulp.task('default', ['usage']);
gulp.task('usage', function() {
"use strict";
var usageLines = [
'',
'',
chalk.green('usage'),
'\tdisplay this help page.',
'',
chalk.green('mongo:start'),
'\truns the mongodb server - this is required for the app to work.',
'',
chalk.green('mongo:stop'),
'\tstops the mongodb server.',
'',
chalk.green('start:dev'),
'\t runs the app server in development mode (doesn\'t use LDAP, generates data).',
'',
chalk.green('start:test'),
'\t runs the app server in test mode for load testing (doesn\'t generate data).',
'',
chalk.green('start:prod'),
'\t runs the app server in production mode (uses LDAP, HTTPS).',
'',
chalk.green('test:client'),
'\truns the client side tests using karma.',
'',
chalk.green('jshint'),
'\trun jshint on all .spec.js and .js files under src and server.',
'',
chalk.green('jscs'),
'\trun jscs on all .spec.js and .js files under src and server.',
'',
chalk.green('code-check'),
'\tshortcut to run both jshint and jscs on the code.',
'',
chalk.green('generate:data'),
'\tgenerate sample data in the database.',
'',
chalk.green('clean:modules'),
'\tdeletes the npm_modules and the src/lib directories.',
'\t' + chalk.magenta('NOTE:') + ' ' + chalk.green('npm install') +
' will be required before running the app.',
'',
chalk.green('clean:db-dev'),
'\tclears the development database.',
''
];
gutil.log(usageLines.join(os.EOL));
});
gulp.task('mongo:start', function(cb) {
"use strict";
var command = 'mongod --config ./server/mongod.conf';
mkdirs('server/datastore/db');
mkdirs('server/datastore/log');
runCommand(command, "Mongodb server", cb);
gutil.log('Mongodb server is now ' + chalk.green('running') + '.');
});
gulp.task('mongo:stop', function(cb) {
"use strict";
var command = 'mongo admin --eval db.shutdownServer();';
runCommand(command, "Shutdown server", cb);
del('server/datastore/mongod-pids');
});
gulp.task('start:dev', ['test:client', 'inject', 'generate:data'], function() {
"use strict";
nodemon({
script: 'server/server.js',
env: { 'NODE_ENV': 'development' },
'ignore': [
'coverage/*',
'server/datastore/*',
'src/*'
]
});
});
gulp.task('start:test', ['test:client', 'inject'], function() {
"use strict";
nodemon({
script: 'server/server.js',
env: { 'NODE_ENV': 'test' },
'ignore': [
'coverage/*',
'server/datastore/*',
'src/*'
]
});
});
gulp.task('start:prod', ['test:client', 'inject'], function() {
"use strict";
nodemon({
script: 'server/server.js',
env: { 'NODE_ENV': 'production' },
'ignore': [
'coverage/*',
'server/datastore/*',
'src/*'
]
});
});
gulp.task('inject', function() {
"use strict";
gulp.src('./src/index.html')
.pipe(
inject(gulp.src(paths.js).pipe(naturalSort()).pipe(angularFilesort()), {relative: true}))
.pipe(gulp.dest('./src'));
});
gulp.task('jshint', function() {
"use strict";
return gulp.src([
'src/**/*.js',
'server/**/*.js',
'gulpfile.js',
'!src/lib/**/*.*',
'!server/secrets/*.*'
])
.pipe(jshint())
.pipe(jshint.reporter(stylish))
.pipe(jshint.reporter('fail'));
});
gulp.task('jscs', function() {
"use strict";
return gulp.src([
'src/**/*.js',
'server/**/*.js',
'gulpfile.js',
'!src/lib/**/*.*',
'!server/secrets/*.*'
])
.pipe(jscs({ configPath: './.jscsrc' }))
.pipe(jscs.reporter())
.pipe(jscs.reporter('fail'));
});
gulp.task('code-check', ['jshint', 'jscs']);
gulp.task('test:client', function(done) {
"use strict";
var server = new KarmaServer({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done);
server.start();
});
gulp.task('test', ['test:client']);
gulp.task('test:load', ['initialize:db-dev'], function(cb) {
"use strict";
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var benchrest = require('bench-rest');
var data = require('./generateData');
data.generateUsers(function(err, userIds) {
if (err) {
console.error(chalk.red(err));
}
else {
var u = 0;
var flow = {
main: [
// Creating a large amount of ideas here
{
post: 'http://127.0.0.1:7357/idea',
json: {
title: "Idea #{INDEX}",
description: "Description #{INDEX}",
authorId: userIds[u++ % userIds.length],
eventId: "",
tags: [],
rolesreq: []
}
}
]
};
var runOptions = {
limit: 50, // concurrent connections
iterations: 300 // number of iterations to perform
};
benchrest(flow, runOptions)
.on('error', function error(err, ctxName) {
if (err.code === 'ECONNREFUSED') {
console.error(chalk.red('Please ensure the server is started on port 7357 by running gulp start:test'));
process.exit(1);
return;
}
console.error(chalk.red('Failed in ' + ctxName + ' with error: '), err);
})
.on('end', function end(stats, errorCount) {
console.log(chalk.red('error count: '), errorCount);
console.log(chalk.green('stats: '), stats);
cb();
});
}
});
});
gulp.task('clean:modules', function() {
"use strict";
return del([
'node_modules',
'src/lib'
]);
});
gulp.task('clean:db-dev', function(cb) {
"use strict";
var command = "mongo flintandsteel-dev --eval db.dropDatabase()";
runCommand(command, "Drop database", cb);
});
gulp.task('generate:data', ['clean:db-dev'], function(cb) {
"use strict";
runCommand('mongo generateData.js', 'Generate data', cb);
});