|
| 1 | +/** webstore-publish.ts |
| 2 | + * ビルド済みzipファイルを受けてChrome拡張をChromeウェブストアに公開するスクリプト |
| 3 | + * @see https://developer.chrome.com/docs/webstore/using-api |
| 4 | + * @see https://developer.chrome.com/docs/webstore/api |
| 5 | + * |
| 6 | + * @env {string} GOOGLEAPI_CLIENT_ID Google Cloud Consoleで作成したOAuth 2.0 クライアント ID |
| 7 | + * @env {string} GOOGLEAPI_CLIENT_SECRET Google Cloud Consoleで作成したOAuth 2.0 クライアント シークレット |
| 8 | + * @env {string} GOOGLEAPI_REFRESH_TOKEN Google OAuth 2.0 Playgroundで取得したリフレッシュトークン |
| 9 | + * @env {string} CHROMEWEBSTORE_EXTENSION_ID Chromeウェブストアで取得した拡張機能ID |
| 10 | + * @env {string} NODE_ENV デフォルトは"production"、"development"の場合はtrusted_testers=true |
| 11 | +**/ |
| 12 | + |
| 13 | +import fs from "fs"; |
| 14 | + |
| 15 | +const __main__ = async () => { |
| 16 | + if (process.argv.length < 3) { |
| 17 | + console.error("zip file path is required."); |
| 18 | + console.error("Usage: node webstore-publish.ts <zip_file_path>"); |
| 19 | + process.exit(1); |
| 20 | + } |
| 21 | + const [/* nodepath */, /* _scriptpath */, zip_file_path] = process.argv; |
| 22 | + if (!fs.existsSync(zip_file_path)) { |
| 23 | + console.error(`zip file not found: ${zip_file_path}`); |
| 24 | + process.exit(1); |
| 25 | + } |
| 26 | + try { |
| 27 | + await __webstore_publish__(zip_file_path); |
| 28 | + } catch (e) { |
| 29 | + console.error("[ERROR]", e); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +const __webstore_publish__ = async ( |
| 35 | + zip_file_path: string, |
| 36 | + client_id: string = process.env.GOOGLEAPI_CLIENT_ID!, |
| 37 | + client_secret: string = process.env.GOOGLEAPI_CLIENT_SECRET!, |
| 38 | + refresh_token: string = process.env.GOOGLEAPI_REFRESH_TOKEN!, |
| 39 | + extension_id: string = process.env.CHROMEWEBSTORE_EXTENSION_ID!, |
| 40 | + trusted_testers: boolean = false, // process.env.NODE_ENV !== "production", // Betaもリンク知ってるひとに公開なので false でいい |
| 41 | +) => { |
| 42 | + console.log("[INFO]", "START PUBLISHING..."); |
| 43 | + |
| 44 | + // (1) リフレッシュトークンを使ってアクセストークンを取得 |
| 45 | + const refreshResponse = await refreshAccessToken(client_id, client_secret, refresh_token); |
| 46 | + const authbody = (await refreshResponse.json()) as OAuthResponse; |
| 47 | + const { access_token, token_type, scope, expires_in } = authbody; |
| 48 | + if (!refreshResponse.ok) throw new Error(`http response of REFRESH is NOT OK: ${refreshResponse.statusText}\n${JSON.stringify(authbody)}`,); |
| 49 | + console.log("[INFO]", "ACCESS TOKEN REFRESHED:", token_type, scope, expires_in); |
| 50 | + if (!access_token) throw new Error(`couldn't retrieve access_token from this refresh_token`); |
| 51 | + |
| 52 | + // (2) アクセストークンを使ってzipファイルをアップロード |
| 53 | + const uploadResponse = await uploadPackageFile(access_token, zip_file_path, extension_id); |
| 54 | + const uploadbody = await uploadResponse.json(); |
| 55 | + console.log("[INFO]", "UPLOAD PACKAGE FILE:", uploadResponse.ok, uploadResponse.status); |
| 56 | + if (!uploadResponse.ok) throw new Error(`http response of UPLOAD is NOT OK: ${uploadResponse.statusText}\n${JSON.stringify(uploadbody)}`); |
| 57 | + console.log("[INFO]", "UPLOAD SUCCESSFULLY DONE:", uploadbody); |
| 58 | + |
| 59 | + // (3) アップロードしたzipファイルを公開申請 |
| 60 | + const publishResponse = await publishUploadedPackageFile(access_token, extension_id, trusted_testers); |
| 61 | + const publishbody = await publishResponse.json(); |
| 62 | + console.log("[INFO]", "PUBLISH NEW PACKGE:", publishResponse.ok, publishResponse.status); |
| 63 | + if (!publishResponse.ok) throw new Error(`http response of PUBLISH is NOT OK: ${publishResponse.statusText}\n${JSON.stringify(publishbody)}`); |
| 64 | + console.log("[INFO]", "PUBLISH SUCCESSFULLY DONE:", publishbody); |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * OAuth 2.0 レスポンス |
| 69 | + * @see https://developer.chrome.com/docs/webstore/using-api?hl=ja#test-oauth |
| 70 | +**/ |
| 71 | +interface OAuthResponse { |
| 72 | + access_token: string; |
| 73 | + expires_in: number; |
| 74 | + refresh_token: string; |
| 75 | + token_type: string; |
| 76 | + scope: string; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * refreshAccessToken |
| 81 | + * デフォルトでは、得られた access_token は40分でExpireするため、 |
| 82 | + * publishのAPIを叩く前に、必ずここで access_token を新たに取得 |
| 83 | + * する必要がある. |
| 84 | + * ということで、こいつは refresh_token なんかを使って access_token |
| 85 | + * を得るメソッドです。 |
| 86 | + * |
| 87 | + * @param {string} client_id |
| 88 | + * @param {string} client_secret |
| 89 | + * @param {string} refresh_token |
| 90 | + * |
| 91 | + * @returns {Promise<OAuthResponse>} |
| 92 | + */ |
| 93 | +async function refreshAccessToken( |
| 94 | + client_id: string, |
| 95 | + client_secret: string, |
| 96 | + refresh_token: string |
| 97 | +): Promise<Response> { |
| 98 | + return fetch("https://www.googleapis.com/oauth2/v4/token", { |
| 99 | + method: "POST", |
| 100 | + headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| 101 | + body: new URLSearchParams({ |
| 102 | + client_id: client_id, |
| 103 | + client_secret: client_secret, |
| 104 | + refresh_token: refresh_token, |
| 105 | + grant_type: "refresh_token", |
| 106 | + }), |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * uploadPackageFile |
| 112 | + * ちゃんとリフレッシュされてるアクセストークンを使って、 |
| 113 | + * 指定されたファイルを指定されたアプリにアップロードする. |
| 114 | + * まだpublishされてないので注意. |
| 115 | + * |
| 116 | + * @param {string} access_token |
| 117 | + * @param {string} zip_file_path |
| 118 | + * @param {string} extension_id |
| 119 | + * |
| 120 | + * @returns {Promise<Response>} |
| 121 | + */ |
| 122 | +async function uploadPackageFile( |
| 123 | + access_token: string, |
| 124 | + zip_file_path: string, |
| 125 | + extension_id: string |
| 126 | +): Promise<Response> { |
| 127 | + const buf = fs.readFileSync(zip_file_path); |
| 128 | + return fetch(`https://www.googleapis.com/upload/chromewebstore/v1.1/items/${extension_id}`, { |
| 129 | + method: "PUT", |
| 130 | + headers: { "Authorization": `Bearer ${access_token}`, "x-goog-api-version": "2" }, |
| 131 | + body: Buffer.from(buf), |
| 132 | + }); |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * publishUploadedPackageFile |
| 137 | + * パブリッシュする. |
| 138 | + * |
| 139 | + * @param {string} access_token |
| 140 | + * @param {string} extension_id |
| 141 | + * @param {boolean} trustedTesters |
| 142 | + * |
| 143 | + * @return {Promise<Response>} |
| 144 | + */ |
| 145 | +async function publishUploadedPackageFile( |
| 146 | + access_token: string, |
| 147 | + extension_id: string, |
| 148 | + trustedTesters: boolean |
| 149 | +) { |
| 150 | + const query = new URLSearchParams({ publishTarget: trustedTesters ? "trustedTesters" : "default" }); |
| 151 | + return fetch(`https://www.googleapis.com/chromewebstore/v1.1/items/${extension_id}/publish?${query.toString()}`, { |
| 152 | + method: "POST", |
| 153 | + headers: { "Authorization": `Bearer ${access_token}`, "x-goog-api-version": "2", "Content-Length": "0" }, |
| 154 | + }); |
| 155 | +} |
| 156 | + |
| 157 | +// Entrypoint |
| 158 | +__main__(); |
0 commit comments