|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | + |
| 3 | +import { Octokit } from '@octokit/rest'; |
| 4 | +import { toString } from 'mdast-util-to-string'; |
| 5 | +import remarkParse from 'remark-parse'; |
| 6 | +import remarkStringify from 'remark-stringify'; |
| 7 | +import { unified } from 'unified'; |
| 8 | + |
| 9 | +const BumpLevels = { |
| 10 | + dep: 0, |
| 11 | + patch: 1, |
| 12 | + minor: 2, |
| 13 | + major: 3, |
| 14 | +} as const; |
| 15 | + |
| 16 | +export async function runReleaseCommand( |
| 17 | + octokit: Octokit, |
| 18 | + params: { |
| 19 | + owner: string; |
| 20 | + repo: string; |
| 21 | + version: string; |
| 22 | + tar_gz_path: string; |
| 23 | + } |
| 24 | +) { |
| 25 | + let changelog; |
| 26 | + try { |
| 27 | + changelog = await readFile('CHANGELOG.md', 'utf8'); |
| 28 | + } catch (err) { |
| 29 | + if (isErrorWithCode(err, 'ENOENT')) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + throw err; |
| 34 | + } |
| 35 | + |
| 36 | + let changelogEntry = getChangelogEntry(changelog, params.version); |
| 37 | + if (!changelogEntry.content) { |
| 38 | + throw new Error(`Could not find changelog entry for ${params.version}`); |
| 39 | + } |
| 40 | + |
| 41 | + const release = await octokit.rest.repos.createRelease({ |
| 42 | + name: params.version, |
| 43 | + tag_name: `v${params.version}`, |
| 44 | + body: changelogEntry.content, |
| 45 | + prerelease: params.version.includes('-'), |
| 46 | + repo: params.repo, |
| 47 | + owner: params.owner, |
| 48 | + }); |
| 49 | + |
| 50 | + await octokit.rest.repos.uploadReleaseAsset({ |
| 51 | + owner: params.owner, |
| 52 | + repo: params.repo, |
| 53 | + release_id: release.data.id, |
| 54 | + name: 'design-system.tar.gz', |
| 55 | + data: await readFile(params.tar_gz_path, 'utf-8'), |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +function isErrorWithCode(err: unknown, code: string) { |
| 60 | + return ( |
| 61 | + typeof err === 'object' && |
| 62 | + err !== null && |
| 63 | + 'code' in err && |
| 64 | + err.code === code |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +interface ChangelogEntry { |
| 69 | + content: string; |
| 70 | + highestLevel: number; |
| 71 | +} |
| 72 | + |
| 73 | +function getChangelogEntry(changelog: string, version: string): ChangelogEntry { |
| 74 | + let ast = unified().use(remarkParse).parse(changelog); |
| 75 | + |
| 76 | + let highestLevel: number = BumpLevels.dep; |
| 77 | + |
| 78 | + let nodes = ast.children; |
| 79 | + let headingStartInfo: |
| 80 | + | { |
| 81 | + index: number; |
| 82 | + depth: number; |
| 83 | + } |
| 84 | + | undefined; |
| 85 | + let endIndex: number | undefined; |
| 86 | + |
| 87 | + for (let i = 0; i < nodes.length; i++) { |
| 88 | + let node = nodes[i]; |
| 89 | + |
| 90 | + if (node.type === 'heading') { |
| 91 | + let stringified: string = toString(node); |
| 92 | + let match = /(major|minor|patch)/.exec(stringified.toLowerCase()); |
| 93 | + |
| 94 | + if (match !== null) { |
| 95 | + let level = BumpLevels[match[0] as 'major' | 'minor' | 'patch']; |
| 96 | + highestLevel = Math.max(level, highestLevel); |
| 97 | + } |
| 98 | + |
| 99 | + if (headingStartInfo === undefined && stringified === version) { |
| 100 | + headingStartInfo = { |
| 101 | + index: i, |
| 102 | + depth: node.depth, |
| 103 | + }; |
| 104 | + |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + if ( |
| 109 | + endIndex === undefined && |
| 110 | + headingStartInfo !== undefined && |
| 111 | + headingStartInfo.depth === node.depth |
| 112 | + ) { |
| 113 | + endIndex = i; |
| 114 | + |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (headingStartInfo) { |
| 121 | + ast.children = ast.children.slice(headingStartInfo.index + 1, endIndex); |
| 122 | + } |
| 123 | + |
| 124 | + return { |
| 125 | + content: unified().use(remarkStringify).stringify(ast), |
| 126 | + highestLevel: highestLevel, |
| 127 | + }; |
| 128 | +} |
0 commit comments