|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +const { exec } = require('child_process'); |
| 19 | + |
| 20 | +const MAX_ATTEMPTS = 15; |
| 21 | +const RETRY_DELAY_SECONDS = 60; |
| 22 | + |
| 23 | +async function pollNpmPublish() { |
| 24 | + const version = process.env.VERSION; |
| 25 | + |
| 26 | + if (!version) { |
| 27 | + console.log(`Couldn't find env var VERSION.`); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const getNpmPublishedVersion = () => |
| 32 | + new Promise((resolve, reject) => { |
| 33 | + exec(`npm view firebase@${version} version`, (error, stdout) => { |
| 34 | + if (error) { |
| 35 | + reject(error); |
| 36 | + } |
| 37 | + const version = stdout.trim(); |
| 38 | + if (!version.match(/^\d+\.\d+\.\d+$/)) { |
| 39 | + reject( |
| 40 | + new Error( |
| 41 | + `npm view did not return a valid semver version. Received: ${version}` |
| 42 | + ) |
| 43 | + ); |
| 44 | + } |
| 45 | + resolve(version); |
| 46 | + }); |
| 47 | + }); |
| 48 | + for (let i = 0; i < MAX_ATTEMPTS; i++) { |
| 49 | + const latestPublishedVersion = await getNpmPublishedVersion(); |
| 50 | + if (latestPublishedVersion === process.env.VERSION) { |
| 51 | + console.log(`Found firebase@${version} in the npm registry.`); |
| 52 | + return; |
| 53 | + } |
| 54 | + console.log(`Didn't find firebase@${version} in the npm registry.`); |
| 55 | + if (i < MAX_ATTEMPTS - 1) { |
| 56 | + console.log(`Trying again in ${RETRY_DELAY_SECONDS} seconds.`); |
| 57 | + await new Promise(resolve => |
| 58 | + setTimeout(resolve, RETRY_DELAY_SECONDS * 1000) |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + console.log( |
| 63 | + `Was not able to find firebase@${version} on npm. Ending process.` |
| 64 | + ); |
| 65 | + process.exit(1); |
| 66 | +} |
| 67 | + |
| 68 | +pollNpmPublish(); |
0 commit comments