|
| 1 | +import path from 'node:path'; |
| 2 | +import { $ } from 'bun'; |
| 3 | +import pkg from '../../package.json'; |
| 4 | + |
| 5 | +export namespace Installation { |
| 6 | + export const VERSION: string = pkg.version; |
| 7 | + |
| 8 | + export type Method = 'npm' | 'bun' | 'curl' | 'unknown'; |
| 9 | + |
| 10 | + export async function method(): Promise<Method> { |
| 11 | + // Check if installed via curl script (default: ~/.treq/bin) |
| 12 | + if ( |
| 13 | + process.execPath.includes(path.join('.treq', 'bin')) || |
| 14 | + process.execPath.includes(path.join('.local', 'bin')) |
| 15 | + ) { |
| 16 | + return 'curl'; |
| 17 | + } |
| 18 | + |
| 19 | + const exec = process.execPath.toLowerCase(); |
| 20 | + |
| 21 | + const checks: Array<{ name: 'npm' | 'bun'; command: () => Promise<string> }> = [ |
| 22 | + { |
| 23 | + name: 'npm', |
| 24 | + command: () => $`npm list -g --depth=0`.throws(false).quiet().text() |
| 25 | + }, |
| 26 | + { |
| 27 | + name: 'bun', |
| 28 | + command: () => $`bun pm ls -g`.throws(false).quiet().text() |
| 29 | + } |
| 30 | + ]; |
| 31 | + |
| 32 | + // Prioritize check matching the current runtime |
| 33 | + checks.sort((a, b) => { |
| 34 | + const aMatches = exec.includes(a.name); |
| 35 | + const bMatches = exec.includes(b.name); |
| 36 | + if (aMatches && !bMatches) return -1; |
| 37 | + if (!aMatches && bMatches) return 1; |
| 38 | + return 0; |
| 39 | + }); |
| 40 | + |
| 41 | + for (const check of checks) { |
| 42 | + const output = await check.command(); |
| 43 | + if (output.includes('@t-req/app')) { |
| 44 | + return check.name; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return 'unknown'; |
| 49 | + } |
| 50 | + |
| 51 | + export async function latest(_installMethod?: Method): Promise<string> { |
| 52 | + const response = await fetch('https://registry.npmjs.org/@t-req/app/latest'); |
| 53 | + if (!response.ok) throw new Error(response.statusText); |
| 54 | + const data = (await response.json()) as { version: string }; |
| 55 | + return data.version; |
| 56 | + } |
| 57 | + |
| 58 | + export async function upgrade(installMethod: Method, target: string): Promise<void> { |
| 59 | + let cmd: ReturnType<typeof $>; |
| 60 | + switch (installMethod) { |
| 61 | + case 'curl': |
| 62 | + cmd = $`curl -fsSL https://t-req.io/install | bash -s -- --version ${target}`; |
| 63 | + break; |
| 64 | + case 'npm': |
| 65 | + cmd = $`npm install -g @t-req/app@${target}`; |
| 66 | + break; |
| 67 | + case 'bun': |
| 68 | + cmd = $`bun install -g @t-req/app@${target}`; |
| 69 | + break; |
| 70 | + default: |
| 71 | + throw new Error( |
| 72 | + `Cannot auto-upgrade: unknown install method. Please upgrade manually:\n` + |
| 73 | + ` npm install -g @t-req/app@${target}\n` + |
| 74 | + ` bun install -g @t-req/app@${target}` |
| 75 | + ); |
| 76 | + } |
| 77 | + const result = await cmd.quiet().throws(false); |
| 78 | + if (result.exitCode !== 0) { |
| 79 | + throw new Error(result.stderr.toString('utf8')); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + export function updateCommand(installMethod: Method, target?: string): string { |
| 84 | + switch (installMethod) { |
| 85 | + case 'curl': { |
| 86 | + const versionFlag = target ? `--version ${target}` : ''; |
| 87 | + return `curl -fsSL https://t-req.io/install | bash${versionFlag ? ` -s -- ${versionFlag}` : ''}`; |
| 88 | + } |
| 89 | + case 'npm': { |
| 90 | + const version = target ? `@${target}` : ''; |
| 91 | + return `npm install -g @t-req/app${version}`; |
| 92 | + } |
| 93 | + case 'bun': { |
| 94 | + const version = target ? `@${target}` : ''; |
| 95 | + return `bun install -g @t-req/app${version}`; |
| 96 | + } |
| 97 | + default: { |
| 98 | + const version = target ? `@${target}` : ''; |
| 99 | + return `npm install -g @t-req/app${version}`; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments