summaryrefslogtreecommitdiff
path: root/packages/cli/src/utils/version.ts
blob: 8ccc37379a092ee8a08a2dd08c245617cc76010c (plain)
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { readPackageUp } from 'read-package-up';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

let cliVersion: string | undefined;

export async function getCliVersion(): Promise<string> {
  if (cliVersion) {
    return cliVersion;
  }

  if (process.env.CLI_VERSION) {
    cliVersion = process.env.CLI_VERSION;
    return cliVersion;
  }

  try {
    const readUpResult = await readPackageUp({ cwd: __dirname });
    cliVersion = readUpResult?.packageJson.version || 'unknown';
  } catch (_e) {
    cliVersion = 'unknown';
  }

  return cliVersion;
}