@@ -3,6 +3,8 @@ const inquirer = require("inquirer");
3
3
const JSONbig = require("json-bigint")({ storeAsString: false });
4
4
const { Command } = require("commander");
5
5
const { localConfig } = require("../config");
6
+ const { loaderInterval, clearLoaderInterval, } = require('../utils');
7
+ const { formatterFunction } = require('../formatters');
6
8
const { paginate } = require('../paginate');
7
9
const { questionsDeployBuckets, questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
8
10
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
@@ -271,27 +273,38 @@ const deployFunction = async ({ functionId, all, yes } = {}) => {
271
273
});
272
274
273
275
const updatesBar = new _progress.MultiBar({
274
- format: ` ${success('{status}')} | {function}({id})` ,
276
+ format: formatterFunction ,
275
277
hideCursor: true,
276
278
clearOnComplete: false,
277
279
stopOnComplete: true,
278
280
noTTYOutput: true
279
281
});
280
- log('Deploying functions')
281
- for (let func of functions) {
282
- const a = updatesBar.create(4, 0, { status: '', function: func.name, id: func['$id'] })
283
- {# log(`Deploying function ${func.name} ( ${func['$id']} )`)#}
282
+
283
+ log('Deploying functions\n');
284
+
285
+ let successfullyDeployed = 0;
286
+
287
+ await Promise.all(functions.map(async (func) => {
288
+ const ignore = func.ignore ? 'appwrite.json' : '.gitignore';
289
+ let functionExists = false;
290
+
291
+ const bar = updatesBar.create(100, 0, { status: 'Deploying', function: func.name, id: func['$id'], ignore })
292
+ bar.update({ status: 'Getting' });
293
+ const updatingInterval = loaderInterval(bar, 8, 1, 80);
284
294
285
295
try {
286
296
response = await functionsGet({
287
297
functionId: func['$id'],
288
298
parseOutput: false,
289
299
});
290
-
300
+ functionExists = true;
291
301
if (response.runtime !== func.runtime) {
292
- throw new Error(`Runtime missmatch! (local=${func.runtime},remote=${response.runtime}) Please delete remote function or update your appwrite.json`);
302
+ bar.update({ status: 'Error', errorMessage: `Runtime missmatch! (local=${func.runtime},remote=${response.runtime}) Please delete remote function or update your appwrite.json` })
303
+ clearLoaderInterval(bar, updatingInterval);
304
+ return;
293
305
}
294
- a.increment({status:'Updating'})
306
+ bar.update({ status: 'Updating' });
307
+
295
308
response = await functionsUpdate({
296
309
functionId: func['$id'],
297
310
name: func.name,
@@ -307,9 +320,23 @@ const deployFunction = async ({ functionId, all, yes } = {}) => {
307
320
parseOutput: false
308
321
});
309
322
} catch (e) {
323
+
310
324
if (e.code == 404) {
311
- a.increment({status:'Creating'})
312
- log(`Function ${func.name} ( ${func['$id']} ) does not exist in the project. Creating ... `);
325
+ functionExists = false;
326
+ } else {
327
+ clearLoaderInterval(bar, updatingInterval)
328
+ bar.update({ status: 'Error', errorMessage: e.message ?? 'General error occurs please try again' });
329
+ return;
330
+ }
331
+ }
332
+
333
+ clearLoaderInterval(bar, updatingInterval)
334
+
335
+ if (!functionExists) {
336
+ bar.update({ status: 'Creating' })
337
+ const creatingInterval = loaderInterval(bar, 8, 1, 80);
338
+
339
+ try {
313
340
response = await functionsCreate({
314
341
functionId: func.$id || 'unique()',
315
342
name: func.name,
@@ -329,15 +356,20 @@ const deployFunction = async ({ functionId, all, yes } = {}) => {
329
356
localConfig.updateFunction(func['$id'], {
330
357
"$id": response['$id'],
331
358
});
332
-
333
359
func["$id"] = response['$id'];
334
- log(`Function ${func.name} created.`);
335
- } else {
336
- throw e;
360
+ bar.update({ status: 'Created' });
361
+
362
+ clearLoaderInterval(bar, creatingInterval);
363
+ } catch (e) {
364
+ clearLoaderInterval(bar, creatingInterval);
365
+
366
+ bar.update({ status: 'Error', errorMessage: e.message ?? 'General error occurs please try again' });
367
+ return;
337
368
}
338
369
}
339
370
340
371
if (func.variables) {
372
+ // TODO:
341
373
// Delete existing variables
342
374
343
375
const { total } = await functionsListVariables({
@@ -375,7 +407,8 @@ const deployFunction = async ({ functionId, all, yes } = {}) => {
375
407
376
408
let result = await awaitPools.wipeVariables(func['$id']);
377
409
if (!result) {
378
- throw new Error("Variable deletion timed out.");
410
+ bar.update({ status: 'Error', errorMessage: 'Variable deletion timed out' })
411
+ return;
379
412
}
380
413
381
414
// Deploy local variables
@@ -396,6 +429,8 @@ const deployFunction = async ({ functionId, all, yes } = {}) => {
396
429
func.entrypoint = answers.entrypoint;
397
430
localConfig.updateFunction(func['$id'], func);
398
431
}
432
+ bar.update({ status: 'Deploying' })
433
+ const deployingInterval = loaderInterval(bar, 8, 1, 80);
399
434
400
435
try {
401
436
response = await functionsCreateDeployment({
@@ -407,20 +442,23 @@ const deployFunction = async ({ functionId, all, yes } = {}) => {
407
442
parseOutput: false
408
443
})
409
444
410
- success(`Deployed ${func.name} ( ${func['$id']} )`);
445
+ bar.update({ status: 'Deployed' })
446
+ successfullyDeployed++;
411
447
412
448
} catch (e) {
413
449
switch (e.code) {
414
450
case 'ENOENT':
415
- error(`Function ${func.name} ( ${func['$id']} ) not found in the current directory. Skipping ...`);
451
+ bar.update({ status: 'Error', errorMessage: 'Not found in the current directory. Skipping...' })
416
452
break;
417
453
default:
418
- throw e;
454
+ bar.update({ status: 'Error', errorMessage: e.message ?? 'General error occurs please try again' })
419
455
}
420
456
}
421
- }
457
+ clearLoaderInterval(bar, deployingInterval);
458
+ }))
422
459
423
- success(`Deployed ${functions.length} functions`);
460
+ updatesBar.stop()
461
+ success(`Deployed ${successfullyDeployed} functions`);
424
462
}
425
463
426
464
const createAttribute = async (databaseId, collectionId, attribute) => {
0 commit comments