|
| 1 | +// Copyright 2021-2024 The Connect Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { readdirSync, readFileSync } from "fs"; |
| 16 | +import { join } from "path"; |
| 17 | +import { existsSync } from "node:fs"; |
| 18 | +import { execSync } from "node:child_process"; |
| 19 | + |
| 20 | +/* |
| 21 | + * Publish connect-query-es. See .github/RELEASING.md |
| 22 | + */ |
| 23 | + |
| 24 | +const tag = determinePublishTag(findWorkspaceVersion("packages")); |
| 25 | +const command = `pnpm --filter "./packages/*" publish --tag ${tag}`; |
| 26 | +execSync(command, { |
| 27 | + stdio: "inherit", |
| 28 | +}); |
| 29 | + |
| 30 | +/** |
| 31 | + * @param {string} version |
| 32 | + * @returns {string} |
| 33 | + */ |
| 34 | +function determinePublishTag(version) { |
| 35 | + if (/^\d+\.\d+\.\d+$/.test(version)) { |
| 36 | + return "latest"; |
| 37 | + } else if (/^\d+\.\d+\.\d+-alpha.*$/.test(version)) { |
| 38 | + return "alpha"; |
| 39 | + } else if (/^\d+\.\d+\.\d+-beta.*$/.test(version)) { |
| 40 | + return "beta"; |
| 41 | + } else if (/^\d+\.\d+\.\d+-rc.*$/.test(version)) { |
| 42 | + return "rc"; |
| 43 | + } else { |
| 44 | + throw new Error(`Unable to determine publish tag from version ${version}`); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * @param {string} packagesDir |
| 50 | + * @returns {string} |
| 51 | + */ |
| 52 | +function findWorkspaceVersion(packagesDir) { |
| 53 | + let version = undefined; |
| 54 | + for (const entry of readdirSync(packagesDir, { withFileTypes: true })) { |
| 55 | + if (!entry.isDirectory()) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + const path = join(packagesDir, entry.name, "package.json"); |
| 59 | + if (existsSync(path)) { |
| 60 | + const pkg = JSON.parse(readFileSync(path, "utf-8")); |
| 61 | + if (pkg.private === true) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + if (!pkg.version) { |
| 65 | + throw new Error(`${path} is missing "version"`); |
| 66 | + } |
| 67 | + if (version === undefined) { |
| 68 | + version = pkg.version; |
| 69 | + } else if (version !== pkg.version) { |
| 70 | + throw new Error(`${path} has unexpected version ${pkg.version}`); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + if (version === undefined) { |
| 75 | + throw new Error(`unable to find workspace version`); |
| 76 | + } |
| 77 | + return version; |
| 78 | +} |
0 commit comments