@sanity/runtime-cli 2.3.0 → 2.5.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.
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.3.0 linux-x64 node-v22.14.0
23
+ @sanity/runtime-cli/2.5.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.3.0/src/commands/blueprints/add.ts)_
66
+ _See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/blueprints/config.ts)_
88
+ _See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/blueprints/deploy.ts)_
105
+ _See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/blueprints/info.ts)_
127
+ _See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/blueprints/init.ts)_
144
+ _See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/blueprints/logs.ts)_
166
+ _See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/blueprints/plan.ts)_
183
+ _See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/blueprints/stacks.ts)_
200
+ _See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/functions/dev.ts)_
220
+ _See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/functions/env/add.ts)_
242
+ _See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/functions/env/remove.ts)_
263
+ _See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/functions/invoke.ts)_
289
+ _See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.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.3.0/src/commands/functions/logs.ts)_
317
+ _See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.0/src/commands/functions/logs.ts)_
318
318
 
319
319
  ## `sanity-run functions test NAME`
320
320
 
@@ -343,7 +343,7 @@ EXAMPLES
343
343
  $ sanity-run functions test <name> --data '{ "id": 1 }' --timeout 60
344
344
  ```
345
345
 
346
- _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v2.3.0/src/commands/functions/test.ts)_
346
+ _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v2.5.0/src/commands/functions/test.ts)_
347
347
 
348
348
  ## `sanity-run help [COMMAND]`
349
349
 
@@ -1,14 +1,10 @@
1
- import type { AuthParams, BlueprintResource } from '../../utils/types.js';
1
+ import type { AuthParams, LocalFunctionResource } from '../../utils/types.js';
2
2
  export declare const stashUrl: string;
3
3
  export declare function stashAsset({ resource, auth, }: {
4
- resource: BlueprintResource;
4
+ resource: LocalFunctionResource;
5
5
  auth: AuthParams;
6
6
  }): Promise<{
7
7
  success: boolean;
8
- assetId: any;
9
- error?: undefined;
10
- } | {
11
- success: boolean;
12
- error: any;
13
- assetId?: undefined;
8
+ assetId?: string;
9
+ error?: string;
14
10
  }>;
@@ -1,17 +1,25 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { cwd } from 'node:process';
4
- import JSZip from 'jszip';
4
+ import AdmZip from 'adm-zip';
5
5
  import config from '../../config.js';
6
6
  import getHeaders from '../../utils/get-headers.js';
7
7
  const { blueprints } = config.server;
8
8
  export const stashUrl = `${blueprints}vX/blueprints/assets/stash`;
9
9
  export async function stashAsset({ resource, auth, }) {
10
+ if (!resource.src)
11
+ throw new Error('Resource src is required');
10
12
  try {
11
- const source = await fs.readFileSync(path.join(cwd(), resource.src), 'utf8');
12
- const zip = new JSZip();
13
- zip.file('index.js', source);
14
- const zipBuffer = await zip.generateAsync({ type: 'nodebuffer' });
13
+ const sourcePath = path.join(cwd(), resource.src);
14
+ const stats = await fs.promises.stat(sourcePath);
15
+ const zip = new AdmZip();
16
+ if (stats.isDirectory()) {
17
+ zip.addLocalFolder(sourcePath);
18
+ }
19
+ else {
20
+ zip.addLocalFile(sourcePath, '', 'index.js');
21
+ }
22
+ const zipBuffer = zip.toBuffer();
15
23
  const base64Zip = zipBuffer.toString('base64');
16
24
  const assetResponse = await fetch(stashUrl, {
17
25
  method: 'POST',
@@ -29,9 +37,8 @@ export async function stashAsset({ resource, auth, }) {
29
37
  }
30
38
  catch (err) {
31
39
  let error = '';
32
- if (err instanceof Error) {
40
+ if (err instanceof Error)
33
41
  error = err.message;
34
- }
35
42
  return { success: false, error };
36
43
  }
37
44
  }
@@ -1,9 +1,14 @@
1
- import type { Blueprint, BlueprintError, BlueprintStack } from '../../utils/types.js';
1
+ import type { BlueprintParserError, LocalBlueprint, LocalResource, Stack } from '../../utils/types.js';
2
2
  export declare const DEFAULT_BLUEPRINT_CONTENT: {
3
3
  blueprintVersion: string;
4
4
  resources: never[];
5
5
  };
6
- export declare function findBlueprintFile(blueprintPath?: string): {
6
+ /**
7
+ * Finds the blueprint file in the given path or current working directory
8
+ * @param blueprintFilePath - The full path of the blueprint file
9
+ * @returns The path, file name, and extension of the blueprint file
10
+ */
11
+ export declare function findBlueprintFile(blueprintFilePath?: string): {
7
12
  path: string;
8
13
  fileName: string;
9
14
  extension: string;
@@ -18,11 +23,11 @@ export declare function readBlueprintOnDisk({ blueprintPath, getStack, token, }?
18
23
  fileName: string;
19
24
  extension: string;
20
25
  };
21
- parsedBlueprint: Blueprint;
22
- errors: BlueprintError[];
26
+ parsedBlueprint: LocalBlueprint;
27
+ errors: BlueprintParserError[];
23
28
  projectId?: string;
24
29
  stackId?: string;
25
- deployedStack?: BlueprintStack;
30
+ deployedStack?: Stack;
26
31
  }>;
27
32
  export declare function writeBlueprintToDisk({ path, fileType, content, }: {
28
33
  path: string;
@@ -38,7 +43,7 @@ export declare function writeConfigFile({ blueprintPath, projectId, stackId, }:
38
43
  projectId: string;
39
44
  stackId?: string;
40
45
  }): void;
41
- export declare function addResourceToBlueprint({ blueprintPath, resource, }: {
42
- blueprintPath?: string;
43
- resource: Record<string, unknown>;
44
- }): Record<string, unknown> | undefined;
46
+ export declare function addResourceToBlueprint({ blueprintFilePath, resource, }: {
47
+ blueprintFilePath?: string;
48
+ resource: LocalResource;
49
+ }): LocalResource | undefined;
@@ -2,7 +2,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
2
  import { createRequire } from 'node:module';
3
3
  import { dirname, extname, join } from 'node:path';
4
4
  import { cwd } from 'node:process';
5
- import { BlueprintErrorType } from '../../utils/types.js';
5
+ import { BlueprintParserErrorType } from '../../utils/types.js';
6
6
  import { blueprintParserValidator } from '../../utils/vendor/parser-validator.js';
7
7
  import { getStack as getStackById } from './stacks.js';
8
8
  const SUPPORTED_FILE_NAMES_IN_PRIORITY_ORDER = [
@@ -16,12 +16,21 @@ export const DEFAULT_BLUEPRINT_CONTENT = {
16
16
  blueprintVersion: '2024-10-01',
17
17
  resources: [],
18
18
  };
19
- export function findBlueprintFile(blueprintPath) {
20
- if (blueprintPath) {
21
- if (existsSync(blueprintPath)) {
22
- return { path: blueprintPath, fileName: blueprintPath, extension: extname(blueprintPath) };
23
- }
24
- throw Error(`Blueprint file not found: ${blueprintPath}`);
19
+ /**
20
+ * Finds the blueprint file in the given path or current working directory
21
+ * @param blueprintFilePath - The full path of the blueprint file
22
+ * @returns The path, file name, and extension of the blueprint file
23
+ */
24
+ export function findBlueprintFile(blueprintFilePath) {
25
+ if (blueprintFilePath) {
26
+ if (existsSync(blueprintFilePath)) {
27
+ return {
28
+ path: blueprintFilePath,
29
+ fileName: blueprintFilePath,
30
+ extension: extname(blueprintFilePath),
31
+ };
32
+ }
33
+ throw Error(`Blueprint file not found: ${blueprintFilePath}`);
25
34
  }
26
35
  for (const fileName of SUPPORTED_FILE_NAMES_IN_PRIORITY_ORDER) {
27
36
  const filePath = join(cwd(), fileName);
@@ -120,7 +129,7 @@ export async function readBlueprintOnDisk({ blueprintPath, getStack, token, } =
120
129
  if (!stack) {
121
130
  errors.push({
122
131
  message: 'Stack not found',
123
- type: BlueprintErrorType.InvalidStack,
132
+ type: BlueprintParserErrorType.InvalidStack,
124
133
  });
125
134
  }
126
135
  deployedStack = stack;
@@ -201,8 +210,8 @@ export function writeConfigFile({ blueprintPath, projectId, stackId, }) {
201
210
  config.stackId = stackId;
202
211
  writeFileSync(configPath, JSON.stringify(config, null, 2));
203
212
  }
204
- export function addResourceToBlueprint({ blueprintPath, resource, }) {
205
- const blueprintFile = findBlueprintFile(blueprintPath);
213
+ export function addResourceToBlueprint({ blueprintFilePath, resource, }) {
214
+ const blueprintFile = findBlueprintFile(blueprintFilePath);
206
215
  if (!blueprintFile)
207
216
  throw Error('Could not find Blueprint file');
208
217
  const { path, extension } = blueprintFile;
@@ -1,4 +1,4 @@
1
- import type { AuthParams, BlueprintOperation } from '../../utils/types.js';
1
+ import type { AuthParams, StackOperation } from '../../utils/types.js';
2
2
  export declare const stacksUrl: string;
3
3
  export declare function getOperation({ stackId, operationId, auth, }: {
4
4
  stackId: string;
@@ -7,7 +7,7 @@ export declare function getOperation({ stackId, operationId, auth, }: {
7
7
  }): Promise<{
8
8
  ok: boolean;
9
9
  error: string | null;
10
- operation: BlueprintOperation | null;
10
+ operation: StackOperation | null;
11
11
  }>;
12
12
  export declare function listOperations({ stackId, auth, }: {
13
13
  stackId: string;
@@ -15,5 +15,5 @@ export declare function listOperations({ stackId, auth, }: {
15
15
  }): Promise<{
16
16
  ok: boolean;
17
17
  error: string | null;
18
- operations: BlueprintOperation[];
18
+ operations: StackOperation[];
19
19
  }>;
@@ -1,7 +1,9 @@
1
- export interface FunctionResourceOptions {
1
+ import type { LocalFunctionResource } from '../../utils/types.js';
2
+ interface FunctionResourceOptions {
2
3
  name: string;
3
4
  type: string;
4
5
  displayName?: string;
6
+ blueprintFilePath?: string;
5
7
  }
6
8
  /**
7
9
  * Creates a new function resource file and adds it to the blueprint
@@ -9,5 +11,6 @@ export interface FunctionResourceOptions {
9
11
  export declare function createFunctionResource(options: FunctionResourceOptions): {
10
12
  filePath: string;
11
13
  resourceAdded: boolean;
12
- resource: Record<string, unknown>;
14
+ resource: LocalFunctionResource;
13
15
  };
16
+ export {};
@@ -1,36 +1,57 @@
1
1
  import { mkdirSync, writeFileSync } from 'node:fs';
2
2
  import { existsSync } from 'node:fs';
3
- import { join } from 'node:path';
3
+ import { dirname, join } from 'node:path';
4
4
  import { cwd } from 'node:process';
5
5
  import { addResourceToBlueprint } from './blueprint.js';
6
+ const DEFAULT_FUNCTION_TEMPLATE = /*js*/ `export async function handler({context, event}) {
7
+ const time = new Date().toLocaleTimeString()
8
+ console.log(\`👋 Your Sanity Function was called at \${time}\`)
9
+ }`;
10
+ const DEFAULT_PACKAGE_JSON = `{
11
+ "name": "<<NAME>>",
12
+ "type": "module",
13
+ "main": "index.js"
14
+ }`;
6
15
  /**
7
16
  * Creates a new function resource file and adds it to the blueprint
8
17
  */
9
18
  export function createFunctionResource(options) {
10
- const { name, type, displayName = name } = options;
19
+ const { name, type, displayName = name, blueprintFilePath } = options;
20
+ let workingDir = cwd();
21
+ if (blueprintFilePath) {
22
+ if (!existsSync(blueprintFilePath)) {
23
+ throw Error(`Blueprint file not found: ${blueprintFilePath}`);
24
+ }
25
+ workingDir = dirname(blueprintFilePath);
26
+ }
11
27
  // Ensure functions directory exists
12
- const functionsDir = join(cwd(), 'functions');
28
+ const functionsDir = join(workingDir, 'functions');
13
29
  if (!existsSync(functionsDir)) {
14
30
  mkdirSync(functionsDir, { recursive: true });
15
31
  }
16
- // Create function file with default template
17
- const functionPath = join(functionsDir, `${name}.js`);
18
- const functionContent = `export async function handler (event) {
19
- console.log(event)
20
- return {event}
21
- }`;
22
- writeFileSync(functionPath, functionContent);
32
+ // Create function directory
33
+ const functionDir = join(functionsDir, name);
34
+ if (!existsSync(functionDir)) {
35
+ mkdirSync(functionDir, { recursive: true });
36
+ }
37
+ // Create index.js with default template
38
+ const indexPath = join(functionDir, 'index.js');
39
+ writeFileSync(indexPath, DEFAULT_FUNCTION_TEMPLATE);
40
+ // Create package.json
41
+ const packagePath = join(functionDir, 'package.json');
42
+ const packageContent = DEFAULT_PACKAGE_JSON.replace('<<NAME>>', name);
43
+ writeFileSync(packagePath, packageContent);
23
44
  // Create resource definition
24
45
  const resourceJson = {
25
46
  displayName,
26
47
  name,
27
48
  type: `sanity.function.${type}`,
28
- src: `functions/${name}.js`,
49
+ src: `functions/${name}`,
29
50
  };
30
51
  // Add to blueprint or return for manual addition
31
- const resource = addResourceToBlueprint({ resource: resourceJson });
52
+ const resource = addResourceToBlueprint({ blueprintFilePath, resource: resourceJson });
32
53
  return {
33
- filePath: functionPath,
54
+ filePath: indexPath,
34
55
  resourceAdded: !resource, // If resource is null, it was added to blueprint
35
56
  resource: resource || resourceJson,
36
57
  };
@@ -1,4 +1,4 @@
1
- import type { AuthParams, BlueprintJob } from '../../utils/types.js';
1
+ import type { AuthParams, StackPayload } from '../../utils/types.js';
2
2
  export declare const stacksUrl: string;
3
3
  export declare function listStacks(auth: AuthParams): Promise<{
4
4
  ok: boolean;
@@ -30,17 +30,17 @@ export declare function getStack({ stackId, auth }: {
30
30
  error: any;
31
31
  stack: any;
32
32
  }>;
33
- export declare function createStack({ blueprint, auth }: {
34
- blueprint: BlueprintJob;
33
+ export declare function createStack({ stackPayload, auth, }: {
34
+ stackPayload: StackPayload;
35
35
  auth: AuthParams;
36
36
  }): Promise<{
37
37
  ok: boolean;
38
38
  error: any;
39
39
  stack: any;
40
40
  }>;
41
- export declare function updateStack({ stackId, blueprint, auth, }: {
41
+ export declare function updateStack({ stackId, stackPayload, auth, }: {
42
42
  stackId: string;
43
- blueprint: BlueprintJob;
43
+ stackPayload: StackPayload;
44
44
  auth: AuthParams;
45
45
  }): Promise<{
46
46
  ok: boolean;
@@ -55,11 +55,11 @@ export async function getStack({ stackId, auth }) {
55
55
  stack,
56
56
  };
57
57
  }
58
- export async function createStack({ blueprint, auth }) {
58
+ export async function createStack({ stackPayload, auth, }) {
59
59
  const response = await fetch(stacksUrl, {
60
60
  method: 'POST',
61
61
  headers: getHeaders(auth),
62
- body: JSON.stringify(blueprint),
62
+ body: JSON.stringify(stackPayload),
63
63
  });
64
64
  const stack = await response.json();
65
65
  return {
@@ -68,11 +68,11 @@ export async function createStack({ blueprint, auth }) {
68
68
  stack,
69
69
  };
70
70
  }
71
- export async function updateStack({ stackId, blueprint, auth, }) {
71
+ export async function updateStack({ stackId, stackPayload, auth, }) {
72
72
  const response = await fetch(`${stacksUrl}/${stackId}`, {
73
73
  method: 'PUT',
74
74
  headers: getHeaders(auth),
75
- body: JSON.stringify(blueprint),
75
+ body: JSON.stringify(stackPayload),
76
76
  });
77
77
  const stack = await response.json();
78
78
  return {
@@ -1,5 +1,5 @@
1
- import type { AuthParams, PayloadOptions } from '../../utils/types.js';
2
- export declare function invoke(id: string, options: PayloadOptions, auth: AuthParams): Promise<{
1
+ import type { AuthParams, InvokePayloadOptions } from '../../utils/types.js';
2
+ export declare function invoke(id: string, options: InvokePayloadOptions, auth: AuthParams): Promise<{
3
3
  ok: boolean;
4
4
  error: any;
5
5
  json: any;
@@ -1,2 +1,2 @@
1
- import type { InvocationResponse, PayloadOptions } from '../../utils/types.js';
2
- export declare function testAction(srcPath: string, options: PayloadOptions): Promise<InvocationResponse>;
1
+ import type { InvocationResponse, InvokePayloadOptions } from '../../utils/types.js';
2
+ export declare function testAction(srcPath: string, options: InvokePayloadOptions): Promise<InvocationResponse>;
@@ -49,14 +49,14 @@ export default class Deploy extends Command {
49
49
  }
50
50
  if (!name)
51
51
  this.error('Stack name is required');
52
- const validResources = resources.filter((r) => r.type);
53
- const functionResources = validResources.filter((r) => r.type.startsWith('sanity.function.'));
52
+ const validResources = resources?.filter((r) => r.type);
53
+ const functionResources = validResources?.filter((r) => r.type.startsWith('sanity.function.'));
54
54
  // First stash all function assets
55
- if (functionResources.length > 0) {
55
+ if (functionResources?.length) {
56
56
  for (const resource of functionResources) {
57
57
  const fnSpinner = Spinner({ text: `Processing ${resource.name}...` }).start();
58
58
  const result = await stashAsset({ resource, auth });
59
- if (result.success) {
59
+ if (result.success && result.assetId) {
60
60
  const src = resource.src;
61
61
  resource.src = result.assetId; // TODO: properly reference asset - for now, the API expects the assetId
62
62
  fnSpinner.success(`${resource.name} <${yellow(result.assetId)}>`);
@@ -69,16 +69,16 @@ export default class Deploy extends Command {
69
69
  }
70
70
  }
71
71
  }
72
- const blueprint = {
72
+ const stackPayload = {
73
73
  name,
74
74
  projectId,
75
75
  document: { resources: validResources },
76
76
  };
77
- this.debug('BLUEPRINT DOCUMENT:', blueprint);
77
+ this.debug('BLUEPRINT DOCUMENT:', stackPayload);
78
78
  const spinner = Spinner({ text: 'Deploying stack...' }).start();
79
79
  const { ok: deployOk, stack, error: deployError, } = deployedStack
80
- ? await updateStack({ stackId: deployedStack.id, blueprint, auth })
81
- : await createStack({ blueprint, auth });
80
+ ? await updateStack({ stackId: deployedStack.id, stackPayload, auth })
81
+ : await createStack({ stackPayload, auth });
82
82
  this.debug('STACK RESPONSE:', stack);
83
83
  if (deployOk) {
84
84
  spinner.success(`${green('Success!')} Stack "${bold(stack.name)}" ${deployedStack ? 'updated' : 'created'} <${yellow(stack.id)}>`);
@@ -1,7 +1,7 @@
1
1
  import { Args, Command, Flags } from '@oclif/core';
2
2
  import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
3
3
  import { testAction } from '../../actions/functions/test.js';
4
- import { findFunctionByName } from '../../utils/find-function.js';
4
+ import { getFunctionSource } from '../../utils/find-function.js';
5
5
  export default class Test extends Command {
6
6
  static args = {
7
7
  name: Args.string({ description: 'The name of the Sanity Function', required: true }),
@@ -29,8 +29,11 @@ export default class Test extends Command {
29
29
  const { args, flags } = await this.parse(Test);
30
30
  const { parsedBlueprint } = await readBlueprintOnDisk({ getStack: false });
31
31
  try {
32
- const func = findFunctionByName(parsedBlueprint, args.name);
33
- const { json, logs, error } = await testAction(func.src, {
32
+ const src = getFunctionSource(parsedBlueprint, args.name);
33
+ if (!src) {
34
+ this.error(`Error: Function ${args.name} has no source code`);
35
+ }
36
+ const { json, logs, error } = await testAction(src, {
34
37
  data: flags.data,
35
38
  file: flags.file,
36
39
  timeout: flags.timeout,
@@ -877,7 +877,7 @@ declare class SelectionRange {
877
877
  the character before its position, 1 the character after, and 0
878
878
  means no association.
879
879
  */
880
- get assoc(): 0 | 1 | -1;
880
+ get assoc(): 1 | 0 | -1;
881
881
  /**
882
882
  The bidirectional text level associated with this cursor, if
883
883
  any.
@@ -1274,8 +1274,8 @@ declare class FacetProvider {
1274
1274
  id: number;
1275
1275
  dynamicSlot(addresses: any): {
1276
1276
  create(state: any): number;
1277
- update(state: any, tr: any): 0 | 1;
1278
- reconfigure: (state: any, oldState: any) => 0 | 1;
1277
+ update(state: any, tr: any): 1 | 0;
1278
+ reconfigure: (state: any, oldState: any) => 1 | 0;
1279
1279
  };
1280
1280
  }
1281
1281
  declare class PrecExtension {
@@ -1,2 +1,2 @@
1
- import type { PayloadOptions } from './types.js';
2
- export default function buildPayload(options: PayloadOptions): object | null;
1
+ import type { InvokePayloadOptions } from './types.js';
2
+ export default function buildPayload(options: InvokePayloadOptions): object | null;
@@ -1,5 +1,5 @@
1
- import type { BlueprintResource, BlueprintStack } from '../types.js';
1
+ import type { LocalBlueprint, LocalResource, Stack, StackResource } from '../types.js';
2
2
  export declare function formatTitle(title: string, name: string): string;
3
- export declare function formatResourceTree(resources: BlueprintResource[]): string;
4
- export declare function formatStackInfo(stack: BlueprintStack, isCurrentStack?: boolean): string;
5
- export declare function formatStacksListing(stacks: BlueprintStack[], currentStackId?: string): string;
3
+ export declare function formatResourceTree(resources: StackResource[] | LocalResource[] | undefined): string;
4
+ export declare function formatStackInfo(stack: Stack | LocalBlueprint, isCurrentStack?: boolean): string;
5
+ export declare function formatStacksListing(stacks: Stack[], currentStackId?: string): string;
@@ -28,7 +28,7 @@ export function formatResourceTree(resources) {
28
28
  const otherOutput = [];
29
29
  otherOutput.push(`${bold('Other Resources')} [${otherResources.length}]`);
30
30
  const otherResourcesOutput = otherResources.map((other) => {
31
- return `"${yellow(other.displayName || other.name || other.src)}"`;
31
+ return `"${yellow(other.displayName ?? other.name ?? 'unnamed')}"`;
32
32
  });
33
33
  otherOutput.push(otherResourcesOutput);
34
34
  output.push(otherOutput);
@@ -37,31 +37,41 @@ export function formatResourceTree(resources) {
37
37
  }
38
38
  export function formatStackInfo(stack, isCurrentStack = false) {
39
39
  const output = [];
40
- const stackName = isCurrentStack ? boldnblue(stack.name) : bold(stack.name);
41
- output.push(`"${stackName}" <${yellow(stack.id)}>${isCurrentStack ? ' (current)' : ''}`);
40
+ const isStack = 'id' in stack;
41
+ if (isStack) {
42
+ const stackName = isCurrentStack ? boldnblue(stack.name) : bold(stack.name);
43
+ output.push(`"${stackName}" <${yellow(stack.id)}>${isCurrentStack ? ' (current)' : ''}`);
44
+ }
45
+ else {
46
+ output.push('Local Blueprint');
47
+ }
42
48
  const infoOutput = [];
43
- infoOutput.push(`${stack.resources.length} resource${stack.resources.length === 1 ? '' : 's'}`);
44
- if (stack.createdAt)
45
- infoOutput.push(`Created: ${formatDate(stack.createdAt)}`);
46
- if (stack.updatedAt)
47
- infoOutput.push(`Updated: ${formatDate(stack.updatedAt)}`);
48
- if (stack.recentOperation) {
49
- const operation = stack.recentOperation;
50
- const operationOutput = [];
51
- if (operation.id)
52
- operationOutput.push(`Recent Operation <${yellow(operation.id)}>:`);
53
- if (operation.status) {
54
- const operationColor = operation.status === 'COMPLETED' ? green : red;
55
- const status = operation.status || 'UNKNOWN';
56
- operationOutput.push(`Status: ${operationColor(status)}`);
57
- }
58
- if (operation.createdAt)
59
- operationOutput.push(`Started: ${formatDate(operation.createdAt)}`);
60
- if (operation.status === 'COMPLETED' && operation.completedAt && operation.createdAt) {
61
- operationOutput.push(`Completed: ${formatDate(operation.completedAt)}`);
62
- operationOutput.push(`Duration: ${yellow(formatDuration(operation.createdAt, operation.completedAt))}`);
49
+ if (stack.resources) {
50
+ infoOutput.push(`${stack.resources.length} resource${stack.resources.length === 1 ? '' : 's'}`);
51
+ }
52
+ if (isStack) {
53
+ if (stack.createdAt)
54
+ infoOutput.push(`Created: ${formatDate(stack.createdAt)}`);
55
+ if (stack.updatedAt)
56
+ infoOutput.push(`Updated: ${formatDate(stack.updatedAt)}`);
57
+ if (stack.recentOperation) {
58
+ const operation = stack.recentOperation;
59
+ const operationOutput = [];
60
+ if (operation.id)
61
+ operationOutput.push(`Recent Operation <${yellow(operation.id)}>:`);
62
+ if (operation.status) {
63
+ const operationColor = operation.status === 'COMPLETED' ? green : red;
64
+ const status = operation.status || 'UNKNOWN';
65
+ operationOutput.push(`Status: ${operationColor(status)}`);
66
+ }
67
+ if (operation.createdAt)
68
+ operationOutput.push(`Started: ${formatDate(operation.createdAt)}`);
69
+ if (operation.status === 'COMPLETED' && operation.completedAt && operation.createdAt) {
70
+ operationOutput.push(`Completed: ${formatDate(operation.completedAt)}`);
71
+ operationOutput.push(`Duration: ${yellow(formatDuration(operation.createdAt, operation.completedAt))}`);
72
+ }
73
+ infoOutput.push(operationOutput);
63
74
  }
64
- infoOutput.push(operationOutput);
65
75
  }
66
76
  output.push(infoOutput);
67
77
  return treeify(output, { plain: true });
@@ -1,2 +1,3 @@
1
- import type { Blueprint, BlueprintResource, BlueprintStack } from './types.js';
2
- export declare function findFunctionByName(blueprint: Blueprint | BlueprintStack, name: string): BlueprintResource;
1
+ import type { LocalBlueprint, LocalFunctionResource, Stack, StackFunctionResource } from './types.js';
2
+ export declare function findFunctionByName(blueprintOrStack: LocalBlueprint | Stack, name: string): LocalFunctionResource | StackFunctionResource;
3
+ export declare function getFunctionSource(blueprintOrStack: LocalBlueprint | Stack, name: string): string;
@@ -1,6 +1,26 @@
1
- export function findFunctionByName(blueprint, name) {
2
- const func = blueprint?.resources.find((r) => r?.type?.startsWith('sanity.function.') && r.name === name);
1
+ import { existsSync, statSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ export function findFunctionByName(blueprintOrStack, name) {
4
+ const func = blueprintOrStack?.resources?.find((r) => r?.type?.startsWith('sanity.function.') && r.name === name);
3
5
  if (!func)
4
6
  throw Error(`Unable to find function ${name}`);
5
7
  return func;
6
8
  }
9
+ export function getFunctionSource(blueprintOrStack, name) {
10
+ const func = findFunctionByName(blueprintOrStack, name);
11
+ const src = 'parameters' in func
12
+ ? func.parameters.src
13
+ : func.src;
14
+ if (!src)
15
+ throw Error(`Function ${name} has no source code`);
16
+ if (!existsSync(src))
17
+ throw Error(`Function source not found: ${src}`);
18
+ if (statSync(src).isDirectory()) {
19
+ const indexPath = join(src, 'index.js');
20
+ if (!existsSync(indexPath)) {
21
+ throw Error(`Function directory ${src} has no index.js`);
22
+ }
23
+ return indexPath;
24
+ }
25
+ return src;
26
+ }
@@ -1,4 +1,4 @@
1
- import type { BlueprintError } from './types.js';
1
+ import type { BlueprintParserError } from './types.js';
2
2
  export default function formatError(response: Response, json: {
3
- error: BlueprintError;
3
+ error: BlueprintParserError;
4
4
  }): string | null;
@@ -1,3 +1,4 @@
1
+ import * as findFunctions from './find-function.js';
1
2
  import * as types from './types.js';
2
3
  export declare const utils: {
3
4
  display: {
@@ -6,8 +7,9 @@ export declare const utils: {
6
7
  colors: typeof import("./display/colors.js");
7
8
  dates: typeof import("./display/dates.js");
8
9
  };
10
+ findFunctions: typeof findFunctions;
11
+ types: typeof types;
9
12
  vendor: {
10
13
  blueprintParserValidator: typeof import("./vendor/parser-validator.js").blueprintParserValidator;
11
14
  };
12
- types: typeof types;
13
15
  };
@@ -1,8 +1,10 @@
1
1
  import { display } from './display/index.js';
2
+ import * as findFunctions from './find-function.js';
2
3
  import * as types from './types.js';
3
4
  import { vendor } from './vendor/index.js';
4
5
  export const utils = {
5
6
  display,
6
- vendor,
7
+ findFunctions,
7
8
  types,
9
+ vendor,
8
10
  };
@@ -1,74 +1,81 @@
1
1
  /** @internal */
2
- export interface PayloadOptions {
3
- data: string | undefined;
4
- file: string | undefined;
5
- timeout?: number | undefined;
6
- }
7
- /** @internal */
8
- export interface InvocationResponse {
9
- error: undefined | unknown;
10
- json: object | undefined;
11
- logs: string | undefined;
2
+ export interface AuthParams {
3
+ token: string;
4
+ projectId: string;
12
5
  }
13
- type LogFunction = (input: string) => void;
14
6
  /** @internal */
15
- export interface BlueprintsContext {
16
- log: LogFunction;
7
+ export interface LocalBlueprint {
8
+ blueprintVersion?: string;
9
+ values?: Record<string, unknown>;
10
+ outputs?: Array<Record<string, unknown>>;
11
+ resources?: Array<LocalResource>;
17
12
  }
18
13
  /** @internal */
19
- export interface Blueprint {
20
- blueprintVersion: string;
21
- values: Record<string, unknown>;
22
- outputs: Array<Record<string, unknown>>;
23
- resources: Array<BlueprintResource>;
14
+ export interface LocalResource {
15
+ name: string;
16
+ type: string;
17
+ displayName?: string;
18
+ [key: string]: unknown;
24
19
  }
25
20
  /** @internal */
26
- export interface BlueprintDocument {
27
- resources: Array<BlueprintResource>;
21
+ export interface LocalFunctionResource extends LocalResource {
22
+ src?: string;
23
+ memory?: number;
24
+ timeout?: number;
25
+ env?: Record<string, string>;
28
26
  }
29
- /** @internal */
30
- export interface BlueprintJob {
27
+ export interface Stack {
28
+ id: string;
31
29
  name: string;
30
+ displayName: string;
32
31
  projectId: string;
33
- document: BlueprintDocument;
32
+ resources: Array<StackResource>;
33
+ createdAt?: string;
34
+ updatedAt?: string;
35
+ recentOperation?: StackOperation;
34
36
  }
35
37
  /** @internal */
36
- export interface BlueprintResource {
38
+ export interface StackResource {
37
39
  id: string;
38
40
  displayName: string;
39
41
  name: string;
40
42
  type: string;
41
- src: string;
42
43
  externalId: string;
44
+ parameters?: Record<string, unknown>;
45
+ }
46
+ /** @internal */
47
+ export interface StackFunctionResource extends StackResource {
48
+ parameters: {
49
+ src: string;
50
+ memory?: number;
51
+ timeout?: number;
52
+ env?: Record<string, string>;
53
+ };
43
54
  }
44
55
  /** @internal */
45
- export interface BlueprintOperation {
56
+ export interface StackOperation {
46
57
  id: string;
47
58
  status: string;
48
59
  createdAt?: string;
49
60
  completedAt?: string;
50
61
  }
51
- export interface BlueprintStack {
52
- id: string;
62
+ /** @internal */
63
+ export interface StackPayload {
53
64
  name: string;
54
- displayName: string;
55
65
  projectId: string;
56
- resources: Array<BlueprintResource>;
57
- createdAt?: string;
58
- updatedAt?: string;
59
- recentOperation?: BlueprintOperation;
66
+ document: LocalBlueprint;
60
67
  }
61
68
  /** @internal */
62
- export declare enum BlueprintErrorType {
63
- MissingProject = "missing_project",
64
- MissingStack = "missing_stack",
65
- InvalidProperty = "invalid_property",
66
- InvalidStack = "invalid_stack"
69
+ export interface InvokePayloadOptions {
70
+ data?: string;
71
+ file?: string;
72
+ timeout?: number;
67
73
  }
68
74
  /** @internal */
69
- export interface BlueprintError {
70
- message: string;
71
- type: BlueprintErrorType;
75
+ export interface InvocationResponse {
76
+ error: undefined | unknown;
77
+ json: object | undefined;
78
+ logs: string | undefined;
72
79
  }
73
80
  /** @internal */
74
81
  export interface BlueprintLog {
@@ -78,15 +85,21 @@ export interface BlueprintLog {
78
85
  level: string;
79
86
  }
80
87
  /** @internal */
81
- export interface AuthParams {
82
- token: string;
83
- projectId: string;
84
- }
85
- /** @internal */
86
88
  export interface FunctionLog {
87
89
  time: string;
88
90
  requestId: string;
89
91
  level: string;
90
92
  message: string;
91
93
  }
92
- export {};
94
+ /** @internal */
95
+ export declare enum BlueprintParserErrorType {
96
+ MissingProject = "missing_project",
97
+ MissingStack = "missing_stack",
98
+ InvalidProperty = "invalid_property",
99
+ InvalidStack = "invalid_stack"
100
+ }
101
+ /** @internal */
102
+ export interface BlueprintParserError {
103
+ message: string;
104
+ type: BlueprintParserErrorType;
105
+ }
@@ -1,8 +1,8 @@
1
1
  /** @internal */
2
- export var BlueprintErrorType;
3
- (function (BlueprintErrorType) {
4
- BlueprintErrorType["MissingProject"] = "missing_project";
5
- BlueprintErrorType["MissingStack"] = "missing_stack";
6
- BlueprintErrorType["InvalidProperty"] = "invalid_property";
7
- BlueprintErrorType["InvalidStack"] = "invalid_stack";
8
- })(BlueprintErrorType || (BlueprintErrorType = {}));
2
+ export var BlueprintParserErrorType;
3
+ (function (BlueprintParserErrorType) {
4
+ BlueprintParserErrorType["MissingProject"] = "missing_project";
5
+ BlueprintParserErrorType["MissingStack"] = "missing_stack";
6
+ BlueprintParserErrorType["InvalidProperty"] = "invalid_property";
7
+ BlueprintParserErrorType["InvalidStack"] = "invalid_stack";
8
+ })(BlueprintParserErrorType || (BlueprintParserErrorType = {}));
@@ -502,5 +502,5 @@
502
502
  ]
503
503
  }
504
504
  },
505
- "version": "2.3.0"
505
+ "version": "2.5.0"
506
506
  }
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.3.0",
4
+ "version": "2.5.0",
5
5
  "author": "Sanity Runtime Team",
6
6
  "type": "module",
7
7
  "license": "MIT",
@@ -42,11 +42,11 @@
42
42
  "dependencies": {
43
43
  "@oclif/core": "^4.2.10",
44
44
  "@oclif/plugin-help": "^6.2.27",
45
+ "adm-zip": "^0.5.16",
45
46
  "array-treeify": "^0.1.3",
46
47
  "color-json": "^3.0.5",
47
48
  "eventsource": "^3.0.6",
48
49
  "inquirer": "^12.5.2",
49
- "jszip": "^3.10.1",
50
50
  "mime-types": "^3.0.1",
51
51
  "xdg-basedir": "^5.1.0",
52
52
  "yocto-spinner": "^0.2.1"
@@ -56,21 +56,21 @@
56
56
  "@codemirror/lang-json": "^6.0.1",
57
57
  "@codemirror/state": "^6.5.2",
58
58
  "@enhance/store": "^1.0.2",
59
- "@oclif/prettier-config": "^0.2.1",
60
59
  "@oclif/test": "^4.1.12",
61
60
  "@rollup/plugin-node-resolve": "^16.0.1",
61
+ "@types/adm-zip": "^0.5.7",
62
62
  "@types/mime-types": "^2.1.4",
63
63
  "@types/node": "18",
64
64
  "codemirror": "^6.0.1",
65
- "mentoss": "^0.9.1",
66
- "oclif": "^4.17.42",
65
+ "mentoss": "^0.9.2",
66
+ "oclif": "^4.17.43",
67
67
  "pretty-bytes": "^6.1.1",
68
68
  "pretty-ms": "^9.2.0",
69
69
  "rollup": "^4.39.0",
70
70
  "shx": "^0.4.0",
71
71
  "ts-node": "^10.9.2",
72
72
  "tsx": "^4.19.3",
73
- "typescript": "^5.8.2",
73
+ "typescript": "^5.8.3",
74
74
  "vitest": "3.1.1"
75
75
  },
76
76
  "oclif": {