-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrom-download.ts
More file actions
77 lines (69 loc) · 2.45 KB
/
Copy pathfrom-download.ts
File metadata and controls
77 lines (69 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @file `opengrepFromDownload()` — fetches upstream OpenGrep and returns a
* `ResolvedOpengrep` pointing at the cached binary. macOS/Linux assets are
* bare binaries (no extraction); Windows ships a zip. The asset-map's
* `isArchive` flag drives extraction vs. copy.
*/
import path from 'node:path'
import { promises as fsPromises } from 'node:fs'
import { getSocketDlxDir } from '../../paths/socket'
import { safeMkdir } from '../../fs/safe'
import { downloadAndExtractTool, downloadToolArchive } from '../from-download'
import { getOpengrepAssetEntry, getOpengrepDownloadUrl } from './asset-names'
import type { BinaryDownloader } from '../from-download'
import type { HashSpec } from '../../integrity'
import type { ResolvedOpengrep } from './types'
export interface OpengrepFromDownloadOptions {
version: string
platformArch: string
integrity?: HashSpec | undefined
cacheDir?: string | undefined
downloader?: BinaryDownloader | undefined
}
export async function opengrepFromDownload(
options: OpengrepFromDownloadOptions,
): Promise<ResolvedOpengrep | undefined> {
const { cacheDir, downloader, integrity, platformArch, version } = {
__proto__: null,
...options,
} as typeof options
const url = getOpengrepDownloadUrl({ version, platformArch })
const entry = getOpengrepAssetEntry(platformArch)
if (!url || !entry) {
return undefined
}
const targetDir =
cacheDir ?? path.join(getSocketDlxDir(), 'opengrep', version, platformArch)
if (entry.isArchive) {
const archive = await downloadAndExtractTool({
url,
name: `opengrep-${version}-${platformArch}-${entry.asset}`,
integrity,
extractedDir: targetDir,
downloader,
})
return {
path: path.join(targetDir, entry.binaryInArchive!),
source: 'download',
integrity: archive.integrity,
}
}
// Bare-binary asset (macOS/Linux): download to dlx cache, then copy
// into the per-version cacheDir under a normalized `opengrep`
// filename so callers don't need to know the upstream asset name.
const archive = await downloadToolArchive({
url,
name: `opengrep-${version}-${platformArch}-${entry.asset}`,
integrity,
downloader,
})
await safeMkdir(targetDir)
const finalPath = path.join(targetDir, 'opengrep')
await fsPromises.copyFile(archive.archivePath, finalPath)
await fsPromises.chmod(finalPath, 0o755)
return {
path: finalPath,
source: 'download',
integrity: archive.integrity,
}
}