@samebase/alchemy-convex 0.29.0 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -39
- package/lib/{HubClient-HeGoVOLO.js → State-t0BZggNl.js} +62 -234
- package/lib/State-t0BZggNl.js.map +1 -0
- package/lib/cli.js +5 -36
- package/lib/cli.js.map +1 -1
- package/lib/index.d.ts +40 -49
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +68 -9
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/LocalDeployment.ts +105 -0
- package/src/ManagementApi.ts +81 -104
- package/src/Providers.ts +3 -3
- package/src/SharedHubClient.ts +31 -7
- package/src/cli.ts +7 -56
- package/src/index.ts +5 -7
- package/lib/HubClient-HeGoVOLO.js.map +0 -1
- package/src/HubClient.ts +0 -310
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Convex.LocalDeployment: the logged-in local deployment of one folder, which
|
|
2
|
+
// `npx convex deployment create <team>:<project>:local --select` makes and `npx convex dev` runs on
|
|
3
|
+
// this machine. Each folder, such as each git worktree, has its own, with its own data.
|
|
4
|
+
//
|
|
5
|
+
// The resource writes its `env` and nothing else. The running `convex dev` owns the backend and
|
|
6
|
+
// pushes the functions, so the stage of a laptop runs after `convex dev` has started, and the
|
|
7
|
+
// first push of a new local deployment takes the variables that the project's default variables
|
|
8
|
+
// for development gave it. The resource reads the deployment from
|
|
9
|
+
// `.convex/local/default/config.json`, where the Convex CLI keeps its name, ports, and admin key,
|
|
10
|
+
// and writes with that admin key through the same call as for a cloud deployment.
|
|
11
|
+
//
|
|
12
|
+
// A destroy changes nothing: the folder's deployment and its variables stay with the folder.
|
|
13
|
+
import { Resource } from "alchemy";
|
|
14
|
+
import { isResolved } from "alchemy/Diff";
|
|
15
|
+
import * as Provider from "alchemy/Provider";
|
|
16
|
+
import * as Effect from "effect/Effect";
|
|
17
|
+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
|
|
18
|
+
import * as Schema from "effect/Schema";
|
|
19
|
+
import { readLocalDeployment } from "./ManagementApi.ts";
|
|
20
|
+
import type { Providers } from "./Providers.ts";
|
|
21
|
+
import { type EnvValue, push } from "./Push.ts";
|
|
22
|
+
|
|
23
|
+
/** The folder has no logged-in local deployment. */
|
|
24
|
+
export class LocalDeploymentNotFound extends Schema.TaggedError<LocalDeploymentNotFound>()(
|
|
25
|
+
"LocalDeploymentNotFound",
|
|
26
|
+
{ rootDir: Schema.String },
|
|
27
|
+
) {
|
|
28
|
+
override get message() {
|
|
29
|
+
return `Convex.LocalDeployment: ${this.rootDir} has no logged-in local deployment. Run \`npx convex deployment create <team>:<project>:local --select\` there, then \`npx convex dev\`.`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The folder's logged-in local deployment, read when the stack's program runs, so that a client
|
|
35
|
+
* can take its `siteUrl`. Fails with `LocalDeploymentNotFound` when the folder has none.
|
|
36
|
+
*/
|
|
37
|
+
const of = (rootDir = ".") => {
|
|
38
|
+
const info = readLocalDeployment(rootDir);
|
|
39
|
+
if (info === undefined) throw new LocalDeploymentNotFound({ rootDir });
|
|
40
|
+
return { name: info.name, url: info.url, siteUrl: info.siteUrl };
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export interface LocalDeploymentProps {
|
|
44
|
+
/** The folder of the Convex app, where `.convex/local/default` is. The working directory without it. */
|
|
45
|
+
readonly rootDir?: string;
|
|
46
|
+
/** The variables of the deployment. A name that leaves `env` is removed. */
|
|
47
|
+
readonly env: Readonly<Record<string, EnvValue>>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface LocalDeploymentAttributes {
|
|
51
|
+
readonly name: string;
|
|
52
|
+
readonly url: string;
|
|
53
|
+
readonly siteUrl: string;
|
|
54
|
+
/** The names that this resource set, so a name that leaves `env` is removed. */
|
|
55
|
+
readonly envNames: readonly string[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type LocalDeployment = Resource<
|
|
59
|
+
"Convex.LocalDeployment",
|
|
60
|
+
LocalDeploymentProps,
|
|
61
|
+
LocalDeploymentAttributes,
|
|
62
|
+
never,
|
|
63
|
+
Providers
|
|
64
|
+
>;
|
|
65
|
+
export const LocalDeployment = Resource<LocalDeployment>("Convex.LocalDeployment")({ of });
|
|
66
|
+
|
|
67
|
+
export const LocalDeploymentProvider = () =>
|
|
68
|
+
Provider.effect(
|
|
69
|
+
LocalDeployment,
|
|
70
|
+
Effect.gen(function* () {
|
|
71
|
+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
72
|
+
return {
|
|
73
|
+
// Always an update: the variables are read and only changed values are written.
|
|
74
|
+
diff: ({ news }) =>
|
|
75
|
+
Effect.succeed(isResolved(news) ? ({ action: "update" } as const) : undefined),
|
|
76
|
+
|
|
77
|
+
read: ({ output }) => Effect.succeed(output),
|
|
78
|
+
|
|
79
|
+
reconcile: Effect.fn(function* ({ news, output }) {
|
|
80
|
+
const rootDir = news.rootDir ?? ".";
|
|
81
|
+
const info = readLocalDeployment(rootDir);
|
|
82
|
+
if (info === undefined) return yield* new LocalDeploymentNotFound({ rootDir });
|
|
83
|
+
// Without `source`, a push only sets the variables and removes the names that left `env`.
|
|
84
|
+
yield* push(spawner, info.adminKey, {
|
|
85
|
+
deployment: info.name,
|
|
86
|
+
url: info.url,
|
|
87
|
+
rootDir,
|
|
88
|
+
source: undefined,
|
|
89
|
+
env: news.env,
|
|
90
|
+
previousEnvNames: output?.name === info.name ? output.envNames : [],
|
|
91
|
+
adopt: true,
|
|
92
|
+
previewName: undefined,
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
name: info.name,
|
|
96
|
+
url: info.url,
|
|
97
|
+
siteUrl: info.siteUrl,
|
|
98
|
+
envNames: Object.keys(news.env).sort(),
|
|
99
|
+
};
|
|
100
|
+
}),
|
|
101
|
+
|
|
102
|
+
delete: () => Effect.void,
|
|
103
|
+
};
|
|
104
|
+
}),
|
|
105
|
+
);
|
package/src/ManagementApi.ts
CHANGED
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
// typed error, resolves credentials lazily, and validates the fields the
|
|
6
6
|
// provider relies on at runtime.
|
|
7
7
|
import { readFileSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
8
9
|
import { parseEnv } from "node:util";
|
|
9
10
|
import { createDeploymentClient, createManagementClient } from "@convex-dev/platform";
|
|
10
11
|
import * as Context from "effect/Context";
|
|
11
12
|
import * as Effect from "effect/Effect";
|
|
12
13
|
import * as Layer from "effect/Layer";
|
|
14
|
+
import * as Option from "effect/Option";
|
|
13
15
|
import * as Redacted from "effect/Redacted";
|
|
14
16
|
import * as Schedule from "effect/Schedule";
|
|
15
17
|
import * as Schema from "effect/Schema";
|
|
@@ -78,13 +80,6 @@ export interface ManagementApiService {
|
|
|
78
80
|
* Convex CLI selected (CONVEX_DEPLOYMENT). Read once for each service.
|
|
79
81
|
*/
|
|
80
82
|
readonly teamId: Effect.Effect<number, ConvexApiError>;
|
|
81
|
-
/**
|
|
82
|
-
* Team id for a numeric id or a slug, for Convex.HubClient, which names the
|
|
83
|
-
* team of the hub. With a team access token, a slug is checked against the
|
|
84
|
-
* `teamSlug` of the team's projects, so a token of another team fails; with
|
|
85
|
-
* the Convex CLI login, a slug goes to the dashboard's team list.
|
|
86
|
-
*/
|
|
87
|
-
readonly resolveTeamId: (team: string | number) => Effect.Effect<number, ConvexApiError>;
|
|
88
83
|
/**
|
|
89
84
|
* True when a deploy key that Convex returned carries the token of the
|
|
90
85
|
* Management API credential itself. With an OAuth token, create_deploy_key
|
|
@@ -197,27 +192,69 @@ export const absentAsUndefined = <A, R>(
|
|
|
197
192
|
),
|
|
198
193
|
);
|
|
199
194
|
|
|
200
|
-
const TeamRow = Schema.Struct({ id: Schema.Number, slug: Schema.String });
|
|
201
|
-
const TeamRows = Schema.Array(TeamRow);
|
|
202
|
-
|
|
203
|
-
/** The first page of GET /teams/{team_id}/projects: every row carries the slug of the team. */
|
|
204
|
-
const ProjectSlugRows = Schema.Struct({
|
|
205
|
-
items: Schema.Array(Schema.Struct({ teamSlug: Schema.String })),
|
|
206
|
-
});
|
|
207
|
-
|
|
208
195
|
/** GET /deployments/{name}: the project of the deployment. */
|
|
209
196
|
const DeploymentProject = Schema.Struct({ projectId: Schema.Number });
|
|
210
197
|
|
|
211
198
|
/** GET /projects/{id}: the team of the project. */
|
|
212
199
|
const ProjectTeam = Schema.Struct({ teamId: Schema.Number });
|
|
213
200
|
|
|
201
|
+
/** A folder's `.convex/local/default/config.json`, as the Convex CLI writes it. */
|
|
202
|
+
const LocalConfig = Schema.Struct({
|
|
203
|
+
deploymentName: Schema.String,
|
|
204
|
+
ports: Schema.Struct({ cloud: Schema.Number, site: Schema.Number }),
|
|
205
|
+
adminKey: Schema.String,
|
|
206
|
+
/** The project of a logged-in local deployment. An anonymous one has none. */
|
|
207
|
+
cloudProjectId: Schema.Number,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
export interface LocalDeploymentInfo {
|
|
211
|
+
/** Such as `local-nicu-samebase_com-1`. */
|
|
212
|
+
readonly name: string;
|
|
213
|
+
/** The client URL, `http://127.0.0.1:<cloud port>`. */
|
|
214
|
+
readonly url: string;
|
|
215
|
+
/** The HTTP actions URL, `http://127.0.0.1:<site port>`, the local `CONVEX_SITE_URL`. */
|
|
216
|
+
readonly siteUrl: string;
|
|
217
|
+
/** The Convex project that the deployment belongs to. */
|
|
218
|
+
readonly projectId: number;
|
|
219
|
+
readonly adminKey: Redacted.Redacted<string>;
|
|
220
|
+
}
|
|
221
|
+
|
|
214
222
|
/**
|
|
215
|
-
* The
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* (`local:local-team-app`, `anonymous:anonymous-app`), which runs on this machine and is in no team.
|
|
223
|
+
* The logged-in local deployment of `rootDir`, which `npx convex deployment create
|
|
224
|
+
* <team>:<project>:local` makes: undefined when the folder has none, or only an anonymous one, which
|
|
225
|
+
* belongs to no project.
|
|
219
226
|
*/
|
|
220
|
-
const
|
|
227
|
+
export const readLocalDeployment = (rootDir = "."): LocalDeploymentInfo | undefined => {
|
|
228
|
+
let raw: unknown;
|
|
229
|
+
try {
|
|
230
|
+
raw = JSON.parse(
|
|
231
|
+
readFileSync(join(rootDir, ".convex", "local", "default", "config.json"), "utf8"),
|
|
232
|
+
);
|
|
233
|
+
} catch {
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
const config = Option.getOrUndefined(Schema.decodeUnknownOption(LocalConfig)(raw));
|
|
237
|
+
if (config === undefined || !config.deploymentName.startsWith("local-")) return undefined;
|
|
238
|
+
return {
|
|
239
|
+
name: config.deploymentName,
|
|
240
|
+
url: `http://127.0.0.1:${config.ports.cloud}`,
|
|
241
|
+
siteUrl: `http://127.0.0.1:${config.ports.site}`,
|
|
242
|
+
projectId: config.cloudProjectId,
|
|
243
|
+
adminKey: Redacted.make(config.adminKey),
|
|
244
|
+
};
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The deployment that the Convex CLI selected: CONVEX_DEPLOYMENT in the environment or in
|
|
249
|
+
* `.env.local` of the working directory, as `npx convex dev` writes it. A cloud deployment
|
|
250
|
+
* (`dev:happy-animal-123`) by its name; a logged-in local deployment (`local:local-team-app`) by the
|
|
251
|
+
* project in the working directory's `.convex/local/default`, when that is the selected one.
|
|
252
|
+
* Undefined when neither has it, and for an anonymous deployment, which is in no project.
|
|
253
|
+
*/
|
|
254
|
+
const selectedDeployment = ():
|
|
255
|
+
| { readonly kind: "cloud"; readonly name: string }
|
|
256
|
+
| { readonly kind: "local"; readonly projectId: number }
|
|
257
|
+
| undefined => {
|
|
221
258
|
const fromEnv = process.env["CONVEX_DEPLOYMENT"];
|
|
222
259
|
const value =
|
|
223
260
|
fromEnv !== undefined && fromEnv !== ""
|
|
@@ -229,8 +266,13 @@ const selectedDeployment = () => {
|
|
|
229
266
|
return undefined;
|
|
230
267
|
}
|
|
231
268
|
})();
|
|
269
|
+
const localName = /^local:(local-[^:]+)$/.exec(value ?? "")?.[1];
|
|
270
|
+
if (localName !== undefined) {
|
|
271
|
+
const local = readLocalDeployment();
|
|
272
|
+
return local?.name === localName ? { kind: "local", projectId: local.projectId } : undefined;
|
|
273
|
+
}
|
|
232
274
|
const name = /^(?:(?:dev|prod|preview):)?([^:]+)$/.exec(value ?? "")?.[1];
|
|
233
|
-
return name === undefined || name.startsWith("anonymous-") ? undefined : name;
|
|
275
|
+
return name === undefined || name.startsWith("anonymous-") ? undefined : { kind: "cloud", name };
|
|
234
276
|
};
|
|
235
277
|
|
|
236
278
|
/** GET /token_details: the team of a team token, or the project of a project token. */
|
|
@@ -289,27 +331,18 @@ const makeService = (source: Effect.Effect<ResolvedCredentials>): ManagementApiS
|
|
|
289
331
|
),
|
|
290
332
|
);
|
|
291
333
|
|
|
292
|
-
/** The slug of the team, from the first project of its listing, or undefined for a team without one. Read once. */
|
|
293
|
-
const teamSlug = (teamId: number) =>
|
|
294
|
-
request("list projects", (c) =>
|
|
295
|
-
c.GET("/teams/{team_id}/projects", { params: { path: { team_id: teamId } } }),
|
|
296
|
-
).pipe(
|
|
297
|
-
Effect.map((body) => Schema.decodeUnknownSync(ProjectSlugRows)(body).items[0]?.teamSlug),
|
|
298
|
-
);
|
|
299
|
-
|
|
300
|
-
const projectTokenError = (projectId: number) =>
|
|
301
|
-
new ConvexApiError({
|
|
302
|
-
operation: "resolve team",
|
|
303
|
-
status: 403,
|
|
304
|
-
message: `the access token belongs to project ${projectId}, and a project token cannot create projects. Use a team access token.`,
|
|
305
|
-
});
|
|
306
|
-
|
|
307
334
|
const teamId: ManagementApiService["teamId"] = Effect.runSync(
|
|
308
335
|
Effect.cached(
|
|
309
336
|
Effect.gen(function* () {
|
|
310
337
|
const details = yield* tokenDetails;
|
|
311
338
|
if (details?.type === "teamToken") return details.teamId;
|
|
312
|
-
if (details?.type === "projectToken")
|
|
339
|
+
if (details?.type === "projectToken") {
|
|
340
|
+
return yield* new ConvexApiError({
|
|
341
|
+
operation: "resolve team",
|
|
342
|
+
status: 403,
|
|
343
|
+
message: `the access token belongs to project ${details.projectId}, and a project token cannot create projects. Use a team access token.`,
|
|
344
|
+
});
|
|
345
|
+
}
|
|
313
346
|
// The Convex CLI login names no team, but the deployment that `npx convex dev` selected
|
|
314
347
|
// belongs to a project, and the project to a team.
|
|
315
348
|
const selected = selectedDeployment();
|
|
@@ -318,15 +351,19 @@ const makeService = (source: Effect.Effect<ResolvedCredentials>): ManagementApiS
|
|
|
318
351
|
operation: "resolve team",
|
|
319
352
|
status: 0,
|
|
320
353
|
message:
|
|
321
|
-
"the Convex CLI login names no team, and no
|
|
354
|
+
"the Convex CLI login names no team, and no deployment of a project is selected. Run `npx convex dev` once with a cloud deployment, or create a local one with `npx convex deployment create <team>:<project>:local --select`.",
|
|
322
355
|
});
|
|
323
356
|
}
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
357
|
+
const projectId =
|
|
358
|
+
selected.kind === "local"
|
|
359
|
+
? selected.projectId
|
|
360
|
+
: Schema.decodeUnknownSync(DeploymentProject)(
|
|
361
|
+
yield* request("get deployment", (c) =>
|
|
362
|
+
c.GET("/deployments/{deployment_name}", {
|
|
363
|
+
params: { path: { deployment_name: selected.name } },
|
|
364
|
+
}),
|
|
365
|
+
),
|
|
366
|
+
).projectId;
|
|
330
367
|
const project = yield* request("get project", (c) =>
|
|
331
368
|
c.GET("/projects/{project_id}", { params: { path: { project_id: projectId } } }),
|
|
332
369
|
);
|
|
@@ -335,65 +372,6 @@ const makeService = (source: Effect.Effect<ResolvedCredentials>): ManagementApiS
|
|
|
335
372
|
),
|
|
336
373
|
);
|
|
337
374
|
|
|
338
|
-
// The Management API addresses teams by numeric id. The only slug lookup
|
|
339
|
-
// it offers is per project, so a slug falls back to the dashboard's team
|
|
340
|
-
// list, which is unofficial. Pass the numeric team id to skip this.
|
|
341
|
-
const resolveTeamId: ManagementApiService["resolveTeamId"] = (team) =>
|
|
342
|
-
Effect.gen(function* () {
|
|
343
|
-
if (typeof team === "number") return team;
|
|
344
|
-
if (/^\d+$/.test(team)) return Number(team);
|
|
345
|
-
const details = yield* tokenDetails;
|
|
346
|
-
if (details?.type === "teamToken") {
|
|
347
|
-
// The Management API has no team-by-id call, so the slug of the
|
|
348
|
-
// team comes from any of its projects. A team without a project
|
|
349
|
-
// cannot be checked: the numeric id, or no team, is the fix.
|
|
350
|
-
const slug = yield* teamSlug(details.teamId);
|
|
351
|
-
if (slug === undefined) {
|
|
352
|
-
return yield* new ConvexApiError({
|
|
353
|
-
operation: "resolve team",
|
|
354
|
-
status: 0,
|
|
355
|
-
message: `team "${team}" cannot be checked against the access token: team ${details.teamId} has no project yet. Set team to ${details.teamId}.`,
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
if (slug !== team) {
|
|
359
|
-
return yield* new ConvexApiError({
|
|
360
|
-
operation: "resolve team",
|
|
361
|
-
status: 403,
|
|
362
|
-
message: `the access token belongs to team ${details.teamId} ("${slug}"), and team is "${team}". Use a token of "${team}", or set team to ${details.teamId}.`,
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
return details.teamId;
|
|
366
|
-
}
|
|
367
|
-
if (details?.type === "projectToken") return yield* projectTokenError(details.projectId);
|
|
368
|
-
const resolved = yield* credentials;
|
|
369
|
-
const rows = yield* Effect.tryPromise({
|
|
370
|
-
try: async () => {
|
|
371
|
-
const response = await fetch("https://api.convex.dev/api/dashboard/teams", {
|
|
372
|
-
headers: { Authorization: `Bearer ${Redacted.value(resolved.accessToken)}` },
|
|
373
|
-
});
|
|
374
|
-
if (!response.ok) {
|
|
375
|
-
throw new Error(`team lookup for "${team}" failed with HTTP ${response.status}`);
|
|
376
|
-
}
|
|
377
|
-
return Schema.decodeUnknownSync(TeamRows)(await response.json());
|
|
378
|
-
},
|
|
379
|
-
catch: (cause) =>
|
|
380
|
-
new ConvexApiError({
|
|
381
|
-
operation: "resolve team",
|
|
382
|
-
status: 0,
|
|
383
|
-
message: cause instanceof Error ? cause.message : String(cause),
|
|
384
|
-
}),
|
|
385
|
-
});
|
|
386
|
-
const match = rows.find((row) => row.slug === team);
|
|
387
|
-
if (match === undefined) {
|
|
388
|
-
return yield* new ConvexApiError({
|
|
389
|
-
operation: "resolve team",
|
|
390
|
-
status: 404,
|
|
391
|
-
message: `no team with slug "${team}" for this token`,
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
return match.id;
|
|
395
|
-
});
|
|
396
|
-
|
|
397
375
|
const isCredentialToken: ManagementApiService["isCredentialToken"] = (secret) =>
|
|
398
376
|
credentials.pipe(
|
|
399
377
|
Effect.map(
|
|
@@ -413,7 +391,6 @@ const makeService = (source: Effect.Effect<ResolvedCredentials>): ManagementApiS
|
|
|
413
391
|
requestVoid,
|
|
414
392
|
deploymentRequest,
|
|
415
393
|
teamId,
|
|
416
|
-
resolveTeamId,
|
|
417
394
|
isCredentialToken,
|
|
418
395
|
isOAuthCredential,
|
|
419
396
|
withToken,
|
package/src/Providers.ts
CHANGED
|
@@ -4,7 +4,7 @@ import * as Provider from "alchemy/Provider";
|
|
|
4
4
|
import * as Layer from "effect/Layer";
|
|
5
5
|
import { type Credentials, fromConfig } from "./Credentials.ts";
|
|
6
6
|
import { DeploymentProvider, DeploymentResource } from "./Deployment.ts";
|
|
7
|
-
import {
|
|
7
|
+
import { LocalDeployment, LocalDeploymentProvider } from "./LocalDeployment.ts";
|
|
8
8
|
import { ManagementApiLive } from "./ManagementApi.ts";
|
|
9
9
|
import { Project, ProjectProvider } from "./Project.ts";
|
|
10
10
|
import { SharedHubClient, SharedHubClientProvider } from "./SharedHubClient.ts";
|
|
@@ -19,13 +19,13 @@ export class Providers extends Provider.ProviderCollection<Providers>()("Convex"
|
|
|
19
19
|
export const providers = (credentials: Layer.Layer<Credentials> = fromConfig()) =>
|
|
20
20
|
Layer.effect(
|
|
21
21
|
Providers,
|
|
22
|
-
Provider.collection([Project, DeploymentResource,
|
|
22
|
+
Provider.collection([Project, DeploymentResource, LocalDeployment, SharedHubClient]),
|
|
23
23
|
).pipe(
|
|
24
24
|
Layer.provide(
|
|
25
25
|
Layer.mergeAll(
|
|
26
26
|
ProjectProvider(),
|
|
27
27
|
DeploymentProvider(),
|
|
28
|
-
|
|
28
|
+
LocalDeploymentProvider(),
|
|
29
29
|
SharedHubClientProvider(),
|
|
30
30
|
),
|
|
31
31
|
),
|
package/src/SharedHubClient.ts
CHANGED
|
@@ -53,6 +53,13 @@ interface CommonProps {
|
|
|
53
53
|
readonly redirectUri: string;
|
|
54
54
|
/** Recorded on the hub's client, such as the commit. The hub records "unknown" without it. */
|
|
55
55
|
readonly softwareVersion?: string;
|
|
56
|
+
/**
|
|
57
|
+
* The run fails unless the hub confirms the client in this run: it fails when the hub refuses or
|
|
58
|
+
* does not answer a registration or a renewal, and when it suspended the client. Without it, such
|
|
59
|
+
* a run gives the off values or keeps the saved client, so the app deploys anyway. Set it where
|
|
60
|
+
* sign-in is the product. A failed run saves nothing, so the next run asks the hub again.
|
|
61
|
+
*/
|
|
62
|
+
readonly required?: boolean;
|
|
56
63
|
}
|
|
57
64
|
|
|
58
65
|
/**
|
|
@@ -343,8 +350,13 @@ const newClient = (props: SharedHubClientProps) => {
|
|
|
343
350
|
return request(readOidcToken(requestUrl, requestToken));
|
|
344
351
|
};
|
|
345
352
|
|
|
346
|
-
/**
|
|
347
|
-
|
|
353
|
+
/**
|
|
354
|
+
* The client after a read on the hub, or undefined when the hub says that the client is gone. A
|
|
355
|
+
* read with no usable answer keeps the saved client, except for a required one: a required client
|
|
356
|
+
* passes a run only when the hub confirms it in that run, so a suspension that an earlier run saw,
|
|
357
|
+
* and did not save because that run failed, cannot pass a run that the hub does not answer.
|
|
358
|
+
*/
|
|
359
|
+
const renew = (saved: SharedHubClientAttributes, required: boolean) =>
|
|
348
360
|
Effect.gen(function* () {
|
|
349
361
|
const { status } = yield* send(
|
|
350
362
|
"The identity hub",
|
|
@@ -369,9 +381,15 @@ const renew = (saved: SharedHubClientAttributes) =>
|
|
|
369
381
|
}
|
|
370
382
|
}).pipe(
|
|
371
383
|
Effect.catchTag("SharedHubClientFailed", (error) =>
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
384
|
+
required
|
|
385
|
+
? Effect.fail(
|
|
386
|
+
new SharedHubClientFailed({
|
|
387
|
+
reason: `${saved.redirectUri} is required, and the hub did not confirm its client: ${error.reason}`,
|
|
388
|
+
}),
|
|
389
|
+
)
|
|
390
|
+
: Effect.logWarning(
|
|
391
|
+
`Convex.SharedHubClient: ${saved.redirectUri} keeps the saved client: ${error.reason}.`,
|
|
392
|
+
).pipe(Effect.as(saved)),
|
|
375
393
|
),
|
|
376
394
|
);
|
|
377
395
|
|
|
@@ -479,9 +497,15 @@ export const SharedHubClientProvider = () =>
|
|
|
479
497
|
}
|
|
480
498
|
const renewed =
|
|
481
499
|
saved?.redirectUri === news.redirectUri && saved.deploymentType === news.deploymentType
|
|
482
|
-
? yield* renew(saved)
|
|
500
|
+
? yield* renew(saved, news.required === true)
|
|
483
501
|
: undefined;
|
|
484
|
-
|
|
502
|
+
const client = renewed ?? (yield* newClient(news));
|
|
503
|
+
if (news.required === true && client.status !== "on") {
|
|
504
|
+
return yield* new SharedHubClientFailed({
|
|
505
|
+
reason: `${news.app} on ${news.redirectUri} is required and has no working client (${client.status}). The warning above names the reason`,
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return client;
|
|
485
509
|
}),
|
|
486
510
|
|
|
487
511
|
// A failed delete does not stop a destroy: the removal date of a preview or development client
|
package/src/cli.ts
CHANGED
|
@@ -1,82 +1,33 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The
|
|
2
|
+
// The command of the package:
|
|
3
3
|
//
|
|
4
|
-
// alchemy-convex hub-client --team <team> --app <app> --callback-path <path> [--env-prefix HUB]
|
|
5
4
|
// alchemy-convex stack-comment
|
|
6
5
|
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
// the deploys, and keeps one comment with the stacks of the repository: see src/StackComment.ts. It
|
|
11
|
-
// needs GITHUB_TOKEN, and reads the store of the team of the token. Both use CONVEX_ACCESS_TOKEN or
|
|
12
|
-
// the Convex CLI login.
|
|
13
|
-
import { execFileSync } from "node:child_process";
|
|
6
|
+
// stack-comment runs as a step of a pull request workflow after the deploys, and keeps one comment
|
|
7
|
+
// with the stacks of the repository: see src/StackComment.ts. It needs GITHUB_TOKEN, and reads the
|
|
8
|
+
// store of the team of CONVEX_ACCESS_TOKEN or of the Convex CLI login.
|
|
14
9
|
import { parseArgs } from "node:util";
|
|
15
10
|
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
16
11
|
import * as Effect from "effect/Effect";
|
|
17
12
|
import * as Layer from "effect/Layer";
|
|
18
|
-
import { fromConfig } from "./Credentials.ts";
|
|
19
|
-
import { registerDevelopmentClient } from "./HubClient.ts";
|
|
20
|
-
import { ManagementApiLive } from "./ManagementApi.ts";
|
|
21
|
-
import { installedCli } from "./Push.ts";
|
|
22
13
|
import { updateStackComment } from "./StackComment.ts";
|
|
23
14
|
import { state } from "./State.ts";
|
|
24
15
|
|
|
25
|
-
const { positionals
|
|
26
|
-
allowPositionals: true,
|
|
27
|
-
options: {
|
|
28
|
-
team: { type: "string" },
|
|
29
|
-
app: { type: "string" },
|
|
30
|
-
"callback-path": { type: "string" },
|
|
31
|
-
"env-prefix": { type: "string" },
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
const { team, app, "callback-path": callbackPath, "env-prefix": envPrefix } = values;
|
|
16
|
+
const { positionals } = parseArgs({ allowPositionals: true, options: {} });
|
|
35
17
|
const USAGE = `Usage:
|
|
36
|
-
alchemy-convex hub-client --team <team> --app <app> --callback-path <path> [--env-prefix HUB]
|
|
37
18
|
alchemy-convex stack-comment`;
|
|
38
19
|
|
|
39
|
-
const hubClient = (hub: { team: string; app: string; callbackPath: string; envPrefix?: string }) =>
|
|
40
|
-
Effect.gen(function* () {
|
|
41
|
-
const [command, ...prefix] = yield* installedCli(process.cwd());
|
|
42
|
-
const callbackUrl = yield* registerDevelopmentClient({
|
|
43
|
-
...hub,
|
|
44
|
-
convex: (args, input) =>
|
|
45
|
-
execFileSync(command, [...prefix, ...args], {
|
|
46
|
-
encoding: "utf8",
|
|
47
|
-
input,
|
|
48
|
-
stdio: ["pipe", "pipe", "inherit"],
|
|
49
|
-
}).trim(),
|
|
50
|
-
});
|
|
51
|
-
console.log(
|
|
52
|
-
callbackUrl === undefined
|
|
53
|
-
? "Hub client renewed."
|
|
54
|
-
: `Hub client registered for ${callbackUrl}.`,
|
|
55
|
-
);
|
|
56
|
-
}).pipe(Effect.provide(ManagementApiLive().pipe(Layer.provide(fromConfig()))));
|
|
57
|
-
|
|
58
20
|
const stackComment = updateStackComment.pipe(
|
|
59
21
|
Effect.tap((url) => Effect.sync(() => console.log(url))),
|
|
60
22
|
Effect.provide(state().pipe(Layer.provide(NodeServices.layer))),
|
|
61
23
|
);
|
|
62
24
|
|
|
63
|
-
|
|
64
|
-
const program: Effect.Effect<unknown, unknown> | undefined =
|
|
65
|
-
// stack-comment reads the store of the team of the credential, so a --team would be ignored.
|
|
66
|
-
subcommand === "stack-comment" && team === undefined
|
|
67
|
-
? stackComment
|
|
68
|
-
: subcommand === "hub-client" &&
|
|
69
|
-
team !== undefined &&
|
|
70
|
-
app !== undefined &&
|
|
71
|
-
callbackPath !== undefined
|
|
72
|
-
? hubClient({ team, app, callbackPath, ...(envPrefix === undefined ? {} : { envPrefix }) })
|
|
73
|
-
: undefined;
|
|
74
|
-
if (program === undefined) {
|
|
25
|
+
if (positionals.join(" ") !== "stack-comment") {
|
|
75
26
|
console.error(USAGE);
|
|
76
27
|
process.exit(1);
|
|
77
28
|
}
|
|
78
29
|
|
|
79
|
-
await Effect.runPromise(
|
|
30
|
+
await Effect.runPromise(stackComment).catch((error: unknown) => {
|
|
80
31
|
console.error(error instanceof Error ? error.message : String(error));
|
|
81
32
|
process.exit(1);
|
|
82
33
|
});
|
package/src/index.ts
CHANGED
|
@@ -19,13 +19,11 @@ export {
|
|
|
19
19
|
ProductionDeploymentNotFound,
|
|
20
20
|
} from "./Deployment.ts";
|
|
21
21
|
export {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
type HubDeploymentType,
|
|
28
|
-
} from "./HubClient.ts";
|
|
22
|
+
LocalDeployment,
|
|
23
|
+
type LocalDeploymentAttributes,
|
|
24
|
+
LocalDeploymentNotFound,
|
|
25
|
+
type LocalDeploymentProps,
|
|
26
|
+
} from "./LocalDeployment.ts";
|
|
29
27
|
export { ConvexApiError, ManagementApi } from "./ManagementApi.ts";
|
|
30
28
|
export {
|
|
31
29
|
AmbiguousProject,
|