File size: 2,932 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
import execa from 'execa'
import semver from 'semver'
import { existsSync } from 'fs'
import { join } from 'path'
import { readdir, readFile } from 'fs/promises'

async function fetchTagsFromRegistry(packageName: string) {
  const res = await fetch(
    `https://registry.npmjs.org/-/package/${packageName}/dist-tags`
  )
  const tags = await res.json()
  return tags
}

async function getTag({
  name,
  version,
  latest,
}: {
  name: string
  version: string
  latest: string
}): Promise<string> {
  const preConfigPath = join(process.cwd(), '.changeset', 'pre.json')

  if (existsSync(preConfigPath)) {
    const { tag, mode } = JSON.parse(await readFile(preConfigPath, 'utf-8'))
    if (mode === 'pre') {
      if (!version.includes('-')) {
        throw new Error(
          `The changeset is in pre mode, but the version of "${name}@${version}" is not prerelease. It is likely a bug from versioning the packages.`
        )
      }

      return tag
    }
  }

  if (version.includes('-')) {
    throw new Error(
      `The changeset is not in pre mode, but the version of "${name}@${version}" is prerelease. It is likely a bug from versioning the packages.`
    )
  }

  // If the current version is less than the latest,
  // it means this is a backport release. Since NPM
  // sets the 'latest' tag by default during publishing,
  // when users install `next@latest`, they might get the
  // backported version instead of the actual "latest"
  // version. Hence, we explicitly set the tag as
  // 'stable' for backports.
  if (semver.lt(version, latest)) {
    return 'stable'
  }

  return 'latest'
}

async function publishNpm() {
  if (!process.env.NPM_TOKEN) {
    throw new Error('NPM_TOKEN is not set')
  }

  const packagesDir = join(process.cwd(), 'packages')
  const packageDirs = await readdir(packagesDir, {
    withFileTypes: true,
  })

  for (const packageDir of packageDirs) {
    if (!packageDir.isDirectory()) {
      continue
    }

    const pkgJson = JSON.parse(
      await readFile(
        join(process.cwd(), 'packages', packageDir.name, 'package.json'),
        'utf-8'
      )
    )

    if (pkgJson.private) {
      continue
    }

    const tags = await fetchTagsFromRegistry(pkgJson.name)
    // If the current version is already published in the
    // registry, skip the process.
    if (semver.eq(pkgJson.version, tags.latest)) {
      console.log(
        `Skipping ${pkgJson.name}@${pkgJson.version} because it is already published.`
      )
      continue
    }

    const tag = await getTag({
      name: pkgJson.name,
      version: pkgJson.version,
      latest: tags.latest,
    })

    const packagePath = join(packagesDir, packageDir.name)
    const args = ['publish', packagePath, '--tag', tag]

    console.log(
      `Running command: "pnpm ${args.join(' ')}" for ${pkgJson.name}@${pkgJson.version}`
    )
    await execa('pnpm', args, { stdio: 'inherit' })
  }
}

publishNpm()