-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.ts
243 lines (212 loc) · 5.93 KB
/
gulpfile.ts
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
/**
* Module dependencies.
*/
import _ from 'lodash';
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import { runCLI } from '@jest/core';
import inquirer from 'inquirer';
import defaultAssets from './config/assets';
import config from './config';
import * as mongooseService from './lib/services/mongoose';
import * as seedService from './lib/services/seed';
const plugins = gulpLoadPlugins();
// default node env if not define
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
// Nodemon
const nodemon = (done) => {
nodemon({
script: 'server.js',
nodeArgs: ['--harmony'],
ext: 'js,html',
verbose: true,
watch: _.union(defaultAssets.views, defaultAssets.config),
});
done();
};
export { nodemon };
// Nodemon (task without verbosity or debugging)
const nodemonDebug = (done) => {
nodemon({
script: 'server.js',
nodeArgs: ['--harmony', '--debug', '--inspect'],
ext: 'js,html',
watch: _.union(defaultAssets.views, defaultAssets.config),
});
done();
};
export { 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();
};
export { watch };
// Jest UT
const jest = (done) => {
runCLI({} as any, ['.'])
.then((result) => {
if (result.results && result.results.numFailedTests > 0) process.exit();
done();
})
.catch((e) => {
console.log(e);
});
};
export { jest };
// Jest Watch
const jestWatch = (done) => {
runCLI({ watch: true } as any, ['.']);
done();
};
export { jestWatch };
// Jest UT
const jestCoverage = (done) => {
runCLI(
{
collectCoverage: true,
collectCoverageFrom: defaultAssets.allJS,
coverageDirectory: 'coverage',
coverageReporters: ['json', 'lcov', 'text'],
} as any,
['.'],
)
.then((result) => {
if (result.results && result.results.numFailedTests > 0) process.exit();
done();
})
.catch((e) => {
console.log(e);
});
};
export { jestCoverage };
// Drops the MongoDB database, used in e2e testing by security
const dropMongo = async () => {
const db = await mongooseService.connect();
await db.connection.dropDatabase();
await mongooseService.disconnect();
};
export { dropMongo };
// Drop database after confirmation, depends of ENV
const dropDB = async () => {
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,
},
];
const answer = await inquirer.prompt(question);
if (!answer.continue) return process.exit(2);
await dropMongo();
} else await dropMongo();
};
export { dropDB };
// Connects to Mongoose based on environment settings and seeds the database
const seedMongoose = async () => {
try {
await mongooseService.connect();
await seedService
.start({
logResults: true,
})
.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 {
await mongooseService.connect();
await mongooseService.loadModels();
await seedService.userSeed(config.seedDB.options.seedUser).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);
export { test };
// Run project tests with coverage
const testWatch = gulp.series(dropDB, jestWatch);
export { testWatch };
// Run project tests with coverage
const testCoverage = gulp.series(dropDB, jestCoverage);
export { testCoverage };
// Run Mongoose Seed
const seed = gulp.series(dropDB, seedMongoose);
export { seed };
// Run Mongoose Seed
const seedUser = gulp.series(seedMongooseUser);
export { seedUser };
// Run Mongoose drop
const drop = gulp.series(dropDB);
export { 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));
export { debug };
// Run project in production mode
const prod = gulp.series(gulp.parallel(nodemonDebug, watch));
export { 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;