create-disposabl-app 0.6.4 → 0.6.5

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/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import { fileURLToPath as fileURLToPath2 } from "node:url";
8
8
  import { Command, CommanderError } from "commander";
9
9
  import * as p2 from "@clack/prompts";
10
10
  // package.json
11
- var version = "0.6.4";
11
+ var version = "0.6.5";
12
12
 
13
13
  // ../disposabl-kit/platforms.ts
14
14
  var platforms = [
@@ -9,12 +9,14 @@
9
9
  */
10
10
 
11
11
  import type * as apps_actions_create from "../apps/actions/create.js";
12
+ import type * as apps_actions_setup from "../apps/actions/setup.js";
12
13
  import type * as apps_internal from "../apps/internal.js";
13
14
  import type * as auth from "../auth.js";
14
15
  import type * as github_actions_http from "../github/actions/http.js";
15
16
  import type * as github_actions_internal from "../github/actions/internal.js";
16
17
  import type * as github_actions_oidc from "../github/actions/oidc.js";
17
18
  import type * as github_actions_provision from "../github/actions/provision.js";
19
+ import type * as github_actions_secrets from "../github/actions/secrets.js";
18
20
  import type * as github_actions_tokens from "../github/actions/tokens.js";
19
21
  import type * as github_internal from "../github/internal.js";
20
22
  import type * as http from "../http.js";
@@ -36,12 +38,14 @@ import type {
36
38
 
37
39
  declare const fullApi: ApiFromModules<{
38
40
  "apps/actions/create": typeof apps_actions_create;
41
+ "apps/actions/setup": typeof apps_actions_setup;
39
42
  "apps/internal": typeof apps_internal;
40
43
  auth: typeof auth;
41
44
  "github/actions/http": typeof github_actions_http;
42
45
  "github/actions/internal": typeof github_actions_internal;
43
46
  "github/actions/oidc": typeof github_actions_oidc;
44
47
  "github/actions/provision": typeof github_actions_provision;
48
+ "github/actions/secrets": typeof github_actions_secrets;
45
49
  "github/actions/tokens": typeof github_actions_tokens;
46
50
  "github/internal": typeof github_internal;
47
51
  http: typeof http;
@@ -34,6 +34,7 @@ type Env = {
34
34
  readonly AUTH_PRIVATE_KEY: string;
35
35
  readonly CONVEX_TEAM_ID: string | undefined;
36
36
  readonly CONVEX_TEAM_TOKEN: string | undefined;
37
+ readonly EXPO_TOKEN: string | undefined;
37
38
  readonly GITHUB_CONNECTOR: string | undefined;
38
39
  readonly GITHUB_INSTALLATION_ID: string | undefined;
39
40
  readonly GITHUB_OWNER: string | undefined;
@@ -4,6 +4,7 @@ import {
4
4
  } from "@disposabl/disposabl-kit/server/lib/functions";
5
5
  import { internal } from "../../_generated/api";
6
6
  import { appArgs, appResult } from "../../validator";
7
+ import { env } from "../../_generated/server";
7
8
  import type { Infer } from "convex/values";
8
9
  type AppResult = Infer<typeof appResult>;
9
10
 
@@ -11,9 +12,21 @@ export const create = internalAction({
11
12
  platforms: true,
12
13
  args: { ...identityFields, ...appArgs },
13
14
  returns: appResult,
14
- handler: async (ctx, args): Promise<AppResult> =>
15
- ctx.runMutation(internal.apps.internal.insert, {
15
+ handler: async (ctx, args): Promise<AppResult> => {
16
+ const app = await ctx.runMutation(internal.apps.internal.insert, {
16
17
  ...args,
17
18
  appId: crypto.randomUUID(),
18
- }),
19
+ });
20
+ if (env.CONVEX_TEAM_TOKEN) {
21
+ await ctx.runAction(internal.apps.actions.setup.setup, {
22
+ userId: args.userId,
23
+ appId: app.appId,
24
+ });
25
+ return ctx.runQuery(internal.apps.internal.get, {
26
+ userId: args.userId,
27
+ appId: app.appId,
28
+ });
29
+ }
30
+ return app;
31
+ },
19
32
  });
@@ -0,0 +1,34 @@
1
+ import {
2
+ identityFields,
3
+ internalAction,
4
+ } from "@disposabl/disposabl-kit/server/lib/functions";
5
+ import { v } from "convex/values";
6
+ import { internal } from "../../_generated/api";
7
+ import { env } from "../../_generated/server";
8
+ import { ensureProject } from "../../previews/token/actions/management";
9
+
10
+ export const setup = internalAction({
11
+ args: { ...identityFields, appId: v.string() },
12
+ returns: v.number(),
13
+ handler: async (ctx, args): Promise<number> => {
14
+ const saved = await ctx.runQuery(internal.apps.internal.project, args);
15
+ const token = env.CONVEX_TEAM_TOKEN;
16
+ const teamId = env.CONVEX_TEAM_ID;
17
+ if (!token || !teamId || !/^\d+$/.test(teamId))
18
+ throw new Error(
19
+ "Managed setup requires CONVEX_TEAM_ID and CONVEX_TEAM_TOKEN on the service backend.",
20
+ );
21
+ const projectId = saved ?? (await ensureProject(token, teamId, args.appId));
22
+ if (saved === null)
23
+ await ctx.runMutation(internal.apps.internal.saveProject, {
24
+ ...args,
25
+ projectId,
26
+ });
27
+ await ctx.runAction(internal.github.actions.provision.run, args);
28
+ await ctx.runAction(internal.github.actions.secrets.configure, {
29
+ ...args,
30
+ projectId,
31
+ });
32
+ return projectId;
33
+ },
34
+ });
@@ -195,3 +195,23 @@ export const githubSetup = internalQuery({
195
195
  };
196
196
  },
197
197
  });
198
+
199
+ export const project = internalQuery({
200
+ args: { ...identityFields, appId: v.string() },
201
+ returns: v.union(v.number(), v.null()),
202
+ handler: async (ctx, { userId, appId }) =>
203
+ (await ownedApp(ctx, userId, appId)).convexProjectId ?? null,
204
+ });
205
+ export const saveProject = internalMutation({
206
+ args: {
207
+ ...identityFields,
208
+ appId: v.string(),
209
+ projectId: v.number(),
210
+ },
211
+ returns: v.null(),
212
+ handler: async (ctx, args) => {
213
+ const app = await ownedApp(ctx, args.userId, args.appId);
214
+ await ctx.db.patch("apps", app._id, { convexProjectId: args.projectId });
215
+ return null;
216
+ },
217
+ });
@@ -18,6 +18,7 @@ const app = defineApp({
18
18
  VERCEL_TEAM_ID: v.optional(v.string()),
19
19
  GITHUB_CONNECTOR: v.optional(v.string()),
20
20
  GITHUB_OWNER: v.optional(v.string()),
21
+ EXPO_TOKEN: v.optional(v.string()),
21
22
  GITHUB_INSTALLATION_ID: v.optional(v.string()),
22
23
  },
23
24
  });
@@ -0,0 +1,101 @@
1
+ "use node";
2
+
3
+ import sodium from "libsodium-wrappers";
4
+ import {
5
+ identityFields,
6
+ internalAction,
7
+ } from "@disposabl/disposabl-kit/server/lib/functions";
8
+ import { v } from "convex/values";
9
+ import { internal } from "../../_generated/api";
10
+ import { env } from "../../_generated/server";
11
+ import {
12
+ management,
13
+ ensureProduction,
14
+ } from "../../previews/token/actions/management";
15
+ import { githubRequest, githubToken } from "./tokens";
16
+
17
+ export const configure = internalAction({
18
+ args: { ...identityFields, appId: v.string(), projectId: v.number() },
19
+ returns: v.null(),
20
+ handler: async (ctx, args) => {
21
+ await ctx.runQuery(internal.apps.internal.get, {
22
+ userId: args.userId,
23
+ appId: args.appId,
24
+ });
25
+ const app = await ctx.runQuery(internal.apps.internal.githubApp, {
26
+ appId: args.appId,
27
+ });
28
+ if (!app) return null;
29
+ const repository = `${app.githubOwner}/${app.githubRepo}`;
30
+ const token = await githubToken(
31
+ app.installationId,
32
+ ["secrets:write"],
33
+ repository,
34
+ );
35
+ const path = `/repos/${repository}/actions/secrets`;
36
+ const existing = await githubRequest<{ secrets: { name: string }[] }>(
37
+ token,
38
+ `${path}?per_page=100`,
39
+ );
40
+ const names = new Set(existing.secrets.map((secret) => secret.name));
41
+ const publicKey = await githubRequest<{ key_id: string; key: string }>(
42
+ token,
43
+ `${path}/public-key`,
44
+ );
45
+ await sodium.ready;
46
+ const save = async (name: string, value: string) => {
47
+ const encrypted = sodium.crypto_box_seal(
48
+ value,
49
+ sodium.from_base64(publicKey.key, sodium.base64_variants.ORIGINAL),
50
+ );
51
+ await githubRequest(
52
+ token,
53
+ `${path}/${name}`,
54
+ {
55
+ key_id: publicKey.key_id,
56
+ encrypted_value: sodium.to_base64(
57
+ encrypted,
58
+ sodium.base64_variants.ORIGINAL,
59
+ ),
60
+ },
61
+ "PUT",
62
+ );
63
+ };
64
+ const managementToken = env.CONVEX_TEAM_TOKEN;
65
+ if (!managementToken)
66
+ throw new Error("Convex team access is not configured.");
67
+ if (!names.has("CONVEX_DEPLOY_KEY")) {
68
+ const deployment = await ensureProduction(
69
+ managementToken,
70
+ args.projectId,
71
+ );
72
+ const { deployKey } = await management<{ deployKey: string }>(
73
+ managementToken,
74
+ `/deployments/${deployment}/create_deploy_key`,
75
+ { name: "github-actions-production" },
76
+ );
77
+ if (!deployKey.startsWith(`prod:${deployment}|`))
78
+ throw new Error("Expected a production deploy key.");
79
+ await save("CONVEX_DEPLOY_KEY", deployKey);
80
+ }
81
+ if (!names.has("CONVEX_DEPLOY_KEY_PREVIEW")) {
82
+ const { previewDeployKey } = await management<{
83
+ previewDeployKey: string;
84
+ }>(
85
+ managementToken,
86
+ `/projects/${args.projectId}/create_preview_deploy_key`,
87
+ { name: "github-actions-preview" },
88
+ );
89
+ if (!previewDeployKey.startsWith("preview:"))
90
+ throw new Error("Expected a preview deploy key.");
91
+ await save("CONVEX_DEPLOY_KEY_PREVIEW", previewDeployKey);
92
+ }
93
+ if (
94
+ app.githubOwner === env.GITHUB_OWNER?.toLowerCase() &&
95
+ env.EXPO_TOKEN &&
96
+ !names.has("EXPO_TOKEN")
97
+ )
98
+ await save("EXPO_TOKEN", env.EXPO_TOKEN);
99
+ return null;
100
+ },
101
+ });
@@ -47,9 +47,10 @@ export async function githubRequest<T>(
47
47
  token: string,
48
48
  path: string,
49
49
  body?: unknown,
50
+ method = body === undefined ? "GET" : "POST",
50
51
  ): Promise<T> {
51
52
  const response = await fetch(`https://api.github.com${path}`, {
52
- method: body === undefined ? "GET" : "POST",
53
+ method,
53
54
  redirect: "error",
54
55
  signal: AbortSignal.timeout(30_000),
55
56
  headers: {
@@ -62,5 +63,9 @@ export async function githubRequest<T>(
62
63
  });
63
64
  if (!response.ok)
64
65
  throw new Error(`GitHub request failed (${response.status}).`);
65
- return response.json() as Promise<T>;
66
+ return (
67
+ response.status === 204 || response.headers.get("content-length") === "0"
68
+ ? undefined
69
+ : await response.json()
70
+ ) as T;
66
71
  }
@@ -156,19 +156,6 @@ export const create = internalMutation({
156
156
  return app.convexProjectId ?? null;
157
157
  },
158
158
  });
159
- export const saveProject = internalMutation({
160
- args: {
161
- ...identityFields,
162
- appId: v.string(),
163
- projectId: v.number(),
164
- },
165
- returns: v.null(),
166
- handler: async (ctx, args) => {
167
- const app = await ownedApp(ctx, args.userId, args.appId);
168
- await ctx.db.patch("apps", app._id, { convexProjectId: args.projectId });
169
- return null;
170
- },
171
- });
172
159
  export const finish = internalMutation({
173
160
  args: { ...identityFields, ...previewIdentity, deployment },
174
161
  returns: v.null(),
@@ -2,12 +2,12 @@ import {
2
2
  identityFields,
3
3
  internalAction,
4
4
  } from "@disposabl/disposabl-kit/server/lib/functions";
5
- import { ConvexError, v } from "convex/values";
5
+ import { v } from "convex/values";
6
6
  import { internal } from "../../../_generated/api";
7
7
  import { env } from "../../../_generated/server";
8
8
  import { previewIdentity, deployment } from "../../../validator";
9
9
  import { validateReference } from "../../internal";
10
- import { ensureDevelopment, ensureProject } from "./management";
10
+ import { ensureDevelopment } from "./management";
11
11
 
12
12
  export const create = internalAction({
13
13
  platforms: true,
@@ -34,29 +34,13 @@ export const create = internalAction({
34
34
  userId: args.userId,
35
35
  appId: args.appId,
36
36
  });
37
- const token = env.CONVEX_TEAM_TOKEN;
38
- const teamId = env.CONVEX_TEAM_ID;
39
- if (!token || !teamId || !/^\d+$/.test(teamId))
40
- throw new ConvexError(
41
- "Managed setup requires CONVEX_TEAM_ID and CONVEX_TEAM_TOKEN on the service backend.",
42
- );
43
- const saved = await ctx.runMutation(
44
- internal.previews.internal.create,
45
- args,
46
- );
47
- await ctx.runAction(internal.github.actions.provision.run, {
37
+ await ctx.runMutation(internal.previews.internal.create, args);
38
+ const projectId = await ctx.runAction(internal.apps.actions.setup.setup, {
48
39
  userId: args.userId,
49
40
  appId: args.appId,
50
41
  });
51
- const projectId = saved ?? (await ensureProject(token, teamId, args.appId));
52
- if (saved === null)
53
- await ctx.runMutation(internal.previews.internal.saveProject, {
54
- userId: args.userId,
55
- appId: args.appId,
56
- projectId,
57
- });
58
42
  const { deployKey, ...deployment } = await ensureDevelopment(
59
- token,
43
+ env.CONVEX_TEAM_TOKEN!,
60
44
  projectId,
61
45
  args.reference,
62
46
  );
@@ -131,3 +131,36 @@ export async function ensureDevelopment(
131
131
  deployKey,
132
132
  };
133
133
  }
134
+
135
+ export async function ensureProduction(token: string, projectId: number) {
136
+ const path = `/projects/${projectId}`;
137
+ const lookup = () =>
138
+ management<Deployment>(token, `${path}/deployment?defaultProd=true`);
139
+ let deployment: Deployment;
140
+ try {
141
+ deployment = await lookup();
142
+ } catch (error) {
143
+ if (!(error instanceof ManagementError) || error.status !== 404)
144
+ throw error;
145
+ try {
146
+ deployment = await management<Deployment>(
147
+ token,
148
+ `${path}/create_deployment`,
149
+ { type: "prod" },
150
+ );
151
+ } catch (error) {
152
+ try {
153
+ deployment = await lookup();
154
+ } catch {
155
+ throw error;
156
+ }
157
+ }
158
+ }
159
+ if (
160
+ deployment.kind !== "cloud" ||
161
+ deployment.deploymentType !== "prod" ||
162
+ deployment.projectId !== projectId
163
+ )
164
+ throw new Error("Expected this app's production deployment.");
165
+ return deployment.name;
166
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-disposabl-app",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "Create a Disposabl app",
5
5
  "type": "module",
6
6
  "license": "MIT",