@sanity/runtime-cli 17.8.0 → 17.10.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.
Files changed (32) hide show
  1. package/README.md +28 -22
  2. package/dist/actions/blueprints/pre-deploy.js +2 -0
  3. package/dist/actions/blueprints/stacks.d.ts +3 -0
  4. package/dist/actions/blueprints/stacks.js +1 -0
  5. package/dist/actions/sanity/projects.d.ts +11 -0
  6. package/dist/actions/sanity/projects.js +48 -22
  7. package/dist/baseCommands.d.ts +6 -0
  8. package/dist/baseCommands.js +9 -1
  9. package/dist/commands/blueprints/stacks.d.ts +2 -1
  10. package/dist/commands/blueprints/stacks.js +14 -6
  11. package/dist/commands/functions/test.d.ts +1 -0
  12. package/dist/commands/functions/test.js +5 -0
  13. package/dist/cores/blueprints/stacks.d.ts +8 -3
  14. package/dist/cores/blueprints/stacks.js +282 -85
  15. package/dist/cores/functions/test.d.ts +2 -19
  16. package/dist/cores/functions/test.js +63 -138
  17. package/dist/server/app.js +4 -6
  18. package/dist/server/static/components/payload-panel.js +1 -1
  19. package/dist/utils/display/prompt.d.ts +4 -1
  20. package/dist/utils/display/prompt.js +16 -2
  21. package/dist/utils/functions/child-process-wrapper.js +3 -2
  22. package/dist/utils/functions/child-runtime/durable.d.ts +2 -2
  23. package/dist/utils/functions/child-runtime/durable.js +70 -3
  24. package/dist/utils/functions/durable-logs.d.ts +16 -0
  25. package/dist/utils/functions/durable-logs.js +52 -0
  26. package/dist/utils/functions/invoke/local.js +16 -8
  27. package/dist/utils/functions/invoke/resolve-invoke-type.d.ts +51 -0
  28. package/dist/utils/functions/invoke/resolve-invoke-type.js +131 -0
  29. package/dist/utils/types.d.ts +39 -6
  30. package/oclif.manifest.json +26 -5
  31. package/package.json +1 -1
  32. package/skills/blueprints-stacks.md +14 -2
@@ -0,0 +1,51 @@
1
+ import type { BlueprintResource } from '@sanity/blueprints';
2
+ import type { Logger } from '../../logger.js';
3
+ import type { EventType, FunctionGroqResource, FunctionResource, FunctionTestFlags, InvokeContextOptions, InvokePayloadOptions } from '../../types.js';
4
+ import buildPayload from '../build-payload.js';
5
+ import { fetchAsset, fetchDocument } from '../fetch-document.js';
6
+ export type InvocationPlan = {
7
+ kind: 'groq';
8
+ inputSource: 'document' | 'media';
9
+ isDurable: boolean;
10
+ } | {
11
+ kind: 'plain';
12
+ isDurable: false;
13
+ } | {
14
+ kind: 'durable';
15
+ inputSource: 'payload';
16
+ isDurable: true;
17
+ };
18
+ export type InputContext = {
19
+ flags: FunctionTestFlags;
20
+ projectId?: string;
21
+ organizationId?: string;
22
+ api?: string;
23
+ apiHost: string;
24
+ token?: string;
25
+ withUserToken?: boolean;
26
+ resources: BlueprintResource[];
27
+ blueprintFileDir: string;
28
+ log: Logger;
29
+ };
30
+ type InvocationDeps = {
31
+ fetchDocument: typeof fetchDocument;
32
+ fetchAsset: typeof fetchAsset;
33
+ buildPayload: typeof buildPayload;
34
+ };
35
+ export declare const defaultInvocationDeps: InvocationDeps;
36
+ export interface InvokeInput {
37
+ payload: Record<string, unknown>;
38
+ before: Record<string, unknown> | null;
39
+ after: Record<string, unknown> | null;
40
+ }
41
+ export declare const resolveInvocationPlan: (resource: FunctionResource) => InvocationPlan;
42
+ /**
43
+ * True when the function's event is delivered through a GROQ rule (a document or
44
+ * media-library event, whether plain or durable), so its payload is a document the
45
+ * rule may narrow before the handler sees it.
46
+ */
47
+ export declare const isGroqInvocation: (resource: FunctionResource) => resource is FunctionGroqResource;
48
+ export declare const collectInvokeInput: (plan: InvocationPlan, input: InputContext, deps: InvocationDeps) => Promise<InvokeInput>;
49
+ export declare const buildContext: (plan: InvocationPlan, input: InputContext) => InvokeContextOptions;
50
+ export declare const buildInvokeOptions: (plan: InvocationPlan, invokeInput: InvokeInput, eventType: EventType) => InvokePayloadOptions;
51
+ export {};
@@ -0,0 +1,131 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { cwd } from 'node:process';
4
+ import { SANITY_FUNCTION_DOCUMENT, SANITY_FUNCTION_DURABLE, SANITY_FUNCTION_MEDIA_LIBRARY_ASSET, SANITY_FUNCTION_PUBSUB, SANITY_FUNCTION_SCHEDULED, SANITY_FUNCTION_SYNC_TAG_INVALIDATE, } from '../../../constants.js';
5
+ import { parseJsonObject } from '../../parse-json-object.js';
6
+ import buildPayload from '../build-payload.js';
7
+ import { fetchAsset, fetchDocument } from '../fetch-document.js';
8
+ export const defaultInvocationDeps = { fetchDocument, fetchAsset, buildPayload };
9
+ const assertNever = (resource) => {
10
+ throw new Error(`No invocation plan for function type: ${resource.type}`);
11
+ };
12
+ const resolveDurableInvocationPlan = (event) => {
13
+ switch (event?.type) {
14
+ case 'document':
15
+ return { kind: 'groq', inputSource: 'document', isDurable: true };
16
+ case 'media-library':
17
+ return { kind: 'groq', inputSource: 'media', isDurable: true };
18
+ case 'sync-tag-invalidate':
19
+ case undefined:
20
+ return { kind: 'durable', inputSource: 'payload', isDurable: true };
21
+ }
22
+ };
23
+ export const resolveInvocationPlan = (resource) => {
24
+ switch (resource.type) {
25
+ case SANITY_FUNCTION_DOCUMENT:
26
+ return { kind: 'groq', inputSource: 'document', isDurable: false };
27
+ case SANITY_FUNCTION_MEDIA_LIBRARY_ASSET:
28
+ return { kind: 'groq', inputSource: 'media', isDurable: false };
29
+ case SANITY_FUNCTION_DURABLE:
30
+ return resolveDurableInvocationPlan(resource.event);
31
+ case SANITY_FUNCTION_SCHEDULED:
32
+ case SANITY_FUNCTION_PUBSUB:
33
+ case SANITY_FUNCTION_SYNC_TAG_INVALIDATE:
34
+ return { kind: 'plain', isDurable: false };
35
+ default:
36
+ return assertNever(resource);
37
+ }
38
+ };
39
+ /**
40
+ * True when the function's event is delivered through a GROQ rule (a document or
41
+ * media-library event, whether plain or durable), so its payload is a document the
42
+ * rule may narrow before the handler sees it.
43
+ */
44
+ export const isGroqInvocation = (resource) => resolveInvocationPlan(resource).kind === 'groq';
45
+ export const collectInvokeInput = async (plan, input, deps) => {
46
+ const source = plan.kind === 'groq' ? plan.inputSource : 'none';
47
+ return resolveInvokeInput(source, input, deps);
48
+ };
49
+ export const buildContext = (plan, input) => {
50
+ const withToken = input.withUserToken ? { token: input.token } : {};
51
+ if (plan.kind === 'plain') {
52
+ return {
53
+ clientOptions: { ...withToken },
54
+ resources: input.resources,
55
+ blueprintFileDir: input.blueprintFileDir,
56
+ };
57
+ }
58
+ const isMedia = plan.kind === 'groq' && plan.inputSource === 'media';
59
+ return {
60
+ clientOptions: {
61
+ apiVersion: input.api,
62
+ dataset: input.flags.dataset,
63
+ projectId: input.projectId,
64
+ organizationId: input.organizationId,
65
+ ...withToken,
66
+ },
67
+ eventResourceType: isMedia ? 'media-library' : 'dataset',
68
+ eventResourceId: (isMedia ? input.flags['media-library-id'] : `${input.projectId}.${input.flags.dataset}`) ||
69
+ '',
70
+ functionResourceType: 'project',
71
+ functionResourceId: input.projectId || '',
72
+ resources: input.resources,
73
+ blueprintFileDir: input.blueprintFileDir,
74
+ };
75
+ };
76
+ export const buildInvokeOptions = (plan, invokeInput, eventType) => {
77
+ switch (plan.kind) {
78
+ case 'groq':
79
+ return {
80
+ event: eventType,
81
+ payload: invokeInput.payload,
82
+ before: invokeInput.before,
83
+ after: invokeInput.after,
84
+ };
85
+ case 'plain':
86
+ // Cron functions have no payload, but pubsub and sync-tag-invalidate carry
87
+ // whatever the producer sent, so pass it through for all of them.
88
+ return { event: 'plain', payload: invokeInput.payload };
89
+ case 'durable':
90
+ return { event: 'durable', payload: invokeInput.payload };
91
+ }
92
+ };
93
+ const resolveInvokeInput = async (source, input, deps) => {
94
+ const { flags } = input;
95
+ const fetchById = (id) => source === 'document'
96
+ ? deps.fetchDocument(id, {
97
+ projectId: input.projectId,
98
+ dataset: flags.dataset,
99
+ apiVersion: input.api,
100
+ apiHost: input.apiHost,
101
+ token: input.token,
102
+ }, input.log)
103
+ : deps.fetchAsset(id, {
104
+ mediaLibraryId: flags['media-library-id'],
105
+ apiVersion: input.api,
106
+ apiHost: input.apiHost,
107
+ token: input.token,
108
+ }, input.log);
109
+ let before = null;
110
+ let after = null;
111
+ if (flags['data-before'] && flags['data-after']) {
112
+ before = parseJsonObject(flags['data-before']);
113
+ after = parseJsonObject(flags['data-after']);
114
+ }
115
+ else if (flags['file-before'] && flags['file-after']) {
116
+ before = parseJsonObject(readFileSync(join(cwd(), flags['file-before']), 'utf8'));
117
+ after = parseJsonObject(readFileSync(join(cwd(), flags['file-after']), 'utf8'));
118
+ }
119
+ else if (flags['document-id-before'] && flags['document-id-after']) {
120
+ before = await fetchById(flags['document-id-before']);
121
+ after = await fetchById(flags['document-id-after']);
122
+ }
123
+ let payload;
124
+ if (after)
125
+ payload = after;
126
+ else if (flags['document-id'])
127
+ payload = await fetchById(flags['document-id']);
128
+ else
129
+ payload = deps.buildPayload({ data: flags.data, file: flags.file });
130
+ return { payload, before, after };
131
+ };
@@ -14,7 +14,7 @@ export interface ActionResponse {
14
14
  error: string | null;
15
15
  }
16
16
  /** @internal */
17
- export type FunctionGroqResource = BlueprintDocumentFunctionResource | BlueprintMediaLibraryAssetFunctionResource;
17
+ export type FunctionGroqResource = BlueprintDocumentFunctionResource | BlueprintMediaLibraryAssetFunctionResource | BlueprintDurableFunctionResource;
18
18
  export type FunctionResource = FunctionGroqResource | BlueprintPubSubFunctionResource | BlueprintScheduledFunctionResource | BlueprintSyncTagInvalidateFunctionResource | BlueprintDurableFunctionResource;
19
19
  /** Function resources that declare an `event`; PubSub functions are triggered by other functions */
20
20
  export type EventFunctionResource = Exclude<FunctionResource, BlueprintPubSubFunctionResource>;
@@ -111,7 +111,7 @@ export interface InvokeNonGroqPayloadOptions {
111
111
  export type InvokePayloadOptions = InvokeGroqPayloadOptions | InvokeNonGroqPayloadOptions;
112
112
  export type InvokePayloadMetadata = Pick<InvokeGroqPayloadOptions, 'event' | 'before' | 'after'> | Pick<InvokeNonGroqPayloadOptions, 'event'>;
113
113
  export type EventType = 'create' | 'update' | 'delete';
114
- export type NonGroqEventType = 'scheduled' | 'nested' | 'durable';
114
+ export type NonGroqEventType = 'plain' | 'nested' | 'durable';
115
115
  export declare function isEventType(arg: string): arg is EventType;
116
116
  /** @internal */
117
117
  export interface InvokeGroqContextOptions {
@@ -162,7 +162,9 @@ export interface InvokeExecutionOptions {
162
162
  /** Function names already on the invoke call stack (innermost last), used to detect cycles. @internal */
163
163
  callStack?: string[];
164
164
  /** Stream a durable step transition to the caller. @internal */
165
- onStepEvent?: (event: DurableStepEvent) => void;
165
+ onStepEvent?: (event: DurableRuntimeEvent) => void;
166
+ /** Don't wait the full duration during a wait durable event @internal */
167
+ noWait?: boolean;
166
168
  }
167
169
  /**
168
170
  * Re-entrant callback that executes a named function from the blueprint on behalf
@@ -181,6 +183,7 @@ export type ParentToChildMessage = {
181
183
  srcPath: string;
182
184
  payload: unknown;
183
185
  isDurable?: boolean;
186
+ noWait?: boolean;
184
187
  } | {
185
188
  type: 'invoke-response';
186
189
  id: string;
@@ -195,7 +198,7 @@ export type ParentToChildMessage = {
195
198
  error: InvocationError;
196
199
  } | {
197
200
  type: 'step-event';
198
- event: DurableStepEvent;
201
+ event: DurableRuntimeEvent;
199
202
  };
200
203
  /**
201
204
  * IPC messages sent from a function child process back up to the parent.
@@ -217,7 +220,7 @@ export type ChildToParentMessage = {
217
220
  error?: InvocationError;
218
221
  } | {
219
222
  type: 'step-event';
220
- event: DurableStepEvent;
223
+ event: DurableRuntimeEvent;
221
224
  };
222
225
  /** @internal */
223
226
  export interface InvocationResponse {
@@ -301,7 +304,7 @@ export type Warning = {
301
304
  suggestions: string[];
302
305
  };
303
306
  /** @internal */
304
- export type DurableOperationSteps = 'run';
307
+ export type DurableOperationSteps = 'run' | 'wait' | 'waitForCondition';
305
308
  /** @internal */
306
309
  export type DurableStepPhase = 'start' | 'complete' | 'error';
307
310
  /**
@@ -315,4 +318,34 @@ export interface DurableStepEvent {
315
318
  error?: InvocationError;
316
319
  durationMs?: number;
317
320
  }
321
+ export interface DurableLogEvent {
322
+ kind: 'log';
323
+ phase: 'log';
324
+ step: string | null;
325
+ level: 'info' | 'warn' | 'error';
326
+ message: string;
327
+ timestamp?: string;
328
+ }
329
+ export type DurableRuntimeEvent = DurableStepEvent | DurableLogEvent;
330
+ export interface FunctionTestFlags {
331
+ data?: string;
332
+ event?: string;
333
+ file?: string;
334
+ timeout?: number;
335
+ api?: string;
336
+ dataset?: string;
337
+ 'project-id'?: string;
338
+ 'organization-id'?: string;
339
+ 'document-id'?: string;
340
+ 'with-user-token'?: boolean;
341
+ 'data-before'?: string;
342
+ 'data-after'?: string;
343
+ 'file-before'?: string;
344
+ 'file-after'?: string;
345
+ 'document-id-before'?: string;
346
+ 'document-id-after'?: string;
347
+ 'media-library-id'?: string;
348
+ 'no-wait'?: boolean;
349
+ json?: boolean;
350
+ }
318
351
  export {};
@@ -1556,9 +1556,10 @@
1556
1556
  "blueprints:stacks": {
1557
1557
  "aliases": [],
1558
1558
  "args": {},
1559
- "description": "Shows all Stacks associated with a project or organization. By default, lists Stacks scoped to the local Blueprint.\n\nUse this to discover existing Stacks you can scope a local Blueprint to (using 'blueprints config --edit'), or to audit what's deployed across your project.\n\nUse --include-projects with --organization-id to also list Stacks from all projects within the organization.",
1559
+ "description": "Shows all Stacks associated with a project or organization. By default, lists Stacks scoped to the local Blueprint.\n\nUse this to discover existing Stacks you can scope a local Blueprint to (using 'blueprints config --edit'), or to audit what's deployed across your project.\n\nWithout a scope, prompts for an organization or project. Use --all to list Stacks across every organization and project you can access, or --include-projects with --organization-id for one organization and its projects.",
1560
1560
  "examples": [
1561
1561
  "<%= config.bin %> <%= command.id %>",
1562
+ "<%= config.bin %> <%= command.id %> --all",
1562
1563
  "<%= config.bin %> <%= command.id %> --project-id <projectId>",
1563
1564
  "<%= config.bin %> <%= command.id %> --organization-id <organizationId>",
1564
1565
  "<%= config.bin %> <%= command.id %> --organization-id <organizationId> --include-projects"
@@ -1620,7 +1621,8 @@
1620
1621
  "description": "Sanity project ID used to scope Blueprint and Stack",
1621
1622
  "exclusive": [
1622
1623
  "organization-id",
1623
- "include-projects"
1624
+ "include-projects",
1625
+ "all"
1624
1626
  ],
1625
1627
  "name": "project-id",
1626
1628
  "hasDynamicHelp": false,
@@ -1635,7 +1637,8 @@
1635
1637
  ],
1636
1638
  "description": "Sanity organization ID used to scope Blueprint and Stack",
1637
1639
  "exclusive": [
1638
- "project-id"
1640
+ "project-id",
1641
+ "all"
1639
1642
  ],
1640
1643
  "name": "organization-id",
1641
1644
  "hasDynamicHelp": false,
@@ -1650,6 +1653,17 @@
1650
1653
  "name": "include-projects",
1651
1654
  "allowNo": false,
1652
1655
  "type": "boolean"
1656
+ },
1657
+ "all": {
1658
+ "description": "List Stacks from every organization and project you have access to",
1659
+ "exclusive": [
1660
+ "project-id",
1661
+ "organization-id",
1662
+ "include-projects"
1663
+ ],
1664
+ "name": "all",
1665
+ "allowNo": false,
1666
+ "type": "boolean"
1653
1667
  }
1654
1668
  },
1655
1669
  "hasDynamicHelp": false,
@@ -1662,7 +1676,7 @@
1662
1676
  "summary": "List remote Stack deployments for your project or organization",
1663
1677
  "enableJsonFlag": true,
1664
1678
  "needs": [
1665
- "scope"
1679
+ "token"
1666
1680
  ],
1667
1681
  "isESM": true,
1668
1682
  "relativePath": [
@@ -2741,6 +2755,13 @@
2741
2755
  "hasDynamicHelp": false,
2742
2756
  "multiple": false,
2743
2757
  "type": "option"
2758
+ },
2759
+ "no-wait": {
2760
+ "description": "Skip durable wait delays instead of sleeping",
2761
+ "name": "no-wait",
2762
+ "required": false,
2763
+ "allowNo": false,
2764
+ "type": "boolean"
2744
2765
  }
2745
2766
  },
2746
2767
  "hasDynamicHelp": false,
@@ -3127,5 +3148,5 @@
3127
3148
  ]
3128
3149
  }
3129
3150
  },
3130
- "version": "17.8.0"
3151
+ "version": "17.10.0"
3131
3152
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sanity/runtime-cli",
3
3
  "description": "Sanity's Runtime CLI for Blueprints and Functions",
4
- "version": "17.8.0",
4
+ "version": "17.10.0",
5
5
  "author": "Sanity Runtime Team",
6
6
  "type": "module",
7
7
  "license": "MIT",
@@ -17,7 +17,9 @@ npx @sanity/cli blueprints stacks --project-id PROJECT_ID --json
17
17
  npx @sanity/cli blueprints stacks --organization-id ORGANIZATION_ID --json
18
18
  ```
19
19
 
20
- The JSON result contains `stacks`, `scopeType`, and `scopeId`; each entry carries `resourceCount` rather than a full `resources` array. Zero results prints `No stacks found` and emits no JSON. Match both the Stack and its scope; names alone may be ambiguous.
20
+ Always pass a scope flag or `--all` when automating. With no scope to resolve the command prompts for an organization, then for a project if you pick project scope; without a terminal, or with `--json`, it fails with a missing-scope error instead.
21
+
22
+ The JSON result contains `stacks`, `scopeType`, and `scopeId`; each entry carries `resourceCount` rather than a full `resources` array. Zero results prints `No stacks found`, and under `--json` returns an empty `stacks` array. Match both the Stack and its scope; names alone may be ambiguous.
21
23
 
22
24
  To also include Stacks scoped to the organization's projects:
23
25
 
@@ -25,4 +27,14 @@ To also include Stacks scoped to the organization's projects:
25
27
  npx @sanity/cli blueprints stacks --organization-id ORGANIZATION_ID --include-projects --json
26
28
  ```
27
29
 
28
- `--include-projects` requires `--organization-id`, cannot be combined with `--project-id`, and only queries the organization's first 50 projects. This command does not change the local Blueprint target; use `blueprints config --edit` for that.
30
+ `--include-projects` requires `--organization-id`, cannot be combined with `--project-id`, and only queries the organization's first 50 projects.
31
+
32
+ To sweep every organization and project the token can reach:
33
+
34
+ ```sh
35
+ npx @sanity/cli blueprints stacks --all --json
36
+ ```
37
+
38
+ `--all` cannot be combined with the scope flags or `--include-projects`. It is best effort: scopes that return an error are skipped and reported as a count, and only the first 200 projects are queried. Its JSON result contains `stacks` and no top-level `scopeType`/`scopeId`; read each entry's own `scopeType` and `scopeId` instead.
39
+
40
+ This command does not change the local Blueprint target; use `blueprints config --edit` for that.