1
- import { createReadStream } from 'fs' ;
2
- import fetch from 'node-fetch' ;
1
+ import axios from 'axios' ;
2
+ import { readFile } from 'fs/promises' ;
3
+ import { Blob } from 'buffer' ;
3
4
import { extension } from '../config.mjs' ;
4
5
5
6
const webStoreId = process . env . CWS_CLIENT_ID ;
@@ -11,19 +12,18 @@ async function getToken() {
11
12
if ( _webStoreToken ) {
12
13
return _webStoreToken ;
13
14
}
14
- const resp = await fetch ( 'https://www.googleapis.com/oauth2/v4/token' , {
15
- method : 'POST' ,
15
+ const post = new URLSearchParams ( {
16
+ client_id : webStoreId ,
17
+ client_secret : webStoreSecret ,
18
+ refresh_token : webStoreToken ,
19
+ grant_type : 'refresh_token' ,
20
+ } ) ;
21
+ const resp = await axios . post ( 'https://www.googleapis.com/oauth2/v4/token' , post . toString ( ) , {
16
22
headers : {
17
23
'Content-Type' : 'application/x-www-form-urlencoded' ,
18
24
} ,
19
- body : new URLSearchParams ( {
20
- client_id : webStoreId ,
21
- client_secret : webStoreSecret ,
22
- refresh_token : webStoreToken ,
23
- grant_type : 'refresh_token' ,
24
- } ) . toString ( ) ,
25
25
} ) ;
26
- const res = await resp . json ( ) ;
26
+ const res = resp . data ;
27
27
if ( res . access_token ) {
28
28
_webStoreToken = res . access_token ;
29
29
return _webStoreToken ;
@@ -32,30 +32,28 @@ async function getToken() {
32
32
}
33
33
}
34
34
35
- async function upload ( readStream , token ) {
36
- const res = await fetch ( `https://www.googleapis.com/upload/chromewebstore/v1.1/items/ ${ extension . chrome . id } ` , {
37
- method : 'PUT' ,
35
+ async function upload ( content , token ) {
36
+ const blob = new Blob ( content ) ;
37
+ const res = await axios . put ( `https://www.googleapis.com/upload/chromewebstore/v1.1/items/ ${ extension . chrome . id } ` , blob , {
38
38
headers : {
39
39
Authorization : `Bearer ${ token } ` ,
40
40
'x-goog-api-version' : '2' ,
41
41
} ,
42
- body : readStream ,
43
42
} ) ;
44
43
45
- return res . json ( ) ;
44
+ return res . data ;
46
45
}
47
46
48
47
async function publish ( target = 'default' , token ) {
49
48
const url = `https://www.googleapis.com/chromewebstore/v1.1/items/${ extension . chrome . id } /publish?publishTarget=${ target } ` ;
50
- const res = await fetch ( url , {
51
- method : 'POST' ,
49
+ const res = await axios . post ( url , '' , {
52
50
headers : {
53
51
Authorization : `Bearer ${ token } ` ,
54
52
'x-goog-api-version' : '2' ,
55
53
} ,
56
54
} ) ;
57
55
58
- return res . json ( ) ;
56
+ return res . data ;
59
57
}
60
58
61
59
async function packCws ( zipPath ) {
@@ -69,9 +67,9 @@ async function packCws(zipPath) {
69
67
return Promise . reject ( new Error ( 'CWS_TOKEN not found' ) ) ;
70
68
}
71
69
72
- const distStream = createReadStream ( zipPath ) ;
70
+ const distContent = await readFile ( zipPath ) ;
73
71
const token = await getToken ( ) ;
74
- await upload ( distStream , token ) ;
72
+ await upload ( distContent , token ) ;
75
73
return publish ( 'default' , token ) ;
76
74
}
77
75
0 commit comments