|
1 | 1 | import type { AuthorInfo, ChangelogOptions, Commit } from './types' |
2 | | -import { notNullish } from '@antfu/utils' |
3 | | -import { cyan, green } from 'ansis' |
| 2 | +import fs from 'node:fs/promises' |
| 3 | +import path from 'node:path' |
4 | 4 | /* eslint-disable no-console */ |
| 5 | +import { notNullish } from '@antfu/utils' |
| 6 | +import { cyan, green, red } from 'ansis' |
5 | 7 | import { $fetch } from 'ofetch' |
6 | 8 |
|
7 | 9 | export async function sendRelease( |
@@ -147,3 +149,46 @@ export async function hasTagOnGitHub(tag: string, options: ChangelogOptions) { |
147 | 149 | return false |
148 | 150 | } |
149 | 151 | } |
| 152 | + |
| 153 | +export async function uploadAssets(options: ChangelogOptions, assets: string | string[]) { |
| 154 | + const headers = getHeaders(options) |
| 155 | + |
| 156 | + let assetList: string[] = [] |
| 157 | + if (typeof assets === 'string') { |
| 158 | + assetList = assets.split(',').map(s => s.trim()).filter(Boolean) |
| 159 | + } |
| 160 | + else if (Array.isArray(assets)) { |
| 161 | + assetList = assets.flatMap(item => |
| 162 | + typeof item === 'string' ? item.split(',').map(s => s.trim()) : [], |
| 163 | + ).filter(Boolean) |
| 164 | + } |
| 165 | + |
| 166 | + // Get the release by tag to obtain the upload_url |
| 167 | + const release = await $fetch(`https://${options.baseUrlApi}/repos/${options.releaseRepo}/releases/tags/${options.to}`, { |
| 168 | + headers, |
| 169 | + }) |
| 170 | + |
| 171 | + for (const asset of assetList) { |
| 172 | + const filePath = path.resolve(asset) |
| 173 | + const fileData = await fs.readFile(filePath) |
| 174 | + const fileName = path.basename(filePath) |
| 175 | + const contentType = 'application/octet-stream' |
| 176 | + |
| 177 | + const uploadUrl = release.upload_url.replace('{?name,label}', `?name=${encodeURIComponent(fileName)}`) |
| 178 | + console.log(cyan(`Uploading ${fileName}...`)) |
| 179 | + try { |
| 180 | + await $fetch(uploadUrl, { |
| 181 | + method: 'POST', |
| 182 | + headers: { |
| 183 | + ...headers, |
| 184 | + 'Content-Type': contentType, |
| 185 | + }, |
| 186 | + body: fileData, |
| 187 | + }) |
| 188 | + console.log(green(`Uploaded ${fileName} successfully.`)) |
| 189 | + } |
| 190 | + catch (error) { |
| 191 | + console.error(red(`Failed to upload ${fileName}: ${error}`)) |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments