version.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const { resolve } = require('node:path')
  2. const { readFile } = require('node:fs/promises')
  3. const { output } = require('proc-log')
  4. const BaseCommand = require('../base-cmd.js')
  5. class Version extends BaseCommand {
  6. static description = 'Bump a package version'
  7. static name = 'version'
  8. static params = [
  9. 'allow-same-version',
  10. 'commit-hooks',
  11. 'git-tag-version',
  12. 'json',
  13. 'preid',
  14. 'sign-git-tag',
  15. 'save',
  16. 'workspace',
  17. 'workspaces',
  18. 'workspaces-update',
  19. 'include-workspace-root',
  20. 'ignore-scripts',
  21. ]
  22. static workspaces = true
  23. static ignoreImplicitWorkspace = false
  24. static usage = ['[<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]']
  25. static async completion (opts) {
  26. const {
  27. conf: {
  28. argv: { remain },
  29. },
  30. } = opts
  31. if (remain.length > 2) {
  32. return []
  33. }
  34. return [
  35. 'major',
  36. 'minor',
  37. 'patch',
  38. 'premajor',
  39. 'preminor',
  40. 'prepatch',
  41. 'prerelease',
  42. 'from-git',
  43. ]
  44. }
  45. async exec (args) {
  46. switch (args.length) {
  47. case 0:
  48. return this.list()
  49. case 1:
  50. return this.change(args)
  51. default:
  52. throw this.usageError()
  53. }
  54. }
  55. async execWorkspaces (args) {
  56. switch (args.length) {
  57. case 0:
  58. return this.listWorkspaces()
  59. case 1:
  60. return this.changeWorkspaces(args)
  61. default:
  62. throw this.usageError()
  63. }
  64. }
  65. async change (args) {
  66. const libnpmversion = require('libnpmversion')
  67. const prefix = this.npm.config.get('tag-version-prefix')
  68. const version = await libnpmversion(args[0], {
  69. ...this.npm.flatOptions,
  70. path: this.npm.prefix,
  71. })
  72. return output.standard(`${prefix}${version}`)
  73. }
  74. async changeWorkspaces (args) {
  75. const updateWorkspaces = require('../utils/update-workspaces.js')
  76. const libnpmversion = require('libnpmversion')
  77. const prefix = this.npm.config.get('tag-version-prefix')
  78. const {
  79. config,
  80. flatOptions,
  81. localPrefix,
  82. } = this.npm
  83. await this.setWorkspaces()
  84. const updatedWorkspaces = []
  85. for (const [name, path] of this.workspaces) {
  86. output.standard(name)
  87. const version = await libnpmversion(args[0], {
  88. ...flatOptions,
  89. 'git-tag-version': false,
  90. path,
  91. })
  92. updatedWorkspaces.push(name)
  93. output.standard(`${prefix}${version}`)
  94. }
  95. return updateWorkspaces({
  96. config,
  97. flatOptions,
  98. localPrefix,
  99. npm: this.npm,
  100. workspaces: updatedWorkspaces,
  101. })
  102. }
  103. async list (results = {}) {
  104. const pj = resolve(this.npm.prefix, 'package.json')
  105. const pkg = await readFile(pj, 'utf8')
  106. .then(data => JSON.parse(data))
  107. .catch(() => ({}))
  108. if (pkg.name && pkg.version) {
  109. results[pkg.name] = pkg.version
  110. }
  111. results.npm = this.npm.version
  112. for (const [key, version] of Object.entries(process.versions)) {
  113. results[key] = version
  114. }
  115. if (this.npm.config.get('json')) {
  116. output.buffer(results)
  117. } else {
  118. output.standard(results)
  119. }
  120. }
  121. async listWorkspaces () {
  122. const results = {}
  123. await this.setWorkspaces()
  124. for (const path of this.workspacePaths) {
  125. const pj = resolve(path, 'package.json')
  126. // setWorkspaces has already parsed package.json so we know it won't error
  127. const pkg = await readFile(pj, 'utf8').then(data => JSON.parse(data))
  128. if (pkg.name && pkg.version) {
  129. results[pkg.name] = pkg.version
  130. }
  131. }
  132. return this.list(results)
  133. }
  134. }
  135. module.exports = Version