forked from Trustroots/trustroots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
208 lines (188 loc) · 5.37 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
/* eslint-disable no-console */
/**
* Module dependencies.
*/
const _ = require('lodash');
const defaultAssets = require('./config/assets/default');
const gulp = require('gulp');
const gulpLoadPlugins = require('gulp-load-plugins');
const minimatch = require('minimatch');
const del = require('del');
const nodemon = require('nodemon');
const print = require('gulp-print').default;
const plugins = gulpLoadPlugins();
// Local settings
let changedTestFiles = [];
// Globbed paths ignored by Nodemon
const nodemonIgnores = [
'bin/**',
'migrations/**',
'modules/*/client/**',
'modules/*/tests/client/**/*.js',
'modules/*/tests/server/**/*.js',
'node_modules/**',
'public/**',
'scripts/**',
'tmp/**',
defaultAssets.server.fontelloConfig,
defaultAssets.server.gulpConfig,
];
// Nodemon task for server
function runNodemon(done) {
nodemon({
script: 'server.js',
// Default port is `5858`
// @link https://nodejs.org/api/debugger.html
nodeArgs: ['--inspect=5858'],
ext: 'js, html',
ignore: nodemonIgnores,
watch: _.union(
defaultAssets.server.views,
defaultAssets.server.allJS,
defaultAssets.server.config,
),
})
.on('crash', function () {
console.error('[Server] Script crashed.');
})
.on('exit', function () {
console.log('[Server] Script exited.');
});
done();
}
// Nodemon task for worker
function runNodemonWorker(done) {
nodemon({
script: 'worker.js',
// Default port is `5858`, but because `nodemon` task is already using it
// we are defining different port for debugging here.
// @link https://nodejs.org/api/debugger.html
nodeArgs: ['--inspect=5859'],
ext: 'js',
ignore: nodemonIgnores,
watch: _.union(
defaultAssets.server.workerJS,
defaultAssets.server.allJS,
defaultAssets.server.config,
),
})
.on('crash', function () {
console.error('[Worker] Script crashed.');
})
.on('exit', function () {
console.log('[Worker] Script exited.');
});
done();
}
// Set NODE_ENV to 'development' and prepare environment
gulp.task(
'env:dev',
gulp.series(function (done) {
process.env.NODE_ENV = 'development';
done();
}),
);
// Set NODE_ENV to 'production' and prepare environment
gulp.task(
'env:prod',
gulp.series(function (done) {
process.env.NODE_ENV = 'production';
done();
}),
);
// Watch server test files
gulp.task('watch:server:run-tests', function watchServerRunTests() {
// Add Server Test file rules
gulp
.watch(
[
'modules/*/tests/server/**/*.js',
...defaultAssets.server.allJS,
defaultAssets.server.migrations,
],
gulp.series('test:server'),
)
.on('change', function (changedFile) {
changedTestFiles = [];
// determine if the changed (watched) file is a server test
if (minimatch(changedFile, 'modules/*/tests/server/**/*.js')) {
changedTestFiles.push(changedFile);
}
});
});
// Clean JS files -task
gulp.task('clean', function clean() {
return del(['public/dist/*.js']);
});
// Generate font icon files from Fontello.com
function fontello() {
return gulp
.src(defaultAssets.server.fontelloConfig)
.pipe(
plugins.fontello({
font: 'font', // Destination dir for Fonts and Glyphs
css: 'css', // Destination dir for CSS Styles,
assetsOnly: false,
}),
)
.pipe(print())
.pipe(gulp.dest('modules/core/client/fonts/fontello'));
}
function mocha(done) {
// Open mongoose connections
const mongooseService = require('./config/lib/mongoose');
const agenda = require('./config/lib/agenda');
const testSuites =
changedTestFiles.length > 0
? changedTestFiles
: 'modules/*/tests/server/**/*.js';
let error;
// Connect mongoose
mongooseService.connect(function (db) {
// Clean out test database to have clean base
mongooseService.dropDatabase(db, function () {
mongooseService.loadModels();
// Run the tests
gulp
.src(testSuites)
.pipe(
plugins.mocha({
reporter: 'spec',
timeout: 10000,
}),
)
.on('error', function (err) {
// If an error occurs, save it
error = err;
console.error(err);
})
.on('end', function () {
// When the tests are done, disconnect agenda/mongoose
// and pass the error state back to gulp
// @TODO: https://github.com/Trustroots/trustroots/issues/438
// @link https://github.com/agenda/agenda/pull/450
agenda._mdb.close(function () {
mongooseService.disconnect(function () {
done(error);
});
});
});
});
});
}
// Run fontello update
gulp.task('fontello', fontello);
gulp.task('test:server', gulp.series(mocha));
// Watch all server files for changes & run server tests (test:server) task on changes
gulp.task(
'test:server:watch',
gulp.series('test:server', 'watch:server:run-tests'),
);
// Run the project in development mode
gulp.task('develop', gulp.series('env:dev', runNodemon));
// Run the project in production mode
gulp.task('prod', gulp.series('env:prod', runNodemon));
// Run worker script in development mode
gulp.task('worker:dev', gulp.series('env:dev', runNodemonWorker));
// Run worker script in production mode
gulp.task('worker:prod', gulp.series('env:prod', runNodemonWorker));