@dench.com/cli 2.9.5-staging.239 → 2.9.6-staging.240

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/apps.ts CHANGED
@@ -8,8 +8,15 @@
8
8
  * the server's requireCrmAccess accepts (and appStore forwards to the file
9
9
  * functions as an apiKey for publish/install/create).
10
10
  */
11
+ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
12
+ import { join, relative, resolve } from "node:path";
11
13
  import type { ConvexHttpClient } from "convex/browser";
12
14
  import { makeFunctionReference } from "convex/server";
15
+ import {
16
+ type AppFile,
17
+ formatValidation,
18
+ validateApp,
19
+ } from "./lib/apps-validate";
13
20
  import { closestMatch } from "./lib/levenshtein";
14
21
 
15
22
  const api = {
@@ -28,6 +35,18 @@ const api = {
28
35
  "functions/appStore:setEnabled",
29
36
  ),
30
37
  setPinned: makeFunctionReference<"mutation">("functions/appStore:setPinned"),
38
+ listTree: makeFunctionReference<"query">("functions/files:listTree"),
39
+ getFileContent: makeFunctionReference<"query">(
40
+ "functions/files:getFileContent",
41
+ ),
42
+ webhooksRegister: makeFunctionReference<"mutation">(
43
+ "functions/appWebhooks:register",
44
+ ),
45
+ webhooksList: makeFunctionReference<"query">("functions/appWebhooks:list"),
46
+ webhooksPoll: makeFunctionReference<"query">("functions/appWebhooks:poll"),
47
+ webhooksDelete: makeFunctionReference<"mutation">(
48
+ "functions/appWebhooks:remove",
49
+ ),
31
50
  };
32
51
 
33
52
  class AppsCliError extends Error {}
@@ -87,9 +106,188 @@ function appsHelp() {
87
106
  dench apps uninstall <slug> Remove an installed app
88
107
  dench apps enable | disable <slug> Toggle whether the app loads
89
108
  dench apps pin | unpin <slug> Toggle the rail pin
109
+ dench apps validate <appPath> [--json] Check the manifest, permissions and every window.dench call
110
+ (local folder, or a workspace path like /apps/x.dench.app)
111
+ dench apps webhooks register <slug> <hookName> [--rotate]
112
+ Inbound webhook URL + signing secret (secret shown once)
113
+ dench apps webhooks list <slug>
114
+ dench apps webhooks poll <slug> <hookName> [--since <ms>] [--limit <n>]
115
+ dench apps webhooks delete <slug> <hookName>
90
116
  `);
91
117
  }
92
118
 
119
+ const TEXT_FILE = /\.(html?|js|mjs|cjs|ts|jsx|tsx|css|json|ya?ml|md|txt|svg)$/i;
120
+
121
+ function readLocalAppFolder(root: string): AppFile[] {
122
+ const files: AppFile[] = [];
123
+ const walk = (dir: string) => {
124
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
125
+ const abs = join(dir, entry.name);
126
+ if (entry.isDirectory()) {
127
+ if (entry.name !== "node_modules" && entry.name !== ".git") walk(abs);
128
+ continue;
129
+ }
130
+ if (!entry.isFile()) continue;
131
+ const path = relative(root, abs).replace(/\\/g, "/");
132
+ if (path !== ".dench.yaml" && !TEXT_FILE.test(path)) continue;
133
+ files.push({ path, content: readFileSync(abs, "utf8") });
134
+ }
135
+ };
136
+ walk(root);
137
+ return files;
138
+ }
139
+
140
+ /** Local folder → disk; otherwise read the workspace tree from Convex. */
141
+ async function loadAppFolder(
142
+ ctx: Ctx,
143
+ appPath: string,
144
+ ): Promise<{ files: AppFile[]; source: "disk" | "workspace" }> {
145
+ const workspaceRoot =
146
+ process.env.DENCH_WORKSPACE_ROOT?.trim() || "/workspace";
147
+ const candidates = [
148
+ resolve(appPath),
149
+ join(workspaceRoot, appPath),
150
+ join(process.cwd(), appPath.replace(/^\/+/, "")),
151
+ ];
152
+ for (const candidate of candidates) {
153
+ if (existsSync(candidate) && statSync(candidate).isDirectory()) {
154
+ return { files: readLocalAppFolder(candidate), source: "disk" };
155
+ }
156
+ }
157
+ const prefix = appPath.startsWith("/") ? appPath : `/${appPath}`;
158
+ const rows: AppFile[] = [];
159
+ const walk = async (dir: string) => {
160
+ const entries = (await ctx.convex.query(
161
+ api.listTree,
162
+ withAuth(ctx, { prefix: dir }),
163
+ )) as Array<{ path: string; isDir: boolean }>;
164
+ for (const entry of entries) {
165
+ if (entry.isDir) {
166
+ await walk(entry.path);
167
+ continue;
168
+ }
169
+ const rel = entry.path.slice(prefix.length).replace(/^\/+/, "");
170
+ if (rel !== ".dench.yaml" && !TEXT_FILE.test(rel)) continue;
171
+ const content = (await ctx.convex.query(
172
+ api.getFileContent,
173
+ withAuth(ctx, { path: entry.path }),
174
+ )) as { content?: string | null } | null;
175
+ if (typeof content?.content === "string") {
176
+ rows.push({ path: rel, content: content.content });
177
+ }
178
+ }
179
+ };
180
+ await walk(prefix);
181
+ if (rows.length === 0) {
182
+ throw new AppsCliError(
183
+ `No app folder at ${appPath} (looked on disk and in the workspace).`,
184
+ );
185
+ }
186
+ return { files: rows, source: "workspace" };
187
+ }
188
+
189
+ async function runValidate(ctx: Ctx) {
190
+ const appPath = firstPositional(ctx.args);
191
+ if (!appPath) {
192
+ throw new AppsCliError("Usage: dench apps validate <appPath> [--json]");
193
+ }
194
+ const { files, source } = await loadAppFolder(ctx, appPath);
195
+ const manifest = files.find((f) => f.path === ".dench.yaml");
196
+ const result = validateApp({
197
+ manifestYaml: manifest?.content ?? null,
198
+ files: files.filter((f) => f.path !== ".dench.yaml"),
199
+ });
200
+ if (ctx.jsonOutput) {
201
+ out(ctx, { appPath, source, ...result });
202
+ } else {
203
+ console.log(formatValidation(result, appPath));
204
+ }
205
+ if (!result.ok) process.exitCode = 1;
206
+ }
207
+
208
+ async function runWebhooks(ctx: Ctx) {
209
+ const verb = ctx.args.shift();
210
+ const usage =
211
+ "Usage: dench apps webhooks register|list|poll|delete <slug> [hookName]";
212
+ switch (verb) {
213
+ case "register": {
214
+ const rotateIndex = ctx.args.indexOf("--rotate");
215
+ const rotate = rotateIndex !== -1;
216
+ if (rotate) ctx.args.splice(rotateIndex, 1);
217
+ const slug = shift(ctx.args, "app slug");
218
+ const hookName = shift(ctx.args, "hook name");
219
+ const result = (await ctx.convex.mutation(
220
+ api.webhooksRegister,
221
+ withAuth(ctx, { slug, hookName, rotateSecret: rotate }),
222
+ )) as {
223
+ url: string | null;
224
+ path: string;
225
+ secret: string | null;
226
+ created: boolean;
227
+ };
228
+ const base = process.env.DENCH_API_URL?.trim().replace(/\/+$/, "");
229
+ const url = result.url ?? (base ? `${base}${result.path}` : result.path);
230
+ if (ctx.jsonOutput) {
231
+ out(ctx, { ...result, url });
232
+ return;
233
+ }
234
+ console.log(
235
+ `${result.created ? "Registered" : "Existing"} webhook "${hookName}" for ${slug}`,
236
+ );
237
+ console.log(` URL: ${url}`);
238
+ if (result.secret) {
239
+ console.log(
240
+ ` Secret: ${result.secret} (shown once — store it with the sender)`,
241
+ );
242
+ } else {
243
+ console.log(" Secret: (unchanged; pass --rotate to mint a new one)");
244
+ }
245
+ return;
246
+ }
247
+ case "list": {
248
+ const slug = shift(ctx.args, "app slug");
249
+ out(
250
+ ctx,
251
+ await ctx.convex.query(api.webhooksList, withAuth(ctx, { slug })),
252
+ );
253
+ return;
254
+ }
255
+ case "poll": {
256
+ const since = takeFlag(ctx.args, "--since");
257
+ const limit = takeFlag(ctx.args, "--limit");
258
+ const slug = shift(ctx.args, "app slug");
259
+ const hookName = shift(ctx.args, "hook name");
260
+ out(
261
+ ctx,
262
+ await ctx.convex.query(
263
+ api.webhooksPoll,
264
+ withAuth(ctx, {
265
+ slug,
266
+ hookName,
267
+ ...(since ? { since: Number(since) } : {}),
268
+ ...(limit ? { limit: Number(limit) } : {}),
269
+ }),
270
+ ),
271
+ );
272
+ return;
273
+ }
274
+ case "delete": {
275
+ const slug = shift(ctx.args, "app slug");
276
+ const hookName = shift(ctx.args, "hook name");
277
+ out(
278
+ ctx,
279
+ await ctx.convex.mutation(
280
+ api.webhooksDelete,
281
+ withAuth(ctx, { slug, hookName }),
282
+ ),
283
+ );
284
+ return;
285
+ }
286
+ default:
287
+ throw new AppsCliError(usage);
288
+ }
289
+ }
290
+
93
291
  export async function runAppsCommand(opts: {
94
292
  convex: ConvexHttpClient;
95
293
  args: string[];
@@ -211,6 +409,12 @@ export async function runAppsCommand(opts: {
211
409
  out(ctx, { ok: true, slug, enabled: sub === "enable" });
212
410
  return;
213
411
  }
412
+ case "validate":
413
+ await runValidate(ctx);
414
+ return;
415
+ case "webhooks":
416
+ await runWebhooks(ctx);
417
+ return;
214
418
  case "pin":
215
419
  case "unpin": {
216
420
  const slug = ctx.args.shift();
@@ -250,5 +454,7 @@ const APPS_SUBCOMMANDS = [
250
454
  "disable",
251
455
  "pin",
252
456
  "unpin",
457
+ "validate",
458
+ "webhooks",
253
459
  "help",
254
460
  ] as const;
@@ -3244,6 +3244,7 @@ export const requestSchemas: Record<string, ZodTypeAny> = {
3244
3244
  }),
3245
3245
  "files.downloadUrl": z.object({ path: z.string() }),
3246
3246
  "files.delete": z.object({ path: z.string() }),
3247
+ "files.mkdir": z.object({ path: z.string() }),
3247
3248
  "files.move": z.object({
3248
3249
  from: z.string().describe("Source workspace path."),
3249
3250
  to: z.string().describe("Destination workspace path."),
@@ -3265,6 +3266,48 @@ export const requestSchemas: Record<string, ZodTypeAny> = {
3265
3266
  lastModifiedBy: z.string().optional(),
3266
3267
  }),
3267
3268
 
3269
+ // Apps (App Store + app webhooks)
3270
+ "apps.list": noInput,
3271
+ "apps.gallery": noInput,
3272
+ "apps.create": z.object({
3273
+ slug: z.string().describe("Folder slug; becomes /apps/<slug>.dench.app."),
3274
+ name: z.string(),
3275
+ icon: z.string().optional().describe("Lucide icon name."),
3276
+ description: z.string().optional(),
3277
+ }),
3278
+ "apps.install": z.object({ listingId: z.string() }),
3279
+ "apps.publish": z.object({
3280
+ appPath: z.string().describe("Workspace path of the .dench.app folder."),
3281
+ slug: z.string(),
3282
+ name: z.string(),
3283
+ description: z.string().optional(),
3284
+ icon: z.string().optional(),
3285
+ visibility: z.enum(["org", "public", "official", "unlisted"]).optional(),
3286
+ version: z.string().optional(),
3287
+ permissions: z.array(z.string()).optional(),
3288
+ }),
3289
+ "apps.uninstall": z.object({ slug: z.string() }),
3290
+ "apps.setEnabled": z.object({ slug: z.string(), enabled: z.boolean() }),
3291
+ "apps.setPinned": z.object({ slug: z.string(), pinned: z.boolean() }),
3292
+ "apps.webhooks.register": z.object({
3293
+ slug: z.string().describe("Installed app slug."),
3294
+ hookName: z
3295
+ .string()
3296
+ .regex(/^[a-z0-9][a-z0-9_-]{0,63}$/i)
3297
+ .describe("Hook name, e.g. `stripe` or `github-push`."),
3298
+ }),
3299
+ "apps.webhooks.list": z.object({ slug: z.string() }),
3300
+ "apps.webhooks.poll": z.object({
3301
+ slug: z.string(),
3302
+ hookName: z.string(),
3303
+ since: z.coerce
3304
+ .number()
3305
+ .optional()
3306
+ .describe("Only events received after this epoch ms."),
3307
+ limit: z.coerce.number().int().min(1).max(200).optional(),
3308
+ }),
3309
+ "apps.webhooks.delete": z.object({ slug: z.string(), hookName: z.string() }),
3310
+
3268
3311
  // Agent config
3269
3312
  "agentConfig.identity.show": noInput,
3270
3313
  "agentConfig.organisation.show": noInput,