@sanity/runtime-cli 4.4.0 → 4.5.1

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 (82) hide show
  1. package/README.md +49 -77
  2. package/dist/actions/blueprints/assets.d.ts +1 -0
  3. package/dist/actions/blueprints/assets.js +21 -4
  4. package/dist/actions/blueprints/blueprint.d.ts +6 -12
  5. package/dist/actions/blueprints/blueprint.js +38 -45
  6. package/dist/actions/blueprints/index.d.ts +33 -0
  7. package/dist/actions/blueprints/index.js +32 -0
  8. package/dist/actions/blueprints/projects.d.ts +9 -0
  9. package/dist/actions/blueprints/projects.js +12 -0
  10. package/dist/actions/blueprints/stacks.d.ts +0 -12
  11. package/dist/actions/blueprints/stacks.js +3 -30
  12. package/dist/actions/functions/test.d.ts +2 -2
  13. package/dist/actions/functions/test.js +2 -2
  14. package/dist/baseCommands.d.ts +24 -0
  15. package/dist/baseCommands.js +69 -0
  16. package/dist/commands/blueprints/add.d.ts +1 -1
  17. package/dist/commands/blueprints/add.js +7 -6
  18. package/dist/commands/blueprints/config.d.ts +1 -1
  19. package/dist/commands/blueprints/config.js +24 -11
  20. package/dist/commands/blueprints/deploy.d.ts +2 -2
  21. package/dist/commands/blueprints/deploy.js +18 -33
  22. package/dist/commands/blueprints/destroy.d.ts +4 -3
  23. package/dist/commands/blueprints/destroy.js +32 -35
  24. package/dist/commands/blueprints/info.d.ts +2 -2
  25. package/dist/commands/blueprints/info.js +16 -36
  26. package/dist/commands/blueprints/init.d.ts +10 -2
  27. package/dist/commands/blueprints/init.js +85 -26
  28. package/dist/commands/blueprints/logs.d.ts +2 -2
  29. package/dist/commands/blueprints/logs.js +18 -32
  30. package/dist/commands/blueprints/plan.d.ts +2 -2
  31. package/dist/commands/blueprints/plan.js +10 -16
  32. package/dist/commands/blueprints/stacks.d.ts +3 -2
  33. package/dist/commands/blueprints/stacks.js +10 -29
  34. package/dist/commands/functions/env/add.d.ts +2 -2
  35. package/dist/commands/functions/env/add.js +6 -17
  36. package/dist/commands/functions/env/list.d.ts +2 -2
  37. package/dist/commands/functions/env/list.js +10 -17
  38. package/dist/commands/functions/env/remove.d.ts +2 -2
  39. package/dist/commands/functions/env/remove.js +6 -17
  40. package/dist/commands/functions/invoke.d.ts +2 -2
  41. package/dist/commands/functions/invoke.js +7 -14
  42. package/dist/commands/functions/logs.d.ts +3 -7
  43. package/dist/commands/functions/logs.js +21 -37
  44. package/dist/commands/functions/test.d.ts +3 -3
  45. package/dist/commands/functions/test.js +13 -14
  46. package/dist/server/app.js +82 -14
  47. package/dist/server/static/api.js +24 -3
  48. package/dist/server/static/components/function-list.js +4 -4
  49. package/dist/server/static/components/response-panel.js +14 -3
  50. package/dist/server/static/index.html +1 -0
  51. package/dist/utils/build-payload.d.ts +1 -1
  52. package/dist/utils/build-payload.js +3 -3
  53. package/dist/utils/bundle/bundle-function.d.ts +8 -0
  54. package/dist/utils/bundle/bundle-function.js +125 -0
  55. package/dist/utils/bundle/cleanup-source-maps.d.ts +10 -0
  56. package/dist/utils/bundle/cleanup-source-maps.js +53 -0
  57. package/dist/utils/bundle/find-up.d.ts +16 -0
  58. package/dist/utils/bundle/find-up.js +39 -0
  59. package/dist/utils/bundle/verify-handler.d.ts +2 -0
  60. package/dist/utils/bundle/verify-handler.js +13 -0
  61. package/dist/utils/child-process-wrapper.js +8 -6
  62. package/dist/utils/display/blueprints-formatting.js +2 -2
  63. package/dist/utils/display/errors.d.ts +4 -0
  64. package/dist/utils/display/errors.js +27 -0
  65. package/dist/utils/display/index.d.ts +1 -0
  66. package/dist/utils/display/index.js +1 -0
  67. package/dist/utils/functions/find-entry-point.d.ts +11 -0
  68. package/dist/utils/functions/find-entry-point.js +75 -0
  69. package/dist/utils/functions/should-bundle.d.ts +2 -0
  70. package/dist/utils/functions/should-bundle.js +23 -0
  71. package/dist/utils/invoke-local.d.ts +2 -2
  72. package/dist/utils/invoke-local.js +49 -8
  73. package/dist/utils/is-record.d.ts +1 -0
  74. package/dist/utils/is-record.js +3 -0
  75. package/dist/utils/parse-json-object.d.ts +1 -0
  76. package/dist/utils/parse-json-object.js +10 -0
  77. package/dist/utils/types.d.ts +13 -4
  78. package/dist/utils/types.js +9 -3
  79. package/oclif.manifest.json +59 -37
  80. package/package.json +5 -1
  81. package/dist/utils/is-json.d.ts +0 -1
  82. package/dist/utils/is-json.js +0 -12
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/4.4.0 linux-x64 node-v22.15.0
23
+ @sanity/runtime-cli/4.5.1 linux-x64 node-v22.15.0
24
24
  $ sanity-run --help [COMMAND]
25
25
  USAGE
26
26
  $ sanity-run COMMAND
@@ -34,10 +34,9 @@ USAGE
34
34
  * [`sanity-run blueprints deploy`](#sanity-run-blueprints-deploy)
35
35
  * [`sanity-run blueprints destroy`](#sanity-run-blueprints-destroy)
36
36
  * [`sanity-run blueprints info`](#sanity-run-blueprints-info)
37
- * [`sanity-run blueprints init`](#sanity-run-blueprints-init)
37
+ * [`sanity-run blueprints init [DIR]`](#sanity-run-blueprints-init-dir)
38
38
  * [`sanity-run blueprints logs`](#sanity-run-blueprints-logs)
39
39
  * [`sanity-run blueprints plan`](#sanity-run-blueprints-plan)
40
- * [`sanity-run blueprints stacks`](#sanity-run-blueprints-stacks)
41
40
  * [`sanity-run functions dev`](#sanity-run-functions-dev)
42
41
  * [`sanity-run functions env add NAME KEY VALUE`](#sanity-run-functions-env-add-name-key-value)
43
42
  * [`sanity-run functions env list NAME`](#sanity-run-functions-env-list-name)
@@ -74,7 +73,7 @@ EXAMPLES
74
73
  $ sanity-run blueprints add function --name my-function --function-type document-publish
75
74
  ```
76
75
 
77
- _See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/blueprints/add.ts)_
76
+ _See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/blueprints/add.ts)_
78
77
 
79
78
  ## `sanity-run blueprints config`
80
79
 
@@ -82,13 +81,12 @@ View or edit Blueprint configuration
82
81
 
83
82
  ```
84
83
  USAGE
85
- $ sanity-run blueprints config [-t] [--project-id <value> -e] [--stack-id <value> ]
84
+ $ sanity-run blueprints config [-t] [--project-id <value> -e]
86
85
 
87
86
  FLAGS
88
87
  -e, --edit Edit the configuration
89
88
  -t, --test-config Validate the configuration
90
89
  --project-id=<value> Update the Project ID in the configuration. Requires --edit flag
91
- --stack-id=<value> Update the Stack ID in the configuration. Requires --edit flag
92
90
 
93
91
  DESCRIPTION
94
92
  View or edit Blueprint configuration
@@ -100,10 +98,10 @@ EXAMPLES
100
98
 
101
99
  $ sanity-run blueprints config --edit
102
100
 
103
- $ sanity-run blueprints config --edit --project-id <projectId> --stack-id <stackId>
101
+ $ sanity-run blueprints config --edit --project-id <projectId>
104
102
  ```
105
103
 
106
- _See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/blueprints/config.ts)_
104
+ _See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/blueprints/config.ts)_
107
105
 
108
106
  ## `sanity-run blueprints deploy`
109
107
 
@@ -121,69 +119,65 @@ DESCRIPTION
121
119
 
122
120
  EXAMPLES
123
121
  $ sanity-run blueprints deploy
122
+
123
+ $ sanity-run blueprints deploy --no-wait
124
124
  ```
125
125
 
126
- _See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/blueprints/deploy.ts)_
126
+ _See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/blueprints/deploy.ts)_
127
127
 
128
128
  ## `sanity-run blueprints destroy`
129
129
 
130
- Destroy a deployed Blueprint Stack
130
+ Destroy a Blueprint deployment
131
131
 
132
132
  ```
133
133
  USAGE
134
- $ sanity-run blueprints destroy [--id <value>] [--force]
134
+ $ sanity-run blueprints destroy [--force]
135
135
 
136
136
  FLAGS
137
- --force Force destroy (skip confirmation)
138
- --id=<value> Stack ID to destroy (defaults to current Stack)
137
+ --force Force destroy (skip confirmation)
139
138
 
140
139
  DESCRIPTION
141
- Destroy a deployed Blueprint Stack
140
+ Destroy a Blueprint deployment
142
141
 
143
142
  EXAMPLES
144
143
  $ sanity-run blueprints destroy
145
-
146
- $ sanity-run blueprints destroy --id ST-a1b2c3
147
144
  ```
148
145
 
149
- _See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/blueprints/destroy.ts)_
146
+ _See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/blueprints/destroy.ts)_
150
147
 
151
148
  ## `sanity-run blueprints info`
152
149
 
153
- Show information about a deployed Blueprint Stack
150
+ Show information about a Blueprint deployment
154
151
 
155
152
  ```
156
153
  USAGE
157
- $ sanity-run blueprints info [--id <value>]
158
-
159
- FLAGS
160
- --id=<value> Stack ID to show info for (defaults to current stack)
154
+ $ sanity-run blueprints info
161
155
 
162
156
  DESCRIPTION
163
- Show information about a deployed Blueprint Stack
157
+ Show information about a Blueprint deployment
164
158
 
165
159
  EXAMPLES
166
160
  $ sanity-run blueprints info
167
-
168
- $ sanity-run blueprints info --id ST-a1b2c3
169
161
  ```
170
162
 
171
- _See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/blueprints/info.ts)_
163
+ _See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/blueprints/info.ts)_
172
164
 
173
- ## `sanity-run blueprints init`
165
+ ## `sanity-run blueprints init [DIR]`
174
166
 
175
167
  Initialize a new Blueprint
176
168
 
177
169
  ```
178
170
  USAGE
179
- $ sanity-run blueprints init [--blueprint-type json|js|ts] [--stack-id <value> --project-id <value>] [-n <value> ]
171
+ $ sanity-run blueprints init [DIR] [--dir <value>] [--blueprint-type json|js|ts] [--project-id <value>]
172
+
173
+ ARGUMENTS
174
+ DIR Directory to create the Blueprint in
180
175
 
181
176
  FLAGS
182
- -n, --stack-name=<value> Name to use for a NEW Stack
183
- --blueprint-type=<option> Blueprint manifest type to use for the Blueprint
184
- <options: json|js|ts>
185
- --project-id=<value> Sanity Project ID to use for the Blueprint
186
- --stack-id=<value> Existing Stack ID to use for the Blueprint
177
+ --blueprint-type=<option> Blueprint manifest type to use for the Blueprint
178
+ <options: json|js|ts>
179
+ --dir=<value> Directory to create the Blueprint in
180
+ --project-id=<value> Sanity Project ID to use for the Blueprint
187
181
 
188
182
  DESCRIPTION
189
183
  Initialize a new Blueprint
@@ -191,18 +185,18 @@ DESCRIPTION
191
185
  EXAMPLES
192
186
  $ sanity-run blueprints init
193
187
 
194
- $ sanity-run blueprints init --blueprint-type <json|js|ts>
188
+ $ sanity-run blueprints init [directory]
195
189
 
196
- $ sanity-run blueprints init --blueprint-type <json|js|ts> --project-id <projectId> --stack-id <stackId>
190
+ $ sanity-run blueprints init --blueprint-type <json|js|ts>
197
191
 
198
- $ sanity-run blueprints init --blueprint-type <json|js|ts> --project-id <projectId> --stack-name <stackName>
192
+ $ sanity-run blueprints init --blueprint-type <json|js|ts> --project-id <projectId>
199
193
  ```
200
194
 
201
- _See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/blueprints/init.ts)_
195
+ _See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/blueprints/init.ts)_
202
196
 
203
197
  ## `sanity-run blueprints logs`
204
198
 
205
- Display logs for a Blueprint stack
199
+ Display logs for a Blueprint deployment
206
200
 
207
201
  ```
208
202
  USAGE
@@ -212,7 +206,7 @@ FLAGS
212
206
  -w, --watch Watch for new logs (streaming mode)
213
207
 
214
208
  DESCRIPTION
215
- Display logs for a Blueprint stack
209
+ Display logs for a Blueprint deployment
216
210
 
217
211
  EXAMPLES
218
212
  $ sanity-run blueprints logs
@@ -220,7 +214,7 @@ EXAMPLES
220
214
  $ sanity-run blueprints logs --watch
221
215
  ```
222
216
 
223
- _See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/blueprints/logs.ts)_
217
+ _See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/blueprints/logs.ts)_
224
218
 
225
219
  ## `sanity-run blueprints plan`
226
220
 
@@ -237,29 +231,7 @@ EXAMPLES
237
231
  $ sanity-run blueprints plan
238
232
  ```
239
233
 
240
- _See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/blueprints/plan.ts)_
241
-
242
- ## `sanity-run blueprints stacks`
243
-
244
- List all Blueprint stacks
245
-
246
- ```
247
- USAGE
248
- $ sanity-run blueprints stacks [--projectId <value>]
249
-
250
- FLAGS
251
- --projectId=<value> Project ID to show stacks for
252
-
253
- DESCRIPTION
254
- List all Blueprint stacks
255
-
256
- EXAMPLES
257
- $ sanity-run blueprints stacks
258
-
259
- $ sanity-run blueprints stacks --projectId a1b2c3
260
- ```
261
-
262
- _See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/blueprints/stacks.ts)_
234
+ _See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/blueprints/plan.ts)_
263
235
 
264
236
  ## `sanity-run functions dev`
265
237
 
@@ -279,7 +251,7 @@ EXAMPLES
279
251
  $ sanity-run functions dev --port 8974
280
252
  ```
281
253
 
282
- _See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/functions/dev.ts)_
254
+ _See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/functions/dev.ts)_
283
255
 
284
256
  ## `sanity-run functions env add NAME KEY VALUE`
285
257
 
@@ -301,7 +273,7 @@ EXAMPLES
301
273
  $ sanity-run functions env add MyFunction API_URL https://api.example.com/
302
274
  ```
303
275
 
304
- _See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/functions/env/add.ts)_
276
+ _See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/functions/env/add.ts)_
305
277
 
306
278
  ## `sanity-run functions env list NAME`
307
279
 
@@ -321,7 +293,7 @@ EXAMPLES
321
293
  $ sanity-run functions env list MyFunction
322
294
  ```
323
295
 
324
- _See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/functions/env/list.ts)_
296
+ _See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/functions/env/list.ts)_
325
297
 
326
298
  ## `sanity-run functions env remove NAME KEY`
327
299
 
@@ -342,7 +314,7 @@ EXAMPLES
342
314
  $ sanity-run functions env remove MyFunction API_URL
343
315
  ```
344
316
 
345
- _See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/functions/env/remove.ts)_
317
+ _See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/functions/env/remove.ts)_
346
318
 
347
319
  ## `sanity-run functions invoke NAME`
348
320
 
@@ -368,7 +340,7 @@ EXAMPLES
368
340
  $ sanity-run functions invoke <name> --file 'payload.json'
369
341
  ```
370
342
 
371
- _See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/functions/invoke.ts)_
343
+ _See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/functions/invoke.ts)_
372
344
 
373
345
  ## `sanity-run functions logs NAME`
374
346
 
@@ -400,7 +372,7 @@ EXAMPLES
400
372
  $ sanity-run functions logs <name> --delete
401
373
  ```
402
374
 
403
- _See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/functions/logs.ts)_
375
+ _See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/functions/logs.ts)_
404
376
 
405
377
  ## `sanity-run functions test NAME`
406
378
 
@@ -409,18 +381,18 @@ Invoke a local Sanity Function
409
381
  ```
410
382
  USAGE
411
383
  $ sanity-run functions test NAME [-d <value>] [-f <value>] [-t <value>] [-a <value>] [--dataset <value>]
412
- [--project <value>]
384
+ [--project-id <value>]
413
385
 
414
386
  ARGUMENTS
415
387
  NAME The name of the Sanity Function
416
388
 
417
389
  FLAGS
418
- -a, --api=<value> Sanity API Version to use
419
- -d, --data=<value> Data to send to the function
420
- -f, --file=<value> Read data from file and send to the function
421
- -t, --timeout=<value> Execution timeout value in seconds
422
- --dataset=<value> The Sanity dataset to use
423
- --project=<value> The Sanity project to use
390
+ -a, --api=<value> Sanity API Version to use
391
+ -d, --data=<value> Data to send to the function
392
+ -f, --file=<value> Read data from file and send to the function
393
+ -t, --timeout=<value> Execution timeout value in seconds
394
+ --dataset=<value> The Sanity dataset to use
395
+ --project-id=<value> Sanity Project ID to use
424
396
 
425
397
  DESCRIPTION
426
398
  Invoke a local Sanity Function
@@ -433,7 +405,7 @@ EXAMPLES
433
405
  $ sanity-run functions test <name> --data '{ "id": 1 }' --timeout 60
434
406
  ```
435
407
 
436
- _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v4.4.0/src/commands/functions/test.ts)_
408
+ _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v4.5.1/src/commands/functions/test.ts)_
437
409
 
438
410
  ## `sanity-run help [COMMAND]`
439
411
 
@@ -6,5 +6,6 @@ export declare function stashAsset({ resource, auth, }: {
6
6
  }): Promise<{
7
7
  success: boolean;
8
8
  assetId?: string;
9
+ outputPath?: string;
9
10
  error?: string;
10
11
  }>;
@@ -3,21 +3,35 @@ import path from 'node:path';
3
3
  import { cwd } from 'node:process';
4
4
  import AdmZip from 'adm-zip';
5
5
  import config from '../../config.js';
6
+ import { bundleFunction } from '../../utils/bundle/bundle-function.js';
7
+ import { shouldBundleFunction } from '../../utils/functions/should-bundle.js';
6
8
  import getHeaders from '../../utils/get-headers.js';
7
9
  const { apiUrl } = config;
8
10
  export const stashUrl = `${apiUrl}vX/blueprints/assets/stash`;
9
11
  export async function stashAsset({ resource, auth, }) {
10
12
  if (!resource.src)
11
13
  throw new Error('Resource src is required');
14
+ let functionPath = path.join(cwd(), resource.src);
15
+ let cleanup = async () => { };
16
+ const shouldBundle = await shouldBundleFunction(resource);
17
+ if (shouldBundle) {
18
+ try {
19
+ const result = await bundleFunction(resource);
20
+ functionPath = result.outputDir;
21
+ cleanup = result.cleanup;
22
+ }
23
+ catch (err) {
24
+ return { success: false, error: err instanceof Error ? err.message : `${err}` };
25
+ }
26
+ }
12
27
  try {
13
- const sourcePath = path.join(cwd(), resource.src);
14
- const stats = await fs.promises.stat(sourcePath);
28
+ const stats = await fs.promises.stat(functionPath);
15
29
  const zip = new AdmZip();
16
30
  if (stats.isDirectory()) {
17
- zip.addLocalFolder(sourcePath);
31
+ zip.addLocalFolder(functionPath);
18
32
  }
19
33
  else {
20
- zip.addLocalFile(sourcePath, '', 'index.js');
34
+ zip.addLocalFile(functionPath, '', 'index.js');
21
35
  }
22
36
  const zipBuffer = zip.toBuffer();
23
37
  const base64Zip = zipBuffer.toString('base64');
@@ -41,4 +55,7 @@ export async function stashAsset({ resource, auth, }) {
41
55
  error = err.message;
42
56
  return { success: false, error };
43
57
  }
58
+ finally {
59
+ await cleanup();
60
+ }
44
61
  }
@@ -1,28 +1,23 @@
1
- import type { BlueprintParserError, LocalBlueprint, LocalResource, Stack } from '../../utils/types.js';
1
+ import type { BlueprintParserError, LocalBlueprint, LocalResource } from '../../utils/types.js';
2
2
  declare const SUPPORTED_FILE_EXTENSIONS: readonly [".json", ".js", ".mjs", ".cjs", ".ts"];
3
3
  type BlueprintFileExtension = (typeof SUPPORTED_FILE_EXTENSIONS)[number];
4
4
  export declare const DEFAULT_BLUEPRINT_CONTENT: LocalBlueprint;
5
5
  /**
6
6
  * Finds the blueprint file in the given path or current working directory
7
- * @param blueprintFilePath - The full path of the blueprint file
7
+ * @param blueprintPath - The path of the blueprint file or directory
8
8
  * @returns The path, file name, and extension of the blueprint file
9
9
  */
10
- export declare function findBlueprintFile(blueprintFilePath?: string): {
10
+ export declare function findBlueprintFile(blueprintPath?: string): {
11
11
  blueprintFilePath: string;
12
12
  fileName: string;
13
13
  extension: BlueprintFileExtension;
14
14
  } | null;
15
15
  /**
16
- * Reads the blueprint file from disk and parses it
17
- * @todo rename to loadBlueprint or similar
18
- * @todo extract "getStack" into a separate function
16
+ * Reads the blueprint file from disk and parses it. Greedily looks for project and stack config
17
+ * @param blueprintPath - The path of the blueprint file or directory- will search up the directory tree!
19
18
  * @returns Known information about the Blueprint, config, and Stack
20
19
  */
21
- export declare function readBlueprintOnDisk({ blueprintFilePath, getStack, token, }?: {
22
- blueprintFilePath?: string;
23
- getStack?: boolean;
24
- token?: string;
25
- }): Promise<{
20
+ export declare function readLocalBlueprint(blueprintPath?: string): Promise<{
26
21
  fileInfo: {
27
22
  blueprintFilePath: string;
28
23
  fileName: string;
@@ -34,7 +29,6 @@ export declare function readBlueprintOnDisk({ blueprintFilePath, getStack, token
34
29
  configPath?: string;
35
30
  projectId?: string;
36
31
  stackId?: string;
37
- deployedStack?: Stack;
38
32
  }>;
39
33
  export declare function writeBlueprintToDisk({ blueprintFilePath, content, }: {
40
34
  blueprintFilePath: string;
@@ -2,8 +2,8 @@ import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'no
2
2
  import { createRequire } from 'node:module';
3
3
  import { basename, dirname, extname, join } from 'node:path';
4
4
  import { cwd } from 'node:process';
5
+ import { findUpSync } from 'find-up';
5
6
  import { blueprintParserValidator } from '../../utils/vendor/parser-validator.js';
6
- import { getStack as getStackById } from './stacks.js';
7
7
  const SUPPORTED_FILE_EXTENSIONS = ['.json', '.js', '.mjs', '.cjs', '.ts'];
8
8
  const SUPPORTED_FILE_NAMES_IN_PRIORITY_ORDER = SUPPORTED_FILE_EXTENSIONS.map((ext) => `blueprint${ext}`);
9
9
  export const DEFAULT_BLUEPRINT_CONTENT = {
@@ -12,49 +12,47 @@ export const DEFAULT_BLUEPRINT_CONTENT = {
12
12
  };
13
13
  /**
14
14
  * Finds the blueprint file in the given path or current working directory
15
- * @param blueprintFilePath - The full path of the blueprint file
15
+ * @param blueprintPath - The path of the blueprint file or directory
16
16
  * @returns The path, file name, and extension of the blueprint file
17
17
  */
18
- export function findBlueprintFile(blueprintFilePath) {
18
+ export function findBlueprintFile(blueprintPath) {
19
19
  let dirToSearch = cwd();
20
- if (blueprintFilePath) {
21
- try {
22
- const stat = statSync(blueprintFilePath);
23
- if (stat.isFile()) {
24
- return {
25
- blueprintFilePath: blueprintFilePath,
26
- fileName: basename(blueprintFilePath),
27
- extension: extname(blueprintFilePath),
28
- };
29
- }
30
- if (stat.isDirectory()) {
31
- dirToSearch = blueprintFilePath;
32
- }
33
- }
34
- catch {
35
- throw Error(`Blueprint file not found: ${blueprintFilePath}`);
36
- }
37
- }
38
- for (const fileName of SUPPORTED_FILE_NAMES_IN_PRIORITY_ORDER) {
39
- const filePath = join(dirToSearch, fileName);
40
- if (existsSync(filePath)) {
20
+ if (blueprintPath) {
21
+ const pathExists = existsSync(blueprintPath);
22
+ if (!pathExists)
23
+ return null;
24
+ const stat = statSync(blueprintPath);
25
+ if (stat.isFile()) {
41
26
  return {
42
- blueprintFilePath: filePath,
43
- fileName,
44
- extension: extname(filePath),
27
+ blueprintFilePath: blueprintPath,
28
+ fileName: basename(blueprintPath),
29
+ extension: extname(blueprintPath),
45
30
  };
46
31
  }
32
+ if (stat.isDirectory()) {
33
+ dirToSearch = blueprintPath;
34
+ }
47
35
  }
48
- return null;
36
+ const blueprintFile = findUpSync(SUPPORTED_FILE_NAMES_IN_PRIORITY_ORDER, {
37
+ cwd: dirToSearch,
38
+ type: 'file',
39
+ allowSymlinks: false,
40
+ });
41
+ if (!blueprintFile)
42
+ return null;
43
+ return {
44
+ blueprintFilePath: blueprintFile,
45
+ fileName: basename(blueprintFile),
46
+ extension: extname(blueprintFile),
47
+ };
49
48
  }
50
49
  /**
51
- * Reads the blueprint file from disk and parses it
52
- * @todo rename to loadBlueprint or similar
53
- * @todo extract "getStack" into a separate function
50
+ * Reads the blueprint file from disk and parses it. Greedily looks for project and stack config
51
+ * @param blueprintPath - The path of the blueprint file or directory- will search up the directory tree!
54
52
  * @returns Known information about the Blueprint, config, and Stack
55
53
  */
56
- export async function readBlueprintOnDisk({ blueprintFilePath, getStack, token, } = {}) {
57
- const blueprintFile = findBlueprintFile(blueprintFilePath);
54
+ export async function readLocalBlueprint(blueprintPath) {
55
+ const blueprintFile = findBlueprintFile(blueprintPath);
58
56
  if (!blueprintFile)
59
57
  throw Error('Could not find Blueprint file! Use the init command.');
60
58
  const { blueprintFilePath: foundPath, fileName, extension } = blueprintFile;
@@ -79,7 +77,6 @@ export async function readBlueprintOnDisk({ blueprintFilePath, getStack, token,
79
77
  break;
80
78
  }
81
79
  case '.ts': {
82
- console.warn('\x1b[2mSupport for Typescript blueprints is experimental.\x1b[0m');
83
80
  try {
84
81
  const { tsImport } = await import('tsx/esm/api');
85
82
  const module = await tsImport(foundPath, dirname(foundPath));
@@ -116,7 +113,7 @@ export async function readBlueprintOnDisk({ blueprintFilePath, getStack, token,
116
113
  const configIds = readConfigFile(foundPath);
117
114
  const configPath = configIds?.configPath;
118
115
  const blueprintMetadata = parsedBlueprint.metadata;
119
- // find/create project resource
116
+ // find projectId
120
117
  let projectId;
121
118
  if (configIds?.projectId) {
122
119
  projectId = configIds.projectId;
@@ -129,7 +126,7 @@ export async function readBlueprintOnDisk({ blueprintFilePath, getStack, token,
129
126
  if (projectResource)
130
127
  projectId = projectResource.id;
131
128
  }
132
- // find/create stack resource
129
+ // find stackId
133
130
  let stackId;
134
131
  if (configIds?.stackId) {
135
132
  stackId = configIds.stackId;
@@ -142,14 +139,9 @@ export async function readBlueprintOnDisk({ blueprintFilePath, getStack, token,
142
139
  if (stackResource)
143
140
  stackId = stackResource.id;
144
141
  }
145
- let deployedStack;
146
- if (getStack && token && stackId && projectId) {
147
- const { stack, ok, error } = await getStackById({ stackId, auth: { token, projectId } });
148
- if (!ok) {
149
- throw Error(error || 'Unable to retrieve Stack');
150
- }
151
- deployedStack = stack;
152
- }
142
+ // LAUNCH LIMIT: 1 Stack per Project - infer stackId from projectId
143
+ if (!stackId && projectId)
144
+ stackId = `ST-${projectId}`;
153
145
  return {
154
146
  fileInfo: { blueprintFilePath: foundPath, fileName, extension },
155
147
  rawBlueprint: rawBlueprint,
@@ -157,11 +149,11 @@ export async function readBlueprintOnDisk({ blueprintFilePath, getStack, token,
157
149
  projectId,
158
150
  stackId,
159
151
  configPath,
160
- deployedStack,
161
152
  parsedBlueprint,
162
153
  };
163
154
  }
164
155
  export function writeBlueprintToDisk({ blueprintFilePath, content = DEFAULT_BLUEPRINT_CONTENT, }) {
156
+ const dir = dirname(blueprintFilePath);
165
157
  const extension = extname(blueprintFilePath);
166
158
  let blueprintContent;
167
159
  switch (extension) {
@@ -187,6 +179,7 @@ export function writeBlueprintToDisk({ blueprintFilePath, content = DEFAULT_BLUE
187
179
  throw Error(`Unsupported blueprint file extension: ${extension}`);
188
180
  }
189
181
  }
182
+ mkdirSync(dir, { recursive: true });
190
183
  writeFileSync(blueprintFilePath, blueprintContent);
191
184
  return blueprintContent;
192
185
  }
@@ -5,3 +5,36 @@ export * as operations from './operations.js';
5
5
  export * as projects from './projects.js';
6
6
  export * as resources from './resources.js';
7
7
  export * as stacks from './stacks.js';
8
+ import type { Stack } from '../../utils/types.js';
9
+ import type { BlueprintParserError } from '../../utils/types.js';
10
+ import { readLocalBlueprint } from './blueprint.js';
11
+ export type BlueprintIssue = {
12
+ code: 'NO_STACK_ID' | 'NO_PROJECT_ID' | 'NO_STACK' | 'PARSE_ERROR';
13
+ message: string;
14
+ errors?: BlueprintParserError[];
15
+ };
16
+ type LocalBlueprintWithIds = Awaited<ReturnType<typeof readLocalBlueprint>> & {
17
+ projectId: string;
18
+ stackId: string;
19
+ };
20
+ type BlueprintSuccess = {
21
+ localBlueprint: LocalBlueprintWithIds;
22
+ deployedStack: Stack;
23
+ issues?: never;
24
+ };
25
+ type BlueprintFailure = {
26
+ localBlueprint: Awaited<ReturnType<typeof readLocalBlueprint>>;
27
+ issues: BlueprintIssue[];
28
+ deployedStack?: never;
29
+ };
30
+ type BlueprintResult = BlueprintSuccess | BlueprintFailure;
31
+ /**
32
+ * Get the local Blueprint and deployed Stack
33
+ * @param blueprintPath - The path of the Blueprint file or directory- will search up the directory tree if not provided
34
+ * @param token - The Sanity API token
35
+ * @returns The local Blueprint and deployed Stack
36
+ */
37
+ export declare function getBlueprintAndStack({ token, blueprintPath, }: {
38
+ token: string;
39
+ blueprintPath?: string;
40
+ }): Promise<BlueprintResult>;
@@ -5,3 +5,35 @@ export * as operations from './operations.js';
5
5
  export * as projects from './projects.js';
6
6
  export * as resources from './resources.js';
7
7
  export * as stacks from './stacks.js';
8
+ import { readLocalBlueprint } from './blueprint.js';
9
+ import { getStack } from './stacks.js';
10
+ /**
11
+ * Get the local Blueprint and deployed Stack
12
+ * @param blueprintPath - The path of the Blueprint file or directory- will search up the directory tree if not provided
13
+ * @param token - The Sanity API token
14
+ * @returns The local Blueprint and deployed Stack
15
+ */
16
+ export async function getBlueprintAndStack({ token, blueprintPath, }) {
17
+ if (!token)
18
+ throw new Error('Auth is required');
19
+ const localBlueprint = await readLocalBlueprint(blueprintPath);
20
+ const { projectId, stackId, errors } = localBlueprint;
21
+ const issues = [];
22
+ if (errors.length > 0) {
23
+ issues.push({ code: 'PARSE_ERROR', message: 'Blueprint parse errors', errors });
24
+ }
25
+ if (!(stackId && projectId)) {
26
+ if (!projectId)
27
+ issues.push({ code: 'NO_PROJECT_ID', message: 'Project ID not found' });
28
+ if (!stackId)
29
+ issues.push({ code: 'NO_STACK_ID', message: 'Stack ID not found' });
30
+ return { localBlueprint, issues };
31
+ }
32
+ const auth = { token, projectId };
33
+ const { error, ok, stack: deployedStack } = await getStack({ stackId, auth });
34
+ if (!ok) {
35
+ issues.push({ code: 'NO_STACK', message: error || 'Unknown error' });
36
+ return { localBlueprint, issues };
37
+ }
38
+ return { localBlueprint: localBlueprint, deployedStack };
39
+ }
@@ -18,4 +18,13 @@ interface ListProjectsResponse {
18
18
  export declare function listProjects({ token }: {
19
19
  token: string;
20
20
  }): Promise<ListProjectsResponse>;
21
+ interface GetProjectResponse {
22
+ ok: boolean;
23
+ error: string | null;
24
+ project: Project;
25
+ }
26
+ export declare function getProject({ token, projectId, }: {
27
+ token: string;
28
+ projectId: string;
29
+ }): Promise<GetProjectResponse>;
21
30
  export {};
@@ -16,3 +16,15 @@ export async function listProjects({ token }) {
16
16
  projects,
17
17
  };
18
18
  }
19
+ export async function getProject({ token, projectId, }) {
20
+ const response = await fetch(`${projectsUrl}/${projectId}`, {
21
+ method: 'GET',
22
+ headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
23
+ });
24
+ const project = await response.json();
25
+ return {
26
+ ok: response.ok,
27
+ error: response.ok ? null : project.error?.message,
28
+ project,
29
+ };
30
+ }