diff options
author | mhatvan <markus_hatvan@aon.at> | 2021-08-05 08:58:28 +0200 |
---|---|---|
committer | Vijay Raghavan Aravamudhan <vraravam@users.noreply.github.com> | 2021-08-05 08:05:49 +0000 |
commit | 305c2c5ecb49a1349be2efb1ef557d61da9a64dc (patch) | |
tree | 19a05ed4ddfc1e5b5bb7fc3ecc071ad562da820f /src/api | |
parent | refactor: minor refactoring: solve name-clash of env vars vs vars in the program (diff) | |
download | ferdium-app-305c2c5ecb49a1349be2efb1ef557d61da9a64dc.tar.gz ferdium-app-305c2c5ecb49a1349be2efb1ef557d61da9a64dc.tar.zst ferdium-app-305c2c5ecb49a1349be2efb1ef557d61da9a64dc.zip |
refactor: general code improvements
- replace deprecated fs.exists with fs.existsSync
- replace console.log with debug
- replace hardcoded FERDI_VERSION in start.js with dynamic one from package.json
- correct JSDoc annotations in Handler.js
- simplify macOSPermissions.js
- updates to various eslint rules
- add FileReader to known globals
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/server/ServerApi.js | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index c60d64197..f91aeb23a 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js | |||
@@ -167,10 +167,10 @@ export default class ServerApi { | |||
167 | } | 167 | } |
168 | const data = await request.json(); | 168 | const data = await request.json(); |
169 | 169 | ||
170 | let services = await this._mapServiceModels(data); | 170 | const services = await this._mapServiceModels(data); |
171 | services = services.filter((service) => service !== null); | 171 | const filteredServices = services.filter(service => !!service); |
172 | debug('ServerApi::getServices resolves', services); | 172 | debug('ServerApi::getServices resolves', filteredServices); |
173 | return services; | 173 | return filteredServices; |
174 | } | 174 | } |
175 | 175 | ||
176 | async createService(recipeId, data) { | 176 | async createService(recipeId, data) { |
@@ -314,18 +314,19 @@ export default class ServerApi { | |||
314 | const paths = fs | 314 | const paths = fs |
315 | .readdirSync(recipesDirectory) | 315 | .readdirSync(recipesDirectory) |
316 | .filter( | 316 | .filter( |
317 | (file) => fs.statSync(path.join(recipesDirectory, file)).isDirectory() | 317 | file => |
318 | && file !== 'temp' | 318 | fs.statSync(path.join(recipesDirectory, file)).isDirectory() && |
319 | && file !== 'dev', | 319 | file !== 'temp' && |
320 | file !== 'dev', | ||
320 | ); | 321 | ); |
321 | 322 | ||
322 | this.recipes = paths | 323 | this.recipes = paths |
323 | .map((id) => { | 324 | .map(id => { |
324 | // eslint-disable-next-line | 325 | // eslint-disable-next-line |
325 | const Recipe = require(id)(RecipeModel); | 326 | const Recipe = require(id)(RecipeModel); |
326 | return new Recipe(loadRecipeConfig(id)); | 327 | return new Recipe(loadRecipeConfig(id)); |
327 | }) | 328 | }) |
328 | .filter((recipe) => recipe.id); | 329 | .filter(recipe => recipe.id); |
329 | 330 | ||
330 | this.recipes = this.recipes.concat(this._getDevRecipes()); | 331 | this.recipes = this.recipes.concat(this._getDevRecipes()); |
331 | 332 | ||
@@ -390,13 +391,11 @@ export default class ServerApi { | |||
390 | 391 | ||
391 | let archivePath; | 392 | let archivePath; |
392 | 393 | ||
393 | if (await fs.exists(internalRecipeFile)) { | 394 | if (fs.existsSync(internalRecipeFile)) { |
394 | console.log('[ServerApi::getRecipePackage] Using internal recipe file'); | 395 | debug('[ServerApi::getRecipePackage] Using internal recipe file'); |
395 | archivePath = internalRecipeFile; | 396 | archivePath = internalRecipeFile; |
396 | } else { | 397 | } else { |
397 | console.log( | 398 | debug('[ServerApi::getRecipePackage] Downloading recipe from server'); |
398 | '[ServerApi::getRecipePackage] Downloading recipe from server', | ||
399 | ); | ||
400 | archivePath = tempArchivePath; | 399 | archivePath = tempArchivePath; |
401 | 400 | ||
402 | const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; | 401 | const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; |
@@ -406,7 +405,7 @@ export default class ServerApi { | |||
406 | const buffer = await res.buffer(); | 405 | const buffer = await res.buffer(); |
407 | fs.writeFileSync(archivePath, buffer); | 406 | fs.writeFileSync(archivePath, buffer); |
408 | } | 407 | } |
409 | console.log(archivePath); | 408 | debug(archivePath); |
410 | 409 | ||
411 | await sleep(10); | 410 | await sleep(10); |
412 | 411 | ||
@@ -416,7 +415,7 @@ export default class ServerApi { | |||
416 | preservePaths: true, | 415 | preservePaths: true, |
417 | unlink: true, | 416 | unlink: true, |
418 | preserveOwner: false, | 417 | preserveOwner: false, |
419 | onwarn: (x) => console.log('warn', recipeId, x), | 418 | onwarn: x => debug('warn', recipeId, x), |
420 | }); | 419 | }); |
421 | 420 | ||
422 | await sleep(10); | 421 | await sleep(10); |
@@ -487,7 +486,7 @@ export default class ServerApi { | |||
487 | 486 | ||
488 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { | 487 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { |
489 | const services = await Promise.all( | 488 | const services = await Promise.all( |
490 | config.services.map(async (s) => { | 489 | config.services.map(async s => { |
491 | const service = s; | 490 | const service = s; |
492 | const request = await sendAuthRequest( | 491 | const request = await sendAuthRequest( |
493 | `${apiBase()}/recipes/${s.service}`, | 492 | `${apiBase()}/recipes/${s.service}`, |
@@ -514,11 +513,11 @@ export default class ServerApi { | |||
514 | 513 | ||
515 | // Helper | 514 | // Helper |
516 | async _mapServiceModels(services) { | 515 | async _mapServiceModels(services) { |
517 | const recipes = services.map((s) => s.recipeId); | 516 | const recipes = services.map(s => s.recipeId); |
518 | await this._bulkRecipeCheck(recipes); | 517 | await this._bulkRecipeCheck(recipes); |
519 | /* eslint-disable no-return-await */ | 518 | /* eslint-disable no-return-await */ |
520 | return Promise.all( | 519 | return Promise.all( |
521 | services.map(async (service) => await this._prepareServiceModel(service)), | 520 | services.map(async service => await this._prepareServiceModel(service)), |
522 | ); | 521 | ); |
523 | /* eslint-enable no-return-await */ | 522 | /* eslint-enable no-return-await */ |
524 | } | 523 | } |
@@ -526,7 +525,7 @@ export default class ServerApi { | |||
526 | async _prepareServiceModel(service) { | 525 | async _prepareServiceModel(service) { |
527 | let recipe; | 526 | let recipe; |
528 | try { | 527 | try { |
529 | recipe = this.recipes.find((r) => r.id === service.recipeId); | 528 | recipe = this.recipes.find(r => r.id === service.recipeId); |
530 | 529 | ||
531 | if (!recipe) { | 530 | if (!recipe) { |
532 | console.warn(`Recipe ${service.recipeId} not loaded`); | 531 | console.warn(`Recipe ${service.recipeId} not loaded`); |
@@ -547,8 +546,8 @@ export default class ServerApi { | |||
547 | ); | 546 | ); |
548 | 547 | ||
549 | return Promise.all( | 548 | return Promise.all( |
550 | recipes.map(async (recipeId) => { | 549 | recipes.map(async recipeId => { |
551 | let recipe = this.recipes.find((r) => r.id === recipeId); | 550 | let recipe = this.recipes.find(r => r.id === recipeId); |
552 | 551 | ||
553 | if (!recipe) { | 552 | if (!recipe) { |
554 | console.warn( | 553 | console.warn( |
@@ -560,7 +559,7 @@ export default class ServerApi { | |||
560 | debug('Rerun ServerAPI::getInstalledRecipes'); | 559 | debug('Rerun ServerAPI::getInstalledRecipes'); |
561 | await this.getInstalledRecipes(); | 560 | await this.getInstalledRecipes(); |
562 | 561 | ||
563 | recipe = this.recipes.find((r) => r.id === recipeId); | 562 | recipe = this.recipes.find(r => r.id === recipeId); |
564 | 563 | ||
565 | if (!recipe) { | 564 | if (!recipe) { |
566 | console.warn(`Could not load recipe ${recipeId}`); | 565 | console.warn(`Could not load recipe ${recipeId}`); |
@@ -570,12 +569,12 @@ export default class ServerApi { | |||
570 | 569 | ||
571 | return recipe; | 570 | return recipe; |
572 | }), | 571 | }), |
573 | ).catch((err) => console.error("Can't load recipe", err)); | 572 | ).catch(err => console.error("Can't load recipe", err)); |
574 | } | 573 | } |
575 | 574 | ||
576 | _mapRecipePreviewModel(recipes) { | 575 | _mapRecipePreviewModel(recipes) { |
577 | return recipes | 576 | return recipes |
578 | .map((recipe) => { | 577 | .map(recipe => { |
579 | try { | 578 | try { |
580 | return new RecipePreviewModel(recipe); | 579 | return new RecipePreviewModel(recipe); |
581 | } catch (e) { | 580 | } catch (e) { |
@@ -583,12 +582,12 @@ export default class ServerApi { | |||
583 | return null; | 582 | return null; |
584 | } | 583 | } |
585 | }) | 584 | }) |
586 | .filter((recipe) => recipe !== null); | 585 | .filter(recipe => recipe !== null); |
587 | } | 586 | } |
588 | 587 | ||
589 | _mapNewsModels(news) { | 588 | _mapNewsModels(news) { |
590 | return news | 589 | return news |
591 | .map((newsItem) => { | 590 | .map(newsItem => { |
592 | try { | 591 | try { |
593 | return new NewsModel(newsItem); | 592 | return new NewsModel(newsItem); |
594 | } catch (e) { | 593 | } catch (e) { |
@@ -596,12 +595,12 @@ export default class ServerApi { | |||
596 | return null; | 595 | return null; |
597 | } | 596 | } |
598 | }) | 597 | }) |
599 | .filter((newsItem) => newsItem !== null); | 598 | .filter(newsItem => newsItem !== null); |
600 | } | 599 | } |
601 | 600 | ||
602 | _mapOrderModels(orders) { | 601 | _mapOrderModels(orders) { |
603 | return orders | 602 | return orders |
604 | .map((orderItem) => { | 603 | .map(orderItem => { |
605 | try { | 604 | try { |
606 | return new OrderModel(orderItem); | 605 | return new OrderModel(orderItem); |
607 | } catch (e) { | 606 | } catch (e) { |
@@ -609,7 +608,7 @@ export default class ServerApi { | |||
609 | return null; | 608 | return null; |
610 | } | 609 | } |
611 | }) | 610 | }) |
612 | .filter((orderItem) => orderItem !== null); | 611 | .filter(orderItem => orderItem !== null); |
613 | } | 612 | } |
614 | 613 | ||
615 | _getDevRecipes() { | 614 | _getDevRecipes() { |
@@ -618,12 +617,13 @@ export default class ServerApi { | |||
618 | const paths = fs | 617 | const paths = fs |
619 | .readdirSync(recipesDirectory) | 618 | .readdirSync(recipesDirectory) |
620 | .filter( | 619 | .filter( |
621 | (file) => fs.statSync(path.join(recipesDirectory, file)).isDirectory() | 620 | file => |
622 | && file !== 'temp', | 621 | fs.statSync(path.join(recipesDirectory, file)).isDirectory() && |
622 | file !== 'temp', | ||
623 | ); | 623 | ); |
624 | 624 | ||
625 | const recipes = paths | 625 | const recipes = paths |
626 | .map((id) => { | 626 | .map(id => { |
627 | let Recipe; | 627 | let Recipe; |
628 | try { | 628 | try { |
629 | // eslint-disable-next-line | 629 | // eslint-disable-next-line |
@@ -635,8 +635,8 @@ export default class ServerApi { | |||
635 | 635 | ||
636 | return false; | 636 | return false; |
637 | }) | 637 | }) |
638 | .filter((recipe) => recipe.id) | 638 | .filter(recipe => recipe.id) |
639 | .map((data) => { | 639 | .map(data => { |
640 | const recipe = data; | 640 | const recipe = data; |
641 | 641 | ||
642 | recipe.icons = { | 642 | recipe.icons = { |