@@ -36,22 +36,22 @@ class AcodePlugin {
3636 } ) ;
3737
3838 this . token = await this . #cacheFile. readFile ( 'utf8' ) ;
39- if ( this . token ) {
40- await this . initFs ( ) ;
41- }
39+ await this . initFs ( ) ;
4240 }
4341
4442 async initFs ( ) {
4543 if ( this . #fsInitialized) return ;
46- if ( ! this . token ) {
47- await this . updateToken ( ) ;
48- }
49-
5044 githubFs . remove ( ) ;
51- githubFs ( this . token ) ;
45+ githubFs ( this . getToken . bind ( this ) ) ;
5246 this . #fsInitialized = true ;
5347 }
5448
49+ async getToken ( ) {
50+ if ( this . token ) return this . token ;
51+ await this . updateToken ( ) ;
52+ return this . token ;
53+ }
54+
5555 async destroy ( ) {
5656 githubFs . remove ( ) ;
5757 this . commands . forEach ( command => {
@@ -61,6 +61,7 @@ class AcodePlugin {
6161
6262 async openRepo ( ) {
6363 await this . initFs ( ) ;
64+ this . token = await this . getToken ( ) ;
6465 pallete (
6566 this . listRepositories . bind ( this ) ,
6667 this . selectBranch . bind ( this ) ,
@@ -72,7 +73,8 @@ class AcodePlugin {
7273 const [ user , repoName ] = repo . split ( '/' ) ;
7374 pallete (
7475 this . listBranches . bind ( this , user , repoName ) ,
75- ( branch ) => this . openRepoAsFolder ( user , repoName , branch ) ,
76+ ( branch ) => this . openRepoAsFolder ( user , repoName , branch )
77+ . catch ( helpers . error ) ,
7678 'Type to search branch' ,
7779 ) ;
7880 }
@@ -89,7 +91,7 @@ class AcodePlugin {
8991 const confirmation = await confirm ( strings [ 'warning' ] , 'Delete this gist?' ) ;
9092 if ( ! confirmation ) return ;
9193
92- const gh = new GitHub ( { token : this . token } ) ;
94+ const gh = await this . #GitHub ( ) ;
9395 const gistApi = gh . getGist ( gist ) ;
9496 await gistApi . delete ( ) ;
9597 this . #gists = this . #gists. filter ( g => g . id !== gist ) ;
@@ -117,7 +119,7 @@ class AcodePlugin {
117119 const confirmation = await confirm ( strings [ 'warning' ] , 'Delete this file?' ) ;
118120 if ( ! confirmation ) return ;
119121
120- const gh = new GitHub ( { token : this . token } ) ;
122+ const gh = await this . #GitHub ( ) ;
121123 const gistApi = gh . getGist ( gist ) ;
122124 await gistApi . update ( {
123125 files : {
@@ -129,8 +131,8 @@ class AcodePlugin {
129131 window . toast ( 'File deleted' ) ;
130132 }
131133
132- async openRepoAsFolder ( user , repo , branch ) {
133- const cachedRepo = this . #getRepo( user , repo ) ;
134+ async openRepoAsFolder ( user , repoName , branch ) {
135+ const cachedRepo = this . #getRepo( user , repoName ) ;
134136 if ( branch === this . NEW ) {
135137 const { from, branch : newBranch } = await multiPrompt (
136138 strings [ 'create new branch' ] ,
@@ -150,8 +152,8 @@ class AcodePlugin {
150152 } ] ,
151153 ) ;
152154 branch = newBranch ;
153- const gh = new GitHub ( { token : this . token } ) ;
154- const repo = gh . getRepo ( user , repo ) ;
155+ const gh = await this . #GitHub ( ) ;
156+ const repo = gh . getRepo ( user , repoName ) ;
155157 await repo . createBranch ( from , newBranch ) ;
156158 }
157159
@@ -160,14 +162,15 @@ class AcodePlugin {
160162 return ;
161163 }
162164
163- const url = githubFs . constructUrl ( 'repo' , user , repo , '/' , branch ) ;
165+ const url = githubFs . constructUrl ( 'repo' , user , repoName , '/' , branch ) ;
164166 openFolder ( url , {
165- name : `${ user } /${ repo } /${ branch } ` ,
167+ name : `${ user } /${ repoName } /${ branch } ` ,
166168 } ) ;
167169 }
168170
169171 async openGist ( ) {
170172 await this . initFs ( ) ;
173+ this . token = await this . getToken ( ) ;
171174
172175 pallete (
173176 this . listGists . bind ( this ) ,
@@ -215,7 +218,7 @@ class AcodePlugin {
215218 } ) ;
216219
217220 helpers . showTitleLoader ( ) ;
218- const gh = new GitHub ( { token : this . token } ) ;
221+ const gh = await this . #GitHub ( ) ;
219222 const gist = gh . getGist ( ) ;
220223 const { data } = await gist . create ( {
221224 description,
@@ -244,7 +247,7 @@ class AcodePlugin {
244247 window . toast ( strings [ 'cancelled' ] ) ;
245248 }
246249 helpers . showTitleLoader ( ) ;
247- const gh = new GitHub ( { token : this . token } ) ;
250+ const gh = await this . #GitHub ( ) ;
248251 await gh . getGist ( gist ) . update ( {
249252 files : {
250253 [ filename ] : {
@@ -298,7 +301,7 @@ class AcodePlugin {
298301 if ( this . #repos. length ) {
299302 return [ ...this . #repos] ;
300303 }
301- const gh = new GitHub ( { token : this . token } ) ;
304+ const gh = await this . #GitHub ( ) ;
302305 const user = gh . getUser ( ) ;
303306 const repos = await user . listRepos ( ) ;
304307 const { data } = repos ;
@@ -323,7 +326,7 @@ class AcodePlugin {
323326 if ( cachedRepo && cachedRepo . branches ) {
324327 list = [ ...cachedRepo . branches ] ;
325328 } else {
326- const gh = new GitHub ( { token : this . token } ) ;
329+ const gh = await this . #GitHub ( ) ;
327330 const repo = gh . getRepo ( user , repoName ) ;
328331 const branches = await repo . listBranches ( ) ;
329332 const { data } = branches ;
@@ -358,7 +361,7 @@ class AcodePlugin {
358361 if ( this . #gists. length ) {
359362 list = [ ...this . #gists] ;
360363 } else {
361- const gh = new GitHub ( { token : this . token } ) ;
364+ const gh = await this . #GitHub ( ) ;
362365 const user = gh . getUser ( ) ;
363366 const gists = await user . listGists ( ) ;
364367 const { data } = gists ;
@@ -384,7 +387,7 @@ class AcodePlugin {
384387 if ( cachedGist && cachedGist . files ) {
385388 list = [ ...cachedGist . files ] ;
386389 } else {
387- const gh = new GitHub ( { token : this . token } ) ;
390+ const gh = await this . #GitHub ( ) ;
388391 const gist = gh . getGist ( gistId ) ;
389392 const { data : { files, owner } } = await gist . read ( ) ;
390393
@@ -433,6 +436,10 @@ class AcodePlugin {
433436 return this . #gists. find ( gist => gist . value === gistId ) ;
434437 }
435438
439+ async #GitHub( ) {
440+ return new GitHub ( { token : await this . getToken ( ) } ) ;
441+ }
442+
436443 get commands ( ) {
437444 return [
438445 {
0 commit comments