distribution.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { resolve } from 'path'
  2. import { Distribution } from 'helios-distribution-types'
  3. import got from 'got'
  4. import { LoggerUtil } from 'common/logging/loggerutil'
  5. import { RestResponse, handleGotError, RestResponseStatus } from 'common/got/RestResponse'
  6. import { pathExists, readFile, writeFile } from 'fs-extra'
  7. export class DistributionAPI {
  8. private static readonly logger = LoggerUtil.getLogger('DistributionAPI')
  9. private readonly REMOTE_URL = 'http://mc.westeroscraft.com/WesterosCraftLauncher/distribution.json'
  10. private readonly DISTRO_FILE = 'distribution.json'
  11. private readonly DISTRO_FILE_DEV = 'distribution_dev.json'
  12. private readonly DEV_MODE = false // placeholder
  13. private distroPath: string
  14. private distroDevPath: string
  15. private rawDistribution!: Distribution
  16. constructor(
  17. private launcherDirectory: string
  18. ) {
  19. this.distroPath = resolve(launcherDirectory, this.DISTRO_FILE)
  20. this.distroDevPath = resolve(launcherDirectory, this.DISTRO_FILE_DEV)
  21. }
  22. public async testLoad(): Promise<Distribution> {
  23. await this.loadDistribution()
  24. return this.rawDistribution
  25. }
  26. protected async loadDistribution(): Promise<void> {
  27. let distro
  28. if(!this.DEV_MODE) {
  29. distro = (await this.pullRemote()).data
  30. if(distro == null) {
  31. distro = await this.pullLocal(false)
  32. } else {
  33. this.writeDistributionToDisk(distro)
  34. }
  35. } else {
  36. distro = await this.pullLocal(true)
  37. }
  38. if(distro == null) {
  39. // TODO Bubble this up nicer
  40. throw new Error('FATAL: Unable to load distribution from remote server or local disk.')
  41. }
  42. this.rawDistribution = distro
  43. }
  44. protected async pullRemote(): Promise<RestResponse<Distribution | null>> {
  45. try {
  46. const res = await got.get<Distribution>(this.REMOTE_URL, { responseType: 'json' })
  47. return {
  48. data: res.body,
  49. responseStatus: RestResponseStatus.SUCCESS
  50. }
  51. } catch(error) {
  52. return handleGotError('Pull Remote', error, DistributionAPI.logger, () => null)
  53. }
  54. }
  55. protected async writeDistributionToDisk(distribution: Distribution): Promise<void> {
  56. await writeFile(this.distroPath, distribution)
  57. }
  58. protected async pullLocal(dev: boolean): Promise<Distribution | null> {
  59. return await this.readDistributionFromFile(!dev ? this.distroPath : this.distroDevPath)
  60. }
  61. protected async readDistributionFromFile(path: string): Promise<Distribution | null> {
  62. if(await pathExists(path)) {
  63. const raw = await readFile(path, 'utf-8')
  64. try {
  65. return JSON.parse(raw)
  66. } catch(error) {
  67. DistributionAPI.logger.error(`Malformed distribution file at ${path}`)
  68. return null
  69. }
  70. } else {
  71. DistributionAPI.logger.error(`No distribution file found at ${path}!`)
  72. return null
  73. }
  74. }
  75. }