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