From 73879e015d7056b57510394050564c6fdd4c81d2 Mon Sep 17 00:00:00 2001 From: ShuangYa Date: Thu, 11 Jul 2024 01:00:02 +0800 Subject: [PATCH] scripts: remove fetch --- scripts/get-snapshot-version.mjs | 2 +- scripts/pack-utils/cws.mjs | 40 +++++++++++++++----------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/scripts/get-snapshot-version.mjs b/scripts/get-snapshot-version.mjs index e44558a..9fea85e 100644 --- a/scripts/get-snapshot-version.mjs +++ b/scripts/get-snapshot-version.mjs @@ -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'; diff --git a/scripts/pack-utils/cws.mjs b/scripts/pack-utils/cws.mjs index 553ca9f..5fbc709 100644 --- a/scripts/pack-utils/cws.mjs +++ b/scripts/pack-utils/cws.mjs @@ -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; @@ -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; @@ -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) { @@ -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); }