Home Reference Source

cables_dev/cables_electron/src/cables.js

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