forked from weareopensource/Node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
267 lines (238 loc) · 7.19 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
/**
* Module dependencies.
*/
const _ = require('lodash');
// const glob = require('glob');
const gulp = require('gulp');
const gulpLoadPlugins = require('gulp-load-plugins');
const path = require('path');
const { runCLI } = require('@jest/core');
const inquirer = require('inquirer');
const plugins = gulpLoadPlugins();
const defaultAssets = require('./config/assets');
const config = require(path.resolve('./config'));
// default node env if not define
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
// Nodemon
const nodemon = (done) => {
plugins.nodemon({
script: 'server.js',
nodeArgs: ['--harmony'],
ext: 'js,html',
verbose: true,
watch: _.union(defaultAssets.views, defaultAssets.allJS, defaultAssets.config),
});
done();
};
exports.nodemon = nodemon;
// Nodemon (task without verbosity or debugging)
const nodemonDebug = (done) => {
plugins.nodemon({
script: 'server.js',
nodeArgs: ['--harmony', '--debug', '--inspect'],
ext: 'js,html',
watch: _.union(defaultAssets.views, defaultAssets.allJS, defaultAssets.config),
});
done();
};
exports.nodemonDebug = nodemonDebug;
// Watch (files For Changes)
const watch = (done) => {
// Start livereload
plugins.refresh.listen();
// Add watch rules
gulp.watch(defaultAssets.views).on('change', plugins.refresh.changed);
gulp.watch(defaultAssets.allJS).on('change', plugins.refresh.changed);
gulp.watch(defaultAssets.gulpConfig);
done();
};
exports.watch = watch;
// Jest UT
const jest = (done) => {
runCLI({}, ['.'])
.then((result) => {
if (result.results && result.results.numFailedTests > 0) process.exit();
done();
})
.catch((e) => {
console.log(e);
});
};
exports.jest = jest;
// Jest Watch
const jestWatch = (done) => {
runCLI({ watch: true }, ['.']);
done();
};
exports.jestWatch = jestWatch;
// Jest UT
const jestCoverage = (done) => {
runCLI(
{
collectCoverage: true,
collectCoverageFrom: defaultAssets.allJS,
coverageDirectory: 'coverage',
coverageReporters: ['json', 'lcov', 'text'],
},
['.'],
)
.then((result) => {
if (result.results && result.results.numFailedTests > 0) process.exit();
done();
})
.catch((e) => {
console.log(e);
});
};
exports.jestCoverage = jestCoverage;
// Drops the MongoDB database, used in e2e testing by security
const dropMongo = (done) => {
const mongooseService = require(path.resolve('./lib/services/mongoose'));
mongooseService
.connect()
.then((db) => {
db.connection.dropDatabase((err) => {
if (err) console.error(err);
else console.log('Successfully dropped db: ', db.connections[0].name);
mongooseService.disconnect(done());
});
})
.catch((e) => {
console.log(e);
});
};
exports.dropMongo = dropMongo;
// Drop database after confirmation, depends of ENV
const dropDB = (done) => {
if (process.env.NODE_ENV !== 'test') {
const question = [
{
type: 'confirm',
name: 'continue',
message: `Do you want really want to dropDB in ${process.env.NODE_ENV} ENV ?(no)`,
default: false,
},
];
inquirer.prompt(question).then((answer) => {
if (!answer.continue) return process.exit(2);
this.dropMongo(done);
});
} else this.dropMongo(done);
};
exports.dropDB = dropDB;
// Connects to Mongoose based on environment settings and seeds the database
const seedMongoose = async () => {
try {
const mongooseService = require(path.resolve('./lib/services/mongoose'));
await mongooseService.connect();
await mongooseService.loadModels();
const AuthService = require(path.resolve('./modules/auth/services/auth.service'));
const UserService = require(path.resolve('./modules/users/services/user.service'));
const TaskService = require(path.resolve('./modules/tasks/services/tasks.service'));
const seed = require(path.resolve('./lib/services/seed'));
await seed
.start(
{
logResults: true,
},
UserService,
AuthService,
TaskService,
)
.catch((e) => {
console.log(e);
});
await mongooseService.disconnect();
} catch (err) {
console.log(err);
}
};
// Connects to Mongoose based on environment settings and seeds the database
const seedMongooseUser = async () => {
try {
const mongooseService = require(path.resolve('./lib/services/mongoose'));
await mongooseService.connect();
await mongooseService.loadModels();
const AuthService = require(path.resolve('./modules/auth/services/auth.service'));
const UserService = require(path.resolve('./modules/users/services/user.service'));
const seed = require(path.resolve('./lib/services/seed'));
await seed.user(config.seedDB.options.seedUser, UserService, AuthService).catch((e) => {
console.log(e);
});
await mongooseService.disconnect();
} catch (err) {
console.log(err);
}
};
// Connects to an SQL database, drop and re-create the schemas
// gulp.task('seed:sequelize', (done) => {
// const sequelize = require('./lib/services/sequelize');
// sequelize.seed()
// .then(() => {
// sequelize.sequelize.close();
// done();
// });
// });
// Run project tests
const test = gulp.series(dropDB, jest);
exports.test = test;
// Run project tests with coverage
const testWatch = gulp.series(dropDB, jestWatch);
exports.testWatch = testWatch;
// Run project tests with coverage
const testCoverage = gulp.series(dropDB, jestCoverage);
exports.testCoverage = testCoverage;
// Run Mongoose Seed
const seed = gulp.series(dropDB, seedMongoose);
exports.seed = seed;
// Run Mongoose Seed
const seedUser = gulp.series(seedMongooseUser);
exports.seedUser = seedUser;
// Run Mongoose drop
const drop = gulp.series(dropDB);
exports.drop = drop;
// Run project in development mode
const dev = gulp.series(gulp.parallel(nodemon, watch));
exports.default = dev;
// Run project in debug mode
const debug = gulp.series(gulp.parallel(nodemonDebug, watch));
exports.debug = debug;
// Run project in production mode
const prod = gulp.series(gulp.parallel(nodemonDebug, watch));
exports.prod = prod;
/**
* Examples for Mocha TODO : switch in readme
*/
// Example Mocha example
// const changedTestFiles = [];
// const mocha = () => {
// const testSuites = changedTestFiles.length ? changedTestFiles : defaultAssets.tests;
// return gulp.src(testSuites)
// .pipe(plugins.mocha({
// reporter: 'spec',
// timeout: 10000,
// }))
// .on('error', (err) => {
// // If an error occurs, save it
// console.log(err);
// });
// };
// exports.mocha = mocha;
// Example Watch server test files
// const watchMocha = (done) => {
// // Start livereload
// plugins.refresh.listen();
// // Add Server Test file rules
// gulp.watch(_.union(defaultAssets.tests, defaultAssets.allJS), gulp.series(mocha)).on('change', plugins.refresh.changed);
// done();
// };
// exports.watchMocha = watchMocha;
// Example Bootstrap the server instance
// Common use case is to run API tests on real instantiated models and db
// const bootstrap = (done) => {
// const app = require('./lib/app');
// app.start().then(() => {
// done();
// });
// };
// exports.bootstrap = bootstrap;