Skip to content

Commit b32a025

Browse files
committed
feat: plugins can now register updates
1 parent 89bf578 commit b32a025

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/base/plugin/updater.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import wrap from 'src/utils/2.pokemon.js';
2+
import { registerModule } from 'src/utils/plugin.js';
3+
import { registerPlugin } from 'src/hooks/updates';
4+
import * as settings from 'src/utils/settings/index.js';
5+
6+
// TODO: translation
7+
const setting = settings.register({
8+
name: 'Disable Auto Updates',
9+
key: 'underscript.disable.plugins.update',
10+
category: 'Plugins',
11+
});
12+
13+
wrap(() => {
14+
const name = 'updater';
15+
function mod(plugin) {
16+
if (!plugin.version) return undefined;
17+
18+
let updater = false;
19+
const update = plugin.settings().add({
20+
name: 'Disable Auto Updates',
21+
key: 'plugin.update',
22+
default: () => setting.value(),
23+
disabled: () => setting.value(),
24+
hidden: () => !updater,
25+
});
26+
setting.on(() => update.refresh());
27+
28+
Object.assign(plugin, 'canUpdate', {
29+
get: () => updater && !setting.value() && !update.value(),
30+
});
31+
32+
return (data = {}) => {
33+
if (!['string', 'object'].includes(typeof data)) throw new Error();
34+
if (updater) throw new Error('Already registered');
35+
const {
36+
downloadURL = typeof data === 'string' ? data : undefined,
37+
updateURL,
38+
} = data;
39+
updater = registerPlugin(plugin, { downloadURL, updateURL });
40+
return () => {
41+
updater();
42+
updater = false;
43+
};
44+
};
45+
}
46+
47+
registerModule(name, mod, 'events', 'settings');
48+
});

src/hooks/updates.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import wrap from 'src/utils/2.pokemon';
99
import Translation from 'src/structures/constants/translation';
1010
import { buttonCSS } from 'src/utils/1.variables';
1111
import sleep from 'src/utils/sleep';
12+
import createParser from 'src/utils/parser';
1213

1314
const HOUR = 60 * 60 * 1000;
1415
const DAY = 24 * HOUR;
@@ -59,6 +60,34 @@ export function register({
5960
pendingUpdates.set(key, data);
6061
}
6162

63+
// TODO: keep registry of update URL's being used? only allow one of any url
64+
export function registerPlugin(plugin, data = {}) {
65+
validate(plugin);
66+
/** @type {FileParser} */
67+
const parser = createParser(data);
68+
const { version } = plugin;
69+
let finished = false;
70+
plugin.events.until(':update', async () => {
71+
if (finished || !plugin.canUpdate) return finished;
72+
const info = await parser.getUpdateData();
73+
const newVersion = await parser.getVersion(info);
74+
if (finished || !plugin.canUpdate) return finished;
75+
if (newVersion !== version) {
76+
register({
77+
plugin,
78+
version: newVersion,
79+
url: await parser.getDownload(info),
80+
});
81+
} else {
82+
unregister(plugin);
83+
}
84+
return false;
85+
});
86+
return () => {
87+
finished = true;
88+
};
89+
}
90+
6291
export function validate(plugin) {
6392
const key = plugin.name || plugin;
6493
const existing = pendingUpdates.get(key);

0 commit comments

Comments
 (0)