-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathprerelease.mjs
More file actions
67 lines (58 loc) · 1.92 KB
/
prerelease.mjs
File metadata and controls
67 lines (58 loc) · 1.92 KB
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
import fs from 'fs/promises'
import path from 'path'
import inquirer from 'inquirer'
import semver from 'semver'
import { getPackages } from '@manypkg/get-packages'
import { exec } from 'child_process'
const cwd = process.cwd()
async function updatePackageJson(pkgJsonPath, version) {
const pkgRaw = await fs.readFile(pkgJsonPath, { encoding: 'utf-8' })
const stringified = pkgRaw.replace(/("version".*?) (".*?")/i, `$1 "${version}"`)
await fs.writeFile(pkgJsonPath, stringified)
}
async function main() {
const { packages } = await getPackages(cwd)
const choices = packages.map(({ packageJson }) => ({
name: `${packageJson.name} (${packageJson.version})`,
value: packageJson.name
})).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 newVersion = semver.inc(version, 'prerelease', tag)
await updatePackageJson(path.resolve(dir, 'package.json'), newVersion)
if(publish) {
// TODO: remove dry-run
await exec(`npm publish ${dir} --tag ${tag} --dry-run`, (error, stdout) => {
if(!error) {
console.log(stdout)
console.log(`${name}@${newVersion} published: https://www.npmjs.com/package/${name}\n`)
}
})
} else {
console.log(`Version for ${name} updated on package.json`)
}
}
main()