-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve.ts
More file actions
92 lines (83 loc) · 2.78 KB
/
resolve.ts
File metadata and controls
92 lines (83 loc) · 2.78 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @file `resolveSbt()` — SBT resolution entry point. Tries each source in
* order:
*
* 1. VFS — smol binary's embedded `sbt-launch.jar` (if packed)
* 2. PATH — `sbt` script on the system PATH
* 3. download — upstream GitHub release tgz (only when `downloadIfMissing` is
* passed) VFS-sourced SBT is a `.jar` that must be invoked as `java -jar
* <path>` using the JRE resolved separately via `resolveJre()`. PATH- and
* download-sourced SBT is the `sbt` script, which finds its own JRE.
* Memoized per option-shape.
*/
import { sbtFromDownload } from './from-download'
import { sbtFromPath } from './from-path'
import { sbtFromVfs } from './from-vfs'
import type { BinaryDownloader } from '../from-download'
import type { HashSpec } from '../../integrity'
import type { ResolvedSbt } from './types'
import { MapCtor } from '../../primordials/map-set'
export interface ResolveSbtOptions {
/**
* When set, the resolver falls through to a GitHub release download after the
* local-discovery tiers miss. Omit to keep the resolver read-only.
*/
downloadIfMissing?:
| {
version: string
integrity?: HashSpec | undefined
cacheDir?: string | undefined
downloader?: BinaryDownloader | undefined
}
| undefined
}
const resolutionCache = new MapCtor<string, Promise<ResolvedSbt | undefined>>()
export function cacheKey(options: ResolveSbtOptions | undefined): string {
options = { __proto__: null, ...options } as typeof options
if (!options?.downloadIfMissing) {
return 'local-only'
}
const { cacheDir, integrity, version } = options.downloadIfMissing
const integrityKey =
typeof integrity === 'string'
? integrity
: integrity
? `${integrity.type}:${integrity.value}`
: ''
return `dl:${version}:${integrityKey}:${cacheDir ?? ''}`
}
export async function doResolveSbt(
options?: ResolveSbtOptions | undefined,
): Promise<ResolvedSbt | undefined> {
options = { __proto__: null, ...options } as typeof options
const fromVfs = await sbtFromVfs()
/* c8 ignore start - smol Node binary only. */
if (fromVfs) {
return fromVfs
}
/* c8 ignore stop */
const fromPath = await sbtFromPath()
if (fromPath) {
return fromPath
}
if (options?.downloadIfMissing) {
return sbtFromDownload(options.downloadIfMissing)
}
return undefined
}
/* c8 ignore start - test-only escape hatch. */
export function resetSbtResolution(): void {
resolutionCache.clear()
}
/* c8 ignore stop */
export function resolveSbt(
options?: ResolveSbtOptions | undefined,
): Promise<ResolvedSbt | undefined> {
const key = cacheKey(options)
let cached = resolutionCache.get(key)
if (!cached) {
cached = doResolveSbt(options)
resolutionCache.set(key, cached)
}
return cached
}