-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-bazel-version-file.ts
More file actions
49 lines (45 loc) · 1.66 KB
/
read-bazel-version-file.ts
File metadata and controls
49 lines (45 loc) · 1.66 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
/**
* @fileoverview `readBazelVersionFile(workspaceDir)` — walks up from
* `workspaceDir` looking for a `.bazelversion` file (the
* Bazel/Bazelisk convention for pinning the project's Bazel version).
*
* Returns the trimmed contents as a version string, or `undefined`
* when no file is found before reaching the filesystem root. The
* walk respects the "first `.bazelversion` wins" convention used by
* Bazelisk itself.
*
* Some special values that `.bazelversion` can hold (`latest`,
* `last_green`, `rolling`, etc.) are returned verbatim; resolution
* of those into concrete X.Y.Z versions is the orchestrator's job.
*/
import path from 'node:path'
import { safeReadFile } from '../../fs/read-file'
import {
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeTrim,
} from '../../primordials/string'
const BAZEL_VERSION_FILE = '.bazelversion'
export async function readBazelVersionFile(
startDir: string,
): Promise<string | undefined> {
let current = path.resolve(startDir)
while (true) {
const candidate = path.join(current, BAZEL_VERSION_FILE)
// eslint-disable-next-line no-await-in-loop
const content = await safeReadFile(candidate, { encoding: 'utf8' })
if (content !== undefined) {
// Strip comments (anything after `#` on a line) + trim.
const hashIdx = StringPrototypeIndexOf(content, '#')
const base =
hashIdx === -1 ? content : StringPrototypeSlice(content, 0, hashIdx)
const version = StringPrototypeTrim(base)
return version.length > 0 ? version : undefined
}
const parent = path.dirname(current)
if (parent === current) {
return undefined
}
current = parent
}
}