|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const childProcess = require('child_process'); |
| 5 | +const path = require('path'); |
| 6 | +const semver = require('semver'); |
| 7 | +const moment = require('moment'); |
| 8 | +const PACKAGE_JSON_PATH = path.join(process.cwd(), 'package.json'); |
| 9 | +const CHANGELOG_PATH = path.join(process.cwd(), 'CHANGELOG.md'); |
| 10 | +const packageFile = require(PACKAGE_JSON_PATH); |
| 11 | + |
| 12 | +function exec(command) { |
| 13 | + return childProcess.execSync(command).toString().slice(0, -1); |
| 14 | +} |
| 15 | + |
| 16 | +const githubRepoUrl = exec('git config --get remote.origin.url').replace( |
| 17 | + /\.git$/, |
| 18 | + '' |
| 19 | +); |
| 20 | + |
| 21 | +function updatePackageJson(version) { |
| 22 | + fs.writeFileSync( |
| 23 | + PACKAGE_JSON_PATH, |
| 24 | + JSON.stringify(Object.assign({}, packageFile, { version }), null, 2) + '\n' |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function updateChangelog(newText) { |
| 29 | + fs.writeFileSync(CHANGELOG_PATH, newText); |
| 30 | +} |
| 31 | + |
| 32 | +function createGitCommit(version) { |
| 33 | + exec( |
| 34 | + `git commit -am 'Build: update package.json and changelog for v${version}'` |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +function createGitTag(version) { |
| 39 | + exec(`git tag v${version}`); |
| 40 | +} |
| 41 | + |
| 42 | +function getCommitSubject(commitHash) { |
| 43 | + return exec(`git --no-pager show -s --oneline --format=%s ${commitHash}`); |
| 44 | +} |
| 45 | + |
| 46 | +function getAbbreviatedCommitHash(commitHash) { |
| 47 | + return exec(`git --no-pager show -s --oneline --format=%h ${commitHash}`); |
| 48 | +} |
| 49 | + |
| 50 | +function getCommitLink(commitHash) { |
| 51 | + return `[${getAbbreviatedCommitHash(commitHash)}](${githubRepoUrl}/commit/${commitHash})`; |
| 52 | +} |
| 53 | + |
| 54 | +function replaceIssueLinks(message) { |
| 55 | + return message.replace(/#(\d+)/g, `[#$1](${githubRepoUrl}/issues/$1)`); |
| 56 | +} |
| 57 | + |
| 58 | +const commitHashes = exec(`git log --format=%H v${packageFile.version}...HEAD`) |
| 59 | + .split('\n') |
| 60 | + .filter(hash => hash); |
| 61 | + |
| 62 | +if (!commitHashes.length) { |
| 63 | + throw new RangeError('No commits since last release'); |
| 64 | +} |
| 65 | + |
| 66 | +const commitSubjects = commitHashes.map(getCommitSubject); |
| 67 | +let newVersion; |
| 68 | + |
| 69 | +if (commitSubjects.some(subject => subject.startsWith('Breaking'))) { |
| 70 | + newVersion = semver.inc(packageFile.version, 'major'); |
| 71 | +} else if ( |
| 72 | + commitSubjects.some( |
| 73 | + subject => subject.startsWith('Update') || subject.startsWith('New') |
| 74 | + ) |
| 75 | +) { |
| 76 | + newVersion = semver.inc(packageFile.version, 'minor'); |
| 77 | +} else { |
| 78 | + newVersion = semver.inc(packageFile.version, 'patch'); |
| 79 | +} |
| 80 | + |
| 81 | +const newChangelogHeader = `## v${newVersion} (${moment |
| 82 | + .utc() |
| 83 | + .format('YYYY[-]MM[-]DD')})`; |
| 84 | +const commitLines = commitHashes.map( |
| 85 | + hash => |
| 86 | + `* ${replaceIssueLinks(getCommitSubject(hash))} (${getCommitLink(hash)})` |
| 87 | +); |
| 88 | +const oldChangelog = fs.readFileSync(CHANGELOG_PATH).toString(); |
| 89 | +const newChangelog = oldChangelog.replace( |
| 90 | + /^(# Changelog\n\n)/, |
| 91 | + `$1${newChangelogHeader}\n\n${commitLines.join('\n')}\n\n` |
| 92 | +); |
| 93 | + |
| 94 | +updatePackageJson(newVersion); |
| 95 | +updateChangelog(newChangelog); |
| 96 | +createGitCommit(newVersion); |
| 97 | +createGitTag(newVersion); |
0 commit comments