Home Reference Source

cables_dev/cables_electron/src/cables.js

  1. // eslint-disable-next-line import/no-extraneous-dependencies
  2. import { app } from "electron";
  3. import { utilProvider, Cables } from "cables-shared-api";
  4. import path from "path";
  5. import fs from "fs";
  6. import mkdirp from "mkdirp";
  7. import { fileURLToPath } from "url";
  8. import settings from "./electron/electron_settings.js";
  9. class CablesElectron extends Cables
  10. {
  11. constructor(provider, dirName, writableDirName, configLocation)
  12. {
  13. super(provider, dirName, writableDirName, configLocation);
  14. this._config.isPackaged = app.isPackaged;
  15. this._config.uiIndexHtml = path.join(this.getUiDistPath(), "index.html");
  16. if (writableDirName && !fs.existsSync(path.join(writableDirName, "/ops"))) mkdirp.sync(path.join(writableDirName, "/ops"));
  17. }
  18. isElectron()
  19. {
  20. return true;
  21. }
  22. getCommunityUrl()
  23. {
  24. return this._config.communityUrl;
  25. }
  26. isPackaged()
  27. {
  28. return this._config.isPackaged;
  29. }
  30. sendErrorReports()
  31. {
  32. return this._config.isPackaged || this._config.forceSendErrorReports;
  33. }
  34. inPackage(filePath)
  35. {
  36. if (!filePath) return false;
  37. if (!this.isPackaged()) return false;
  38. return filePath.startsWith(this._cables.getOpsPath());
  39. }
  40. getPackagedPath()
  41. {
  42. return this.getOpsPath();
  43. }
  44. getDistPath()
  45. {
  46. if (this._config.path.standaloneDist)
  47. {
  48. return path.join(this._dirname, this._config.path.standaloneDist);
  49. }
  50. return path.join(this.getApiPath(), "dist");
  51. }
  52. getAssetPath()
  53. {
  54. const currentProjectDir = settings.getCurrentProjectDir();
  55. let assetPath = "";
  56. if (!currentProjectDir)
  57. {
  58. assetPath = path.join(this._writeableDirName, "assets/");
  59. }
  60. else
  61. {
  62. assetPath = path.join(currentProjectDir);
  63. }
  64. if (!fs.existsSync(assetPath)) mkdirp.sync(assetPath);
  65. return assetPath;
  66. }
  67. getAssetLibraryPath()
  68. {
  69. if (!this._config.path.assets) path.join(this.getPublicPath(), "assets/library/");
  70. return this._config.path.assets.startsWith("/") ? this._config.path.assets : path.join(this._dirname, this._config.path.assets, "library/");
  71. }
  72. getExportAssetTargetPath()
  73. {
  74. return "";
  75. }
  76. getGenPath()
  77. {
  78. const genPath = path.join(this._writeableDirName, "gen/");
  79. if (!fs.existsSync(genPath)) mkdirp.sync(genPath);
  80. return genPath;
  81. }
  82. getOpDocsCachePath()
  83. {
  84. const cachePath = path.join(this.getGenPath(), "opdocs_collections/");
  85. if (!fs.existsSync(cachePath)) mkdirp.sync(cachePath);
  86. return cachePath;
  87. }
  88. getUserOpsPath()
  89. {
  90. if (!settings.getCurrentProjectDir()) return path.join(this.getOpsPath(), "/", this.USER_OPS_SUBDIR);
  91. return path.join(settings.getCurrentProjectDir(), "/ops/", this.USER_OPS_SUBDIR);
  92. }
  93. getTeamOpsPath()
  94. {
  95. if (!settings.getCurrentProjectDir()) return path.join(this.getOpsPath(), "/", this.TEAM_OPS_SUBDIR);
  96. return path.join(settings.getCurrentProjectDir(), "/ops/", this.TEAM_OPS_SUBDIR);
  97. }
  98. getPatchOpsPath()
  99. {
  100. if (!settings.getCurrentProjectDir()) return path.join(this.getOpsPath(), "/", this.PATCH_OPS_SUBDIR);
  101. return path.join(settings.getCurrentProjectDir(), "/ops/", this.PATCH_OPS_SUBDIR);
  102. }
  103. getProjectOpsPath(create = false)
  104. {
  105. if (!settings.getCurrentProjectDir()) return null;
  106. const opsPath = path.join(settings.getCurrentProjectDir(), "/ops/");
  107. if (create && !fs.existsSync(opsPath)) mkdirp.sync(opsPath);
  108. return opsPath;
  109. }
  110. getOsOpsDir()
  111. {
  112. return path.join(app.getPath("userData"), "ops/");
  113. }
  114. _createDirectories()
  115. {
  116. if (!fs.existsSync(this.getGenPath())) mkdirp.sync(this.getGenPath());
  117. if (!fs.existsSync(this.getOpDocsCachePath())) mkdirp.sync(this.getOpDocsCachePath());
  118. if (!fs.existsSync(this.getOpDocsFile()))
  119. {
  120. if (!fs.existsSync(this.getOpDocsFile())) fs.writeFileSync(this.getOpDocsFile(), JSON.stringify({ "generated": Date.now(), "opDocs": [] }));
  121. }
  122. if (!fs.existsSync(this.getOpLookupFile())) fs.writeFileSync(this.getOpLookupFile(), JSON.stringify({ "names": {}, "ids": {} }));
  123. }
  124. }
  125. const metaUrl = new URL(".", import.meta.url);
  126. const __dirname = fileURLToPath(metaUrl.href);
  127. const customConfig = process.env.npm_config_apiconfig;
  128. let configLocation = path.resolve(__dirname, "..", "cables.json");
  129. if (customConfig)
  130. {
  131. configLocation = "../cables_env_" + process.env.npm_config_apiconfig + ".json";
  132. configLocation = path.join(__dirname, configLocation);
  133. if (!fs.existsSync(configLocation))
  134. {
  135. console.error("custom config set to ", configLocation, "but file does not exists, do you need to run `npm run build`?");
  136. process.exit(1);
  137. }
  138. }
  139. export default new CablesElectron(utilProvider, __dirname, app.getPath("userData"), configLocation);