-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.ts
72 lines (53 loc) · 2.18 KB
/
update.ts
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
import Command from '../command';
import Config from '../config';
import ISteamMod from '../interfaces/iSteamMod';
import Mod from '../mod';
import Processor from '../processor';
import SteamApi from '../steam/steamApi';
import SteamCmd from '../steam/steamCmd';
import CronCommand from './cron';
export default class ActivateCommand extends Command {
public static description = 'Updates currently downloaded mods from Steam Workshop.';
public async init(): Promise<void> {
Config.ensureIsInitialized();
Config.ensureIsLatestVersion();
}
public async run(): Promise<void> {
// Filter for only installed mods
const configMods = Config.get('mods');
const mods: ISteamMod[] = Mod.filterSteamMods(configMods).filter(mod => mod.updatedAt !== undefined);
if (mods.length === 0) {
console.log('No installed mods.');
return;
}
// Check for those mods that need updates from SteamAPI
const apiMods = await SteamApi.getPublishedItems(mods.map(mod => mod.steamId));
const queuedMods = [];
for (const [index, mod] of mods.entries()) {
const apiMod = apiMods[index];
if (apiMod.time_updated !== mod.updatedAt) {
mod.updatedAt = apiMod.time_updated;
queuedMods.push(mod);
}
}
if (queuedMods.length === 0) {
console.log('All mods are up-to-date.');
return;
} else {
console.log(`Found updates for ${queuedMods.map(mod => mod.name).join(', ')}`);
}
// Run SteamCmd
await SteamCmd.download(queuedMods.map(mod => mod.steamId));
// Update keys
const updatedMods = Processor.updateKeys(queuedMods);
// Update magma.json
for (const mod of updatedMods) {
const index = configMods.findIndex(configMod => configMod.id === mod.id);
configMods[index] = mod;
}
console.log(`Updated ${updatedMods.map(mod => mod.name).join(', ')}`);
Config.set('mods', configMods);
// Run the cron command to inform others that the mods have been updated
CronCommand.run([]);
}
}