11import Octokit from '@octokit/rest'
2- import { getNow } from './helper'
2+ import { getNow , pathJoin } from './helper'
33import { PluginConfig , ImgType } from './interface'
44import { join } from 'path'
55import urlJoin from 'url-join'
66import { ImgInfo } from 'picgo/dist/utils/interfaces'
7- import slash from 'slash'
8- // export default octokit
97
10- function pathJoin ( ...arg ) {
11- return slash ( join . apply ( null , arg ) )
12- }
13-
14- class Octo {
8+ export class Octo {
159 owner : string = ''
1610 repo : string = ''
1711 branch : string = ''
1812 path : string = ''
1913 token : string = ''
2014 customUrl : string = ''
21- octokit = new Octokit ( )
15+ octokit : Octokit = null
2216 constructor ( {
2317 repo,
2418 branch,
@@ -34,11 +28,8 @@ class Octo {
3428 this . path = path
3529 this . token = token
3630 this . customUrl = customUrl
37- }
38- authenticate ( ) {
39- this . octokit . authenticate ( {
40- type : 'token' ,
41- token : this . token
31+ this . octokit = new Octokit ( {
32+ auth : token ? `token ${ token } ` : undefined
4233 } )
4334 }
4435
@@ -66,9 +57,9 @@ class Octo {
6657 return { sha, tree }
6758 }
6859 async getDataJson ( ) : Promise < {
69- lastSync : string ;
70- data : any [ ] ;
71- sha ?: string ;
60+ lastSync : string
61+ data : any [ ]
62+ sha ?: string
7263 } > {
7364 const { owner, repo } = this
7465 const defaultRet = {
@@ -107,7 +98,6 @@ class Octo {
10798 }
10899 createDataJson ( data ) {
109100 const { owner, repo, branch, path } = this
110- console . log ( pathJoin ( path , 'data.json' ) )
111101 return this . octokit . repos . createFile ( {
112102 owner,
113103 repo,
@@ -118,6 +108,7 @@ class Octo {
118108 } )
119109 }
120110 async upload ( img : ImgInfo ) {
111+ /* istanbul ignore next */
121112 const { owner, repo, branch, path = '' } = this
122113 const { fileName } = img
123114 const d = await this . octokit . repos . createFile ( {
@@ -134,6 +125,7 @@ class Octo {
134125 sha : d . data . content . sha
135126 }
136127 }
128+ /* istanbul ignore next */
137129 throw d
138130 }
139131 removeFile ( img : ImgType ) {
@@ -152,13 +144,27 @@ class Octo {
152144 if ( customUrl ) {
153145 return urlJoin ( customUrl , path , fileName )
154146 }
155- return urlJoin ( `https://raw.githubusercontent.com/` , owner , repo , branch , path , fileName )
147+ return urlJoin (
148+ `https://raw.githubusercontent.com/` ,
149+ owner ,
150+ repo ,
151+ branch ,
152+ path ,
153+ fileName
154+ )
156155 }
157156}
158157
159158let ins : Octo = null
160159
161160export function getIns ( config : PluginConfig ) : Octo {
162161 if ( ins ) return ins
163- return new Octo ( config )
162+ ins = new Octo ( config )
163+ return ins
164+ }
165+
166+ /* istanbul ignore next */
167+ export function clearIns ( ) {
168+ // just for test
169+ ins = null
164170}
0 commit comments