This repository was archived by the owner on Feb 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwrapper.js
More file actions
57 lines (44 loc) · 1.44 KB
/
wrapper.js
File metadata and controls
57 lines (44 loc) · 1.44 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
"use strict";
const spawnSync = require("child_process").spawnSync;
function getCommand() {
return process.env.PRETTIER_SWIFT_SWIFTC || process.env.SWIFTC || "swiftc";
}
function checkVersion() {
const result = spawnSync(getCommand(), ["--version"]);
if (result.error || result.status) {
throw new Error(result.error || result.stderr.toString());
}
const stdout = result.stdout.toString();
const match = /Swift version ([0-9]+\.[0-9]+(\.[0-9]+)?)/.exec(stdout);
if (!match) {
// eslint-disable-next-line no-console
console.error("Could not detect Swift version:", stdout);
throw new Error("Unsupported Swift version (required: >4.1): " + stdout);
}
const components = match[1].split(".");
if (components[0] === "4") {
if (components[1] < 2) {
throw new Error("Unsupported Swift version. Required: 4.2");
} else {
return;
}
} else if (components[0] < 4) {
throw new Error("Unsupported Swift version. Required: 4.2");
}
// eslint-disable-next-line no-console
console.error("Potentially unsupported Swift version. Use Swift 4.2");
}
function emitSyntax(text) {
const result = spawnSync(getCommand(), ["-frontend", "-emit-syntax", "-"], {
input: text,
timeout: 60000
});
if (result.error || result.status) {
throw new Error(result.error || result.stderr.toString());
}
return JSON.parse(result.stdout.toString());
}
module.exports = {
emitSyntax,
checkVersion
};