|
| 1 | +// @ts-check |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import * as fs from "node:fs"; |
| 4 | +import * as util from "node:util"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Apply additional dist-tags to published packages |
| 8 | + * Usage: node apply-additional-tags.mjs --tags <tags> --token <token> |
| 9 | + * node apply-additional-tags.mjs --tags <tags> --dry-run |
| 10 | + * Where tags is a comma-separated list of tags (e.g., "next,v0.79-stable") |
| 11 | + */ |
| 12 | + |
| 13 | +const registry = "https://registry.npmjs.org/"; |
| 14 | +const packages = [ |
| 15 | + "@react-native-macos/virtualized-lists", |
| 16 | + "react-native-macos", |
| 17 | +]; |
| 18 | + |
| 19 | +/** |
| 20 | + * @typedef {{ |
| 21 | + * tags?: string; |
| 22 | + * token?: string; |
| 23 | + * "dry-run"?: boolean; |
| 24 | + * }} Options; |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * @param {Options} options |
| 29 | + * @returns {number} |
| 30 | + */ |
| 31 | +function main({ tags, token, "dry-run": dryRun }) { |
| 32 | + if (!tags) { |
| 33 | + console.log("No additional tags to apply"); |
| 34 | + return 0; |
| 35 | + } |
| 36 | + |
| 37 | + if (!dryRun && !token) { |
| 38 | + console.error("Error: npm auth token is required (use --dry-run to preview)"); |
| 39 | + return 1; |
| 40 | + } |
| 41 | + |
| 42 | + const packageJson = JSON.parse( |
| 43 | + fs.readFileSync("./packages/react-native/package.json", "utf-8") |
| 44 | + ); |
| 45 | + const version = packageJson.version; |
| 46 | + |
| 47 | + if (dryRun) { |
| 48 | + console.log(""); |
| 49 | + console.log("=== Additional dist-tags that would be applied ==="); |
| 50 | + for (const tag of tags.split(",")) { |
| 51 | + for (const pkg of packages) { |
| 52 | + console.log(` ${pkg}@${version} -> ${tag}`); |
| 53 | + } |
| 54 | + } |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + for (const tag of tags.split(",")) { |
| 59 | + for (const pkg of packages) { |
| 60 | + console.log(`Adding dist-tag '${tag}' to ${pkg}@${version}`); |
| 61 | + const result = spawnSync( |
| 62 | + "npm", |
| 63 | + [ |
| 64 | + "dist-tag", |
| 65 | + "add", |
| 66 | + `${pkg}@${version}`, |
| 67 | + tag, |
| 68 | + "--registry", |
| 69 | + registry, |
| 70 | + `--//registry.npmjs.org/:_authToken=${token}`, |
| 71 | + ], |
| 72 | + { stdio: "inherit", shell: true } |
| 73 | + ); |
| 74 | + |
| 75 | + if (result.status !== 0) { |
| 76 | + console.error(`Failed to add dist-tag '${tag}' to ${pkg}@${version}`); |
| 77 | + return 1; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +const { values } = util.parseArgs({ |
| 86 | + args: process.argv.slice(2), |
| 87 | + options: { |
| 88 | + tags: { |
| 89 | + type: "string", |
| 90 | + }, |
| 91 | + token: { |
| 92 | + type: "string", |
| 93 | + }, |
| 94 | + "dry-run": { |
| 95 | + type: "boolean", |
| 96 | + default: false, |
| 97 | + }, |
| 98 | + }, |
| 99 | + strict: true, |
| 100 | +}); |
| 101 | + |
| 102 | +process.exitCode = main(values); |
0 commit comments