-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplatform.node.ts
161 lines (145 loc) · 3.93 KB
/
platform.node.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// NOTE: Node type definitions are not installed on purpose to prevent
// accidental usage outside this file. As a result, there's quite a few
// @ts-ignore statements in this file...
import { AsyncLocalStorage } from "node:async_hooks";
import { readFileSync } from "node:fs";
import { dirname, join, parse } from "node:path";
import * as process from "node:process";
import { AUTOMETRICS_DEFAULT_SERVICE_NAME } from "./constants.ts";
import { getGitRepositoryUrl, getPackageStringField } from "./platformUtils.ts";
/**
* Returns the version of the application, based on environment variables.
*
* If no relevant environment variables are set, it attempts to use the version
* from the closest `package.json` file.
*
* @internal
*/
export function getVersion(): string | undefined {
return (
// @ts-ignore
process.env.AUTOMETRICS_VERSION ??
// @ts-ignore
process.env.npm_package_version ??
// @ts-ignore
process.env.PACKAGE_VERSION ??
detectPackageVersion()
);
}
/**
* Returns the commit hash of the current build of the application, based on
* environment variables.
*
* @internal
*/
export function getCommit(): string | undefined {
// @ts-ignore
return process.env.AUTOMETRICS_COMMIT || process.env.COMMIT_SHA;
}
/**
* Returns the branch of the current build of the application, based on
* environment variables.
*
* @internal
*/
export function getBranch(): string | undefined {
// @ts-ignore
return process.env.AUTOMETRICS_BRANCH || process.env.BRANCH_NAME;
}
/**
* Returns the current working directory for the process we're running in.
*
* @internal
*/
export function getCwd(): string {
// @ts-ignore
return process.cwd();
}
/**
* Returns the URL to the repository where the project's source code is located.
*
* @internal
*/
export function getRepositoryUrl(): string | undefined {
// @ts-ignore
return process.env.AUTOMETRICS_REPOSITORY_URL ?? detectRepositoryUrl();
}
/**
* Returns a hint as to which provider is being used to host the repository.
*
* @internal
*/
export function getRepositoryProvider(): string | undefined {
// @ts-ignore
return process.env.AUTOMETRICS_REPOSITORY_PROVIDER;
}
/**
* Returns the service name based on environment variables.
*
* If no relevant environment variables are set, it attempts to use the version
* from the closest `package.json` file.
*
* @internal
*/
export function getServiceName(): string {
return (
// @ts-ignore
process.env.AUTOMETRICS_SERVICE_NAME ??
// @ts-ignore
process.env.OTEL_SERVICE_NAME ??
detectPackageName() ??
AUTOMETRICS_DEFAULT_SERVICE_NAME
);
}
/**
* Returns a new `AsyncLocalStorage` instance for storing caller information.
*
* @internal
*/
export function getALSInstance() {
return new AsyncLocalStorage<{
callerFunction?: string;
callerModule?: string;
}>();
}
/**
* Returns a boolean indicating whether a given path is the file-system root or not.
*
* @internal
*/
export function isRootPath(pathToCheck: string): boolean {
return pathToCheck === parse(getCwd()).root;
}
function detectPackageName(): string | undefined {
try {
const gitConfig = readClosest("package.json");
return getPackageStringField(gitConfig, "name");
} catch {}
}
function detectPackageVersion(): string | undefined {
try {
const gitConfig = readClosest("package.json");
return getPackageStringField(gitConfig, "version");
} catch {}
}
function detectRepositoryUrl(): string | undefined {
try {
const gitConfig = readClosest(".git/config");
return getGitRepositoryUrl(gitConfig);
} catch {}
}
export function readClosest(path: string): Uint8Array {
let basePath = getCwd();
while (basePath.length > 0) {
try {
return readFileSync(join(basePath, path));
} catch {
// Break once we've tried the file-system root
if (isRootPath(basePath)) {
break;
}
basePath = dirname(basePath);
}
}
throw new Error(`Could not read ${path}`);
}