-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathomegga.main.js
executable file
·109 lines (98 loc) · 2.96 KB
/
omegga.main.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
const chokidar = require('chokidar');
const path = require('path');
const log = (...args) =>
(global.Omegga ?? global.Logger).log(
'reloader'.underline,
'>>'.green,
...args
);
const error = (...args) =>
(global.Omegga ?? global.Logger).error(
'reloader'.underline,
'!>'.red,
...args
);
module.exports = class Reloader {
constructor(omegga, config, store) {
this.omegga = omegga;
this.config = config;
this.store = store;
}
async init() {
const patterns = [
!this.config['watch-all-ts'] && 'omegga.plugin.ts',
!this.config['watch-all-js'] && 'omegga.plugin.js',
!this.config['watch-all-js'] && 'omegga.main.js',
this.config['watch-all-js'] && '**/*.js',
this.config['watch-all-ts'] && '**/*.ts',
].filter(Boolean);
let wasLoaded = {};
let active = false;
this.watchers = patterns.map(pattern => {
const watcher = chokidar.watch(
path.join(this.omegga.pluginLoader.path, pattern)
);
watcher
.on('change', async file => {
if (active) return;
active = true;
// ignore omegga.d.ts changes
if (path.basename(file) === 'omegga.d.ts') {
active = false;
return;
}
// find the plugin that has this file
const plugin = this.omegga.pluginLoader.plugins.find(p =>
file.startsWith(p.path)
);
// somehow you snuck in an incomplete plugin
if (!plugin) {
active = false;
return;
}
if (plugin.loadedPlugin === this)
log('I used the reloader to reload the reloader'.rainbow);
const name = plugin.getName();
// announce we found a change
log(
name.yellow,
`- Detected change in ${path.basename(file)}`,
!plugin.isLoaded() && !wasLoaded[name]
? 'but plugin is not loaded.'
: ''
);
let ok;
if (plugin.isLoaded()) {
wasLoaded[name] = true;
log(name.yellow, '- Unloading plugin');
ok = await plugin.unload();
if (!ok) {
active = false;
return error(name.brightRed, '- Error unloading plugin');
}
}
if (!plugin.isLoaded() && wasLoaded[name]) {
ok = await plugin.load();
if (!ok) {
active = false;
return error(name.brightRed, '- Error loading plugin');
}
log(name.yellow, '- Reloaded plugin');
}
active = false;
})
.on('error', error => error(`Watcher error: ${error}`))
.on('ready', () =>
log(
'Watching plugins at',
path.join(this.omegga.pluginLoader.path, pattern)
)
);
return watcher;
});
}
async stop() {
log('Closing watcher...');
await Promise.all(this.watchers.map(w => w.close()));
}
};