Skip to content

Commit 9364a45

Browse files
committed
refactor(releases): extend downloadSocketBtmRelease API to support asset patterns
Update the `asset` parameter in `SocketBtmAssetConfig` to accept either an exact asset name (string) or a pattern (prefix/suffix object or RegExp). When a pattern is provided, the function automatically resolves it to the actual asset name by finding the latest matching asset in the release. This eliminates the need for a separate `findReleaseAsset()` call in consuming code and provides a cleaner, more intuitive API. Key changes: - `asset` parameter now accepts `string | AssetPattern` type - Pattern resolution integrated into `downloadSocketBtmRelease()` - Automatic tag detection when using patterns - Maintains backward compatibility with exact asset names
1 parent b464ddb commit 9364a45

1 file changed

Lines changed: 40 additions & 7 deletions

File tree

src/releases/socket-btm.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import {
1212
type Platform,
1313
} from '../constants/platform.js'
1414
import {
15+
type AssetPattern,
1516
downloadGitHubRelease,
1617
type DownloadGitHubReleaseConfig,
18+
findReleaseAsset,
1719
SOCKET_BTM_REPO,
1820
} from './github.js'
1921

@@ -57,9 +59,9 @@ export interface SocketBtmAssetConfig {
5759
downloadDir?: string
5860
/** Tool/package name for directory structure and release matching. */
5961
tool: string
60-
/** Asset name pattern on GitHub. */
61-
asset: string
62-
/** Output filename. @default asset */
62+
/** Asset name or pattern on GitHub. Can be an exact name (string) or a pattern (prefix/suffix or RegExp). */
63+
asset: string | AssetPattern
64+
/** Output filename. @default resolved asset name */
6365
output?: string
6466
/** Specific release tag to download. */
6567
tag?: string
@@ -149,8 +151,39 @@ export async function downloadSocketBtmRelease(
149151
removeMacOSQuarantine = false,
150152
} = config as SocketBtmAssetConfig
151153

152-
// Default output to asset name if not provided
153-
const outputName = output || asset
154+
// Resolve asset pattern to actual asset name if needed.
155+
let resolvedAsset: string
156+
let resolvedTag = tag
157+
158+
if (typeof asset === 'string') {
159+
// Exact asset name provided.
160+
resolvedAsset = asset
161+
} else {
162+
// Pattern provided - need to find matching asset.
163+
if (tag) {
164+
throw new Error(
165+
'Cannot use asset pattern with explicit tag. Either provide exact asset name or omit tag.',
166+
)
167+
}
168+
169+
// Find matching asset in latest release.
170+
const result = await findReleaseAsset(
171+
toolPrefix,
172+
asset,
173+
{ owner: SOCKET_BTM_REPO.owner, repo: SOCKET_BTM_REPO.repo },
174+
{ quiet },
175+
)
176+
177+
if (!result) {
178+
throw new Error(`No ${tool} release with matching asset pattern found`)
179+
}
180+
181+
resolvedAsset = result.assetName
182+
resolvedTag = result.tag
183+
}
184+
185+
// Default output to resolved asset name if not provided
186+
const outputName = output || resolvedAsset
154187

155188
// For non-binary assets, use a simple 'assets' directory instead of platform-arch
156189
const platformArch = 'assets'
@@ -163,9 +196,9 @@ export async function downloadSocketBtmRelease(
163196
toolName: tool,
164197
platformArch,
165198
binaryName: outputName,
166-
assetName: asset,
199+
assetName: resolvedAsset,
167200
toolPrefix,
168-
tag,
201+
tag: resolvedTag,
169202
quiet,
170203
removeMacOSQuarantine,
171204
}

0 commit comments

Comments
 (0)