|
| 1 | +import * as clc from "cli-color"; |
| 2 | +import * as marked from "marked"; |
| 3 | +import * as path from "path"; |
| 4 | +import * as semver from "semver"; |
| 5 | +import TerminalRenderer = require("marked-terminal"); |
| 6 | +import Table = require("cli-table"); |
| 7 | + |
| 8 | +import { listExtensionVersions, parseRef } from "./extensionsApi"; |
| 9 | +import { readFile } from "./localHelper"; |
| 10 | +import { logger } from "../logger"; |
| 11 | +import { logLabeledWarning } from "../utils"; |
| 12 | + |
| 13 | +marked.setOptions({ |
| 14 | + renderer: new TerminalRenderer(), |
| 15 | +}); |
| 16 | + |
| 17 | +const EXTENSIONS_CHANGELOG = "CHANGELOG.md"; |
| 18 | +const VERSION_LINE_REGEX = /##.*(\d+\.\d+\.\d+).*/; |
| 19 | + |
| 20 | +/* |
| 21 | + * getReleaseNotesForUpdate fetches all version between toVersion and fromVersion and returns the relase notes |
| 22 | + * for those versions if they exist. |
| 23 | + * @param extensionRef |
| 24 | + * @param fromVersion the version you are updating from |
| 25 | + * @param toVersion the version you are upodating to |
| 26 | + * @returns a Record of version number to releaseNotes for that version |
| 27 | + */ |
| 28 | +export async function getReleaseNotesForUpdate(args: { |
| 29 | + extensionRef: string; |
| 30 | + fromVersion: string; |
| 31 | + toVersion: string; |
| 32 | +}): Promise<Record<string, string>> { |
| 33 | + const releaseNotes: Record<string, string> = {}; |
| 34 | + const filter = `id<="${args.toVersion}" AND id>"${args.fromVersion}"`; |
| 35 | + const extensionVersions = await listExtensionVersions(args.extensionRef, filter); |
| 36 | + extensionVersions.sort((ev1, ev2) => { |
| 37 | + return -semver.compare(ev1.spec.version, ev2.spec.version); |
| 38 | + }); |
| 39 | + for (const extensionVersion of extensionVersions) { |
| 40 | + if (extensionVersion.releaseNotes) { |
| 41 | + const version = parseRef(extensionVersion.ref).version!; |
| 42 | + releaseNotes[version] = extensionVersion.releaseNotes; |
| 43 | + } |
| 44 | + } |
| 45 | + return releaseNotes; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * displayReleaseNotes prints out a nicely formatted table containing all release notes in an update. |
| 50 | + * If there is a major version change, it also prints a warning and highlights those release notes. |
| 51 | + */ |
| 52 | +export function displayReleaseNotes(releaseNotes: Record<string, string>, fromVersion: string) { |
| 53 | + const versions = [fromVersion].concat(Object.keys(releaseNotes)); |
| 54 | + const breakingVersions = breakingChangesInUpdate(versions); |
| 55 | + const table = new Table({ head: ["Version", "What's New"], style: { head: ["yellow", "bold"] } }); |
| 56 | + for (const [version, note] of Object.entries(releaseNotes)) { |
| 57 | + if (breakingVersions.includes(version)) { |
| 58 | + table.push([clc.yellow.bold(version), marked(note)]); |
| 59 | + } else { |
| 60 | + table.push([version, marked(note)]); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + logger.info(clc.bold("What's new with this update:")); |
| 65 | + if (breakingVersions.length) { |
| 66 | + logLabeledWarning( |
| 67 | + "warning", |
| 68 | + "This is a major version update, which means it may contain breaking changes." + |
| 69 | + " Read the release notes carefully before continuing with this update." |
| 70 | + ); |
| 71 | + } |
| 72 | + logger.info(table.toString()); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * breakingChangesInUpdate identifies which versions in an update are major changes. |
| 77 | + * Exported for testing. |
| 78 | + */ |
| 79 | +export function breakingChangesInUpdate(versionsInUpdate: string[]): string[] { |
| 80 | + const breakingVersions: string[] = []; |
| 81 | + const semvers = versionsInUpdate.map((v) => semver.parse(v)!).sort(semver.compare); |
| 82 | + for (let i = 1; i < semvers.length; i++) { |
| 83 | + const hasMajorBump = semvers[i - 1].major < semvers[i].major; |
| 84 | + const hasMinorBumpInPreview = |
| 85 | + semvers[i - 1].major == 0 && semvers[i].major == 0 && semvers[i - 1].minor < semvers[i].minor; |
| 86 | + if (hasMajorBump || hasMinorBumpInPreview) { |
| 87 | + breakingVersions.push(semvers[i].raw); |
| 88 | + } |
| 89 | + } |
| 90 | + return breakingVersions; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * getLocalChangelog checks directory for a CHANGELOG.md, and parses it into a map of |
| 95 | + * version to release notes for that version. |
| 96 | + * @param directory The directory to check for |
| 97 | + * @returns |
| 98 | + */ |
| 99 | +export function getLocalChangelog(directory: string): Record<string, string> { |
| 100 | + const rawChangelog = readFile(path.resolve(directory, EXTENSIONS_CHANGELOG)); |
| 101 | + return parseChangelog(rawChangelog); |
| 102 | +} |
| 103 | + |
| 104 | +// Exported for testing. |
| 105 | +export function parseChangelog(rawChangelog: string): Record<string, string> { |
| 106 | + const changelog: Record<string, string> = {}; |
| 107 | + let currentVersion = ""; |
| 108 | + for (const line of rawChangelog.split("\n")) { |
| 109 | + const matches = line.match(VERSION_LINE_REGEX); |
| 110 | + if (matches) { |
| 111 | + currentVersion = matches[1]; // The first capture group is the SemVer. |
| 112 | + } else if (currentVersion) { |
| 113 | + // Throw away lines that aren't under a specific version. |
| 114 | + if (!changelog[currentVersion]) { |
| 115 | + changelog[currentVersion] = line; |
| 116 | + } else { |
| 117 | + changelog[currentVersion] += `\n${line}`; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return changelog; |
| 122 | +} |
0 commit comments