-
Notifications
You must be signed in to change notification settings - Fork 0
/
prerelease.ts
106 lines (95 loc) · 2.92 KB
/
prerelease.ts
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
import fs from 'fs/promises'
import path from 'path'
import { exec } from 'child_process'
import inquirer from 'inquirer'
import semver from 'semver'
import { getPackages } from '@manypkg/get-packages'
const cwd = process.cwd()
async function updatePackageJson(pkgJsonPath: string, version: string | null) {
const pkgRaw = await fs.readFile(pkgJsonPath, { encoding: 'utf-8' })
const stringified = pkgRaw.replace(
/("version".*?) (".*?")/i,
`$1 "${version}"`,
)
await fs.writeFile(pkgJsonPath, stringified)
}
async function ignorePackage(pkgName: string) {
const changesetConfigPath = path.resolve(cwd, '.changeset/config.json')
const rawConfig = await fs.readFile(changesetConfigPath, {
encoding: 'utf-8',
})
const jsonConfig = JSON.parse(rawConfig)
const ignorePkgs = jsonConfig.ignore || []
if (!ignorePkgs.includes(pkgName)) {
jsonConfig.ignore = [pkgName, ...ignorePkgs]
const stringified = JSON.stringify(jsonConfig, null, 2)
await fs.writeFile(changesetConfigPath, stringified, { encoding: 'utf-8' })
}
}
async function main() {
const { packages } = await getPackages(cwd)
const choices = packages
.map(({ packageJson }) => ({
name: `${packageJson.name} (${packageJson.version})`,
value: packageJson.name,
}))
// @ts-ignore
.concat(new inquirer.Separator())
const { pkgName } = await inquirer.prompt([
{
pageSize: 12,
name: 'pkgName',
message: 'Which package to make a pre-release?',
type: 'list',
choices,
},
])
const { packageJson, dir } = packages.find(
({ packageJson }) => packageJson.name === pkgName,
)!
const { version, name } = packageJson
const prereleaseTag = semver.prerelease(version)?.[0]
const { tag, publish } = await inquirer.prompt([
{
name: 'tag',
message: 'Which tag should be used for the pre-release?',
type: 'list',
choices: [
{
name: 'alpha',
},
{
name: 'beta',
},
],
default: prereleaseTag,
},
{
name: 'publish',
message: 'Should the package be published?',
type: 'confirm',
},
])
const increase = prereleaseTag === tag ? 'prerelease' : 'preminor'
const newVersion = semver.inc(version, increase, tag)
await updatePackageJson(path.resolve(dir, 'package.json'), newVersion)
// Avoid chageset publishing it, by adding the package to the ignore list
await ignorePackage(name)
if (publish) {
// TODO: remove dry-run, kept here for testing before merging
await exec(`npm publish ${dir} --tag ${tag}`, (error, stdout, stderr) => {
if (!error) {
console.log(stdout)
console.log(
`${name}@${newVersion} published: \nhttps://www.npmjs.com/package/${name}\n`,
)
} else {
console.error(error)
console.error(stderr)
}
})
} else {
console.log(`Version for ${name} updated on package.json.`)
}
}
main()