forked from supabase/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.handler.ts
More file actions
187 lines (167 loc) · 6.5 KB
/
Copy pathlink.handler.ts
File metadata and controls
187 lines (167 loc) · 6.5 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { Effect, Option } from "effect";
import { StateManager, projectStateManagerPathsFromRoot } from "@supabase/stack/effect";
import { ensureProjectStateIgnored } from "../../config/project-gitignore.ts";
import { ProjectHome } from "../../config/project-home.service.ts";
import { refreshLinkedProjectSnapshot } from "../../config/project-link-refresh.ts";
import {
ProjectLinkRemote,
formatLinkedProjectLabel,
linkedProjectVersionServices,
} from "../../config/project-link-remote.service.ts";
import { ProjectLinkState } from "../../config/project-link-state.service.ts";
import { Output } from "../../../shared/output/output.service.ts";
import { Analytics } from "../../../shared/telemetry/analytics.service.ts";
import { withAnalyticsContext } from "../../../shared/telemetry/analytics-context.ts";
import type { LinkFlags } from "./link.command.ts";
import { NoAccessibleProjectsError, ProjectRefRequiredError } from "./link.errors.ts";
const promptForAccessibleProject = Effect.fnUntraced(function* () {
const output = yield* Output;
const remote = yield* ProjectLinkRemote;
const projects = yield* remote.listAccessibleProjects;
if (projects.length === 0) {
return yield* Effect.fail(
new NoAccessibleProjectsError({
detail: "No accessible Supabase projects were found for this account.",
suggestion: "Create a project in the dashboard or log in with a different account.",
}),
);
}
return yield* output.promptSelect(
"Select a Supabase project to link",
projects.map((project) => ({
value: project.ref,
label: project.name,
hint: `${project.ref} | ${project.region} | ${project.status}`,
})),
{
mode: "auto",
placeholder: "Search projects...",
maxItems: 10,
},
);
});
const chooseProjectRef = Effect.fnUntraced(function* (flagProjectRef: Option.Option<string>) {
const output = yield* Output;
if (Option.isSome(flagProjectRef)) {
return flagProjectRef.value.trim();
}
const projectLinkState = yield* ProjectLinkState;
const cachedLinkState = yield* projectLinkState.load;
if (Option.isSome(cachedLinkState)) {
if (!output.interactive) {
yield* output.info(
`This local project is already linked to ${formatLinkedProjectLabel(cachedLinkState.value.project)}; refreshing linked project metadata.`,
);
return cachedLinkState.value.project.ref;
}
yield* output.info(
`This local project is already linked to ${formatLinkedProjectLabel(cachedLinkState.value.project)}.`,
);
const action = yield* output.promptSelect(
"What would you like to do?",
[
{
value: "refresh",
label: "Refresh linked metadata",
hint: `Refresh the current linked project metadata for ${formatLinkedProjectLabel(cachedLinkState.value.project)}`,
},
{
value: "relink",
label: "Choose a different project",
hint: "Select another accessible Supabase project",
},
],
{ mode: "select" },
);
if (action === "refresh") {
return cachedLinkState.value.project.ref;
}
return yield* promptForAccessibleProject();
}
if (!output.interactive) {
return yield* Effect.fail(
new ProjectRefRequiredError({
detail: "A project ref is required in non-interactive mode.",
suggestion: "Pass --project-ref or link this checkout interactively first.",
}),
);
}
return yield* promptForAccessibleProject();
});
const printLinkedVersions = Effect.fnUntraced(function* (
versions: Record<string, string | undefined>,
) {
const output = yield* Output;
for (const service of linkedProjectVersionServices) {
const version = versions[service];
if (version !== undefined) {
yield* output.info(`${service}: ${version}`);
}
}
});
export const link = Effect.fnUntraced(function* (flags: LinkFlags) {
const output = yield* Output;
const analytics = yield* Analytics;
const projectHome = yield* ProjectHome;
const stateManager = yield* StateManager.pipe(
Effect.provide(StateManager.make(projectStateManagerPathsFromRoot(projectHome.projectHomeDir))),
);
yield* output.intro("Link local project to Supabase");
const projectRef = yield* chooseProjectRef(flags.projectRef);
yield* ensureProjectStateIgnored(projectHome.projectRoot);
const refreshed = yield* refreshLinkedProjectSnapshot(
projectRef,
yield* stateManager.scanMetadata(),
);
const linkedProject = refreshed.linkedProject;
yield* output.success(`Linked to project ${linkedProject.ref}.`, {
project_ref: linkedProject.ref,
project_name: linkedProject.name,
region: linkedProject.region,
status: linkedProject.status,
versions: linkedProject.versions,
unavailable_services: linkedProject.unavailableServices,
});
yield* output.info("Updated cached linked service versions:");
yield* printLinkedVersions(linkedProject.versions);
if (linkedProject.unavailableServices.length > 0) {
yield* output.warn(
`Some remote service versions could not be fetched and will keep using CLI defaults: ${linkedProject.unavailableServices.join(", ")}`,
);
}
if (refreshed.stacksNeedingUpdate.length > 0) {
yield* output.warn(
[
"Linked project versions changed for local stack metadata:",
...refreshed.stacksNeedingUpdate.map(
({ stackName, diff }) =>
` ${stackName}: ${diff.map(({ service, pinnedVersion, availableVersion }) => `${service} ${pinnedVersion} -> ${availableVersion}`).join(", ")}`,
),
"Run `supabase stack update` to adopt the refreshed pinned versions.",
].join("\n"),
);
}
const groups = {
organization: linkedProject.organizationSlug,
project: linkedProject.ref,
} as const;
yield* analytics.groupIdentify("organization", linkedProject.organizationSlug, {
organization_id: linkedProject.organizationId,
organization_slug: linkedProject.organizationSlug,
});
yield* analytics
.groupIdentify("project", linkedProject.ref, {
project_name: linkedProject.name,
project_ref: linkedProject.ref,
organization_slug: linkedProject.organizationSlug,
})
.pipe(withAnalyticsContext({ groups }));
yield* analytics
.capture("cli_project_linked", {
project_ref: linkedProject.ref,
project_name: linkedProject.name,
organization_slug: linkedProject.organizationSlug,
})
.pipe(withAnalyticsContext({ groups }));
yield* output.outro(`Linked local project to ${linkedProject.name} (${linkedProject.ref}).`);
});