|
| 1 | +--- |
| 2 | +name: test-studio-script-runner |
| 3 | +description: Explains the dev/test-studio Script Runner tool. Use when adding, editing, running, or documenting scripts in dev/test-studio/src/script-runner, or when the user mentions the Scripts tool, Studio runner scripts, script variables, or browser-side Sanity Studio scripts. |
| 4 | +--- |
| 5 | + |
| 6 | +# Test Studio Script Runner |
| 7 | + |
| 8 | +Use this skill when working with the `Scripts` tool in `dev/test-studio`. |
| 9 | + |
| 10 | +## Key Files |
| 11 | + |
| 12 | +- Tool plugin: `dev/test-studio/src/script-runner/index.tsx` |
| 13 | +- Runner UI: `dev/test-studio/src/script-runner/ScriptRunnerTool.tsx` |
| 14 | +- Script registry: `dev/test-studio/src/script-runner/registry.ts` |
| 15 | +- Script contract: `dev/test-studio/src/script-runner/types.ts` |
| 16 | +- Script modules: `dev/test-studio/src/script-runner/scripts/*/index.ts` |
| 17 | +- Agent-facing docs: `dev/test-studio/src/script-runner/README.md` |
| 18 | + |
| 19 | +Read `README.md` and `types.ts` before changing the runner or adding scripts. |
| 20 | + |
| 21 | +## What The Tool Does |
| 22 | + |
| 23 | +The runner is a Sanity Studio custom tool registered in the `kitchen-sink` workspace. |
| 24 | + |
| 25 | +- Home route: `<studio>/kitchen-sink/scripts` |
| 26 | +- Script route: `<studio>/kitchen-sink/scripts/<script-name>` |
| 27 | + |
| 28 | +Scripts are browser-side TypeScript modules discovered at build time with Vite `import.meta.glob`. |
| 29 | +They run inside Sanity Studio with the logged-in user's permissions and receive the Studio client. |
| 30 | + |
| 31 | +## Adding A Script |
| 32 | + |
| 33 | +Add a folder under `dev/test-studio/src/script-runner/scripts/`. The folder name should match the |
| 34 | +script name. Put the registered entrypoint in `index.ts`; any helper files can live beside it. |
| 35 | + |
| 36 | +```text |
| 37 | +scripts/ |
| 38 | + my-script-name/ |
| 39 | + index.ts |
| 40 | + helpers.ts |
| 41 | +``` |
| 42 | + |
| 43 | +Only `scripts/*/index.ts` files are discovered at build time. |
| 44 | + |
| 45 | +```ts |
| 46 | +import type {StudioScript} from '../../types' |
| 47 | + |
| 48 | +const script: StudioScript = { |
| 49 | + name: 'my-script-name', |
| 50 | + title: 'My script name', |
| 51 | + description: 'What this script does.', |
| 52 | + apiVersion: '2026-03-01', |
| 53 | + inputs: [ |
| 54 | + { |
| 55 | + name: 'documentId', |
| 56 | + title: 'Document ID', |
| 57 | + defaultValue: 'example-id', |
| 58 | + required: true, |
| 59 | + }, |
| 60 | + ], |
| 61 | + async run({client, inputs, log}) { |
| 62 | + log.info(`Running for ${inputs.documentId}`) |
| 63 | + await client.fetch('*[_type == "post"][0...1]') |
| 64 | + log.success('Done') |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +export default script |
| 69 | +``` |
| 70 | + |
| 71 | +Script names must be unique and use lowercase letters, numbers, and hyphens. Keep the folder name |
| 72 | +and script `name` aligned. The script name becomes the URL segment. |
| 73 | + |
| 74 | +## Runtime Contract |
| 75 | + |
| 76 | +`run()` receives: |
| 77 | + |
| 78 | +- `client`: Sanity Studio client, configured with the script `apiVersion` or the runner default. |
| 79 | +- `inputs`: string values from the run screen, keyed by input `name`. |
| 80 | +- `log`: `info`, `success`, `warning`, and `error` methods that append output in the UI. |
| 81 | +- `signal`: an `AbortSignal` reserved for script code that supports cancellation. |
| 82 | + |
| 83 | +## String Variables |
| 84 | + |
| 85 | +Use `inputs` for string variables. Each input renders as a text field on the script run screen. |
| 86 | +Required inputs disable the run button until non-empty. |
| 87 | + |
| 88 | +Available input fields: |
| 89 | + |
| 90 | +- `name` |
| 91 | +- `title` |
| 92 | +- `description` |
| 93 | +- `defaultValue` |
| 94 | +- `placeholder` |
| 95 | +- `required` |
| 96 | + |
| 97 | +All values passed to scripts are strings. Validate and trim values inside `run()` when needed. |
| 98 | + |
| 99 | +## Browser-Safe Rules |
| 100 | + |
| 101 | +Script runner modules execute in the browser. Do not use: |
| 102 | + |
| 103 | +- `fs`, `path`, or other Node built-ins |
| 104 | +- `process.exit` |
| 105 | +- direct environment variable access |
| 106 | +- `SANITY_AUTH_TOKEN` |
| 107 | + |
| 108 | +Do not create a separate Sanity client from env vars. Use the provided `client`. |
| 109 | + |
| 110 | +If a task needs Node-only APIs or token-based CLI behavior, keep it in `dev/test-studio/scripts/` |
| 111 | +instead of the Studio script runner. |
| 112 | + |
| 113 | +## Verification |
| 114 | + |
| 115 | +After changing the runner or adding scripts, run: |
| 116 | + |
| 117 | +```bash |
| 118 | +pnpm lint |
| 119 | +pnpm --filter test-studio build |
| 120 | +``` |
| 121 | + |
| 122 | +If `pnpm --filter test-studio build` fails because workspace package `dist` output is missing, build |
| 123 | +with dependencies first: |
| 124 | + |
| 125 | +```bash |
| 126 | +pnpm --filter test-studio... build |
| 127 | +``` |
0 commit comments