forked from rayzrdev/SharpBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
106 lines (90 loc) · 3.14 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
// The bot will have other problems if they're using Node v6<, so the only version
// we probably need to worry about is v6.
if (parseInt(process.versions.node.split('.')[0]) <= 6) {
console.error('[ERROR] SharpBot requires Node v7 or greater. Please download it at https://nodejs.org/en/download/current.');
console.error('| Windows | https://nodejs.org/en/download/current');
console.error('| macOS | https://nodejs.org/en/download/current');
console.error('| Linux | https://nodejs.org/en/download/package-manager');
process.exit(1);
}
const moduleError = /Error: Cannot find module '([a-zA-Z0-9+_-]+)'/;
const gulp = require('gulp');
const eslint = require('gulp-eslint');
const spawn = require('child_process').spawn;
const isDev = process.argv[2] === '--dev';
let bot;
const paths = {
srcFiles: 'src/**/!(_)*.js',
configFiles: 'data/configs/config.json',
gulpFile: 'gulpfile.js'
};
function killBot() {
if (bot) bot.kill();
}
gulp.task('kill', () => {
killBot();
});
gulp.task('lint', () =>
gulp.src([
paths.srcFiles,
paths.gulpFile
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
);
const mainTasks = ['kill'];
if (isDev) {
mainTasks.push('lint');
}
gulp.task('main', mainTasks, () => {
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 === null) {
// Killed by killBot()
return;
}
if (code === 42) {
// Restart
console.error('Restart code detected.');
gulp.start('main');
} else if (code === 666 || code === 154) {
// TODO: 154 = 666 & 255, so why does it sometimes exit with 154 and sometimes with 666?
// Full process stop
console.log('Process exit code detected.');
process.exit(1);
} else {
// Wait for changes
console.log(`Bot has exited with code ${code}. Waiting for changes...`);
}
});
});
gulp.task('watch', () => {
gulp.watch([
paths.srcFiles,
paths.configFiles
], ['main']);
});
gulp.task('default', ['main', 'watch']);
process.on('exit', () => {
killBot();
});