forked from SebOuellette/LiveBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleScripts.js
124 lines (107 loc) · 4.18 KB
/
handleScripts.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
// Copyright 2017 Sebastian Ouellette
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
"use strict";
let loadAllScripts = () => {
let files = fs.readdirSync('./scripts');
// Remove the template file out of the equasion if there is one
if (files.includes('template.js'))
files.splice(files.indexOf('template.js'), 1);
// If there are no scripts don't even bother
if (!files.length) return;
// Preoad all the scripts
console.log('Loading scripts...');
files.forEach((file) => {
if (file.endsWith('.js') && file != 'template.js') {
delete require.cache[require.resolve(`./scripts/${file}`)];
require('./scripts/' + file);
console.log(`%c ${file} loaded!`, 'color:Green');
}
});
// Start all the loaded scripts
console.log('Starting scripts...');
files.forEach((file) => {
if (file.endsWith('.js')) {
try {
if (
!require.cache[require.resolve(`./scripts/${file}`)].exports
.start
)
throw { message: '.exports.start is not a function' };
console.log(`%c ${file} started!`, 'color:Green');
require.cache[
require.resolve(`./scripts/${file}`)
].exports.start();
} catch (err) {
// If it's missing the start function then display a short error and say which plugin it is
if (err.message.includes('.exports.start is not a function'))
return console.log(
`%c ${file} was unable to start`,
'color:Red'
);
console.log(
`%c ${file} was unable to start\n${err}`,
'color:Red'
);
}
}
});
};
let unloadAllScripts = () => {
let files = fs.readdirSync('./scripts');
// Stop and unload all the scripts
console.log('Stopping scripts...');
files.forEach((file) => {
if (
file.endsWith('.js') &&
file != 'template.js' &&
require.cache[require.resolve(`./scripts/${file}`)]
) {
try {
if (
!require.cache[require.resolve(`./scripts/${file}`)].exports
.stop
)
throw { message: '.exports.stop is not a function' };
console.log(`%c ${file} stopped!`, 'color:Red');
require.cache[
require.resolve(`./scripts/${file}`)
].exports.stop();
} catch (err) {
// If it's missing the start function then display a short error and say which plugin it is
if (err.message.includes('.exports.stop is not a function'))
return console.log(
`%c ${file} was unable to stop`,
'color:Red'
);
console.log(
`%c ${file} was unable to stop\n${err}`,
'color:Red'
);
}
delete require.cache[require.resolve(`./scripts/${file}`)];
console.log(`%c ${file} unloaded!`, 'color:Red');
}
});
};
let getData = () => {
let object = {};
let files = fs.readdirSync('./scripts');
files.forEach((file) => {
if (file.endsWith('.js') && file != 'template.js') {
object[file] =
require.cache[
require.resolve(`./scripts/${file}`)
].exports.info;
object[file].file = file;
}
});
return object;
};