|
| 1 | +const execa = require(require.resolve('execa')) |
| 2 | +const { promisify } = require('util') |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const read = promisify(fs.readFile) |
| 6 | +const write = fs.writeFileSync |
| 7 | + |
| 8 | +function extractSpecificChangelog (changelog, version) { |
| 9 | + if (!changelog) { |
| 10 | + return null |
| 11 | + } |
| 12 | + const escapedVersion = version.replace(/\./g, '\\.') |
| 13 | + const regex = new RegExp( |
| 14 | + `(#+?\\s\\[?v?${escapedVersion}\\]?[\\s\\S]*?)(#+?\\s\\[?v?\\d+?\\.\\d+?\\.\\d+?\\]?)`, |
| 15 | + 'g' |
| 16 | + ) |
| 17 | + const matches = regex.exec(changelog) |
| 18 | + return matches ? matches[1] : null |
| 19 | +} |
| 20 | + |
| 21 | +async function commitChangelog (current, next) { |
| 22 | + const { stdout } = await execa('npx', ['lerna-changelog', '--next-version', `v${next}`]) |
| 23 | + const escapedVersion = next.replace(/\./g, '\\.') |
| 24 | + const regex = new RegExp( |
| 25 | + `(#+?\\s\\[?v?${escapedVersion}\\]?[\\s\\S]*?)(#+?\\s\\[?v?\\d\\.\\d\\.\\d\\]?)`, |
| 26 | + 'g' |
| 27 | + ) |
| 28 | + const matches = regex.exec(stdout.toString()) |
| 29 | + const head = matches ? matches[1] : stdout |
| 30 | + const changelog = await read('./CHANGELOG.md', 'utf8') |
| 31 | + return write('./CHANGELOG.md', `${head}\n\n${changelog}`) |
| 32 | +} |
| 33 | + |
| 34 | +module.exports = { |
| 35 | + updateChangelog: false, |
| 36 | + beforeCommitChanges: ({ nextVersion, exec, dir }) => { |
| 37 | + return new Promise((resolve) => { |
| 38 | + const pkg = require('./package.json') |
| 39 | + commitChangelog(pkg.version, nextVersion).then(resolve) |
| 40 | + }) |
| 41 | + }, |
| 42 | + formatCommitMessage: ({ version, releaseType, mergeStrategy, baseBranch }) => |
| 43 | + `${releaseType} release v${version}`, |
| 44 | + formatPullRequestTitle: ({ version, releaseType }) => `${releaseType} release v${version}`, |
| 45 | + shouldRelease: () => true, |
| 46 | + releases: { |
| 47 | + extractChangelog: ({ version, dir }) => { |
| 48 | + const changelogPath = path.resolve(dir, 'CHANGELOG.md') |
| 49 | + try { |
| 50 | + const changelogFile = fs.readFileSync(changelogPath, 'utf-8').toString() |
| 51 | + const ret = extractSpecificChangelog(changelogFile, version) |
| 52 | + return ret |
| 53 | + } catch (err) { |
| 54 | + if (err.code === 'ENOENT') { |
| 55 | + return null |
| 56 | + } |
| 57 | + throw err |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments