Skip to content

Commit

Permalink
chore: add script for setting npm latest tag on v2 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Jan 31, 2023
1 parent 1451a16 commit 82a1c3d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -15,6 +15,7 @@
"publish-canary": "RELEASE_TAG=canary npm run publish-tag",
"publish-current-branch": "RELEASE_TAG=`git rev-parse --abbrev-ref HEAD | sed 's/\\//\\_/g'` npm run publish-tag",
"publish-tag": "RELEASE_TAG=\"${RELEASE_TAG:-canary}\"; npm run build && lerna publish --canary --force-publish --no-git-tag-version --dist-tag=$RELEASE_TAG --preid=$RELEASE_TAG --exact",
"postpublish": "node scripts/setLatestForV2OnlyModules.js",
"package": "npm run package-yarn && npm run package-cli",
"package-cli": "(cd packages/@sanity/cli && npm run pack)",
"package-yarn": "(cd packages/@sanity/cli && npm run package-yarn)",
Expand Down
48 changes: 48 additions & 0 deletions scripts/setLatestForV2OnlyModules.js
@@ -0,0 +1,48 @@
/* eslint-disable no-sync, no-console, import/no-dynamic-require */
const fs = require('fs')
const path = require('path')
const execa = require('execa')
const semver = require('semver')

main()

async function main() {
const pkgsPath = path.join(__dirname, '..', 'packages', '@sanity')
const pkgs = fs
.readdirSync(pkgsPath)
.filter((pkg) => fs.statSync(path.join(pkgsPath, pkg)).isDirectory())

for (const pkg of pkgs) {
const pkgName = `@sanity/${pkg}`
const latestVersion = (await execa('npm', ['show', pkgName, 'version'])).stdout.trim()

// For 3.x modules, we want to keep that as latest
if (!latestVersion.startsWith('2.')) {
continue
}

// If on 2.x range, check if the in-repo version is newer than what is published as `latest`.
// This happens because we publish blindly with `--tag v2`, but for modules that were removed
// in v3, we actually want the latest 2.x version to be the latest version.
const localVersion = require(path.join(pkgsPath, pkg, 'package.json')).version
if (
!localVersion ||
!localVersion.startsWith('2.') ||
semver.gte(latestVersion, localVersion)
) {
console.log('%s is at latest already, skipping', pkgName)
continue
}

console.log(
'%s is newer locally (%s) than latest (%s) - adjusting `latest` tag',
pkgName,
localVersion,
latestVersion
)

console.log(
(await execa('npm', ['dist-tag', 'add', `${pkgName}@${localVersion}`, 'latest'])).stdout
)
}
}

0 comments on commit 82a1c3d

Please sign in to comment.