File size: 3,453 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import execa from 'execa'
import { existsSync } from 'fs'
import { writeFile, readFile, unlink } from 'fs/promises'
import { join } from 'path'

// NOTE: This type may change over time.
type ChangesetStatusJson = {
  changesets: {
    releases: {
      name: string
      type: string
      summary: string
      id: string
    }[]
  }[]
  releases: {
    name: string
    type: string
    oldVersion: string
    changesets: string[]
    newVersion: string
  }[]
}

async function versionPackages() {
  const preConfigPath = join(process.cwd(), '.changeset', 'pre.json')

  // Exit previous pre mode to prepare for the next release.
  if (existsSync(preConfigPath)) {
    if (require(preConfigPath).mode !== 'exit') {
      // Since current repository is in pre mode, need
      // to exit before versioning the packages.
      await execa('pnpm', ['changeset', 'pre', 'exit'], {
        stdio: 'inherit',
      })
    }
  }

  // For prereleases, we need to set the "mode" on `pre.json`, which
  // can be done by running `changeset pre enter <mode>`.
  const releaseType = process.env.RELEASE_TYPE
  switch (releaseType) {
    case 'canary': {
      // Enter pre mode as "canary" tag.
      await execa('pnpm', ['changeset', 'pre', 'enter', 'canary'], {
        stdio: 'inherit',
      })

      console.log(
        '▲   Preparing to bump the canary version, checking if there are any changesets.'
      )

      // Create an empty changeset for `next` to bump the canary version
      // even if there are no changesets for `next`.
      await execa('pnpm', [
        'changeset',
        'status',
        '--output',
        './changeset-status.json',
      ])

      let hasNextChangeset = false
      if (existsSync('./changeset-status.json')) {
        const changesetStatus: ChangesetStatusJson = JSON.parse(
          await readFile('./changeset-status.json', 'utf8')
        )

        console.log('▲   Changeset Status:')
        console.log(changesetStatus)

        hasNextChangeset =
          changesetStatus.releases.find(
            (release) => release.name === 'next'
          ) !== undefined

        await unlink('./changeset-status.json')
      }

      if (!hasNextChangeset) {
        console.log(
          '▲   No changesets found for `next`, creating an empty changeset.'
        )
        // TODO: Since this is temporary until we hard-require a changeset, we will
        // need to remove this in the future to prevent publishing empty releases.
        await writeFile(
          join(process.cwd(), '.changeset', `next-canary-${Date.now()}.md`),
          `---\n'next': patch\n---`
        )
      }
      break
    }
    case 'release-candidate': {
      // Enter pre mode as "rc" tag.
      await execa('pnpm', ['changeset', 'pre', 'enter', 'rc'], {
        stdio: 'inherit',
      })
      break
    }
    case 'stable': {
      // No additional steps needed for 'stable' releases since we've already
      // exited any pre-release mode. Only need to run `changeset version` after.
      break
    }
    default: {
      throw new Error(`Invalid release type: ${releaseType}`)
    }
  }

  await execa('pnpm', ['changeset', 'version'], {
    stdio: 'inherit',
  })
  // TODO: Update the pnpm-lock.yaml since the packages' depend on
  // each other. Remove this once they use `workspace:` protocol.
  await execa('pnpm', ['install', '--no-frozen-lockfile'], {
    stdio: 'inherit',
  })
}

versionPackages()