command-set.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { writeFileSync } from 'fs'
  2. import { basename } from 'node:path'
  3. import { changePackageVersion } from './change-package-version'
  4. import { getRecursiveFileList } from './get-recursive-file-list'
  5. export function commandSet(version: string, path: string = '.') {
  6. if (!version) {
  7. console.error(`Version is required`)
  8. process.exit(1)
  9. }
  10. if (
  11. !version
  12. // Strip first character if it's a `@`
  13. .replace(/^@/, '')
  14. .includes('@')
  15. ) {
  16. console.error(`Invalid package version: ${version}. Provide package with version, e.g. @solana/web3.js@1.0.0`)
  17. process.exit(1)
  18. }
  19. // Take anything after the second `@` as the version, the rest is the package name
  20. const [pkg, ...rest] = version.split('@').reverse()
  21. const pkgName = rest.reverse().join('@')
  22. // Make sure pkgVersions has a ^ prefix, if not add it
  23. const pkgVersion = pkg.startsWith('^') ? pkg : `^${pkg}`
  24. console.log(`Setting package ${pkgName} to ${pkgVersion} in ${path}`)
  25. const files = getRecursiveFileList(path).filter((file) => basename(file) === 'package.json')
  26. let count = 0
  27. for (const file of files) {
  28. const [changed, content] = changePackageVersion(file, pkgName, pkgVersion)
  29. if (changed) {
  30. writeFileSync(file, JSON.stringify(content, null, 2) + '\n')
  31. count++
  32. }
  33. }
  34. if (count === 0) {
  35. console.log(`No files updated`)
  36. } else {
  37. console.log(`Updated ${count} files`)
  38. }
  39. }