-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into v2
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2021-2024 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { readdirSync, readFileSync } from "fs"; | ||
import { join } from "path"; | ||
import { existsSync } from "node:fs"; | ||
|
||
/* | ||
USAGE: node get-workspace-publish-tag.js | ||
Looks at the version used in the workspace, and returns one of: | ||
- latest - for versions such as 3.1.4, 2.0.0, but also 0.1.2 | ||
- alpha - for versions such as 1.0.0-alpha.8 | ||
- beta - for versions such as 1.0.0-beta.8 | ||
- rc - for versions such as 1.0.0-rc.8 | ||
*/ | ||
|
||
const version = findWorkspaceVersion("packages"); | ||
|
||
if (/^\d\.\d\.\d$/.test(version)) { | ||
process.stdout.write("latest"); | ||
} else if (/^\d\.\d\.\d-alpha.*$/.test(version)) { | ||
process.stdout.write("alpha"); | ||
} else if (/^\d\.\d\.\d-beta.*$/.test(version)) { | ||
process.stdout.write("beta"); | ||
} else if (/^\d\.\d\.\d-rc.*$/.test(version)) { | ||
process.stdout.write("rc"); | ||
} else { | ||
throw new Error(`Unable to determine publish tag from version ${version}`); | ||
} | ||
|
||
/** | ||
* @param {string} packagesDir | ||
* @returns {string} | ||
*/ | ||
function findWorkspaceVersion(packagesDir) { | ||
let version = undefined; | ||
for (const entry of readdirSync(packagesDir, { withFileTypes: true })) { | ||
if (!entry.isDirectory()) { | ||
continue; | ||
} | ||
const path = join(packagesDir, entry.name, "package.json"); | ||
if (existsSync(path)) { | ||
const pkg = JSON.parse(readFileSync(path, "utf-8")); | ||
if (pkg.private === true) { | ||
continue; | ||
} | ||
if (!pkg.version) { | ||
throw new Error(`${path} is missing "version"`); | ||
} | ||
if (version === undefined) { | ||
version = pkg.version; | ||
} else if (version !== pkg.version) { | ||
throw new Error(`${path} has unexpected version ${pkg.version}`); | ||
} | ||
} | ||
} | ||
if (version === undefined) { | ||
throw new Error(`unable to find workspace version`); | ||
} | ||
return version; | ||
} |