|
| 1 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 2 | + |
| 3 | +const { |
| 4 | + getInfo, |
| 5 | + getInfoFromPullRequest, |
| 6 | +} = require("@changesets/get-github-info"); |
| 7 | +const changesetsChangelogGithub = |
| 8 | + require("@changesets/changelog-github").default; |
| 9 | + |
| 10 | +/** |
| 11 | + * Vendored `@changesets/changelog-github` but with the changelog entry prefix used as a suffix instead. |
| 12 | + * Before: #1 abcdef0 Thanks @eps1lon! - This is a summary |
| 13 | + * After: This is a summary (#1 abcdef0 by @eps1lon!) |
| 14 | + * |
| 15 | + * The change itself is the most important bit that should be easily scannable. |
| 16 | + * PR and commit are just metadata for further inspection. |
| 17 | + * |
| 18 | + * @type {import("@changesets/types").ChangelogFunctions} |
| 19 | + */ |
| 20 | +const changelogFunctions = { |
| 21 | + ...changesetsChangelogGithub, |
| 22 | + getReleaseLine: async (changeset, type, options) => { |
| 23 | + if (!options || !options.repo) { |
| 24 | + throw new Error( |
| 25 | + 'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]', |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + /** @type {number | undefined} */ |
| 30 | + let prFromSummary; |
| 31 | + /** @type {string | undefined} */ |
| 32 | + let commitFromSummary; |
| 33 | + /** @type {string[]} */ |
| 34 | + let usersFromSummary = []; |
| 35 | + |
| 36 | + const replacedChangelog = changeset.summary |
| 37 | + .replace(/^\s*(?:pr|pull|pull\s+request):\s*#?(\d+)/im, (_, pr) => { |
| 38 | + let num = Number(pr); |
| 39 | + if (!isNaN(num)) prFromSummary = num; |
| 40 | + return ""; |
| 41 | + }) |
| 42 | + .replace(/^\s*commit:\s*([^\s]+)/im, (_, commit) => { |
| 43 | + commitFromSummary = commit; |
| 44 | + return ""; |
| 45 | + }) |
| 46 | + .replace(/^\s*(?:author|user):\s*@?([^\s]+)/gim, (_, user) => { |
| 47 | + usersFromSummary.push(user); |
| 48 | + return ""; |
| 49 | + }) |
| 50 | + .trim(); |
| 51 | + |
| 52 | + const [firstLine, ...futureLines] = replacedChangelog |
| 53 | + .split("\n") |
| 54 | + .map((l) => l.trimRight()); |
| 55 | + |
| 56 | + const links = await (async () => { |
| 57 | + if (prFromSummary !== undefined) { |
| 58 | + let { links } = await getInfoFromPullRequest({ |
| 59 | + repo: options.repo, |
| 60 | + pull: prFromSummary, |
| 61 | + }); |
| 62 | + if (commitFromSummary) { |
| 63 | + const shortCommitId = commitFromSummary.slice(0, 7); |
| 64 | + links = { |
| 65 | + ...links, |
| 66 | + commit: `[\`${shortCommitId}\`](https://github.com/${options.repo}/commit/${commitFromSummary})`, |
| 67 | + }; |
| 68 | + } |
| 69 | + return links; |
| 70 | + } |
| 71 | + const commitToFetchFrom = commitFromSummary || changeset.commit; |
| 72 | + if (commitToFetchFrom) { |
| 73 | + let { links } = await getInfo({ |
| 74 | + repo: options.repo, |
| 75 | + commit: commitToFetchFrom, |
| 76 | + }); |
| 77 | + return links; |
| 78 | + } |
| 79 | + return { |
| 80 | + commit: null, |
| 81 | + pull: null, |
| 82 | + user: null, |
| 83 | + }; |
| 84 | + })(); |
| 85 | + |
| 86 | + const users = usersFromSummary.length |
| 87 | + ? usersFromSummary |
| 88 | + .map( |
| 89 | + (userFromSummary) => |
| 90 | + `[@${userFromSummary}](https://github.com/${userFromSummary})`, |
| 91 | + ) |
| 92 | + .join(", ") |
| 93 | + : links.user; |
| 94 | + |
| 95 | + const suffix = [ |
| 96 | + links.pull === null ? null : `${links.pull}`, |
| 97 | + links.commit === null ? null : `${links.commit}`, |
| 98 | + users === null ? null : `by ${users}`, |
| 99 | + ] |
| 100 | + .filter((part) => part !== null) |
| 101 | + .join(" "); |
| 102 | + |
| 103 | + return `\n\n- ${firstLine}${suffix ? ` (${suffix})` : ""}\n${futureLines |
| 104 | + .map((l) => ` ${l}`) |
| 105 | + .join("\n")}`; |
| 106 | + }, |
| 107 | +}; |
| 108 | + |
| 109 | +exports["default"] = changelogFunctions; |
0 commit comments