This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
82 lines (70 loc) · 2.11 KB
/
index.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
const { Plugin } = require('powercord/entities');
const watch = require('node-watch'); // from powercord/package-lock.json
module.exports = class HotRemount extends Plugin {
async startPlugin () {
this.registerCommand();
powercord.once('loaded', this.onLoaded.bind(this));
}
pluginWillUnload () {
powercord.api.commands.unregisterCommand('watch');
}
onLoaded () {
this.settings.get('plugins', []).forEach((id) => this.startWatch(id));
}
registerCommand () {
powercord.api.commands.registerCommand({
command: 'watch',
label: 'Hot remount',
usage: '{c} < plugin-id >',
description: 'Track changes in a plugin and automatically remount it',
executor: this.run.bind(this),
autocomplete: this.autocomplete.bind(this)
});
}
run ([ id ]) {
if (powercord.pluginManager.plugins.has(id)) {
if (this.settings.get('plugins', []).includes(id)) {
return { result: 'Already watching' };
}
this.settings.set('plugins', [
...this.settings.get('plugins', []),
id
]);
this.startWatch(id);
return false;
}
return { result: 'Plugin not found' };
}
autocomplete ([ findId, ...args ]) {
if (args.length) {
return;
}
return {
commands: [ ...powercord.pluginManager.plugins ]
.filter(([ id ]) => id.includes(findId))
.map(([ id ]) => ({ command: id })),
header: 'Plugins list'
};
}
startWatch (id) {
const plugin = powercord.pluginManager.plugins.get(id);
const watcher = watch(plugin.entityPath, { recursive: true }, global._.debounce(() => {
powercord.pluginManager.remount(id);
}, 350));
this.notice(id, plugin.manifest.name, () => {
const plugins = this.settings.get('plugins', []);
plugins.splice(plugins.indexOf(id), 1);
this.settings.set('plugins', plugins);
watcher.close();
});
}
notice (id, name, onClick) {
powercord.api.notices.sendAnnouncement(`hot-remount-stop-${id}`, {
message: `Watching the plugin "${name}"`,
button: {
text: 'Stop',
onClick
}
});
}
};