Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
1,336 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const chalk = require('chalk') | ||
const semver = require('semver') | ||
const { prompt } = require('enquirer') | ||
const execa = require('execa') | ||
const currentVersion = require('../package.json').version | ||
|
||
const versionIncrements = ['patch', 'minor', 'major'] | ||
|
||
const inc = (i) => semver.inc(currentVersion, i) | ||
const run = (bin, args, opts = {}) => | ||
execa(bin, args, { stdio: 'inherit', ...opts }) | ||
const step = (msg) => console.log(chalk.cyan(msg)) | ||
|
||
async function main() { | ||
let targetVersion | ||
|
||
const { release } = await prompt({ | ||
type: 'select', | ||
name: 'release', | ||
message: 'Select release type', | ||
choices: versionIncrements.map((i) => `${i} (${inc(i)})`).concat(['custom']) | ||
}) | ||
|
||
if (release === 'custom') { | ||
targetVersion = ( | ||
await prompt({ | ||
type: 'input', | ||
name: 'version', | ||
message: 'Input custom version', | ||
initial: currentVersion | ||
}) | ||
).version | ||
} else { | ||
targetVersion = release.match(/\((.*)\)/)[1] | ||
} | ||
|
||
if (!semver.valid(targetVersion)) { | ||
throw new Error(`Invalid target version: ${targetVersion}`) | ||
} | ||
|
||
const { yes: tagOk } = await prompt({ | ||
type: 'confirm', | ||
name: 'yes', | ||
message: `Releasing v${targetVersion}. Confirm?` | ||
}) | ||
|
||
if (!tagOk) { | ||
return | ||
} | ||
|
||
// Update the package version. | ||
step('\nUpdating the package version...') | ||
updatePackage(targetVersion) | ||
|
||
// Build the package. | ||
step('\nBuilding the package...') | ||
await run('yarn', ['build']) | ||
|
||
// Generate the changelog. | ||
step('\nGenerating the changelog...') | ||
await run('yarn', ['changelog']) | ||
await run('yarn', ['prettier', '--write', 'CHANGELOG.md']) | ||
|
||
const { yes: changelogOk } = await prompt({ | ||
type: 'confirm', | ||
name: 'yes', | ||
message: `Changelog generated. Does it look good?` | ||
}) | ||
|
||
if (!changelogOk) { | ||
return | ||
} | ||
|
||
// Commit changes to the Git and create a tag. | ||
step('\nCommitting changes...') | ||
await run('git', ['add', 'CHANGELOG.md', 'package.json']) | ||
await run('git', ['commit', '-m', `release: v${targetVersion}`]) | ||
await run('git', ['tag', `v${targetVersion}`]) | ||
|
||
// Publish the package. | ||
step('\nPublishing the package...') | ||
await run('yarn', [ | ||
'publish', | ||
'--new-version', | ||
targetVersion, | ||
'--no-commit-hooks', | ||
'--no-git-tag-version' | ||
]) | ||
|
||
// Push to GitHub. | ||
step('\nPushing to GitHub...') | ||
await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`]) | ||
await run('git', ['push']) | ||
} | ||
|
||
function updatePackage(version) { | ||
const pkgPath = path.resolve(path.resolve(__dirname, '..'), 'package.json') | ||
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')) | ||
|
||
pkg.version = version | ||
|
||
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n') | ||
} | ||
|
||
main().catch((err) => console.error(err)) |
Oops, something went wrong.