Source: Utils/Functions/Net/GithubReleases/script.js

  1. const Downloader = include("utils.functions.net.download");
  2. const Resource = include("utils.functions.net.resource");
  3. const { createTempDir, remove, cat } = include("utils.functions.filesystem.files");
  4. /**
  5. * A dedicated class used for accessing and downloading releases hosted on GitHub
  6. */
  7. module.GitHubReleaseDownloader = class GitHubReleaseDownloader {
  8. /**
  9. * Constructor
  10. *
  11. * @param {string} repositoryOwner The owner of the GitHub repository
  12. * @param {string} repositoryName The name of the GitHub repository
  13. * @param {SetupWizard} wizard The setup wizard
  14. */
  15. constructor(repositoryOwner, repositoryName, wizard) {
  16. this.repositoryOwner = repositoryOwner;
  17. this.repositoryName = repositoryName;
  18. this.wizard = wizard;
  19. this.versions = this.fetchReleases();
  20. }
  21. /**
  22. * Fetches the GitHub releases
  23. *
  24. * @returns {object[]} The downloaded versions json array
  25. */
  26. fetchReleases() {
  27. const tmpDir = createTempDir();
  28. const releasesFile = new Downloader()
  29. .wizard(this.wizard)
  30. .url(`https://api.github.com/repos/${this.repositoryOwner}/${this.repositoryName}/releases`)
  31. .message(tr("Fetching versions list..."))
  32. .to(tmpDir + "/releases.json")
  33. .get();
  34. const versions = JSON.parse(cat(releasesFile));
  35. remove(tmpDir);
  36. return versions;
  37. }
  38. /**
  39. * Fetches the tags belonging to the releases for the given GitHub repository
  40. *
  41. * @returns {string[]} An array containing the tags for the GitHub releases
  42. */
  43. getReleases() {
  44. return this.versions.map(version => version.tag_name);
  45. }
  46. /**
  47. * Fetches the tag belonging to the latest release for the given GitHub repository
  48. *
  49. * @returns {string} The tag belonging to the latest release
  50. */
  51. getLatestRelease() {
  52. return this.versions[0].tag_name;
  53. }
  54. /**
  55. * Downloads all assets that belong to the release with the given tag that match a given regex
  56. *
  57. * @param {string} tag The tag name of the release
  58. * @param {RegExp} assetRegex An optional regex describing the desired asset(s)
  59. * @returns {string[]} An array of paths leading to the downloaded files
  60. */
  61. download(tag, assetRegex) {
  62. const version = this.versions.find(version => version.tag_name === tag);
  63. return version.assets
  64. .filter(asset => !assetRegex || asset.name.match(assetRegex))
  65. .map(asset =>
  66. new Resource()
  67. .wizard(this.wizard)
  68. .url(asset.browser_download_url)
  69. .name(asset.name)
  70. .get()
  71. );
  72. }
  73. };