|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Copyright (c) Microsoft Corporation. |
| 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 | +// @ts-check |
| 18 | + |
| 19 | +// Moves a dist-tag (next, beta or latest) to the packages that publish_all_packages.sh published |
| 20 | +// under "<tag>-pending", once all of them are live. npm processes a package after accepting it, |
| 21 | +// and that took up to 110 minutes, so this waits for hours before giving up. |
| 22 | +// |
| 23 | +// usage: promote_npm_packages.js <npm-publish.json> |
| 24 | + |
| 25 | +const { execFileSync } = require('child_process'); |
| 26 | +const fs = require('fs'); |
| 27 | +const os = require('os'); |
| 28 | +const path = require('path'); |
| 29 | + |
| 30 | +const POLL_INTERVAL_MS = 60 * 1000; |
| 31 | +const TIMEOUT_MS = 5 * 60 * 60 * 1000; |
| 32 | + |
| 33 | +/** @type {{ version: string, tag: string, packages: string[] }} */ |
| 34 | +const { version, tag, packages } = JSON.parse(fs.readFileSync(process.argv[2], 'utf8')); |
| 35 | +const pendingTag = `${tag}-pending`; |
| 36 | + |
| 37 | +/** |
| 38 | + * @param {string[]} args |
| 39 | + * @param {{ quiet?: boolean }} options |
| 40 | + */ |
| 41 | +function npm(args, { quiet = false } = {}) { |
| 42 | + return execFileSync('npm', args, { encoding: 'utf8', stdio: ['ignore', 'pipe', quiet ? 'pipe' : 'inherit'] }).trim(); |
| 43 | +} |
| 44 | + |
| 45 | +/** @param {string} name */ |
| 46 | +function isLive(name) { |
| 47 | + try { |
| 48 | + return npm(['view', `${name}@${version}`, 'version', '--prefer-online'], { quiet: true }) === version; |
| 49 | + } catch { |
| 50 | + return false; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +async function waitUntilLive() { |
| 55 | + const deadline = Date.now() + TIMEOUT_MS; |
| 56 | + let missing = packages; |
| 57 | + while (true) { |
| 58 | + missing = missing.filter(name => !isLive(name)); |
| 59 | + if (!missing.length) |
| 60 | + return; |
| 61 | + if (Date.now() > deadline) |
| 62 | + throw new Error(`Not live after ${TIMEOUT_MS / 3600000} hours: ${missing.map(name => `${name}@${version}`).join(', ')}`); |
| 63 | + console.log(`Waiting for ${missing.map(name => `${name}@${version}`).join(', ')}`); |
| 64 | + await new Promise(f => setTimeout(f, POLL_INTERVAL_MS)); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function checkInstall() { |
| 69 | + // Install them together, as users do, to check that the tarballs download and the dependencies resolve. |
| 70 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'promote-')); |
| 71 | + npm(['install', '--prefix', dir, '--ignore-scripts', '--no-audit', '--no-fund', ...packages.map(name => `${name}@${version}`)]); |
| 72 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 73 | +} |
| 74 | + |
| 75 | +function moveDistTags() { |
| 76 | + for (const name of packages) { |
| 77 | + const distTags = JSON.parse(npm(['view', name, 'dist-tags', '--json', '--prefer-online'])); |
| 78 | + const current = distTags[tag]; |
| 79 | + if (current === version) { |
| 80 | + console.log(`${name}@${tag} is already ${version}`); |
| 81 | + } else if (current && publishTime(name, current) > publishTime(name, version)) { |
| 82 | + // A later run promoted a newer version first. |
| 83 | + console.log(`${name}@${tag} stays at ${current}, which was published after ${version}`); |
| 84 | + } else { |
| 85 | + npm(['dist-tag', 'add', `${name}@${version}`, tag]); |
| 86 | + } |
| 87 | + if (distTags[pendingTag] === version) |
| 88 | + npm(['dist-tag', 'rm', name, pendingTag]); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** @type {Map<string, Record<string, string>>} */ |
| 93 | +const publishTimes = new Map(); |
| 94 | + |
| 95 | +/** |
| 96 | + * @param {string} name |
| 97 | + * @param {string} packageVersion |
| 98 | + */ |
| 99 | +function publishTime(name, packageVersion) { |
| 100 | + if (!publishTimes.has(name)) |
| 101 | + publishTimes.set(name, JSON.parse(npm(['view', name, 'time', '--json', '--prefer-online']))); |
| 102 | + return publishTimes.get(name)?.[packageVersion] ?? ''; |
| 103 | +} |
| 104 | + |
| 105 | +(async () => { |
| 106 | + console.log(`Promoting ${version} to @${tag}: ${packages.join(', ')}`); |
| 107 | + await waitUntilLive(); |
| 108 | + checkInstall(); |
| 109 | + moveDistTags(); |
| 110 | + console.log('Done.'); |
| 111 | +})().catch(error => { |
| 112 | + console.error(`ERROR: ${error.message}`); |
| 113 | + process.exit(1); |
| 114 | +}); |
0 commit comments