|
| 1 | +import spawn from '@npmcli/promise-spawn' |
| 2 | +import getPackages from 'get-monorepo-packages' |
| 3 | +import path from 'path' |
| 4 | +import fs from 'fs' |
| 5 | +import { exists } from '../utils/exists' |
| 6 | + |
| 7 | +export type Config = { |
| 8 | + isDryRun: boolean |
| 9 | + tags: Tag[] |
| 10 | +} |
| 11 | + |
| 12 | +export type Tag = { |
| 13 | + name: string |
| 14 | + versionNumber: string |
| 15 | + raw: string |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * |
| 20 | + * @returns list of tags |
| 21 | + * @example ["@segment/[email protected]", "@segment/[email protected]"] |
| 22 | + */ |
| 23 | +export const getCurrentGitTags = async (): Promise<Tag[]> => { |
| 24 | + const { stdout, stderr, code } = await spawn('git', [ |
| 25 | + 'tag', |
| 26 | + '--points-at', |
| 27 | + 'HEAD', |
| 28 | + '--column', |
| 29 | + ]) |
| 30 | + if (code !== 0) { |
| 31 | + throw new Error(stderr.toString()) |
| 32 | + } |
| 33 | + |
| 34 | + return parseRawTags(stdout.toString()) |
| 35 | +} |
| 36 | + |
| 37 | +export const getConfig = async ({ |
| 38 | + DRY_RUN, |
| 39 | + TAGS, |
| 40 | +}: NodeJS.ProcessEnv): Promise<Config> => { |
| 41 | + const isDryRun = Boolean(DRY_RUN) |
| 42 | + const tags = TAGS ? parseRawTags(TAGS) : await getCurrentGitTags() |
| 43 | + |
| 44 | + if (!tags.length) { |
| 45 | + throw new Error('No git tags found.') |
| 46 | + } |
| 47 | + return { |
| 48 | + isDryRun, |
| 49 | + tags, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +const getChangelogPath = (packageName: string): string | undefined => { |
| 54 | + const result = getPackages('.').find((p) => |
| 55 | + p.package.name.includes(packageName) |
| 56 | + ) |
| 57 | + if (!result) |
| 58 | + throw new Error(`could not find package with name: ${packageName}.`) |
| 59 | + |
| 60 | + let changelogPath = undefined |
| 61 | + for (const fileName of ['CHANGELOG.MD', 'CHANGELOG.md']) { |
| 62 | + if (changelogPath) break |
| 63 | + const myPath = path.join(result.location, fileName) |
| 64 | + const pathExists = fs.existsSync(myPath) |
| 65 | + if (pathExists) { |
| 66 | + changelogPath = myPath |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (changelogPath) { |
| 71 | + return changelogPath |
| 72 | + } else { |
| 73 | + console.log(`could not find changelog path for ${result.location}`) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * |
| 79 | + * @returns list of tags |
| 80 | + * @example ["@segment/[email protected]", "@segment/[email protected]"] |
| 81 | + */ |
| 82 | +const createGithubRelease = async ( |
| 83 | + tag: string, |
| 84 | + releaseNotes?: string |
| 85 | +): Promise<void> => { |
| 86 | + const { stderr, code } = await spawn('gh', [ |
| 87 | + 'release', |
| 88 | + 'create', |
| 89 | + tag, |
| 90 | + '--title', |
| 91 | + tag, |
| 92 | + '--notes', |
| 93 | + releaseNotes || '', |
| 94 | + ]) |
| 95 | + if (code !== 0) { |
| 96 | + throw new Error(stderr.toString()) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * |
| 102 | + * @param rawTag - ex. "@segment/[email protected]" |
| 103 | + */ |
| 104 | +const extractPartsFromTag = (rawTag: string): Tag | undefined => { |
| 105 | + const [name, version] = rawTag.split(/@(\d.*)/) |
| 106 | + if (!name || !version) return undefined |
| 107 | + return { |
| 108 | + name, |
| 109 | + versionNumber: version?.replace('\n', '') as string, |
| 110 | + raw: rawTag, |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * |
| 116 | + * @param rawTags - string delimited list of tags (e.g. `@segment/[email protected] @segment/[email protected]`) |
| 117 | + */ |
| 118 | +export const parseRawTags = (rawTags: string): Tag[] => { |
| 119 | + return rawTags.trim().split(' ').map(extractPartsFromTag).filter(exists) |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * |
| 124 | + * @returns the release notes that correspond to a given tag. |
| 125 | + */ |
| 126 | +export const parseReleaseNotes = ( |
| 127 | + changelogText: string, |
| 128 | + versionNumber: string |
| 129 | +): string => { |
| 130 | + const h2tag = /(##\s.*\d.*)/gi |
| 131 | + let begin: number |
| 132 | + let end: number |
| 133 | + |
| 134 | + changelogText.split('\n').forEach((line, idx) => { |
| 135 | + if (begin && end) return |
| 136 | + if (line.includes(versionNumber)) { |
| 137 | + begin = idx + 1 |
| 138 | + } else if (begin && h2tag.test(line)) { |
| 139 | + end = idx - 1 |
| 140 | + } |
| 141 | + }) |
| 142 | + |
| 143 | + const result = changelogText.split('\n').filter((_, idx) => { |
| 144 | + return idx >= begin && idx <= (end ?? Infinity) |
| 145 | + }) |
| 146 | + return result.join('\n') |
| 147 | +} |
| 148 | + |
| 149 | +const getReleaseNotes = (tag: Tag): string | undefined => { |
| 150 | + const { name, versionNumber } = tag |
| 151 | + const changelogPath = getChangelogPath(name) |
| 152 | + if (!changelogPath) { |
| 153 | + console.log(`no changelog path for ${name}... skipping.`) |
| 154 | + return |
| 155 | + } |
| 156 | + const changelogText = fs.readFileSync(changelogPath, { encoding: 'utf8' }) |
| 157 | + const releaseNotes = parseReleaseNotes(changelogText, versionNumber) |
| 158 | + if (!releaseNotes) { |
| 159 | + console.log( |
| 160 | + `Could not find release notes for tags ${tag.raw} in ${changelogPath}.` |
| 161 | + ) |
| 162 | + } |
| 163 | + return releaseNotes |
| 164 | +} |
| 165 | + |
| 166 | +const createGithubReleaseFromTag = async ( |
| 167 | + tag: Tag, |
| 168 | + { dryRun = false } = {} |
| 169 | +): Promise<void> => { |
| 170 | + const notes = getReleaseNotes(tag) |
| 171 | + if (notes) { |
| 172 | + console.log( |
| 173 | + `\n ---> Outputting release titled: ${tag.raw} with notes: \n ${notes}` |
| 174 | + ) |
| 175 | + } |
| 176 | + |
| 177 | + if (dryRun) { |
| 178 | + console.log(`--> Dry run: ${tag.raw} not released.`) |
| 179 | + return undefined |
| 180 | + } |
| 181 | + |
| 182 | + await createGithubRelease(tag.raw, notes) |
| 183 | + return undefined |
| 184 | +} |
| 185 | + |
| 186 | +export const createReleaseFromTags = async (config: Config) => { |
| 187 | + console.log('Processing tags:', config.tags, '\n') |
| 188 | + |
| 189 | + for (const tag of config.tags) { |
| 190 | + await createGithubReleaseFromTag(tag, { dryRun: config.isDryRun }) |
| 191 | + } |
| 192 | +} |
0 commit comments