Last active
May 4, 2024 21:03
-
-
Save r1tsuu/e75bbab1afcabad71ec28f0deb5614a4 to your computer and use it in GitHub Desktop.
script to bump all Payload 3.0 related packages versions to latest.
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
/** | |
* Place this to ./src/scripts/bump-payload.ts | |
* Then in your package.json add script: | |
* "bump-payload": "tsx ./src/scripts/bump-payload.ts ./package.json && pnpm i" | |
* Run `pnpm bump-payload` | |
*/ | |
import { execSync } from 'child_process'; | |
import fs from 'fs'; | |
import path from 'path'; | |
const args = process.argv.slice(2); | |
const packageJsonPath = path.resolve(import.meta.dirname, '../../', args[0]); | |
console.log(`Starting, package.json path - ${packageJsonPath}`); | |
const bumpDeps = ({ | |
deps, | |
shouldMatchVersion, | |
version, | |
}: { | |
deps: Record<string, string>; | |
/** whatever use "^" or not */ | |
shouldMatchVersion?: boolean; | |
version: string; | |
}) => { | |
Object.keys(deps).forEach((packageName) => { | |
if (packageName === 'payload' || packageName.startsWith('@payloadcms/')) { | |
deps[packageName] = shouldMatchVersion ? version : `^${version}`; | |
} | |
}); | |
}; | |
const start = async () => { | |
const releases = await fetch(`https://api.github.com/repos/payloadcms/payload/releases`).then( | |
(res) => | |
res.json() as Promise< | |
{ | |
tag_name: string; | |
}[] | |
>, | |
); | |
const latest = releases.find((release) => release.tag_name.includes('3.0.0'))?.tag_name; | |
if (!latest) { | |
console.error('Release was not found'); | |
process.exit(1); | |
} | |
console.log(`Found latest payload 3.0 version - ${latest}`); | |
const packageJson = fs.readFileSync(packageJsonPath, 'utf-8'); | |
if (!packageJson) { | |
console.error('package.json was not found'); | |
process.exit(1); | |
} | |
const packageJsonObject = JSON.parse(packageJson) as { | |
dependencies?: Record<string, string>; | |
devDependencies?: Record<string, string>; | |
peerDependencies?: Record<string, string>; | |
}; | |
if (packageJsonObject.dependencies) | |
bumpDeps({ deps: packageJsonObject.dependencies, version: latest }); | |
if (packageJsonObject.devDependencies) | |
bumpDeps({ | |
deps: packageJsonObject.devDependencies, | |
shouldMatchVersion: true, | |
version: latest, | |
}); | |
if (packageJsonObject.peerDependencies) | |
bumpDeps({ deps: packageJsonObject.peerDependencies, version: latest }); | |
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJsonObject), { | |
encoding: 'utf-8', | |
flag: 'w', | |
}); | |
execSync(`npx prettier ${packageJsonPath} --write`); | |
console.log(`Successfully bumped versions to ${latest}`); | |
}; | |
start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment