|
| 1 | +const { ManifestPlugin } = require('release-please/build/src/plugin') |
| 2 | + |
| 3 | +const matchLine = (line, re) => { |
| 4 | + const trimmed = line.trim().replace(/^[*#\s]+/, '') |
| 5 | + if (typeof re === 'string') { |
| 6 | + return trimmed === re |
| 7 | + } |
| 8 | + return trimmed.match(re) |
| 9 | +} |
| 10 | + |
| 11 | +module.exports = class WorkspaceDeps extends ManifestPlugin { |
| 12 | + run (pullRequests) { |
| 13 | + for (const { pullRequest } of pullRequests) { |
| 14 | + const depLinks = pullRequest.body.releaseData.reduce((acc, release) => { |
| 15 | + if (release.component) { |
| 16 | + const url = matchLine(release.notes.split('\n')[0], /^\[[^\]]+\]\((.*?)\)/) |
| 17 | + if (url) { |
| 18 | + acc[release.component] = url[1] |
| 19 | + } |
| 20 | + } |
| 21 | + return acc |
| 22 | + }, {}) |
| 23 | + |
| 24 | + for (const release of pullRequest.body.releaseData) { |
| 25 | + const lines = release.notes.split('\n') |
| 26 | + const newLines = [] |
| 27 | + |
| 28 | + let inWorkspaceDeps = false |
| 29 | + let collectWorkspaceDeps = false |
| 30 | + |
| 31 | + for (const line of lines) { |
| 32 | + if (matchLine(line, 'The following workspace dependencies were updated')) { |
| 33 | + // We are in the section with our workspace deps |
| 34 | + // Set the flag and discard this line since we dont want it in the final output |
| 35 | + inWorkspaceDeps = true |
| 36 | + } else if (inWorkspaceDeps) { |
| 37 | + if (collectWorkspaceDeps) { |
| 38 | + const depMatch = matchLine(line, /^(\S+) bumped from \S+ to (\S+)$/) |
| 39 | + if (depMatch) { |
| 40 | + // If we have a line that is a workspace dep update, then reformat |
| 41 | + // it and save it to the new lines |
| 42 | + const [, depName, newVersion] = depMatch |
| 43 | + const url = depLinks[depName] |
| 44 | + if (url) { |
| 45 | + newLines.push(` * [\`${depName}@${newVersion}\`](${url})`) |
| 46 | + } else { |
| 47 | + newLines.push(` * \`${depName}@${newVersion}\``) |
| 48 | + } |
| 49 | + } else { |
| 50 | + // Anything else means we are done with dependencies so ignore |
| 51 | + // this line and dont look for any more |
| 52 | + collectWorkspaceDeps = false |
| 53 | + } |
| 54 | + } else if (matchLine(line, 'dependencies')) { |
| 55 | + // Only collect dependencies discard dev deps and everything else |
| 56 | + collectWorkspaceDeps = true |
| 57 | + } else if (matchLine(line, '') || matchLine(line, /^#/)) { |
| 58 | + inWorkspaceDeps = false |
| 59 | + newLines.push(line) |
| 60 | + } |
| 61 | + } else { |
| 62 | + newLines.push(line) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + release.notes = newLines.join('\n') |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return pullRequests |
| 71 | + } |
| 72 | +} |
0 commit comments