init_app.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const fs = require("fs");
  2. const { resolve } = require("path");
  3. const { $ } = require("./js/shell");
  4. const { displayError, displayMessage } = require("./js/displayMessage.js");
  5. const { processExit } = require("./js/processExit.js");
  6. const checkConda = async () => {
  7. try {
  8. displayMessage("Checking conda installation...");
  9. await $("conda --version");
  10. displayMessage("");
  11. } catch (error) {
  12. displayError(
  13. "Please install conda from https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html"
  14. );
  15. processExit(1);
  16. }
  17. };
  18. const updateConda = async () => {
  19. await $("conda update -y -n base -c defaults conda");
  20. };
  21. const FORCE_REINSTALL = process.env.FORCE_REINSTALL ? true : false;
  22. const DEBUG_ALWAYS_RETURN_UPDATED =
  23. FORCE_REINSTALL || process.env.DEBUG_ALWAYS_RETURN_UPDATED ? true : false;
  24. const getGitCommitHash = () =>
  25. fs.readFileSync("./.git/refs/heads/main", "utf8");
  26. const AppliedGitVersion = {
  27. file: resolve(__dirname, ".git_version"),
  28. get: () =>
  29. fs.existsSync(AppliedGitVersion.file)
  30. ? fs.readFileSync(AppliedGitVersion.file, "utf8")
  31. : null,
  32. save: () => fs.writeFileSync(AppliedGitVersion.file, getGitCommitHash()),
  33. };
  34. const syncRepo = async () => {
  35. if (!fs.existsSync(".git")) {
  36. displayMessage("Linking to tts-generation-webui repository");
  37. // this is a clone over the files from https://github.com/rsxdalv/tts-generation-webui
  38. await $("git init -b main");
  39. await $(
  40. "git remote add origin https://github.com/rsxdalv/tts-generation-webui"
  41. );
  42. await $("git fetch");
  43. await $("git reset --hard origin/main"); // Required when the versioned files existed in path before "git init" of this repo.
  44. await $("git branch --set-upstream-to=origin/main");
  45. return true;
  46. } else {
  47. displayMessage("Pulling updates from tts-generation-webui");
  48. try {
  49. await $("git pull");
  50. const newHash = getGitCommitHash();
  51. if (AppliedGitVersion.get() === newHash) {
  52. displayMessage("Current git version: " + newHash);
  53. displayMessage("No updates found, skipping...");
  54. return false || DEBUG_ALWAYS_RETURN_UPDATED;
  55. }
  56. return true;
  57. } catch (error) {
  58. displayMessage("There was a problem while pulling updates. Aborting...");
  59. throw error;
  60. }
  61. }
  62. };
  63. async function main() {
  64. // http
  65. // .createServer(function (req, res) {
  66. // // res.writeHead(200, { "Content-Type": "text/html" });
  67. // res.writeHead(200, { "Content-Type": "text/plain" });
  68. // process.stdout.on("data", (data) => {
  69. // res.write(data);
  70. // });
  71. // })
  72. // .listen(8080);
  73. const version = "0.0.6";
  74. displayMessage("\n\nStarting init app (version: " + version + ")...\n\n");
  75. if (process.env.DEBUG_ALWAYS_RETURN_UPDATED) {
  76. displayMessage("Forcing update");
  77. }
  78. try {
  79. await checkConda();
  80. // await updateConda();
  81. // check if there are any packages actually installed inside of conda
  82. const isUpdated = await syncRepo();
  83. if (!isUpdated) return;
  84. const {
  85. initializeApp,
  86. setupReactUI,
  87. repairTorch,
  88. } = require("./js/initializeApp.js");
  89. await initializeApp();
  90. await setupReactUI();
  91. await repairTorch();
  92. AppliedGitVersion.save();
  93. } catch (error) {
  94. displayError(error.message);
  95. processExit(1);
  96. }
  97. displayMessage("\n\nFinished init app.\n");
  98. processExit(0);
  99. }
  100. main();