|
1 | 1 | import fs from "fs"; |
2 | | -import { getGuiPath } from "./tea-dir"; |
| 2 | +import { getGuiPath, getTeaPath } from "./tea-dir"; |
3 | 3 | import log from "./logger"; |
4 | | -import { cliBinPath, asyncExec } from "./cli"; |
| 4 | +// import { cliBinPath, asyncExec } from "./cli"; |
5 | 5 | import { createInitialSessionFile } from "./auth"; |
6 | | -import { SemVer, isValidSemVer } from "@tea/libtea"; |
7 | | - |
8 | | -const MINIMUM_TEA_VERSION = "0.31.2"; |
9 | | - |
10 | | -const destinationDirectory = getGuiPath(); |
11 | | - |
12 | | -// TODO: move device_id generation here |
| 6 | +import * as https from "https"; |
| 7 | +import { spawn } from "child_process"; |
| 8 | +import path from "path"; |
| 9 | +import { parse as semverParse } from "@tea/libtea"; |
| 10 | + |
| 11 | +type InitState = "NOT_INITIALIZED" | "PENDING" | "INITIALIZED"; |
| 12 | + |
| 13 | +class InitWatcher<T> { |
| 14 | + private initState: InitState; |
| 15 | + private initFunction: () => Promise<T>; |
| 16 | + private initialValue: T | undefined; |
| 17 | + private initializationPromise: Promise<T> | undefined; |
| 18 | + |
| 19 | + constructor(initFunction: () => Promise<T>) { |
| 20 | + this.initState = "NOT_INITIALIZED"; |
| 21 | + this.initFunction = initFunction; |
| 22 | + this.initialValue = undefined; |
| 23 | + this.initializationPromise = undefined; |
| 24 | + } |
13 | 25 |
|
14 | | -// Get the binary path from the current app directory |
15 | | -const binaryUrl = "https://tea.xyz/$(uname)/$(uname -m)"; |
| 26 | + async initialize(): Promise<T> { |
| 27 | + if (this.initState === "NOT_INITIALIZED") { |
| 28 | + this.initState = "PENDING"; |
| 29 | + this.initializationPromise = this.retryFunction(this.initFunction, 3) |
| 30 | + .then((value) => { |
| 31 | + this.initialValue = value; |
| 32 | + this.initState = "INITIALIZED"; |
| 33 | + return value; |
| 34 | + }) |
| 35 | + .catch((error) => { |
| 36 | + this.initState = "NOT_INITIALIZED"; |
| 37 | + this.initializationPromise = undefined; |
| 38 | + throw error; |
| 39 | + }); |
| 40 | + } |
16 | 41 |
|
17 | | -let initializePromise: Promise<string> | null = null; |
| 42 | + return this.initializationPromise as Promise<T>; |
| 43 | + } |
18 | 44 |
|
19 | | -export async function initializeTeaCli(): Promise<string> { |
20 | | - if (initializePromise) { |
21 | | - return initializePromise; |
| 45 | + async retryFunction(func: () => Promise<T>, retries: number, currentAttempt = 1): Promise<T> { |
| 46 | + try { |
| 47 | + const result = await func(); |
| 48 | + return result; |
| 49 | + } catch (error) { |
| 50 | + if (currentAttempt < retries) { |
| 51 | + return this.retryFunction(func, retries, currentAttempt + 1); |
| 52 | + } else { |
| 53 | + throw error; |
| 54 | + } |
| 55 | + } |
22 | 56 | } |
23 | 57 |
|
24 | | - log.info("Initializing tea cli"); |
25 | | - initializePromise = initializeTeaCliInternal(); |
| 58 | + reset(): void { |
| 59 | + this.initState = "NOT_INITIALIZED"; |
| 60 | + this.initializationPromise = undefined; |
| 61 | + } |
26 | 62 |
|
27 | | - initializePromise.catch((error) => { |
28 | | - log.info("Error initializing tea cli, resetting promise:", error); |
29 | | - initializePromise = null; |
30 | | - }); |
| 63 | + async observe(): Promise<T> { |
| 64 | + return await this.initialize(); |
| 65 | + } |
31 | 66 |
|
32 | | - return initializePromise; |
| 67 | + getState(): InitState { |
| 68 | + return this.initState; |
| 69 | + } |
33 | 70 | } |
34 | 71 |
|
35 | | -async function initializeTeaCliInternal(): Promise<string> { |
36 | | - let binCheck = ""; |
37 | | - let needsUpdate = false; |
| 72 | +const teaCliPrefix = path.join(getTeaPath(), "tea.xyz/v*"); |
38 | 73 |
|
39 | | - // Create the destination directory if it doesn't exist |
40 | | - if (!fs.existsSync(destinationDirectory)) { |
41 | | - fs.mkdirSync(destinationDirectory, { recursive: true }); |
| 74 | +export const cliInitializationState = new InitWatcher<string>(async () => { |
| 75 | + if (!fs.existsSync(path.join(teaCliPrefix, "bin/tea"))) { |
| 76 | + return installTeaCli(); |
| 77 | + } else { |
| 78 | + const dir = fs.readlinkSync(teaCliPrefix); |
| 79 | + const v = semverParse(dir)?.toString(); |
| 80 | + if (!v) throw new Error(`couldn't parse to semver: ${dir}`); |
| 81 | + return v; |
42 | 82 | } |
| 83 | +}); |
43 | 84 |
|
44 | | - // replace this with max's pr |
45 | | - const curlCommand = `curl --insecure -L -o "${cliBinPath}" "${binaryUrl}"`; |
| 85 | +cliInitializationState.initialize(); |
46 | 86 |
|
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"); |
55 | | - needsUpdate = true; |
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`); |
62 | | - } |
| 87 | +export async function initializeTeaCli(): Promise<string> { |
| 88 | + if ( |
| 89 | + cliInitializationState.getState() === "INITIALIZED" && |
| 90 | + !fs.existsSync(path.join(teaCliPrefix, "bin/tea")) |
| 91 | + ) { |
| 92 | + cliInitializationState.reset(); |
63 | 93 | } |
| 94 | + return cliInitializationState.observe(); |
| 95 | +} |
64 | 96 |
|
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`); |
| 97 | +//NOTE copy pasta from https://github.com/teaxyz/setup/blob/main/action.js |
| 98 | +//FIXME ideally we'd not copy pasta this |
| 99 | +//NOTE using `tar` is not ideal ∵ Windows and even though tar is POSIX it's still not guaranteed to be available |
| 100 | +async function installTeaCli() { |
| 101 | + const PREFIX = `${process.env.HOME}/.tea`; |
| 102 | + |
| 103 | + const midfix = (() => { |
| 104 | + switch (process.arch) { |
| 105 | + case "arm64": |
| 106 | + return `${process.platform}/aarch64`; |
| 107 | + case "x64": |
| 108 | + return `${process.platform}/x86-64`; |
| 109 | + default: |
| 110 | + throw new Error(`unsupported platform: ${process.platform}/${process.arch}`); |
| 111 | + } |
| 112 | + })(); |
| 113 | + |
| 114 | + /// versions.txt is guaranteed semver-sorted |
| 115 | + const v: string | undefined = await new Promise((resolve, reject) => { |
| 116 | + https |
| 117 | + .get(`https://dist.tea.xyz/tea.xyz/${midfix}/versions.txt`, (rsp) => { |
| 118 | + if (rsp.statusCode != 200) return reject(rsp.statusCode); |
| 119 | + rsp.setEncoding("utf8"); |
| 120 | + const chunks: string[] = []; |
| 121 | + rsp.on("data", (x) => chunks.push(x)); |
| 122 | + rsp.on("end", () => { |
| 123 | + resolve(chunks.join("").trim().split("\n").at(-1)); |
| 124 | + }); |
| 125 | + }) |
| 126 | + .on("error", reject); |
| 127 | + }); |
| 128 | + |
| 129 | + if (!v) throw new Error(`invalid versions.txt for tea/cli`); |
| 130 | + |
| 131 | + fs.mkdirSync(PREFIX, { recursive: true }); |
| 132 | + |
| 133 | + const exitcode = await new Promise((resolve, reject) => { |
| 134 | + https |
| 135 | + .get(`https://dist.tea.xyz/tea.xyz/${midfix}/v${v}.tar.gz`, (rsp) => { |
| 136 | + if (rsp.statusCode != 200) return reject(rsp.statusCode); |
| 137 | + const tar = spawn("tar", ["xzf", "-"], { |
| 138 | + stdio: ["pipe", "inherit", "inherit"], |
| 139 | + cwd: PREFIX |
| 140 | + }); |
| 141 | + rsp.pipe(tar.stdin); |
| 142 | + tar.on("close", resolve); |
| 143 | + }) |
| 144 | + .on("error", reject); |
| 145 | + }); |
| 146 | + |
| 147 | + if (exitcode != 0) { |
| 148 | + throw new Error(`tar: ${exitcode}`); |
71 | 149 | } |
72 | 150 |
|
73 | | - const version = binCheck.toString().split(" ")[1]; |
74 | | - log.info("binary tea version:", version); |
75 | | - return isValidSemVer(version.trim()) ? version : ""; |
| 151 | + const oldwd = process.cwd(); |
| 152 | + process.chdir(`${PREFIX}/tea.xyz`); |
| 153 | + if (fs.existsSync(`v*`)) fs.unlinkSync(`v*`); |
| 154 | + fs.symlinkSync(`v${v}`, `v*`, "dir"); |
| 155 | + if (fs.existsSync(`v0`)) fs.unlinkSync(`v0`); |
| 156 | + fs.symlinkSync(`v${v}`, `v0`, "dir"); //FIXME |
| 157 | + process.chdir(oldwd); |
| 158 | + |
| 159 | + return v; |
76 | 160 | } |
77 | 161 |
|
78 | 162 | export default async function initialize(): Promise<string> { |
|
0 commit comments