turborepo/scripts/version.js
Chris Olszewski b0bb0afba6
feat(release): allow for overriding npm dist tag (#9899)
### Description

Npm requires all published versions to have a dist tag. If we want to
backport a fix we don't want to release this under the `latest` or
`canary` tag. Instead we should release it under a `v${SPECIFIC_MINOR}`
tag if we want to cut another patch under an old version (or
`v${SPECIFIC_MAJOR}` for a minor release for a previous major)

This updates what we write to `version.txt` which is what gets used for
our [NPM
publish](https://github.com/vercel/turborepo/blob/main/cli/Makefile#L2).

### Testing Instructions

[`prepatch` release without an
override](3667901443 (step):5:12)
[`prepatch` release with an
override](3667903266 (step):5:12)
2025-02-04 20:58:28 +00:00

25 lines
1,007 B
JavaScript
Executable file

#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const semver = require("semver");
// These values come from the invocation of release.
const increment = process.argv[2];
const tagOverride = process.argv[3];
// Now we get the current version of the package.
const versionFilePath = path.join(__dirname, "..", "version.txt");
const versionFileContents = fs.readFileSync(versionFilePath, "utf-8");
const [currentVersion] = versionFileContents.split("\n");
// Now that we know current state, figure out what the target state is.
// If we're doing a "pre" release, set the identifier to canary
const identifier = increment.startsWith("pre") ? "canary" : "latest";
const newVersion = semver.inc(currentVersion, increment, identifier);
// Parse the output semver identifier to identify which npm tag to publish to.
const parsed = semver.parse(newVersion);
const tag = tagOverride || parsed?.prerelease[0] || "latest";
fs.writeFileSync(versionFilePath, `${newVersion}\n${tag}\n`);