diff options
author | Vijay Raghavan Aravamudhan <vraravam@users.noreply.github.com> | 2021-08-08 00:01:37 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-08 05:31:37 +0530 |
commit | 97d51a7763b14c92ee71ff9a012311dd9498d803 (patch) | |
tree | bd36005031ecb1148f27aa541e7a92a5e3aa4c0c /src/api | |
parent | 5.6.1-nightly.17 [skip ci] (diff) | |
download | ferdium-app-97d51a7763b14c92ee71ff9a012311dd9498d803.tar.gz ferdium-app-97d51a7763b14c92ee71ff9a012311dd9498d803.tar.zst ferdium-app-97d51a7763b14c92ee71ff9a012311dd9498d803.zip |
refactor: path-references refactoring and using 'import' instead of 'require' (#1752)
* refactor references to 'userData' and 'appData' directories to move hardcoding into single location
* convert to es6 for lower memory usage as per https://codesource.io/the-difference-between-import-and-require-in-javascript/
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/server/ServerApi.js | 50 |
1 files changed, 21 insertions, 29 deletions
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index f91aeb23a..2111eb80b 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js | |||
@@ -1,6 +1,6 @@ | |||
1 | import path from 'path'; | 1 | import { join } from 'path'; |
2 | import tar from 'tar'; | 2 | import tar from 'tar'; |
3 | import fs from 'fs-extra'; | 3 | import { readdirSync, statSync, writeFileSync, copySync, ensureDirSync, pathExistsSync, readJsonSync, removeSync } from 'fs-extra'; |
4 | import { app, require as remoteRequire } from '@electron/remote'; | 4 | import { app, require as remoteRequire } from '@electron/remote'; |
5 | 5 | ||
6 | import ServiceModel from '../../models/Service'; | 6 | import ServiceModel from '../../models/Service'; |
@@ -13,7 +13,7 @@ import OrderModel from '../../models/Order'; | |||
13 | import { sleep } from '../../helpers/async-helpers'; | 13 | import { sleep } from '../../helpers/async-helpers'; |
14 | 14 | ||
15 | import { SERVER_NOT_LOADED } from '../../config'; | 15 | import { SERVER_NOT_LOADED } from '../../config'; |
16 | import { osArch, osPlatform, RECIPES_PATH } from '../../environment'; | 16 | import { osArch, osPlatform, asarRecipesPath, userDataRecipesPath, userDataPath } from '../../environment'; |
17 | import apiBase from '../apiBase'; | 17 | import apiBase from '../apiBase'; |
18 | import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; | 18 | import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; |
19 | 19 | ||
@@ -311,11 +311,10 @@ export default class ServerApi { | |||
311 | // Recipes | 311 | // Recipes |
312 | async getInstalledRecipes() { | 312 | async getInstalledRecipes() { |
313 | const recipesDirectory = getRecipeDirectory(); | 313 | const recipesDirectory = getRecipeDirectory(); |
314 | const paths = fs | 314 | const paths = readdirSync(recipesDirectory) |
315 | .readdirSync(recipesDirectory) | ||
316 | .filter( | 315 | .filter( |
317 | file => | 316 | file => |
318 | fs.statSync(path.join(recipesDirectory, file)).isDirectory() && | 317 | statSync(join(recipesDirectory, file)).isDirectory() && |
319 | file !== 'temp' && | 318 | file !== 'temp' && |
320 | file !== 'dev', | 319 | file !== 'dev', |
321 | ); | 320 | ); |
@@ -381,17 +380,17 @@ export default class ServerApi { | |||
381 | 380 | ||
382 | async getRecipePackage(recipeId) { | 381 | async getRecipePackage(recipeId) { |
383 | try { | 382 | try { |
384 | const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); | 383 | const recipesDirectory = userDataRecipesPath(); |
385 | const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); | 384 | const recipeTempDirectory = join(recipesDirectory, 'temp', recipeId); |
386 | const tempArchivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); | 385 | const tempArchivePath = join(recipeTempDirectory, 'recipe.tar.gz'); |
387 | 386 | ||
388 | const internalRecipeFile = path.join(RECIPES_PATH, `${recipeId}.tar.gz`); | 387 | const internalRecipeFile = asarRecipesPath(`${recipeId}.tar.gz`); |
389 | 388 | ||
390 | fs.ensureDirSync(recipeTempDirectory); | 389 | ensureDirSync(recipeTempDirectory); |
391 | 390 | ||
392 | let archivePath; | 391 | let archivePath; |
393 | 392 | ||
394 | if (fs.existsSync(internalRecipeFile)) { | 393 | if (pathExistsSync(internalRecipeFile)) { |
395 | debug('[ServerApi::getRecipePackage] Using internal recipe file'); | 394 | debug('[ServerApi::getRecipePackage] Using internal recipe file'); |
396 | archivePath = internalRecipeFile; | 395 | archivePath = internalRecipeFile; |
397 | } else { | 396 | } else { |
@@ -403,7 +402,7 @@ export default class ServerApi { | |||
403 | const res = await fetch(packageUrl); | 402 | const res = await fetch(packageUrl); |
404 | debug('Recipe downloaded', recipeId); | 403 | debug('Recipe downloaded', recipeId); |
405 | const buffer = await res.buffer(); | 404 | const buffer = await res.buffer(); |
406 | fs.writeFileSync(archivePath, buffer); | 405 | writeFileSync(archivePath, buffer); |
407 | } | 406 | } |
408 | debug(archivePath); | 407 | debug(archivePath); |
409 | 408 | ||
@@ -420,13 +419,11 @@ export default class ServerApi { | |||
420 | 419 | ||
421 | await sleep(10); | 420 | await sleep(10); |
422 | 421 | ||
423 | const { id } = fs.readJsonSync( | 422 | const { id } = readJsonSync(join(recipeTempDirectory, 'package.json')); |
424 | path.join(recipeTempDirectory, 'package.json'), | 423 | const recipeDirectory = join(recipesDirectory, id); |
425 | ); | 424 | copySync(recipeTempDirectory, recipeDirectory); |
426 | const recipeDirectory = path.join(recipesDirectory, id); | 425 | removeSync(recipeTempDirectory); |
427 | fs.copySync(recipeTempDirectory, recipeDirectory); | 426 | removeSync(join(recipesDirectory, recipeId, 'recipe.tar.gz')); |
428 | fs.remove(recipeTempDirectory); | ||
429 | fs.remove(path.join(recipesDirectory, recipeId, 'recipe.tar.gz')); | ||
430 | 427 | ||
431 | return id; | 428 | return id; |
432 | } catch (err) { | 429 | } catch (err) { |
@@ -475,14 +472,10 @@ export default class ServerApi { | |||
475 | } | 472 | } |
476 | 473 | ||
477 | async getLegacyServices() { | 474 | async getLegacyServices() { |
478 | const file = path.join( | 475 | const file = userDataPath('settings', 'services.json'); |
479 | app.getPath('userData'), | ||
480 | 'settings', | ||
481 | 'services.json', | ||
482 | ); | ||
483 | 476 | ||
484 | try { | 477 | try { |
485 | const config = fs.readJsonSync(file); | 478 | const config = readJsonSync(file); |
486 | 479 | ||
487 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { | 480 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { |
488 | const services = await Promise.all( | 481 | const services = await Promise.all( |
@@ -614,11 +607,10 @@ export default class ServerApi { | |||
614 | _getDevRecipes() { | 607 | _getDevRecipes() { |
615 | const recipesDirectory = getDevRecipeDirectory(); | 608 | const recipesDirectory = getDevRecipeDirectory(); |
616 | try { | 609 | try { |
617 | const paths = fs | 610 | const paths = readdirSync(recipesDirectory) |
618 | .readdirSync(recipesDirectory) | ||
619 | .filter( | 611 | .filter( |
620 | file => | 612 | file => |
621 | fs.statSync(path.join(recipesDirectory, file)).isDirectory() && | 613 | statSync(join(recipesDirectory, file)).isDirectory() && |
622 | file !== 'temp', | 614 | file !== 'temp', |
623 | ); | 615 | ); |
624 | 616 | ||