Skip to content

Commit 49061a2

Browse files
authored
feat: assets support glob (#71)
1 parent f4da46a commit 49061a2

File tree

4 files changed

+58
-28
lines changed

4 files changed

+58
-28
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
"convert-gitmoji": "^0.1.5",
5959
"execa": "^9.6.0",
6060
"ofetch": "^1.4.1",
61-
"semver": "^7.7.2"
61+
"semver": "^7.7.2",
62+
"tinyglobby": "^0.2.14"
6263
},
6364
"devDependencies": {
6465
"@antfu/eslint-config": "^4.13.2",

pnpm-lock.yaml

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cli
2727
.option('--emoji', 'Use emojis in section titles', { default: true })
2828
.option('--group', 'Nest commit messages under their scopes')
2929
.option('--dry', 'Dry run')
30-
.option('--assets <paths...>', 'Files to upload as assets to the release')
30+
.option('--assets <paths...>', 'Files to upload as assets to the release. Use quotes to prevent shell glob expansion, e.g., "--assets \'dist/*.js\'"')
3131
.help()
3232

3333
async function readTokenFromGitHubCli() {

src/github.ts

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'node:path'
55
import { notNullish } from '@antfu/utils'
66
import { cyan, green, red } from 'ansis'
77
import { $fetch } from 'ofetch'
8+
import { glob } from 'tinyglobby'
89

910
export async function sendRelease(
1011
options: ChangelogOptions,
@@ -152,7 +153,6 @@ export async function hasTagOnGitHub(tag: string, options: ChangelogOptions) {
152153

153154
export async function uploadAssets(options: ChangelogOptions, assets: string | string[]) {
154155
const headers = getHeaders(options)
155-
156156
let assetList: string[] = []
157157
if (typeof assets === 'string') {
158158
assetList = assets.split(',').map(s => s.trim()).filter(Boolean)
@@ -163,32 +163,58 @@ export async function uploadAssets(options: ChangelogOptions, assets: string | s
163163
).filter(Boolean)
164164
}
165165

166+
// Expand glob patterns to actual file paths
167+
const expandedAssets: string[] = []
168+
for (const pattern of assetList) {
169+
try {
170+
// Use the pattern directly without shell expansion
171+
const matches = await glob(pattern)
172+
if (matches.length) {
173+
expandedAssets.push(...matches)
174+
}
175+
else {
176+
// If no matches found, treat as literal path
177+
expandedAssets.push(pattern)
178+
}
179+
}
180+
catch (error) {
181+
console.error(red(`Failed to process glob pattern "${pattern}": ${error}`))
182+
// Keep the original pattern as fallback
183+
expandedAssets.push(pattern)
184+
}
185+
}
186+
166187
// Get the release by tag to obtain the upload_url
167188
const release = await $fetch(`https://${options.baseUrlApi}/repos/${options.releaseRepo}/releases/tags/${options.to}`, {
168189
headers,
169190
})
170191

171-
for (const asset of assetList) {
192+
for (const asset of expandedAssets) {
172193
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}...`))
179194
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.`))
195+
const fileData = await fs.readFile(filePath)
196+
const fileName = path.basename(filePath)
197+
const contentType = 'application/octet-stream'
198+
199+
const uploadUrl = release.upload_url.replace('{?name,label}', `?name=${encodeURIComponent(fileName)}`)
200+
console.log(cyan(`Uploading ${fileName}...`))
201+
try {
202+
await $fetch(uploadUrl, {
203+
method: 'POST',
204+
headers: {
205+
...headers,
206+
'Content-Type': contentType,
207+
},
208+
body: fileData,
209+
})
210+
console.log(green(`Uploaded ${fileName} successfully.`))
211+
}
212+
catch (error) {
213+
console.error(red(`Failed to upload ${fileName}: ${error}`))
214+
}
189215
}
190216
catch (error) {
191-
console.error(red(`Failed to upload ${fileName}: ${error}`))
217+
console.error(red(`Failed to read file ${filePath}: ${error}`))
192218
}
193219
}
194220
}

0 commit comments

Comments
 (0)