2018-08-29 05:19:18 +02:00
|
|
|
import EventListener from './eventlistener';
|
|
|
|
import asar from 'asar';
|
2018-08-30 04:39:23 +02:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2018-08-29 05:19:18 +02:00
|
|
|
|
|
|
|
import { Modals } from 'ui';
|
|
|
|
import { Utils } from 'common';
|
2018-08-29 22:02:41 +02:00
|
|
|
import PluginManager from './pluginmanager';
|
2018-08-30 04:39:23 +02:00
|
|
|
import Globals from './globals';
|
2018-08-29 05:19:18 +02:00
|
|
|
|
2018-08-30 04:39:23 +02:00
|
|
|
export default class {
|
2018-08-29 05:19:18 +02:00
|
|
|
|
2018-08-30 04:39:23 +02:00
|
|
|
static async installPackageEvent(pkg, upload) {
|
2018-08-29 05:19:18 +02:00
|
|
|
try {
|
|
|
|
const config = JSON.parse(asar.extractFile(pkg.path, 'config.json').toString());
|
|
|
|
const { info, main } = config;
|
|
|
|
|
|
|
|
let icon = null;
|
|
|
|
if (info.icon && info.icon_type) {
|
|
|
|
const extractIcon = asar.extractFile(pkg.path, info.icon);
|
|
|
|
icon = `data:${info.icon_type};base64,${Utils.arrayBufferToBase64(extractIcon)}`;
|
|
|
|
}
|
|
|
|
if (icon) config.iconEncoded = icon;
|
|
|
|
const isPlugin = info.type && info.type === 'plugin' || main.endsWith('.js');
|
|
|
|
|
2018-08-30 04:39:23 +02:00
|
|
|
config.path = pkg.path;
|
|
|
|
|
2018-08-29 05:19:18 +02:00
|
|
|
/*
|
|
|
|
config.permissions = [
|
|
|
|
{
|
|
|
|
HEADER: 'Test Permission Header',
|
|
|
|
BODY: 'Test Permission Body'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HEADER: 'Test Permission Header',
|
|
|
|
BODY: 'Test Permission Body'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HEADER: 'Test Permission Header',
|
|
|
|
BODY: 'Test Permission Body'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HEADER: 'Test Permission Header',
|
|
|
|
BODY: 'Test Permission Body'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
*/
|
|
|
|
// Show install modal
|
2018-08-29 22:02:41 +02:00
|
|
|
const modalResult = await Modals.installModal(isPlugin ? 'plugin' : 'theme', config).promise;
|
2018-08-29 05:19:18 +02:00
|
|
|
|
|
|
|
if (modalResult === 0) {
|
|
|
|
// Upload it instead
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(modalResult);
|
|
|
|
|
|
|
|
} catch (err) {}
|
|
|
|
}
|
|
|
|
|
2018-08-30 04:39:23 +02:00
|
|
|
// TODO lots of stuff
|
|
|
|
/**
|
|
|
|
* Installs or updates defined package
|
|
|
|
* @param {Byte[]|String} bytesOrPath byte array of binary or path to local file
|
|
|
|
* @param {String} name Package name
|
|
|
|
* @param {Boolean} update Does an older version already exist
|
|
|
|
*/
|
|
|
|
static async installPackage(bytesOrPath, name, update = false) {
|
|
|
|
const bytes = typeof bytesOrPath === 'string' ? fs.readFileSync(bytesOrPath) : bytesOrPath;
|
|
|
|
const outputPath = path.join(Globals.getPath('plugins'), `${name}.bd`);
|
|
|
|
fs.writeFileSync(outputPath, bytes);
|
|
|
|
|
|
|
|
if (!update) {
|
|
|
|
return PluginManager.preloadPackedContent(`${name}.bd`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PluginManager.reloadContent(PluginManager.getPluginByName(name));
|
|
|
|
}
|
|
|
|
|
2018-08-29 05:19:18 +02:00
|
|
|
}
|