1- const path = require ( 'path' ) ;
2- const fs = require ( 'fs-extra' ) ;
1+ import path from 'path' ;
2+ import fs from 'fs-extra' ;
33/**
44 * A class responsible for caching and restoring cached GitHub responses
55 * results.
@@ -13,7 +13,7 @@ const fs = require('fs-extra');
1313 * The class stores data in user's home data folder in
1414 * `api-console/cache/github-http-cache.json` folder.
1515 */
16- class GithubCache {
16+ export class GithubCache {
1717 /**
1818 * @param {Object } logger A logger object to use to log messages.
1919 */
@@ -54,35 +54,33 @@ class GithubCache {
5454 * Loads cache data into memory and returns the data.
5555 * @return {Promise } Promise resolved to cache file contents.
5656 */
57- loadCache ( ) {
57+ async loadCache ( ) {
5858 if ( this . _data ) {
59- return Promise . resolve ( this . _data ) ;
59+ return this . _data ;
6060 }
6161 if ( this . logger ) {
6262 this . logger . debug ( '[GithubCache] Loading cache from ' + this . cacheLocation ) ;
6363 }
64- return fs . ensureFile ( this . cacheLocation )
65- . then ( ( ) => fs . readJson ( this . cacheLocation , { throws : false } ) )
66- . then ( ( data ) => {
67- if ( data === null ) {
68- data = undefined ;
69- if ( this . logger ) {
70- this . logger . debug ( '[GithubCache] Cache file does not exists.' ) ;
71- }
64+ await fs . ensureFile ( this . cacheLocation ) ;
65+ let data = await fs . readJson ( this . cacheLocation , { throws : false } ) ;
66+ if ( data === null ) {
67+ data = undefined ;
68+ if ( this . logger ) {
69+ this . logger . debug ( '[GithubCache] Cache file does not exists.' ) ;
7270 }
73- this . _data = data ;
74- return data ;
75- } ) ;
71+ }
72+ this . _data = data ;
73+ return data ;
7674 }
7775 /**
7876 * Restores latest `etag`
7977 *
8078 * @param {String } url The full URL used to cache the results.
8179 * @return {Promise }
8280 */
83- lastEtag ( url ) {
84- return this . loadCache ( )
85- . then ( ( data ) => this . _findEtag ( data , url ) ) ;
81+ async lastEtag ( url ) {
82+ const data = await this . loadCache ( ) ;
83+ return await this . _findEtag ( data , url ) ;
8684 }
8785 /**
8886 * Finds etag in cached data.
@@ -111,9 +109,9 @@ class GithubCache {
111109 * @param {String } url The full URL used to cache the results.
112110 * @return {Promise }
113111 */
114- getCachedResult ( url ) {
115- return this . loadCache ( )
116- . then ( ( data ) => this . _findResponse ( data , url ) ) ;
112+ async getCachedResult ( url ) {
113+ const data = await this . loadCache ( ) ;
114+ return await this . _findResponse ( data , url ) ;
117115 }
118116 /**
119117 * Finds response in cached data.
@@ -148,20 +146,20 @@ class GithubCache {
148146 * @param {Object } response Response data to cache
149147 * @return {Promise }
150148 */
151- storeResponse ( url , etag , response ) {
152- return this . loadCache ( )
153- . then ( ( data ) => this . _addCacheEntry ( data , url , etag , response ) ) ;
149+ async storeResponse ( url , etag , response ) {
150+ const data = await this . loadCache ( ) ;
151+ return await this . _addCacheEntry ( data , url , etag , response ) ;
154152 }
155153 /**
156154 * Appends response data to the cache.
157155 *
158- * @param {Obect } data Responses cache object
156+ * @param {Object } data Responses cache object
159157 * @param {String } url Request URL
160158 * @param {String } etag Response etag
161159 * @param {Object } response GitHub response.
162160 * @return {Promise }
163161 */
164- _addCacheEntry ( data , url , etag , response ) {
162+ async _addCacheEntry ( data , url , etag , response ) {
165163 if ( this . logger ) {
166164 this . logger . debug ( '[GithubCache] Adding cache entry for ' + url ) ;
167165 }
@@ -173,18 +171,17 @@ class GithubCache {
173171 response
174172 } ;
175173 this . _data = data ;
176- return this . _store ( ) ;
174+ await this . _store ( ) ;
177175 }
178176 /**
179177 * Stores current data to the cache file.
180178 * @return {Promise }
181179 */
182- _store ( ) {
180+ async _store ( ) {
183181 const data = this . _data || { } ;
184182 if ( this . logger ) {
185183 this . logger . debug ( '[GithubCache] Storing cache data to ' + this . cacheLocation ) ;
186184 }
187- return fs . writeJson ( this . cacheLocation , data ) ;
185+ await fs . writeJson ( this . cacheLocation , data ) ;
188186 }
189187}
190- exports . GithubCache = GithubCache ;
0 commit comments