|
| 1 | +import readline from "node:readline"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import {fileURLToPath} from "node:url"; |
| 4 | + |
| 5 | +function handleDependencyBump(line) { |
| 6 | + line = line.replace("[@ui5](https://github.com/ui5)", "@ui5"); |
| 7 | + const moduleMatch = line.match(/Bump (@ui5\/[^\s]+).*to ([^ ]+)/); |
| 8 | + if (moduleMatch) { |
| 9 | + const [, moduleName, moduleVersion] = moduleMatch; |
| 10 | + const changelogPath = fileURLToPath( |
| 11 | + new URL(`./CHANGELOG.md`, import.meta.resolve(`${moduleName}/package.json`))); |
| 12 | + const changelog = fs.readFileSync(changelogPath, { |
| 13 | + encoding: "utf8" |
| 14 | + }); |
| 15 | + const sectionRegExp = |
| 16 | + new RegExp(`^## \\[v${moduleVersion.replace(".", "\\.")}\\].+\\n((?:.|\\n)+?)(?=^<a )`, "m"); |
| 17 | + const changelogMatch = changelog.match(sectionRegExp); |
| 18 | + if (!changelogMatch) { |
| 19 | + throw new Error(`Failed to find relevant changelog for ${moduleName}@${moduleVersion}`); |
| 20 | + } |
| 21 | + let versionChangelog = changelogMatch[1]; |
| 22 | + // In case of an empty changelog, we still match the newline with a length of 1 |
| 23 | + if (versionChangelog.length > 1) { |
| 24 | + versionChangelog = versionChangelog.replace(/^### /gm, "#### "); |
| 25 | + versionChangelog = versionChangelog.replace(/^./gm, " $&"); |
| 26 | + const repoUrl = `https://github.com/SAP/${moduleName.replace("@ui5/", "ui5-")}/tree/v${moduleVersion}`; |
| 27 | + line += ` |
| 28 | + - Changes contained in [${moduleName}@${moduleVersion}](${repoUrl}): |
| 29 | +
|
| 30 | +${versionChangelog}`; |
| 31 | + } else { |
| 32 | + // In case of an empty changelog: Only add the required newline |
| 33 | + line += "\n"; |
| 34 | + } |
| 35 | + } |
| 36 | + return line; |
| 37 | +} |
| 38 | + |
| 39 | +function readStdin() { |
| 40 | + return new Promise((resolve, reject) => { |
| 41 | + const rl = readline.createInterface({ |
| 42 | + input: process.stdin, |
| 43 | + }); |
| 44 | + |
| 45 | + let buffer = ""; |
| 46 | + rl.on("line", (line) => { |
| 47 | + try { |
| 48 | + if (line.startsWith("- Bump")) { |
| 49 | + buffer += `${handleDependencyBump(line)}`; |
| 50 | + } else { |
| 51 | + buffer += `${line}\n`; |
| 52 | + } |
| 53 | + } catch (err) { |
| 54 | + reject(err); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + rl.on("pause", () => { |
| 59 | + resolve(buffer); |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +readStdin().then((result) => { |
| 65 | + process.stdout.write(result); // Don't use console.log since one new line at the end is already enough |
| 66 | +}); |
0 commit comments