| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | 'use strict'; |
| 4 | |
| 5 | const {join} = require('path'); |
| 6 | const {getPublicPackages, handleError} = require('./utils'); |
| 7 | const theme = require('./theme'); |
| 8 | |
| 9 | const confirmVersionAndTags = require('./publish-commands/confirm-version-and-tags'); |
| 10 | const parseParams = require('./publish-commands/parse-params'); |
| 11 | const publishToNPM = require('./publish-commands/publish-to-npm'); |
| 12 | const validateTags = require('./publish-commands/validate-tags'); |
| 13 | const validateSkipPackages = require('./publish-commands/validate-skip-packages'); |
| 14 | |
| 15 | const run = async () => { |
| 16 | try { |
| 17 | const params = parseParams(); |
| 18 | |
| 19 | const isExperimental = params.tag === 'experimental'; |
| 20 | |
| 21 | params.cwd = join(__dirname, '..', '..'); |
| 22 | params.packages = await getPublicPackages(isExperimental); |
| 23 | |
| 24 | if (params.onlyPackages.length > 0 && params.skipPackages.length > 0) { |
| 25 | console.error( |
| 26 | '--onlyPackages and --skipPackages cannot be used together' |
| 27 | ); |
| 28 | process.exit(1); |
| 29 | } |
| 30 | |
| 31 | if (params.onlyPackages.length > 0) { |
| 32 | params.packages = params.packages.filter(packageName => { |
| 33 | return params.onlyPackages.includes(packageName); |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | // Pre-filter any skipped packages to simplify the following commands. |
| 38 | // As part of doing this we can also validate that none of the skipped packages were misspelled. |
| 39 | params.skipPackages.forEach(packageName => { |
| 40 | const index = params.packages.indexOf(packageName); |
| 41 | if (index < 0) { |
| 42 | console.log( |
| 43 | theme`Invalid skip package {package ${packageName}} specified.` |
| 44 | ); |
| 45 | process.exit(1); |
| 46 | } else { |
| 47 | params.packages.splice(index, 1); |
| 48 | } |
| 49 | }); |
| 50 | |
| 51 | await validateTags(params); |
| 52 | await confirmVersionAndTags(params); |
| 53 | await validateSkipPackages(params); |
| 54 | // npm ownership / whoami no longer applies — OIDC trusted publishing |
| 55 | // verifies the publisher via the registry's per-package config, not via a |
| 56 | // logged-in npm user. |
| 57 | |
| 58 | const packageNames = params.packages; |
| 59 | |
| 60 | let failed = false; |
| 61 | for (let i = 0; i < packageNames.length; i++) { |
| 62 | try { |
| 63 | const packageName = packageNames[i]; |
| 64 | await publishToNPM(params, packageName, null); |
| 65 | } catch (error) { |
| 66 | failed = true; |
| 67 | console.error(error.message); |
| 68 | console.log(); |
| 69 | console.log( |
| 70 | theme.error`Publish failed. Will attempt to publish remaining packages.` |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | if (failed) { |
| 75 | console.log(theme.error`One or more packages failed to publish.`); |
| 76 | process.exit(1); |
| 77 | } |
| 78 | } catch (error) { |
| 79 | handleError(error); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | run(); |