|
| 1 | +const { exec, spawn } = require("node:child_process"); |
| 2 | +const { createReadStream } = require("node:fs"); |
| 3 | +const { createInterface } = require("node:readline"); |
| 4 | +const { resolve: resolvePath } = require("node:path"); |
| 5 | +const { writeFile } = require("node:fs/promises"); |
| 6 | + |
| 7 | +/** |
| 8 | + * Returns a string of the new changelog entries by running `npx changelog-maker |
| 9 | + * --format=markdown`. |
| 10 | + * |
| 11 | + * @returns {Promise<string>} |
| 12 | + */ |
| 13 | +async function getNewChangelogEntries() { |
| 14 | + const { stdout, stderr } = await new Promise((resolve, reject) => { |
| 15 | + // Echo an empty string to pass as the GitHub Personal Access Token |
| 16 | + // (PAT). This causes the process to error if no PAT is found in the |
| 17 | + // changelog-maker configuration file. |
| 18 | + exec("echo '' | npx changelog-maker --format=markdown", (err, stdout, stderr) => { |
| 19 | + if (err) { |
| 20 | + reject(err); |
| 21 | + } else { |
| 22 | + resolve({ stdout, stderr }); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + }); |
| 27 | + |
| 28 | + return { stdout, stderr }; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Returns the text of the changelog file, excluding header lines. |
| 33 | + * |
| 34 | + * @param {string} changelogPath Path to changelog file |
| 35 | + * @returns {Promise<string>} |
| 36 | + */ |
| 37 | +async function getExistingChangelogText(changelogPath) { |
| 38 | + const data = await new Promise((resolve, reject) => { |
| 39 | + try { |
| 40 | + const rl = createInterface(createReadStream(changelogPath)); |
| 41 | + |
| 42 | + let lines = ""; |
| 43 | + let lineNumber = 1; |
| 44 | + |
| 45 | + rl.on('line', function (line) { |
| 46 | + if (lineNumber > 2) { |
| 47 | + lines += line + "\n"; |
| 48 | + } |
| 49 | + |
| 50 | + lineNumber++; |
| 51 | + }); |
| 52 | + |
| 53 | + rl.on('close', () => { |
| 54 | + resolve(lines); |
| 55 | + }); |
| 56 | + |
| 57 | + rl.on('error', (err) => { |
| 58 | + reject(err); |
| 59 | + }); |
| 60 | + |
| 61 | + } catch (e) { |
| 62 | + reject(e); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + return data; |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +/** |
| 71 | + * Returns the string for the new changelog file. |
| 72 | + * |
| 73 | + * @param {string} newEntries New changelog entries |
| 74 | + * @param {string} existingText Existing changelog text |
| 75 | + * @param {string} author Author of the release |
| 76 | + * @returns {string} |
| 77 | + */ |
| 78 | +function generateChangelogText(newEntries, existingText, author = "github-actions\\[bot]") { |
| 79 | + const packageVersion = require("../package.json").version; |
| 80 | + const currentDateString = new Date().toISOString().split(/T/)[0]; |
| 81 | + |
| 82 | + const notableChanges = Array.from(newEntries.matchAll(/ (- [^(]+) \([^)]+\)( \[#\d+]\([^)]+\))?/g)) |
| 83 | + .map(matches => matches[1]) |
| 84 | + .join("\n"); |
| 85 | + |
| 86 | + return `# node-api-headers Changelog |
| 87 | +
|
| 88 | +## ${currentDateString} Version ${packageVersion}, ${author} |
| 89 | +
|
| 90 | +### Notable changes |
| 91 | +
|
| 92 | +${notableChanges} |
| 93 | +
|
| 94 | +### Commits |
| 95 | +
|
| 96 | +${newEntries.trim()} |
| 97 | +
|
| 98 | +${existingText.trim()} |
| 99 | +`; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Throws an error (asynchronously) if there are uncommitted changes to the changelog file. |
| 104 | + * |
| 105 | + * @param {string} changelogPath Path to changelog file |
| 106 | + * @returns {Promise<void>} |
| 107 | + */ |
| 108 | +function assertCleanChangelog(changelogPath) { |
| 109 | + return new Promise((resolve, reject) => { |
| 110 | + const spawned = spawn("git", ["diff", "--quiet", changelogPath]); |
| 111 | + spawned.on('exit', function (exitCode) { |
| 112 | + if (exitCode === 0) { |
| 113 | + resolve(undefined); |
| 114 | + } else { |
| 115 | + reject(new Error(`There are uncommitted changes to ${changelogPath}. Commit, revert, or stash changes first.`)); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + spawned.on('error', function (err) { |
| 120 | + reject(err); |
| 121 | + }); |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +async function main() { |
| 126 | + const changelogPath = resolvePath(__dirname, "..", "CHANGELOG.md"); |
| 127 | + await assertCleanChangelog(changelogPath); |
| 128 | + const [{ stdout: newEntires, stderr }, existingText] = await Promise.all([getNewChangelogEntries(), getExistingChangelogText(changelogPath)]); |
| 129 | + const changelogText = generateChangelogText(newEntires, existingText); |
| 130 | + |
| 131 | + await writeFile(changelogPath, changelogText); |
| 132 | + if (stderr) { |
| 133 | + console.error("stderr from changelog-maker:\n", stderr) |
| 134 | + } |
| 135 | + console.log(`Changelog written to ${changelogPath}`); |
| 136 | +} |
| 137 | + |
| 138 | +main().catch(e => { |
| 139 | + console.error(e); |
| 140 | + process.exitCode = 1; |
| 141 | +}); |
0 commit comments