|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const chalk = require('chalk') |
| 4 | +const semver = require('semver') |
| 5 | +const { prompt } = require('enquirer') |
| 6 | +const execa = require('execa') |
| 7 | +const currentVersion = require('../package.json').version |
| 8 | + |
| 9 | +const versionIncrements = ['patch', 'minor', 'major'] |
| 10 | + |
| 11 | +const inc = (i) => semver.inc(currentVersion, i) |
| 12 | +const run = (bin, args, opts = {}) => |
| 13 | + execa(bin, args, { stdio: 'inherit', ...opts }) |
| 14 | +const step = (msg) => console.log(chalk.cyan(msg)) |
| 15 | + |
| 16 | +async function main() { |
| 17 | + let targetVersion |
| 18 | + |
| 19 | + const { release } = await prompt({ |
| 20 | + type: 'select', |
| 21 | + name: 'release', |
| 22 | + message: 'Select release type', |
| 23 | + choices: versionIncrements.map((i) => `${i} (${inc(i)})`).concat(['custom']) |
| 24 | + }) |
| 25 | + |
| 26 | + if (release === 'custom') { |
| 27 | + targetVersion = ( |
| 28 | + await prompt({ |
| 29 | + type: 'input', |
| 30 | + name: 'version', |
| 31 | + message: 'Input custom version', |
| 32 | + initial: currentVersion |
| 33 | + }) |
| 34 | + ).version |
| 35 | + } else { |
| 36 | + targetVersion = release.match(/\((.*)\)/)[1] |
| 37 | + } |
| 38 | + |
| 39 | + if (!semver.valid(targetVersion)) { |
| 40 | + throw new Error(`Invalid target version: ${targetVersion}`) |
| 41 | + } |
| 42 | + |
| 43 | + const { yes: tagOk } = await prompt({ |
| 44 | + type: 'confirm', |
| 45 | + name: 'yes', |
| 46 | + message: `Releasing v${targetVersion}. Confirm?` |
| 47 | + }) |
| 48 | + |
| 49 | + if (!tagOk) { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + // Update the package version. |
| 54 | + step('\nUpdating the package version...') |
| 55 | + updatePackage(targetVersion) |
| 56 | + |
| 57 | + // Build the package. |
| 58 | + step('\nBuilding the package...') |
| 59 | + await run('yarn', ['build']) |
| 60 | + |
| 61 | + // Generate the changelog. |
| 62 | + step('\nGenerating the changelog...') |
| 63 | + await run('yarn', ['changelog']) |
| 64 | + await run('yarn', ['prettier', '--write', 'CHANGELOG.md']) |
| 65 | + |
| 66 | + const { yes: changelogOk } = await prompt({ |
| 67 | + type: 'confirm', |
| 68 | + name: 'yes', |
| 69 | + message: `Changelog generated. Does it look good?` |
| 70 | + }) |
| 71 | + |
| 72 | + if (!changelogOk) { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + // Commit changes to the Git and create a tag. |
| 77 | + step('\nCommitting changes...') |
| 78 | + await run('git', ['add', 'CHANGELOG.md', 'package.json']) |
| 79 | + await run('git', ['commit', '-m', `release: v${targetVersion}`]) |
| 80 | + await run('git', ['tag', `v${targetVersion}`]) |
| 81 | + |
| 82 | + // Publish the package. |
| 83 | + step('\nPublishing the package...') |
| 84 | + await run('yarn', [ |
| 85 | + 'publish', |
| 86 | + '--new-version', |
| 87 | + targetVersion, |
| 88 | + '--no-commit-hooks', |
| 89 | + '--no-git-tag-version' |
| 90 | + ]) |
| 91 | + |
| 92 | + // Push to GitHub. |
| 93 | + step('\nPushing to GitHub...') |
| 94 | + await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`]) |
| 95 | + await run('git', ['push']) |
| 96 | +} |
| 97 | + |
| 98 | +function updatePackage(version) { |
| 99 | + const pkgPath = path.resolve(path.resolve(__dirname, '..'), 'package.json') |
| 100 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')) |
| 101 | + |
| 102 | + pkg.version = version |
| 103 | + |
| 104 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n') |
| 105 | +} |
| 106 | + |
| 107 | +main().catch((err) => console.error(err)) |
0 commit comments