Repository navigation
Expand file tree
/
Copy pathversion.ts
More file actions
55 lines (51 loc) · 2.06 KB
/
Copy pathversion.ts
File metadata and controls
55 lines (51 loc) · 2.06 KB
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
import { readFileSync } from 'node:fs'
/**
* The published package version, and the `User-Agent` built from it.
*
* Its own module because both the command tree and the HTTP client need the
* version, and the client cannot reach `program.ts` — `program` builds the
* commands, which reach the client, so importing it back would close a cycle.
*
* Read from `package.json` rather than inlined so a release cannot ship a
* version string that disagrees with the package it came from. The bundle keeps
* `dist/index.js` one directory below the manifest, and npm always publishes the
* manifest, so the relative path holds for an installed package as well as a
* local build. Read LAZILY with a fallback: hosts that bundle this module into
* a different layout (the sim server's embedded CLI, Trigger.dev workers) have
* no manifest at the relative path, and an import-time throw took their whole
* task graph down.
*/
function readPackageVersion(): string {
try {
const metadata: unknown = JSON.parse(
readFileSync(new URL('../package.json', import.meta.url), 'utf8')
)
if (
typeof metadata === 'object' &&
metadata !== null &&
'version' in metadata &&
typeof metadata.version === 'string'
) {
return metadata.version
}
} catch {
// Fall through to the sentinel: an embedded host has no manifest to read.
}
return '0.0.0-embedded'
}
let cachedVersion: string | null = null
export function cliVersion(): string {
cachedVersion ??= readPackageVersion()
return cachedVersion
}
/**
* Identifies the CLI to the API, the way every other terminal client does.
*
* Without it a CLI request is indistinguishable from any other API traffic, so
* a bug that only reproduces on one CLI version cannot be found in the server's
* own logs. The runtime and platform ride along for the same reason: they are
* the first things asked about a transport failure that only some users see.
*/
export function userAgent(): string {
return `sim-cli/${cliVersion()} node/${process.versions.node} (${process.platform}; ${process.arch})`
}