Initial loading logic for packed themes

This commit is contained in:
Jiiks 2018-12-01 03:53:49 +02:00
parent 7fb5c8a378
commit b1bd5190c1
2 changed files with 19 additions and 26 deletions

View File

@ -13,6 +13,7 @@ import Security from './security';
import { ReactComponents } from './reactcomponents'; import { ReactComponents } from './reactcomponents';
import Reflection from './reflection'; import Reflection from './reflection';
import DiscordApi from './discordapi'; import DiscordApi from './discordapi';
import ThemeManager from './thememanager';
export default class PackageInstaller { export default class PackageInstaller {
@ -64,47 +65,39 @@ export default class PackageInstaller {
/** /**
* Installs or updates defined package * Installs or updates defined package
* @param {Byte[]|String} bytesOrPath byte array of binary or path to local file * @param {Byte[]|String} bytesOrPath byte array of binary or path to local file
* @param {String} name Package name * @param {String} nameOrId Package name
* @param {Boolean} update Does an older version already exist * @param {Boolean} update Does an older version already exist
*/ */
static async installPackage(bytesOrPath, id, update = false) { static async installPackage(bytesOrPath, nameOrId, contentType, update = false) {
let outputPath = null; let outputPath = null;
try { try {
const bytes = typeof bytesOrPath === 'string' ? fs.readFileSync(bytesOrPath) : bytesOrPath;
const outputName = `${id}.bd`; const bytes = typeof bytesOrPath === 'string' ? fs.readFileSync(bytesOrPath) : bytesOrPath;
outputPath = path.join(Globals.getPath('plugins'), outputName); const outputName = `${nameOrId}.bd`;
outputPath = path.join(Globals.getPath(contentType + 's'), outputName);
fs.writeFileSync(outputPath, bytes); fs.writeFileSync(outputPath, bytes);
if (!update) return PluginManager.preloadPackedContent(outputName); const manager = contentType === 'plugin' ? PluginManager : ThemeManager;
const oldContent = PluginManager.getPluginById(id); if (!update) return manager.preloadPackedContent(outputName);
if (update && oldContent.packed && oldContent.packed.packageName !== id) { const oldContent = manager.findContent(nameOrId);
await oldContent.unload(true);
await oldContent.unload(true);
if (oldContent.packed && oldContent.packed.packageName !== nameOrId) {
rimraf(oldContent.packed.packagePath, err => { rimraf(oldContent.packed.packagePath, err => {
if(err) console.log(err); if (err) throw err;
}); });
} else {
return PluginManager.preloadPackedContent(outputName);
}
if (update && !oldContent.packed) {
await oldContent.unload(true);
rimraf(oldContent.contentPath, err => { rimraf(oldContent.contentPath, err => {
if (err) console.log(err); if (err) throw err;
}); });
return PluginManager.preloadPackedContent(outputName);
} }
return PluginManager.reloadContent(oldContent); return manager.preloadPackedContent(outputName);
} catch (err) { } catch (err) {
if (outputPath) {
rimraf(outputPath, err => {
if (err) console.log(err);
});
}
throw err; throw err;
} }
} }

View File

@ -109,7 +109,7 @@
}, },
async install() { async install() {
try { try {
const installed = await PackageInstaller.installPackage(this.modal.filePath, this.modal.config.info.id, this.alreadyInstalled); const installed = await PackageInstaller.installPackage(this.modal.filePath, this.modal.config.info.id || this.modal.config.info.name, this.modal.contentType, this.alreadyInstalled);
this.installed = installed; this.installed = installed;
} catch (err) { } catch (err) {
console.log(err); console.log(err);