Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin Dependencies #1030

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 115 additions & 3 deletions src/lib/installPlugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import loader from "dialogs/loader";
import fsOperation from "fileSystem";
import purchaseListener from "handlers/purchase";
import JSZip from "jszip";
import Url from "utils/Url";
import helpers from "utils/helpers";
import constants from "./constants";
import loadPlugin from "./loadPlugin";

Expand All @@ -10,8 +12,14 @@ import loadPlugin from "./loadPlugin";
* @param {string} id
* @param {string} name
* @param {string} purchaseToken
* @param {(message: any) => void} setMessage
*/
export default async function installPlugin(id, name, purchaseToken) {
export default async function installPlugin(
id,
name,
purchaseToken,
setMessage,
) {
const title = name || "Plugin";
const loaderDialog = loader.create(title, strings.installing);
let pluginDir;
Expand Down Expand Up @@ -41,7 +49,7 @@ export default async function installPlugin(id, name, purchaseToken) {
}

try {
loaderDialog.show();
if (!setMessage) loaderDialog.show();

const plugin = await fsOperation(pluginUrl).readFile(
undefined,
Expand All @@ -64,6 +72,14 @@ export default async function installPlugin(id, name, purchaseToken) {
await zip.files["plugin.json"].async("text"),
);

if (pluginJson.dependencies) {
for (const dependency of pluginJson.dependencies) {
const _setMessage = setMessage ? setMessage : loaderDialog.setMessage;
const hasError = await resolveDependency(dependency, _setMessage);
if (hasError) throw new Error(strings.failed);
}
}

if (!pluginDir) {
pluginJson.source = pluginUrl;
id = pluginJson.id;
Expand Down Expand Up @@ -107,7 +123,7 @@ export default async function installPlugin(id, name, purchaseToken) {
}
throw err;
} finally {
loaderDialog.destroy();
if (!setMessage) loaderDialog.destroy();
}
}

Expand Down Expand Up @@ -139,3 +155,99 @@ async function createFileRecursive(parent, dir) {
await createFileRecursive(newParent, dir);
}
}

/** Resolve dependency
* @param {string} id
* @param {(message: any) => void} setMessage
* @returns {Promise<boolean>} has error
*/
async function resolveDependency(id, setMessage) {
let purchaseToken;
let product;
let isPaid = false;

try {
const remoteDependency = await fsOperation(
constants.API_BASE,
`plugin/${id}`,
)
.readFile("json")
.catch(() => null);

if (!remoteDependency) return true;

const version = await isInstalled(id);
if (remoteDependency?.version === version) return false;

plugin = Object.assign({}, remoteDependency);
bajrangCoder marked this conversation as resolved.
Show resolved Hide resolved

if (!Number.parseFloat(remoteDependency.price)) return true;

isPaid = remoteDependency.price > 0;
[product] = await helpers.promisify(iap.getProducts, [
remoteDependency.sku,
]);
if (product) {
const purchase = await getPurchase(product.productId);
purchaseToken = purchase?.purchaseToken;
}

if (isPaid && !purchaseToken) {
if (!product) throw new Error("Product not found");
const apiStatus = await helpers.checkAPIStatus();

if (!apiStatus) {
alert(strings.error, strings.api_error);
return true;
}

iap.setPurchaseUpdatedListener(...purchaseListener(onpurchase, onerror));
setMessage(strings["loading..."]);
await helpers.promisify(iap.purchase, product.json);

async function onpurchase(e) {
const purchase = await getPurchase(product.productId);
await ajax.post(Url.join(constants.API_BASE, "plugin/order"), {
data: {
id: id,
token: purchase?.purchaseToken,
package: BuildInfo.packageName,
},
});
purchaseToken = purchase?.purchaseToken;
}

async function onerror(error) {
helpers.error(error);
return true;
}
}

setMessage(
`${strings.installing.replace("...", "")} ${remoteDependency.name}...`,
);
await installPlugin(dependency, undefined, purchaseToken, setMessage);
} catch (error) {
helpers.error(error);
}

async function getPurchase(sku) {
const purchases = await helpers.promisify(iap.getPurchases);
const purchase = purchases.find((p) => p.productIds.includes(sku));
return purchase;
}

/**
*
* @param {string} id
* @returns {Promise<string>} plugin version
*/
async function isInstalled(id) {
if (await fsOperation(PLUGIN_DIR, id).exists()) {
const plugin = await fsOperation(PLUGIN_DIR, id, "plugin.json").readFile(
"json",
);
return plugin.version;
}
}
}