|
1 | 1 | import fs from "fs"; |
2 | 2 | import { getGuiPath } from "./tea-dir"; |
3 | 3 | import log from "./logger"; |
4 | | -import semver from "semver"; |
5 | 4 | import { cliBinPath, asyncExec } from "./cli"; |
6 | 5 | import { createInitialSessionFile } from "./auth"; |
7 | | -import semverCompare from "semver/functions/compare"; |
| 6 | +import { SemVer, isValidSemVer } from "@tea/libtea"; |
8 | 7 |
|
9 | | -// Versions before this do not support the --json flag |
10 | | -const MINIMUM_TEA_VERSION = "0.28.3"; |
| 8 | +const MINIMUM_TEA_VERSION = "0.31.2"; |
11 | 9 |
|
12 | 10 | const destinationDirectory = getGuiPath(); |
13 | 11 |
|
@@ -35,55 +33,46 @@ export async function initializeTeaCli(): Promise<string> { |
35 | 33 | } |
36 | 34 |
|
37 | 35 | async function initializeTeaCliInternal(): Promise<string> { |
38 | | - try { |
39 | | - let binCheck = ""; |
40 | | - let needsUpdate = false; |
| 36 | + let binCheck = ""; |
| 37 | + let needsUpdate = false; |
41 | 38 |
|
42 | | - // Create the destination directory if it doesn't exist |
43 | | - if (!fs.existsSync(destinationDirectory)) { |
44 | | - fs.mkdirSync(destinationDirectory, { recursive: true }); |
45 | | - } |
| 39 | + // Create the destination directory if it doesn't exist |
| 40 | + if (!fs.existsSync(destinationDirectory)) { |
| 41 | + fs.mkdirSync(destinationDirectory, { recursive: true }); |
| 42 | + } |
46 | 43 |
|
47 | | - // replace this with max's pr |
48 | | - const curlCommand = `curl --insecure -L -o "${cliBinPath}" "${binaryUrl}"`; |
49 | | - |
50 | | - const exists = fs.existsSync(cliBinPath); |
51 | | - if (exists) { |
52 | | - log.info("binary tea already exists at", cliBinPath); |
53 | | - try { |
54 | | - binCheck = await asyncExec(`cd ${destinationDirectory} && ./tea --version`); |
55 | | - const teaVersion = binCheck.toString().split(" ")[1]; |
56 | | - if (semverCompare(teaVersion, MINIMUM_TEA_VERSION) < 0) { |
57 | | - log.info("binary tea version is too old, updating"); |
58 | | - needsUpdate = true; |
59 | | - } |
60 | | - } catch (error) { |
61 | | - // probably binary is not executable or no permission |
62 | | - log.error("Error checking tea binary version:", error); |
| 44 | + // replace this with max's pr |
| 45 | + const curlCommand = `curl --insecure -L -o "${cliBinPath}" "${binaryUrl}"`; |
| 46 | + |
| 47 | + const exists = fs.existsSync(cliBinPath); |
| 48 | + if (exists) { |
| 49 | + log.info("binary tea already exists at", cliBinPath); |
| 50 | + try { |
| 51 | + binCheck = await asyncExec(`cd ${destinationDirectory} && ./tea --version`); |
| 52 | + const teaVersion = binCheck.toString().split(" ")[1].trim(); |
| 53 | + if (new SemVer(teaVersion).compare(new SemVer(MINIMUM_TEA_VERSION)) < 0) { |
| 54 | + log.info("binary tea version is too old, updating"); |
63 | 55 | needsUpdate = true; |
64 | | - await asyncExec(`cd ${destinationDirectory} && rm tea`); |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - if (!exists || needsUpdate) { |
69 | | - try { |
70 | | - await asyncExec(curlCommand); |
71 | | - log.info("Binary downloaded and saved to", cliBinPath); |
72 | | - await asyncExec("chmod u+x " + cliBinPath); |
73 | | - log.info("Binary is now ready for use at", cliBinPath); |
74 | | - binCheck = await asyncExec(`cd ${destinationDirectory} && ./tea --version`); |
75 | | - } catch (error) { |
76 | | - log.error("Error setting-up tea binary:", error); |
77 | 56 | } |
| 57 | + } catch (error) { |
| 58 | + // probably binary is not executable or no permission |
| 59 | + log.error("Error checking tea binary version:", error); |
| 60 | + needsUpdate = true; |
| 61 | + await asyncExec(`cd ${destinationDirectory} && rm tea`); |
78 | 62 | } |
| 63 | + } |
79 | 64 |
|
80 | | - const version = binCheck.toString().split(" ")[1]; |
81 | | - log.info("binary tea version:", version); |
82 | | - return semver.valid(version.trim()) ? version : ""; |
83 | | - } catch (error) { |
84 | | - log.error(error); |
85 | | - return ""; |
| 65 | + if (!exists || needsUpdate) { |
| 66 | + await asyncExec(curlCommand); |
| 67 | + log.info("Binary downloaded and saved to", cliBinPath); |
| 68 | + await asyncExec("chmod u+x " + cliBinPath); |
| 69 | + log.info("Binary is now ready for use at", cliBinPath); |
| 70 | + binCheck = await asyncExec(`cd ${destinationDirectory} && ./tea --version`); |
86 | 71 | } |
| 72 | + |
| 73 | + const version = binCheck.toString().split(" ")[1]; |
| 74 | + log.info("binary tea version:", version); |
| 75 | + return isValidSemVer(version.trim()) ? version : ""; |
87 | 76 | } |
88 | 77 |
|
89 | 78 | export default async function initialize(): Promise<string> { |
|
0 commit comments