forked from supabase/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.layer.ts
More file actions
89 lines (80 loc) · 2.86 KB
/
Copy pathruntime.layer.ts
File metadata and controls
89 lines (80 loc) · 2.86 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
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
import { note } from "@clack/prompts";
import { Effect, Layer, Option, Path } from "effect";
import { CliConfig } from "../../next/config/cli-config.service.ts";
import { CLI_VERSION } from "../cli/version.ts";
import { RuntimeInfo } from "../runtime/runtime-info.service.ts";
import { Tty } from "../runtime/tty.service.ts";
import { getConfigDir, getEffectiveConsent, readTelemetryConfig } from "./consent.ts";
import { makeTelemetryIdentity, resolveIdentity } from "./identity.ts";
import type { TelemetryConfig } from "./types.ts";
import { TelemetryRuntime } from "./runtime.service.ts";
const CI_ENV_VARS = ["CI", "GITHUB_ACTIONS", "GITLAB_CI", "CIRCLECI", "JENKINS_URL", "BUILDKITE"];
function identityFromConfig(config: Option.Option<TelemetryConfig>) {
if (Option.isSome(config)) {
return {
deviceId: config.value.device_id,
sessionId: config.value.session_id,
distinctId: config.value.distinct_id,
isFirstRun: false,
} as const;
}
return {
deviceId: crypto.randomUUID(),
sessionId: crypto.randomUUID(),
distinctId: undefined,
isFirstRun: false,
} as const;
}
export const telemetryRuntimeLayer = Layer.effect(
TelemetryRuntime,
Effect.gen(function* () {
const cliConfig = yield* CliConfig;
const path = yield* Path.Path;
const configDir = yield* getConfigDir;
const tracesDir = path.join(configDir, "traces");
const tty = yield* Tty;
const runtimeInfo = yield* RuntimeInfo;
const config = yield* readTelemetryConfig(configDir);
const isTty = tty.stdoutIsTty;
const consent = yield* getEffectiveConsent(config);
let identity;
if (consent === "granted") {
if (Option.isNone(config) && isTty) {
yield* Effect.sync(() =>
note(
"Supabase collects anonymous usage data to improve the CLI.\nYou can opt out at any time:\n\n supabase telemetry disable\n\nLearn more: https://supabase.com/docs/guides/local-development/cli/getting-started#telemetry",
"Telemetry",
),
);
}
identity = yield* resolveIdentity(configDir);
} else {
identity = identityFromConfig(config);
}
const showDebug =
(Option.isSome(cliConfig.debug) && cliConfig.debug.value === "1") ||
(Option.isSome(cliConfig.telemetryDebug) && cliConfig.telemetryDebug.value === "1");
let isCi = false;
for (const envVar of CI_ENV_VARS) {
if (process.env[envVar] !== undefined) {
isCi = true;
break;
}
}
return TelemetryRuntime.of({
configDir,
tracesDir,
consent,
showDebug,
deviceId: identity.deviceId,
sessionId: identity.sessionId,
identity: makeTelemetryIdentity(identity.distinctId),
isFirstRun: identity.isFirstRun,
isTty,
isCi,
os: runtimeInfo.platform,
arch: runtimeInfo.arch,
cliVersion: CLI_VERSION,
});
}),
);