forked from rayzrdev/SharpBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
68 lines (60 loc) · 2.06 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
const moduleError = /module\.js:\d+\n\s+throw err\;\n\s+\^\n\nError: Cannot find module '([a-zA-Z0-9+_-]+)'/g;
const gulp = require('gulp');
const eslint = require('gulp-eslint');
const spawn = require('child_process').spawn;
var bot;
const paths = {
srcFiles: 'src/**/!(_)*.js',
configFiles: 'src/**/!(_)*.json',
gulpFile: 'gulpfile.js'
};
gulp.task('lint', () =>
gulp.src([
paths.srcFiles,
paths.gulpFile
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
);
gulp.task('main', ['lint'], () => {
if (bot) bot.kill();
bot = spawn('node', ['src/bot.js'], { 'stdio': ['inherit', 'inherit', 'pipe'] });
bot.stderr.on('data', data => {
process.stderr.write(data);
if (moduleError.test(data.toString())) {
console.error(`
#########################################################################################################################
Node has failed to load a module! If you just updated, you may need to run \'yarn\' again to install/update dependencies.
'yarn' will attempt to run now and install the dependencies for you.
#########################################################################################################################
`);
spawn('yarn', ['--force'], { 'stdio': 'inherit' }).on('close', code => {
if (code === 0) {
console.log(`
New modules have been installed. The bot will now restart.
`);
gulp.start('main');
}
});
}
});
bot.on('close', code => {
if (code === 8) {
console.error('Error detected, attempting to restart the bot...');
gulp.start('main');
} else {
console.log('Bot has gracefully exited. Waiting for changes...');
}
});
});
gulp.task('watch', () => {
gulp.watch([
paths.srcFiles,
paths.configFiles
], ['main']);
});
gulp.task('default', ['main', 'watch']);
process.on('exit', () => {
if (bot) bot.kill();
});