Skip to content

Commit 09a445b

Browse files
committed
fix issues with installing dependencies
1 parent 46fb97b commit 09a445b

File tree

1 file changed

+41
-54
lines changed

1 file changed

+41
-54
lines changed

src/lib/installPlugin.js

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ajax from "@deadlyjack/ajax";
12
import loader from "dialogs/loader";
23
import fsOperation from "fileSystem";
34
import purchaseListener from "handlers/purchase";
@@ -166,71 +167,57 @@ async function resolveDependency(id, setMessage) {
166167
let product;
167168
let isPaid = false;
168169

169-
try {
170-
const remoteDependency = await fsOperation(
171-
constants.API_BASE,
172-
`plugin/${id}`,
173-
)
174-
.readFile("json")
175-
.catch(() => null);
170+
const remoteDependency = await fsOperation(constants.API_BASE, `plugin/${id}`)
171+
.readFile("json")
172+
.catch(() => null);
176173

177-
if (!remoteDependency) return true;
174+
if (!remoteDependency) throw new Error("Invalid plugin dependency");
178175

179-
const version = await isInstalled(id);
180-
if (remoteDependency?.version === version) return false;
176+
const version = await getInstalledPluginVersion(id);
177+
if (remoteDependency?.version === version) return false;
181178

182-
plugin = Object.assign({}, remoteDependency);
179+
isPaid = remoteDependency.price > 0;
180+
[product] = await helpers.promisify(iap.getProducts, [remoteDependency.sku]);
181+
if (product) {
182+
const purchase = await getPurchase(product.productId);
183+
purchaseToken = purchase?.purchaseToken;
184+
}
183185

184-
if (!Number.parseFloat(remoteDependency.price)) return true;
186+
if (isPaid && !purchaseToken) {
187+
if (!product) throw new Error("Product not found");
188+
const apiStatus = await helpers.checkAPIStatus();
185189

186-
isPaid = remoteDependency.price > 0;
187-
[product] = await helpers.promisify(iap.getProducts, [
188-
remoteDependency.sku,
189-
]);
190-
if (product) {
191-
const purchase = await getPurchase(product.productId);
192-
purchaseToken = purchase?.purchaseToken;
190+
if (!apiStatus) {
191+
alert(strings.error, strings.api_error);
192+
return true;
193193
}
194194

195-
if (isPaid && !purchaseToken) {
196-
if (!product) throw new Error("Product not found");
197-
const apiStatus = await helpers.checkAPIStatus();
198-
199-
if (!apiStatus) {
200-
alert(strings.error, strings.api_error);
201-
return true;
202-
}
203-
204-
iap.setPurchaseUpdatedListener(...purchaseListener(onpurchase, onerror));
205-
setMessage(strings["loading..."]);
206-
await helpers.promisify(iap.purchase, product.json);
207-
208-
async function onpurchase(e) {
209-
const purchase = await getPurchase(product.productId);
210-
await ajax.post(Url.join(constants.API_BASE, "plugin/order"), {
211-
data: {
212-
id: id,
213-
token: purchase?.purchaseToken,
214-
package: BuildInfo.packageName,
215-
},
216-
});
217-
purchaseToken = purchase?.purchaseToken;
218-
}
195+
iap.setPurchaseUpdatedListener(...purchaseListener(onpurchase, onerror));
196+
setMessage(strings["loading..."]);
197+
await helpers.promisify(iap.purchase, product.json);
219198

220-
async function onerror(error) {
221-
helpers.error(error);
222-
return true;
223-
}
199+
async function onpurchase(e) {
200+
const purchase = await getPurchase(product.productId);
201+
await ajax.post(Url.join(constants.API_BASE, "plugin/order"), {
202+
data: {
203+
id: id,
204+
token: purchase?.purchaseToken,
205+
package: BuildInfo.packageName,
206+
},
207+
});
208+
purchaseToken = purchase?.purchaseToken;
224209
}
225210

226-
setMessage(
227-
`${strings.installing.replace("...", "")} ${remoteDependency.name}...`,
228-
);
229-
await installPlugin(dependency, undefined, purchaseToken, setMessage);
230-
} catch (error) {
231-
helpers.error(error);
211+
async function onerror(error) {
212+
throw error;
213+
}
232214
}
233215

216+
setMessage(
217+
`${strings.installing.replace("...", "")} ${remoteDependency.name}...`,
218+
);
219+
await installPlugin(id, undefined, purchaseToken, setMessage);
220+
234221
async function getPurchase(sku) {
235222
const purchases = await helpers.promisify(iap.getPurchases);
236223
const purchase = purchases.find((p) => p.productIds.includes(sku));
@@ -242,7 +229,7 @@ async function resolveDependency(id, setMessage) {
242229
* @param {string} id
243230
* @returns {Promise<string>} plugin version
244231
*/
245-
async function isInstalled(id) {
232+
async function getInstalledPluginVersion(id) {
246233
if (await fsOperation(PLUGIN_DIR, id).exists()) {
247234
const plugin = await fsOperation(PLUGIN_DIR, id, "plugin.json").readFile(
248235
"json",

0 commit comments

Comments
 (0)