|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { program } = require('commander'); |
| 4 | +const { promisify } = require('util'); |
| 5 | +const exec = promisify(require('child_process').exec); |
| 6 | +const { readFileSync } = require('fs'); |
| 7 | + |
| 8 | +program |
| 9 | + .command('release [newversion] [tagmessage]') |
| 10 | + .description('Bump version, build, commit, tag, and promote to local stable branch') |
| 11 | + .action(release) |
| 12 | +program |
| 13 | + .command('publish-gh') |
| 14 | + .description('Push master and stable branches to GitHub with tags') |
| 15 | + .action(publishGH) |
| 16 | +program.parse(process.argv); |
| 17 | + |
| 18 | +/* ----- HELPER ----- */ |
| 19 | + |
| 20 | +function getVersion() { |
| 21 | + // Uses readFileSync() instead of require() to prevent caching of values. |
| 22 | + const pkg = JSON.parse(readFileSync('./package.json')); |
| 23 | + return `v${pkg.version}`; |
| 24 | +} |
| 25 | + |
| 26 | +/* ----- SUBTASKS ----- */ |
| 27 | + |
| 28 | +// Bump version using NPM (only affects package*.json, doesn't commit). |
| 29 | +function bumpVersion(newversion) { |
| 30 | + console.log('Bumping version number.'); |
| 31 | + return exec(`npm --no-git-tag-version version ${newversion}`); |
| 32 | +} |
| 33 | + |
| 34 | +// Build, commit, and tag in master with the new release version. |
| 35 | +async function buildCommitTag(tagmessage) { |
| 36 | + console.log('Running build process in master branch.'); |
| 37 | + await exec(`git checkout master && npm run build`); |
| 38 | + |
| 39 | + const version = getVersion(); |
| 40 | + const fullTagMessage = tagmessage ? `${version} ${tagmessage}` : version; |
| 41 | + |
| 42 | + console.log('Adding all changes and performing final commit.'); |
| 43 | + await exec(`git add -A && git commit --allow-empty -m "Build ${version}"`); |
| 44 | + |
| 45 | + console.log('Tagging with provided tag message.'); |
| 46 | + return exec(`git tag -a ${version} -m "${fullTagMessage}"`); |
| 47 | +} |
| 48 | + |
| 49 | +// Pushes master into the local stable branch. |
| 50 | +async function promoteToStable() { |
| 51 | + console.log('Getting repo root location.'); |
| 52 | + const res = await exec('git rev-parse --show-toplevel'); |
| 53 | + const repoRoot = res.stdout.trim('\n'); |
| 54 | + |
| 55 | + console.log('Pushing release to local stable branch.'); |
| 56 | + return exec(`git push --follow-tags ${repoRoot} master:stable`) |
| 57 | +} |
| 58 | + |
| 59 | +/* ----- TASKS ----- */ |
| 60 | + |
| 61 | +async function release(newversion, tagmessage) { |
| 62 | + await bumpVersion(newversion || 'patch'); |
| 63 | + await buildCommitTag(tagmessage); |
| 64 | + await promoteToStable(); |
| 65 | +} |
| 66 | + |
| 67 | +function publishGH() { |
| 68 | + return exec('git push --follow-tags origin master stable'); |
| 69 | +} |
0 commit comments