import { existsSync, readFileSync } from 'node:fs' import { join } from 'node:path' import { homedir } from 'node:os' import * as tela from './index.ts' declare global { // eslint-disable-next-line vars-on-top var __TELA_API_URL__: string | undefined // eslint-disable-next-line vars-on-top var __TELA_APP_URL__: string | undefined // eslint-disable-next-line vars-on-top var tela: typeof import('./index.ts') } const envPath = join(homedir(), '.tela', '.env') if (existsSync(envPath)) { const env: Record = {} for (const line of readFileSync(envPath, 'utf-8').split('\n')) { const match = line.match(/^([^#=]+)=(.*)$/) if (match) { env[match[1]?.trim() ?? ''] = match[2]?.trim() ?? '' } } globalThis.__TELA_API_URL__ = process.env.TELA_API_URL?.trim() || env.TELA_API_URL globalThis.__TELA_APP_URL__ = process.env.TELA_APP_URL?.trim() || env.TELA_APP_URL } else { globalThis.__TELA_API_URL__ = process.env.TELA_API_URL?.trim() globalThis.__TELA_APP_URL__ = process.env.TELA_APP_URL?.trim() } globalThis.tela = tela // Device-flow access tokens last 15 minutes; refresh once up front so a normal run never // hits an expired token. A failed refresh surfaces on the first API call instead. await tela.refreshSessionIfNeeded().catch(() => {})