@sanity/runtime-cli 2.5.3 → 3.0.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 (35) hide show
  1. package/README.md +20 -16
  2. package/dist/actions/blueprints/index.d.ts +7 -16
  3. package/dist/actions/blueprints/index.js +7 -16
  4. package/dist/actions/functions/env/index.d.ts +2 -6
  5. package/dist/actions/functions/env/index.js +2 -6
  6. package/dist/actions/functions/index.d.ts +5 -14
  7. package/dist/actions/functions/index.js +5 -12
  8. package/dist/actions/functions/test.d.ts +2 -2
  9. package/dist/actions/functions/test.js +2 -2
  10. package/dist/commands/functions/test.d.ts +3 -0
  11. package/dist/commands/functions/test.js +17 -0
  12. package/dist/index.d.ts +1 -4
  13. package/dist/index.js +1 -4
  14. package/dist/server/app.js +48 -4
  15. package/dist/server/static/api.d.ts +4 -0
  16. package/dist/server/static/api.js +26 -0
  17. package/dist/server/static/components/api-base.d.ts +2 -0
  18. package/dist/server/static/components/dataset-dropdown.d.ts +1 -0
  19. package/dist/server/static/components/dataset-dropdown.js +45 -0
  20. package/dist/server/static/components/payload-panel.js +18 -1
  21. package/dist/server/static/components/projects-dropdown.d.ts +1 -0
  22. package/dist/server/static/components/projects-dropdown.js +42 -0
  23. package/dist/server/static/index.html +2 -0
  24. package/dist/utils/child-process-wrapper.js +27 -1
  25. package/dist/utils/display/index.d.ts +4 -10
  26. package/dist/utils/display/index.js +4 -10
  27. package/dist/utils/index.d.ts +4 -15
  28. package/dist/utils/index.js +4 -10
  29. package/dist/utils/invoke-local.d.ts +2 -2
  30. package/dist/utils/invoke-local.js +10 -1
  31. package/dist/utils/types.d.ts +6 -0
  32. package/dist/utils/vendor/index.d.ts +1 -4
  33. package/dist/utils/vendor/index.js +1 -4
  34. package/oclif.manifest.json +26 -1
  35. package/package.json +21 -1
package/README.md CHANGED
@@ -20,7 +20,7 @@ $ npm install -g @sanity/runtime-cli
20
20
  $ sanity-run COMMAND
21
21
  running command...
22
22
  $ sanity-run (--version)
23
- @sanity/runtime-cli/2.5.3 linux-x64 node-v22.14.0
23
+ @sanity/runtime-cli/3.0.0 linux-x64 node-v22.14.0
24
24
  $ sanity-run --help [COMMAND]
25
25
  USAGE
26
26
  $ sanity-run COMMAND
@@ -63,7 +63,7 @@ EXAMPLES
63
63
  $ sanity-run blueprints add function
64
64
  ```
65
65
 
66
- _See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/blueprints/add.ts)_
66
+ _See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/blueprints/add.ts)_
67
67
 
68
68
  ## `sanity-run blueprints config`
69
69
 
@@ -85,7 +85,7 @@ EXAMPLES
85
85
  $ sanity-run blueprints config --edit
86
86
  ```
87
87
 
88
- _See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/blueprints/config.ts)_
88
+ _See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/blueprints/config.ts)_
89
89
 
90
90
  ## `sanity-run blueprints deploy`
91
91
 
@@ -102,7 +102,7 @@ EXAMPLES
102
102
  $ sanity-run blueprints deploy
103
103
  ```
104
104
 
105
- _See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/blueprints/deploy.ts)_
105
+ _See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/blueprints/deploy.ts)_
106
106
 
107
107
  ## `sanity-run blueprints info`
108
108
 
@@ -124,7 +124,7 @@ EXAMPLES
124
124
  $ sanity-run blueprints info --id abc123
125
125
  ```
126
126
 
127
- _See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/blueprints/info.ts)_
127
+ _See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/blueprints/info.ts)_
128
128
 
129
129
  ## `sanity-run blueprints init`
130
130
 
@@ -141,7 +141,7 @@ EXAMPLES
141
141
  $ sanity-run blueprints init
142
142
  ```
143
143
 
144
- _See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/blueprints/init.ts)_
144
+ _See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/blueprints/init.ts)_
145
145
 
146
146
  ## `sanity-run blueprints logs`
147
147
 
@@ -163,7 +163,7 @@ EXAMPLES
163
163
  $ sanity-run blueprints logs --watch
164
164
  ```
165
165
 
166
- _See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/blueprints/logs.ts)_
166
+ _See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/blueprints/logs.ts)_
167
167
 
168
168
  ## `sanity-run blueprints plan`
169
169
 
@@ -180,7 +180,7 @@ EXAMPLES
180
180
  $ sanity-run blueprints plan
181
181
  ```
182
182
 
183
- _See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/blueprints/plan.ts)_
183
+ _See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/blueprints/plan.ts)_
184
184
 
185
185
  ## `sanity-run blueprints stacks`
186
186
 
@@ -197,7 +197,7 @@ EXAMPLES
197
197
  $ sanity-run blueprints stacks
198
198
  ```
199
199
 
200
- _See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/blueprints/stacks.ts)_
200
+ _See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/blueprints/stacks.ts)_
201
201
 
202
202
  ## `sanity-run functions dev`
203
203
 
@@ -217,7 +217,7 @@ EXAMPLES
217
217
  $ sanity-run functions dev --port 8974
218
218
  ```
219
219
 
220
- _See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/functions/dev.ts)_
220
+ _See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/functions/dev.ts)_
221
221
 
222
222
  ## `sanity-run functions env add NAME KEY VALUE`
223
223
 
@@ -239,7 +239,7 @@ EXAMPLES
239
239
  $ sanity-run functions env add MyFunction API_URL https://api.example.com/
240
240
  ```
241
241
 
242
- _See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/functions/env/add.ts)_
242
+ _See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/functions/env/add.ts)_
243
243
 
244
244
  ## `sanity-run functions env remove NAME KEY`
245
245
 
@@ -260,7 +260,7 @@ EXAMPLES
260
260
  $ sanity-run functions env remove MyFunction API_URL
261
261
  ```
262
262
 
263
- _See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/functions/env/remove.ts)_
263
+ _See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/functions/env/remove.ts)_
264
264
 
265
265
  ## `sanity-run functions invoke NAME`
266
266
 
@@ -286,7 +286,7 @@ EXAMPLES
286
286
  $ sanity-run functions invoke <name> --file 'payload.json'
287
287
  ```
288
288
 
289
- _See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/functions/invoke.ts)_
289
+ _See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/functions/invoke.ts)_
290
290
 
291
291
  ## `sanity-run functions logs NAME`
292
292
 
@@ -314,7 +314,7 @@ EXAMPLES
314
314
  $ sanity-run functions logs <name> --limit 100
315
315
  ```
316
316
 
317
- _See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/functions/logs.ts)_
317
+ _See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/functions/logs.ts)_
318
318
 
319
319
  ## `sanity-run functions test NAME`
320
320
 
@@ -322,15 +322,19 @@ Invoke a local Sanity Function
322
322
 
323
323
  ```
324
324
  USAGE
325
- $ sanity-run functions test NAME [-d <value>] [-f <value>] [-t <value>]
325
+ $ sanity-run functions test NAME [-d <value>] [-f <value>] [-t <value>] [-a <value>] [--dataset <value>]
326
+ [--project <value>]
326
327
 
327
328
  ARGUMENTS
328
329
  NAME The name of the Sanity Function
329
330
 
330
331
  FLAGS
332
+ -a, --api=<value> Sanity API Version to use
331
333
  -d, --data=<value> Data to send to the function
332
334
  -f, --file=<value> Read data from file and send to the function
333
335
  -t, --timeout=<value> Execution timeout value in seconds
336
+ --dataset=<value> The Sanity dataset to use
337
+ --project=<value> The Sanity project to use
334
338
 
335
339
  DESCRIPTION
336
340
  Invoke a local Sanity Function
@@ -343,7 +347,7 @@ EXAMPLES
343
347
  $ sanity-run functions test <name> --data '{ "id": 1 }' --timeout 60
344
348
  ```
345
349
 
346
- _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.3/src/commands/functions/test.ts)_
350
+ _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/functions/test.ts)_
347
351
 
348
352
  ## `sanity-run help [COMMAND]`
349
353
 
@@ -1,16 +1,7 @@
1
- import * as assets from './assets.js';
2
- import * as blueprint from './blueprint.js';
3
- import * as logs from './logs.js';
4
- import * as operations from './operations.js';
5
- import * as projects from './projects.js';
6
- import * as resources from './resources.js';
7
- import * as stacks from './stacks.js';
8
- export declare const blueprintsActions: {
9
- assets: typeof assets;
10
- blueprint: typeof blueprint;
11
- logs: typeof logs;
12
- operations: typeof operations;
13
- projects: typeof projects;
14
- resources: typeof resources;
15
- stacks: typeof stacks;
16
- };
1
+ export * as assets from './assets.js';
2
+ export * as blueprint from './blueprint.js';
3
+ export * as logs from './logs.js';
4
+ export * as operations from './operations.js';
5
+ export * as projects from './projects.js';
6
+ export * as resources from './resources.js';
7
+ export * as stacks from './stacks.js';
@@ -1,16 +1,7 @@
1
- import * as assets from './assets.js';
2
- import * as blueprint from './blueprint.js';
3
- import * as logs from './logs.js';
4
- import * as operations from './operations.js';
5
- import * as projects from './projects.js';
6
- import * as resources from './resources.js';
7
- import * as stacks from './stacks.js';
8
- export const blueprintsActions = {
9
- assets,
10
- blueprint,
11
- logs,
12
- operations,
13
- projects,
14
- resources,
15
- stacks,
16
- };
1
+ export * as assets from './assets.js';
2
+ export * as blueprint from './blueprint.js';
3
+ export * as logs from './logs.js';
4
+ export * as operations from './operations.js';
5
+ export * as projects from './projects.js';
6
+ export * as resources from './resources.js';
7
+ export * as stacks from './stacks.js';
@@ -1,6 +1,2 @@
1
- import * as remove from './remove.js';
2
- import * as update from './update.js';
3
- export declare const envActions: {
4
- remove: typeof remove;
5
- update: typeof update;
6
- };
1
+ export * as remove from './remove.js';
2
+ export * as update from './update.js';
@@ -1,6 +1,2 @@
1
- import * as remove from './remove.js';
2
- import * as update from './update.js';
3
- export const envActions = {
4
- remove,
5
- update,
6
- };
1
+ export * as remove from './remove.js';
2
+ export * as update from './update.js';
@@ -1,14 +1,5 @@
1
- import * as dev from './dev.js';
2
- import * as invoke from './invoke.js';
3
- import * as logs from './logs.js';
4
- import * as test from './test.js';
5
- export declare const functionsActions: {
6
- dev: typeof dev;
7
- env: {
8
- remove: typeof import("./env/remove.js");
9
- update: typeof import("./env/update.js");
10
- };
11
- invoke: typeof invoke;
12
- logs: typeof logs;
13
- test: typeof test;
14
- };
1
+ export * as dev from './dev.js';
2
+ export * as env from './env/index.js';
3
+ export * as invoke from './invoke.js';
4
+ export * as logs from './logs.js';
5
+ export * as test from './test.js';
@@ -1,12 +1,5 @@
1
- import * as dev from './dev.js';
2
- import * as env from './env/index.js';
3
- import * as invoke from './invoke.js';
4
- import * as logs from './logs.js';
5
- import * as test from './test.js';
6
- export const functionsActions = {
7
- dev,
8
- env: env.envActions,
9
- invoke,
10
- logs,
11
- test,
12
- };
1
+ export * as dev from './dev.js';
2
+ export * as env from './env/index.js';
3
+ export * as invoke from './invoke.js';
4
+ export * as logs from './logs.js';
5
+ export * as test from './test.js';
@@ -1,2 +1,2 @@
1
- import type { InvocationResponse, InvokePayloadOptions } from '../../utils/types.js';
2
- export declare function testAction(srcPath: string, options: InvokePayloadOptions): Promise<InvocationResponse>;
1
+ import type { InvocationResponse, InvokeContextOptions, InvokePayloadOptions } from '../../utils/types.js';
2
+ export declare function testAction(srcPath: string, options: InvokePayloadOptions, context: InvokeContextOptions): Promise<InvocationResponse>;
@@ -1,10 +1,10 @@
1
1
  import buildPayload from '../../utils/build-payload.js';
2
2
  import invoke from '../../utils/invoke-local.js';
3
- export async function testAction(srcPath, options) {
3
+ export async function testAction(srcPath, options, context) {
4
4
  const payload = buildPayload(options);
5
5
  const { timeout } = options;
6
6
  try {
7
- const { json, logs } = await invoke(srcPath, payload, timeout);
7
+ const { json, logs } = await invoke(srcPath, payload, context, timeout);
8
8
  return { error: undefined, json, logs };
9
9
  }
10
10
  catch (error) {
@@ -9,6 +9,9 @@ export default class Test extends Command {
9
9
  data: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
10
10
  file: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
11
11
  timeout: import("@oclif/core/interfaces").OptionFlag<number | undefined, import("@oclif/core/interfaces").CustomOptions>;
12
+ api: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
+ dataset: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
14
+ project: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
12
15
  };
13
16
  run(): Promise<void>;
14
17
  }
@@ -24,6 +24,19 @@ export default class Test extends Command {
24
24
  description: 'Execution timeout value in seconds',
25
25
  required: false,
26
26
  }),
27
+ api: Flags.string({
28
+ char: 'a',
29
+ description: 'Sanity API Version to use',
30
+ required: false,
31
+ }),
32
+ dataset: Flags.string({
33
+ description: 'The Sanity dataset to use',
34
+ required: false,
35
+ }),
36
+ project: Flags.string({
37
+ description: 'The Sanity project to use',
38
+ required: false,
39
+ }),
27
40
  };
28
41
  async run() {
29
42
  const { args, flags } = await this.parse(Test);
@@ -37,6 +50,10 @@ export default class Test extends Command {
37
50
  data: flags.data,
38
51
  file: flags.file,
39
52
  timeout: flags.timeout,
53
+ }, {
54
+ apiVersion: flags.api,
55
+ dataset: flags.dataset,
56
+ projectId: flags.project,
40
57
  });
41
58
  if (!error) {
42
59
  this.log('Logs:');
package/dist/index.d.ts CHANGED
@@ -1,5 +1,2 @@
1
1
  import { run } from '@oclif/core';
2
- import { blueprintsActions } from './actions/blueprints/index.js';
3
- import { functionsActions } from './actions/functions/index.js';
4
- import { utils } from './utils/index.js';
5
- export { blueprintsActions, functionsActions, utils, run };
2
+ export { run };
package/dist/index.js CHANGED
@@ -1,5 +1,2 @@
1
1
  import { run } from '@oclif/core';
2
- import { blueprintsActions } from './actions/blueprints/index.js';
3
- import { functionsActions } from './actions/functions/index.js';
4
- import { utils } from './utils/index.js';
5
- export { blueprintsActions, functionsActions, utils, run };
2
+ export { run };
@@ -1,14 +1,15 @@
1
1
  import { existsSync, readFileSync } from 'node:fs';
2
2
  import { default as mime } from 'mime-types';
3
3
  import { readBlueprintOnDisk } from '../actions/blueprints/blueprint.js';
4
+ import config from '../config.js';
4
5
  import invoke from '../utils/invoke-local.js';
5
6
  import * as http from 'node:http';
6
7
  const host = 'localhost';
7
8
  const app = (port) => {
8
9
  const requestListener = async (req, res) => {
9
10
  res.setHeader('Content-Type', 'application/json');
10
- switch (req.url) {
11
- case '/blueprint': {
11
+ switch (true) {
12
+ case req.url === '/blueprint': {
12
13
  try {
13
14
  const { parsedBlueprint } = await readBlueprintOnDisk({ getStack: false });
14
15
  res.setHeader('Content-Type', 'application/json');
@@ -21,7 +22,7 @@ const app = (port) => {
21
22
  }
22
23
  break;
23
24
  }
24
- case '/invoke': {
25
+ case req.url === '/invoke': {
25
26
  if (req.method === 'POST') {
26
27
  let body = '';
27
28
  req.on('data', (data) => {
@@ -29,9 +30,10 @@ const app = (port) => {
29
30
  });
30
31
  req.on('end', async () => {
31
32
  const { data, func } = JSON.parse(body);
33
+ const { context, event } = data;
32
34
  res.setHeader('Content-Type', 'application/json');
33
35
  try {
34
- const response = await invoke(func, { data: JSON.parse(data) });
36
+ const response = await invoke(func, event, context);
35
37
  res.writeHead(200);
36
38
  res.end(JSON.stringify(response));
37
39
  }
@@ -47,6 +49,48 @@ const app = (port) => {
47
49
  }
48
50
  break;
49
51
  }
52
+ case req.url === '/projects': {
53
+ res.setHeader('Content-Type', 'application/json');
54
+ try {
55
+ const response = await fetch(`${config.apiUrl}/v2021-06-07/projects?includeOrganizationProjects=true`, {
56
+ headers: {
57
+ Authorization: `Bearer ${config.token}`,
58
+ },
59
+ });
60
+ const json = await response.json();
61
+ res.writeHead(200);
62
+ res.end(JSON.stringify(json));
63
+ }
64
+ catch (error) {
65
+ res.writeHead(200);
66
+ res.end(JSON.stringify([]));
67
+ }
68
+ break;
69
+ }
70
+ case req.url?.startsWith('/datasets'): {
71
+ const url = req.url || '';
72
+ const matches = url.match(/[?&]project=([^&]+)/) || [];
73
+ const projectId = matches ? matches[1] : null;
74
+ res.setHeader('Content-Type', 'application/json');
75
+ try {
76
+ let json = [];
77
+ if (projectId) {
78
+ const response = await fetch(`${config.apiUrl}/v2024-08-25/projects/${projectId}/datasets?includeStats=true`, {
79
+ headers: {
80
+ Authorization: `Bearer ${config.token}`,
81
+ },
82
+ });
83
+ json = await response.json();
84
+ }
85
+ res.writeHead(200);
86
+ res.end(JSON.stringify(json));
87
+ }
88
+ catch (error) {
89
+ res.writeHead(200);
90
+ res.end(JSON.stringify([]));
91
+ }
92
+ break;
93
+ }
50
94
  default: {
51
95
  const requestPath = req.url?.endsWith('/') ? `${req.url}index.html` : req.url;
52
96
  const filePath = new URL(`./static${requestPath}`, import.meta.url).pathname;
@@ -1,10 +1,14 @@
1
1
  export default function API(): {
2
2
  blueprint: typeof blueprint;
3
3
  invoke: typeof invoke;
4
+ projects: typeof projects;
5
+ datasets: typeof datasets;
4
6
  store: any;
5
7
  subscribe: any;
6
8
  unsubscribe: any;
7
9
  };
8
10
  declare function blueprint(): void;
9
11
  declare function invoke(payloadText?: string): void;
12
+ declare function projects(): void;
13
+ declare function datasets(selectedProject: any): void;
10
14
  export {};
@@ -8,6 +8,8 @@ export default function API() {
8
8
  return {
9
9
  blueprint,
10
10
  invoke,
11
+ projects,
12
+ datasets,
11
13
  store,
12
14
  subscribe: store.subscribe,
13
15
  unsubscribe: store.unsubscribe,
@@ -51,3 +53,27 @@ function blueprint() {
51
53
  store.functions = []
52
54
  })
53
55
  }
56
+
57
+ function projects() {
58
+ fetch('/projects')
59
+ .then((response) => response.json())
60
+ .then((projects) => {
61
+ store.projects = projects
62
+ store.selectedProject = projects[0].id
63
+ })
64
+ .catch(() => {
65
+ store.projects = []
66
+ })
67
+ }
68
+
69
+ function datasets(selectedProject) {
70
+ fetch(`/datasets?project=${selectedProject}`)
71
+ .then((response) => response.json())
72
+ .then((datasets) => {
73
+ store.datasets = datasets
74
+ store.selectedDataset = datasets[0].name
75
+ })
76
+ .catch(() => {
77
+ store.datasets = []
78
+ })
79
+ }
@@ -2,6 +2,8 @@ export class ApiBaseElement extends HTMLElement {
2
2
  api: {
3
3
  blueprint: () => void;
4
4
  invoke: (payloadText?: string) => void;
5
+ projects: () => void;
6
+ datasets: (selectedProject: any) => void;
5
7
  store: any;
6
8
  subscribe: any;
7
9
  unsubscribe: any;
@@ -0,0 +1,45 @@
1
+ /* globals customElements */
2
+ import {ApiBaseElement} from './api-base.js'
3
+
4
+ const template = `<fieldset class="mar-t-sm">
5
+ <label>Dataset</label>
6
+ <select id="dataset">
7
+ </select>
8
+ </fieldset>
9
+ `
10
+
11
+ class DatasetDropdown extends ApiBaseElement {
12
+ datasetSelected = (event) => {
13
+ this.api.store.selectedDataset = event.srcElement.value
14
+ }
15
+ renderDatasets = () => {
16
+ if (this.api.store?.datasets?.length > 0) {
17
+ this.select.innerHTML = this.api.store.datasets
18
+ .map((dataset) => {
19
+ const selected = this.api.store.selectedDataset === dataset.name ? 'selected' : ''
20
+ return `<option value="${dataset.name}" ${selected}>${dataset.name}</option>`
21
+ })
22
+ .join('')
23
+ } else {
24
+ this.select.innerHTML = '<option>No Datasets found</option>'
25
+ }
26
+ }
27
+ queryDatasets = () => {
28
+ this.api.datasets(this.api.store.selectedProject)
29
+ }
30
+
31
+ connectedCallback() {
32
+ this.innerHTML = template
33
+ this.select = this.querySelector('select')
34
+ this.select.addEventListener('change', this.datasetSelected)
35
+ this.api.subscribe(this.renderDatasets, ['datasets'])
36
+ this.api.subscribe(this.queryDatasets, ['selectedProject'])
37
+ }
38
+
39
+ disconnectedCallback() {
40
+ this.select.removeEventListener('change', this.datasetSelected)
41
+ this.api.unsubscribe(this.renderProjects)
42
+ }
43
+ }
44
+
45
+ customElements.define('dataset-dropdown', DatasetDropdown)
@@ -5,13 +5,27 @@ import {ApiBaseElement} from './api-base.js'
5
5
  const template = `<m-box>
6
6
  <h2 class="mar-t-0">Payload</h2>
7
7
  <div id="payload" name="payload"></div>
8
+ <form class="mar-t-sm">
9
+ <fieldset>
10
+ <label>API Version</label>
11
+ <input name="apiVersion" id="apiversion">
12
+ </fieldset>
13
+ <projects-dropdown></projects-dropdown>
14
+ <dataset-dropdown></dataset-dropdown>
15
+ </form>
8
16
  <button ord="primary" class="mar-t-sm sanity-button">Invoke</button>
9
17
  </m-box>
10
18
  `
11
19
  class PayloadPanel extends ApiBaseElement {
12
20
  invoke = () => {
13
21
  const payloadText = this.api.store.payload.state.doc.text.join('') || '{}'
14
- this.api.invoke(payloadText)
22
+ const event = JSON.parse(payloadText)
23
+ const context = {
24
+ apiVersion: this.apiVersion.value,
25
+ dataset: this.api.store.selectedDataset,
26
+ projectId: this.api.store.selectedProject,
27
+ }
28
+ this.api.invoke({context, event})
15
29
  }
16
30
  updateButtonText = ({inprogress}) => {
17
31
  if (inprogress) {
@@ -26,6 +40,9 @@ class PayloadPanel extends ApiBaseElement {
26
40
  connectedCallback() {
27
41
  this.innerHTML = template
28
42
  this.payload = this.querySelector('#payload')
43
+ this.apiVersion = this.querySelector('#apiversion')
44
+ this.datasetname = this.querySelector('#datasetname')
45
+ this.productId = this.querySelector('#project')
29
46
  this.button = this.querySelector('button')
30
47
  this.button.addEventListener('click', this.invoke)
31
48
  this.api.subscribe(this.updateButtonText, ['inprogress'])
@@ -0,0 +1,42 @@
1
+ /* globals customElements */
2
+ import {ApiBaseElement} from './api-base.js'
3
+
4
+ const template = `<fieldset class="mar-t-sm">
5
+ <label>Project</label>
6
+ <select id="project">
7
+ </select>
8
+ </fieldset>
9
+ `
10
+
11
+ class ProjectsDropdown extends ApiBaseElement {
12
+ projectSelected = (event) => {
13
+ this.api.store.selectedProject = event.srcElement.value
14
+ }
15
+ renderProjects = () => {
16
+ if (this.api.store.projects.length > 0) {
17
+ this.select.innerHTML = this.api.store.projects
18
+ .map((proj) => {
19
+ const selected = this.api.store.selectedProject === proj.id ? 'selected' : ''
20
+ return `<option value="${proj.id}" ${selected}>${proj.displayName}</option>`
21
+ })
22
+ .join('')
23
+ } else {
24
+ this.select.innerHTML = '<option>No Projects found</option>'
25
+ }
26
+ }
27
+
28
+ connectedCallback() {
29
+ this.innerHTML = template
30
+ this.select = this.querySelector('select')
31
+ this.select.addEventListener('change', this.projectSelected)
32
+ this.api.subscribe(this.renderProjects, ['projects'])
33
+ this.api.projects()
34
+ }
35
+
36
+ disconnectedCallback() {
37
+ this.select.removeEventListener('change', this.projectSelected)
38
+ this.api.unsubscribe(this.renderProjects)
39
+ }
40
+ }
41
+
42
+ customElements.define('projects-dropdown', ProjectsDropdown)
@@ -47,6 +47,8 @@
47
47
  </div>
48
48
  </footer>
49
49
 
50
+ <script src="./components/dataset-dropdown.js" type="module"></script>
51
+ <script src="./components/projects-dropdown.js" type="module"></script>
50
52
  <script src="./components/function-list.js" type="module"></script>
51
53
  <script src="./components/network-spinner.js" type="module"></script>
52
54
  <script src="./components/payload-panel.js" type="module"></script>
@@ -13,6 +13,30 @@ export function getFunctionSource(src) {
13
13
  return src
14
14
  }
15
15
 
16
+ async function enhanceContext(context = {}) {
17
+ const {apiHost, apiVersion, dataset, projectId, token} = context
18
+ const enhancedContext = {...context}
19
+
20
+ try {
21
+ if (apiHost && apiVersion && dataset && projectId && token) {
22
+ // @ts-ignore
23
+ const {createClient} = await import('@sanity/client')
24
+ const client = createClient({
25
+ projectId,
26
+ dataset,
27
+ useCdn: true,
28
+ apiHost,
29
+ apiVersion,
30
+ token,
31
+ })
32
+ enhancedContext.client = client
33
+ }
34
+ } catch {
35
+ // ignore as user hasn't provided the @sanity/client
36
+ }
37
+ return enhancedContext
38
+ }
39
+
16
40
  // Start when payload data arrives from parent process
17
41
  process.on('message', async (data) => {
18
42
  let jsonData = null
@@ -23,6 +47,8 @@ process.on('message', async (data) => {
23
47
  return
24
48
  }
25
49
  const {srcPath, payload} = jsonData
50
+ const {context, ...event} = payload
51
+ const enhancedContext = await enhanceContext(context)
26
52
  let logs = ''
27
53
  let json = null
28
54
 
@@ -41,7 +67,7 @@ process.on('message', async (data) => {
41
67
  }
42
68
 
43
69
  if (handler && typeof handler === 'function') {
44
- json = await handler(payload)
70
+ json = await handler({context: enhancedContext, event})
45
71
  }
46
72
 
47
73
  // revert changes to stdout
@@ -1,10 +1,4 @@
1
- import * as blueprintsFormatting from './blueprints-formatting.js';
2
- import * as colors from './colors.js';
3
- import * as dates from './dates.js';
4
- import * as logsFormatting from './logs-formatting.js';
5
- export declare const display: {
6
- blueprintsFormatting: typeof blueprintsFormatting;
7
- logsFormatting: typeof logsFormatting;
8
- colors: typeof colors;
9
- dates: typeof dates;
10
- };
1
+ export * as blueprintsFormatting from './blueprints-formatting.js';
2
+ export * as colors from './colors.js';
3
+ export * as dates from './dates.js';
4
+ export * as logsFormatting from './logs-formatting.js';
@@ -1,10 +1,4 @@
1
- import * as blueprintsFormatting from './blueprints-formatting.js';
2
- import * as colors from './colors.js';
3
- import * as dates from './dates.js';
4
- import * as logsFormatting from './logs-formatting.js';
5
- export const display = {
6
- blueprintsFormatting,
7
- logsFormatting,
8
- colors,
9
- dates,
10
- };
1
+ export * as blueprintsFormatting from './blueprints-formatting.js';
2
+ export * as colors from './colors.js';
3
+ export * as dates from './dates.js';
4
+ export * as logsFormatting from './logs-formatting.js';
@@ -1,15 +1,4 @@
1
- import * as findFunctions from './find-function.js';
2
- import * as types from './types.js';
3
- export declare const utils: {
4
- display: {
5
- blueprintsFormatting: typeof import("./display/blueprints-formatting.js");
6
- logsFormatting: typeof import("./display/logs-formatting.js");
7
- colors: typeof import("./display/colors.js");
8
- dates: typeof import("./display/dates.js");
9
- };
10
- findFunctions: typeof findFunctions;
11
- types: typeof types;
12
- vendor: {
13
- blueprintParserValidator: typeof import("./vendor/parser-validator.js").blueprintParserValidator;
14
- };
15
- };
1
+ export * as display from './display/index.js';
2
+ export * as findFunction from './find-function.js';
3
+ export * as types from './types.js';
4
+ export * as vendor from './vendor/index.js';
@@ -1,10 +1,4 @@
1
- import { display } from './display/index.js';
2
- import * as findFunctions from './find-function.js';
3
- import * as types from './types.js';
4
- import { vendor } from './vendor/index.js';
5
- export const utils = {
6
- display,
7
- findFunctions,
8
- types,
9
- vendor,
10
- };
1
+ export * as display from './display/index.js';
2
+ export * as findFunction from './find-function.js';
3
+ export * as types from './types.js';
4
+ export * as vendor from './vendor/index.js';
@@ -1,2 +1,2 @@
1
- import type { InvocationResponse } from './types.js';
2
- export default function invoke(srcPath: string, payload: null | object, timeout?: number): Promise<InvocationResponse>;
1
+ import type { InvocationResponse, InvokeContextOptions } from './types.js';
2
+ export default function invoke(srcPath: string, data: null | object, context: InvokeContextOptions, timeout?: number): Promise<InvocationResponse>;
@@ -1,10 +1,11 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { cwd } from 'node:process';
3
3
  import { setTimeout } from 'node:timers';
4
+ import config from '../config.js';
4
5
  function getChildProcessWrapperPath() {
5
6
  return new URL('./child-process-wrapper.js', import.meta.url).pathname;
6
7
  }
7
- export default async function invoke(srcPath, payload, timeout = 5) {
8
+ export default async function invoke(srcPath, data, context, timeout = 5) {
8
9
  return new Promise((resolve, reject) => {
9
10
  let child;
10
11
  let timer;
@@ -36,6 +37,14 @@ export default async function invoke(srcPath, payload, timeout = 5) {
36
37
  shutdown();
37
38
  reject(new Error(`Timed out after hitting its ${timeout}s timeout!`));
38
39
  }, timeout * 1000);
40
+ const payload = {
41
+ ...data,
42
+ context: {
43
+ apiHost: config.apiUrl,
44
+ token: config.token,
45
+ ...context,
46
+ },
47
+ };
39
48
  child.send(JSON.stringify({ srcPath, payload }, null, 2));
40
49
  }
41
50
  function shutdown() {
@@ -72,6 +72,12 @@ export interface InvokePayloadOptions {
72
72
  timeout?: number;
73
73
  }
74
74
  /** @internal */
75
+ export interface InvokeContextOptions {
76
+ apiVersion?: string;
77
+ dataset?: string;
78
+ projectId?: string;
79
+ }
80
+ /** @internal */
75
81
  export interface InvocationResponse {
76
82
  error: undefined | unknown;
77
83
  json: object | undefined;
@@ -1,4 +1 @@
1
- import { blueprintParserValidator } from './parser-validator.js';
2
- export declare const vendor: {
3
- blueprintParserValidator: typeof blueprintParserValidator;
4
- };
1
+ export { blueprintParserValidator } from './parser-validator.js';
@@ -1,4 +1 @@
1
- import { blueprintParserValidator } from './parser-validator.js';
2
- export const vendor = {
3
- blueprintParserValidator,
4
- };
1
+ export { blueprintParserValidator } from './parser-validator.js';
@@ -406,6 +406,31 @@
406
406
  "hasDynamicHelp": false,
407
407
  "multiple": false,
408
408
  "type": "option"
409
+ },
410
+ "api": {
411
+ "char": "a",
412
+ "description": "Sanity API Version to use",
413
+ "name": "api",
414
+ "required": false,
415
+ "hasDynamicHelp": false,
416
+ "multiple": false,
417
+ "type": "option"
418
+ },
419
+ "dataset": {
420
+ "description": "The Sanity dataset to use",
421
+ "name": "dataset",
422
+ "required": false,
423
+ "hasDynamicHelp": false,
424
+ "multiple": false,
425
+ "type": "option"
426
+ },
427
+ "project": {
428
+ "description": "The Sanity project to use",
429
+ "name": "project",
430
+ "required": false,
431
+ "hasDynamicHelp": false,
432
+ "multiple": false,
433
+ "type": "option"
409
434
  }
410
435
  },
411
436
  "hasDynamicHelp": false,
@@ -502,5 +527,5 @@
502
527
  ]
503
528
  }
504
529
  },
505
- "version": "2.5.3"
530
+ "version": "3.0.0"
506
531
  }
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": "2.5.3",
4
+ "version": "3.0.0",
5
5
  "author": "Sanity Runtime Team",
6
6
  "type": "module",
7
7
  "license": "MIT",
@@ -10,6 +10,24 @@
10
10
  "homepage": "https://github.com/sanity-io/runtime-cli",
11
11
  "main": "dist/index.js",
12
12
  "types": "dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ },
18
+ "./actions/blueprints": {
19
+ "types": "./dist/actions/blueprints/index.d.ts",
20
+ "import": "./dist/actions/blueprints/index.js"
21
+ },
22
+ "./actions/functions": {
23
+ "types": "./dist/actions/functions/index.d.ts",
24
+ "import": "./dist/actions/functions/index.js"
25
+ },
26
+ "./utils": {
27
+ "types": "./dist/utils/index.d.ts",
28
+ "import": "./dist/utils/index.js"
29
+ }
30
+ },
13
31
  "engines": {
14
32
  "node": ">=18.20.0"
15
33
  },
@@ -35,6 +53,7 @@
35
53
  "lint:write": "biome check --write",
36
54
  "prepack": "oclif manifest && oclif readme",
37
55
  "postpack": "shx rm -f oclif.manifest.json",
56
+ "pretest": "cd test/integration && npm install",
38
57
  "test": "vitest run",
39
58
  "posttest": "npm run lint",
40
59
  "test:watch": "vitest"
@@ -59,6 +78,7 @@
59
78
  "@enhance/store": "^1.0.2",
60
79
  "@oclif/test": "^4.1.12",
61
80
  "@rollup/plugin-node-resolve": "^16.0.1",
81
+ "@sanity/client": "^6.29.0",
62
82
  "@types/adm-zip": "^0.5.7",
63
83
  "@types/mime-types": "^2.1.4",
64
84
  "@types/node": "18",