Skip to content

Commit

Permalink
scripts: remove fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
sylingd committed Jul 10, 2024
1 parent 1be3456 commit 73879e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion scripts/get-snapshot-version.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { readFile, mkdir, writeFile } from 'fs/promises';
import { mkdir, writeFile } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

Expand Down
40 changes: 19 additions & 21 deletions scripts/pack-utils/cws.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createReadStream } from 'fs';
import fetch from 'node-fetch';
import axios from 'axios';
import { readFile } from 'fs/promises';
import { Blob } from 'buffer';
import { extension } from '../config.mjs';

const webStoreId = process.env.CWS_CLIENT_ID;
Expand All @@ -11,19 +12,18 @@ async function getToken() {
if (_webStoreToken) {
return _webStoreToken;
}
const resp = await fetch('https://www.googleapis.com/oauth2/v4/token', {
method: 'POST',
const post = new URLSearchParams({
client_id: webStoreId,
client_secret: webStoreSecret,
refresh_token: webStoreToken,
grant_type: 'refresh_token',
});
const resp = await axios.post('https://www.googleapis.com/oauth2/v4/token', post.toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
client_id: webStoreId,
client_secret: webStoreSecret,
refresh_token: webStoreToken,
grant_type: 'refresh_token',
}).toString(),
});
const res = await resp.json();
const res = resp.data;
if (res.access_token) {
_webStoreToken = res.access_token;
return _webStoreToken;
Expand All @@ -32,30 +32,28 @@ async function getToken() {
}
}

async function upload(readStream, token) {
const res = await fetch(`https://www.googleapis.com/upload/chromewebstore/v1.1/items/${extension.chrome.id}`, {
method: 'PUT',
async function upload(content, token) {
const blob = new Blob(content);
const res = await axios.put(`https://www.googleapis.com/upload/chromewebstore/v1.1/items/${extension.chrome.id}`, blob, {
headers: {
Authorization: `Bearer ${token}`,
'x-goog-api-version': '2',
},
body: readStream,
});

return res.json();
return res.data;
}

async function publish(target = 'default', token) {
const url = `https://www.googleapis.com/chromewebstore/v1.1/items/${extension.chrome.id}/publish?publishTarget=${target}`;
const res = await fetch(url, {
method: 'POST',
const res = await axios.post(url, '', {
headers: {
Authorization: `Bearer ${token}`,
'x-goog-api-version': '2',
},
});

return res.json();
return res.data;
}

async function packCws(zipPath) {
Expand All @@ -69,9 +67,9 @@ async function packCws(zipPath) {
return Promise.reject(new Error('CWS_TOKEN not found'));
}

const distStream = createReadStream(zipPath);
const distContent = await readFile(zipPath);
const token = await getToken();
await upload(distStream, token);
await upload(distContent, token);
return publish('default', token);
}

Expand Down

0 comments on commit 73879e0

Please sign in to comment.