forked from swift-actions/setup-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-version.ts
42 lines (34 loc) · 860 Bytes
/
get-version.ts
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
import { exec } from "@actions/exec";
export async function getVersion(
command: string = "swift",
args: string[] = ["--version"]
) {
let output = "";
let error = "";
const options = {
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
},
stderr: (data: Buffer) => {
error += data.toString();
},
},
};
await exec(command, args, options);
if (!output && error) {
throw new Error("Error getting swift version " + error);
}
return versionFromString(output);
}
export function versionFromString(subject: string): string | null {
const match = subject.match(
/Swift\ version (?<version>[0-9]+\.[0-9+]+(\.[0-9]+)?)/
) || {
groups: { version: null },
};
if (!match.groups || !match.groups.version) {
return null;
}
return match.groups.version;
}