Skip to content

Commit

Permalink
Updating the extension to add a file selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Garethp committed Oct 8, 2019
1 parent 7242c49 commit fead60d
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,46 @@ const init = (context) => {
return;
}

if (!fs.existsSync(path.resolve(__dirname + '/backup.json'))) {
api.showErrorNotification(`Backup file does not exist: ${path.resolve(__dirname + '/backup.json')}`)
return;
}

fs.readFile(path.resolve(__dirname + '/backup.json'), (error, jsonString) => {
const activeGameId = getActiveGameId(state);

if (error) {
api.showErrorNotification(error);
return;
}

const installedMods = getInstalledMods(state);
let mods = JSON.parse(jsonString);
mods = mods.filter(mod => mod.game === activeGameId)
.filter(mod => !installedMods.some(installedMod => installedMod.game === mod.game && installedMod.modId === mod.modId && installedMod.fileId === mod.fileId))
;

for (const mod of mods) {
api.events.emit('mod-update', mod.game, mod.modId, mod.fileId);
}

api.showErrorNotification(`${mods.length} mods for ${activeGameId} restored`);
});
api.selectFile({ create: false, title: 'Select your backup file to import'})
.then(fileName => {
fs.readFile(path.resolve(fileName), (error, jsonString) => {
const activeGameId = getActiveGameId(state);

if (error) {
api.showErrorNotification(error);
return;
}

const installedMods = getInstalledMods(state);
let mods = JSON.parse(jsonString);
mods = mods.filter(mod => mod.game === activeGameId)
.filter(mod => !installedMods.some(installedMod => installedMod.game === mod.game && installedMod.modId === mod.modId && installedMod.fileId === mod.fileId))
;

for (const mod of mods) {
api.events.emit('mod-update', mod.game, mod.modId, mod.fileId);
}

api.showErrorNotification(`${mods.length} mods for ${activeGameId} restored`);
});
});
});

context.registerAction('mod-icons', 999, 'show', {}, 'Modlist Backup', () => {
const state = api.store.getState();
const mods = getInstalledMods(state);

fs.writeFile(path.resolve(__dirname + '/backup.json'), JSON.stringify(mods, null, 4), error => {
if (error) {
api.showErrorNotification(error);
return;
}

api.showErrorNotification(`Modlist backed up to ${path.resolve(__dirname + '/backup.json')}`);
});
api.selectFile({ create: true, title: 'Select file to export to'})
.then(fileName => {
fs.writeFile(path.resolve(fileName), JSON.stringify(mods, null, 4), error => {
if (error) {
api.showErrorNotification(error);
return;
}

api.showErrorNotification(`Modlist backed up to ${path.resolve(fileName)}`);
});
});
});
};

Expand Down

0 comments on commit fead60d

Please sign in to comment.