Spaces:
Sleeping

File size: 2,173 Bytes
dcedb25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

// Which RELEASE this install is, for the dashboard to display (#703).
//
// Deliberately not `server/package.json`: that version tracks the server
// workspace and has sat at 0.2.1 across many releases, as has the OpenAPI
// spec's 0.4.1. The number users know the app by lives in desktop/package.json
// — it is what the release recipe bumps and what the installers are named for.
//
// Order:
//   1. FREEAPI_VERSION — the desktop shell sets it from app.getVersion(); a
//      packaged/self-hosted deployment can set it too.
//   2. desktop/package.json, found by walking up from this module. Present in a
//      source checkout, and the Docker runtime image copies it in for this.
//   3. null — say nothing rather than state a version that isn't the release.
//      The dashboard omits the row entirely when this is null.

const MAX_WALK_UP = 6;

function readVersion(file: string): string | null {
  try {
    const parsed = JSON.parse(readFileSync(file, 'utf8')) as { version?: unknown };
    return typeof parsed.version === 'string' && parsed.version.trim() ? parsed.version.trim() : null;
  } catch {
    return null;
  }
}

function findDesktopVersion(): string | null {
  let dir: string;
  try {
    dir = path.dirname(fileURLToPath(import.meta.url));
  } catch {
    return null;
  }
  for (let i = 0; i < MAX_WALK_UP; i++) {
    const found = readVersion(path.join(dir, 'desktop', 'package.json'));
    if (found) return found;
    const parent = path.dirname(dir);
    if (parent === dir) break;
    dir = parent;
  }
  return null;
}

let cached: string | null | undefined;

/** The released app version, or null when it cannot be established honestly. */
export function getAppVersion(): string | null {
  if (cached !== undefined) return cached;
  const fromEnv = process.env.FREEAPI_VERSION?.trim();
  cached = fromEnv ? fromEnv : findDesktopVersion();
  return cached;
}

/** Test seam — the resolver caches, and env changes must be able to take effect. */
export function resetAppVersionCache(): void {
  cached = undefined;
}