version.mjs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. import { fileURLToPath, pathToFileURL } from 'node:url'
  4. import assert from 'node:assert/strict'
  5. export const root = path.resolve(fileURLToPath(new URL('../../', import.meta.url)))
  6. export function releaseVersion(directory = root, tag) {
  7. const bundle = JSON.parse(fs.readFileSync(path.join(directory, 'packages/bundle/package.json'), 'utf8'))
  8. const workspace = JSON.parse(fs.readFileSync(path.join(directory, 'package.json'), 'utf8'))
  9. const meta = JSON.parse(fs.readFileSync(path.join(directory, 'packages/meta/package.json'), 'utf8'))
  10. assert.match(bundle.version, /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[a-z]+\.[1-9]\d*)?$/)
  11. assert.equal(workspace.version, bundle.version, 'Workspace and main release version must agree')
  12. assert.equal(meta.version, bundle.version, 'Full and main release version must agree')
  13. assert.equal(bundle.name, '@linfengqaqtat/dsh-scriptor')
  14. const expectedTag = `scriptor-v${bundle.version}`
  15. if (tag) assert.equal(tag, expectedTag, 'Release tag must match the package version')
  16. const changelog = fs.readFileSync(path.join(directory, 'CHANGELOG.md'), 'utf8')
  17. assert.ok(changelog.includes(`## [${bundle.version}]`), 'Missing versioned changelog entry')
  18. for (const file of ['README.md', 'packages/bundle/README.md', 'packages/meta/README.md', 'packages/meta/RELEASE_NOTES.md', 'docs/user/install.md', 'docs/user/upgrade-backup.md']) {
  19. const text = fs.readFileSync(path.join(directory, file), 'utf8')
  20. for (const match of text.matchAll(/linfengqaqtat-dsh-scriptor-([\d][\w.-]*)\.tgz/g)) {
  21. assert.equal(match[1], bundle.version, `Stale installation example in ${file}`)
  22. }
  23. for (const match of text.matchAll(/releases\/tag\/scriptor-v([\d][\w.-]*)/g)) {
  24. assert.equal(match[1], bundle.version, `Stale download link in ${file}`)
  25. }
  26. for (const match of text.matchAll(/dsh-scriptor(?:-full)?@(\d+[\w.-]*)/g)) {
  27. assert.equal(match[1], bundle.version, `Stale registry installation example in ${file}`)
  28. }
  29. }
  30. return { version: bundle.version, tag: expectedTag, prerelease: bundle.version.includes('-'),
  31. packageName: bundle.name, filename: `linfengqaqtat-dsh-scriptor-${bundle.version}.tgz` }
  32. }
  33. if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url) {
  34. console.log(JSON.stringify(releaseVersion(root, process.env.RELEASE_TAG), null, 2))
  35. }