File size: 1,807 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
import * as path from 'path';

export interface Platform {
  os: 'darwin' | 'linux' | 'windows';
  arch: 'amd64' | 'arm64';
}

export function detectPlatform(): Platform {
  const platform = process.platform as string;

  // Only support x64 (amd64) and arm64
  let arch: 'amd64' | 'arm64';
  if (process.arch === 'x64') {
    arch = 'amd64';
  } else if (process.arch === 'arm64') {
    arch = 'arm64';
  } else {
    throw new Error(
      `Unsupported architecture: ${process.arch}. ` + `Only x64 (amd64) and arm64 are supported.`
    );
  }

  const osMap: Record<string, 'darwin' | 'linux' | 'windows'> = {
    darwin: 'darwin',
    linux: 'linux',
    win32: 'windows',
  };

  const os_name = osMap[platform];
  if (!os_name) {
    throw new Error(`Unsupported platform: ${platform}`);
  }

  return { os: os_name, arch };
}

export function getBinaryName(platform: Platform): string {
  const { os, arch } = platform;
  const archName = arch === 'arm64' ? 'arm64' : 'amd64';

  if (os === 'windows') {
    return `pinchtab-${os}-${archName}.exe`;
  }
  return `pinchtab-${os}-${archName}`;
}

export function getBinDir(): string {
  return path.join(process.env.HOME || process.env.USERPROFILE || '', '.pinchtab', 'bin');
}

export function getBinaryPath(binaryName: string, version?: string): string {
  // Allow override via environment variable (for Docker, custom builds, etc.)
  if (process.env.PINCHTAB_BINARY_PATH) {
    return process.env.PINCHTAB_BINARY_PATH;
  }

  // Version-specific path: ~/.pinchtab/bin/0.7.0/pinchtab-darwin-arm64
  // This allows multiple versions to coexist and prevents silent overwrites
  if (version) {
    return path.join(getBinDir(), version, binaryName);
  }

  // Fallback to version-less for backwards compat
  return path.join(getBinDir(), binaryName);
}