@sanity/runtime-cli 2.2.0 → 2.4.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 +30 -22
- package/dist/actions/blueprints/assets.d.ts +4 -8
- package/dist/actions/blueprints/assets.js +14 -7
- package/dist/actions/blueprints/blueprint.d.ts +14 -9
- package/dist/actions/blueprints/blueprint.js +19 -10
- package/dist/actions/blueprints/operations.d.ts +3 -3
- package/dist/actions/blueprints/resources.d.ts +5 -2
- package/dist/actions/blueprints/resources.js +34 -13
- package/dist/actions/blueprints/stacks.d.ts +5 -5
- package/dist/actions/blueprints/stacks.js +4 -4
- package/dist/actions/functions/invoke.d.ts +2 -2
- package/dist/actions/functions/logs.d.ts +6 -1
- package/dist/actions/functions/logs.js +4 -2
- package/dist/actions/functions/test.d.ts +2 -2
- package/dist/commands/blueprints/deploy.js +8 -8
- package/dist/commands/functions/invoke.js +2 -2
- package/dist/commands/functions/logs.d.ts +4 -0
- package/dist/commands/functions/logs.js +37 -11
- package/dist/commands/functions/test.js +9 -6
- package/dist/server/static/vendor/vendor.bundle.d.ts +3 -3
- package/dist/utils/build-payload.d.ts +2 -2
- package/dist/utils/display/blueprints-formatting.d.ts +4 -4
- package/dist/utils/display/blueprints-formatting.js +34 -24
- package/dist/utils/find-function.d.ts +3 -2
- package/dist/utils/find-function.js +22 -2
- package/dist/utils/format-error.d.ts +4 -0
- package/dist/utils/format-error.js +9 -0
- package/dist/utils/types.d.ts +60 -47
- package/dist/utils/types.js +7 -7
- package/oclif.manifest.json +29 -8
- package/package.json +6 -6
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.
|
|
23
|
+
@sanity/runtime-cli/2.4.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.
|
|
66
|
+
_See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
88
|
+
_See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
105
|
+
_See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
127
|
+
_See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
144
|
+
_See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
166
|
+
_See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
183
|
+
_See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
200
|
+
_See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
220
|
+
_See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
242
|
+
_See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.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.
|
|
263
|
+
_See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.0/src/commands/functions/env/remove.ts)_
|
|
264
264
|
|
|
265
265
|
## `sanity-run functions invoke NAME`
|
|
266
266
|
|
|
@@ -281,12 +281,12 @@ DESCRIPTION
|
|
|
281
281
|
Invoke a remote Sanity Function
|
|
282
282
|
|
|
283
283
|
EXAMPLES
|
|
284
|
-
$ sanity-run functions invoke <
|
|
284
|
+
$ sanity-run functions invoke <name> --data '{ "id": 1 }'
|
|
285
285
|
|
|
286
|
-
$ sanity-run functions invoke <
|
|
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.
|
|
289
|
+
_See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.0/src/commands/functions/invoke.ts)_
|
|
290
290
|
|
|
291
291
|
## `sanity-run functions logs NAME`
|
|
292
292
|
|
|
@@ -294,19 +294,27 @@ Retrieve logs for a Sanity Function
|
|
|
294
294
|
|
|
295
295
|
```
|
|
296
296
|
USAGE
|
|
297
|
-
$ sanity-run functions logs NAME
|
|
297
|
+
$ sanity-run functions logs NAME [-l <value>] [-j]
|
|
298
298
|
|
|
299
299
|
ARGUMENTS
|
|
300
300
|
NAME The name of the Sanity Function
|
|
301
301
|
|
|
302
|
+
FLAGS
|
|
303
|
+
-j, --json Return logs in JSON format
|
|
304
|
+
-l, --limit=<value> [default: 50] Total number of log entries to retrieve
|
|
305
|
+
|
|
302
306
|
DESCRIPTION
|
|
303
307
|
Retrieve logs for a Sanity Function
|
|
304
308
|
|
|
305
309
|
EXAMPLES
|
|
306
|
-
$ sanity-run functions logs <
|
|
310
|
+
$ sanity-run functions logs <name>
|
|
311
|
+
|
|
312
|
+
$ sanity-run functions logs <name> --json
|
|
313
|
+
|
|
314
|
+
$ sanity-run functions logs <name> --limit 100
|
|
307
315
|
```
|
|
308
316
|
|
|
309
|
-
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
317
|
+
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.0/src/commands/functions/logs.ts)_
|
|
310
318
|
|
|
311
319
|
## `sanity-run functions test NAME`
|
|
312
320
|
|
|
@@ -328,14 +336,14 @@ DESCRIPTION
|
|
|
328
336
|
Invoke a local Sanity Function
|
|
329
337
|
|
|
330
338
|
EXAMPLES
|
|
331
|
-
$ sanity-run functions test
|
|
339
|
+
$ sanity-run functions test <name> --data '{ "id": 1 }'
|
|
332
340
|
|
|
333
|
-
$ sanity-run functions test
|
|
341
|
+
$ sanity-run functions test <name> --file 'payload.json'
|
|
334
342
|
|
|
335
|
-
$ sanity-run functions test
|
|
343
|
+
$ sanity-run functions test <name> --data '{ "id": 1 }' --timeout 60
|
|
336
344
|
```
|
|
337
345
|
|
|
338
|
-
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v2.
|
|
346
|
+
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v2.4.0/src/commands/functions/test.ts)_
|
|
339
347
|
|
|
340
348
|
## `sanity-run help [COMMAND]`
|
|
341
349
|
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import type { AuthParams,
|
|
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:
|
|
4
|
+
resource: LocalFunctionResource;
|
|
5
5
|
auth: AuthParams;
|
|
6
6
|
}): Promise<{
|
|
7
7
|
success: boolean;
|
|
8
|
-
assetId
|
|
9
|
-
error?:
|
|
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
|
|
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
|
|
12
|
-
const
|
|
13
|
-
zip
|
|
14
|
-
|
|
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 {
|
|
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
|
-
|
|
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:
|
|
22
|
-
errors:
|
|
26
|
+
parsedBlueprint: LocalBlueprint;
|
|
27
|
+
errors: BlueprintParserError[];
|
|
23
28
|
projectId?: string;
|
|
24
29
|
stackId?: string;
|
|
25
|
-
deployedStack?:
|
|
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({
|
|
42
|
-
|
|
43
|
-
resource:
|
|
44
|
-
}):
|
|
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 {
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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:
|
|
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({
|
|
205
|
-
const blueprintFile = findBlueprintFile(
|
|
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,
|
|
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:
|
|
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:
|
|
18
|
+
operations: StackOperation[];
|
|
19
19
|
}>;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
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:
|
|
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(
|
|
28
|
+
const functionsDir = join(workingDir, 'functions');
|
|
13
29
|
if (!existsSync(functionsDir)) {
|
|
14
30
|
mkdirSync(functionsDir, { recursive: true });
|
|
15
31
|
}
|
|
16
|
-
// Create function
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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}
|
|
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:
|
|
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,
|
|
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({
|
|
34
|
-
|
|
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,
|
|
41
|
+
export declare function updateStack({ stackId, stackPayload, auth, }: {
|
|
42
42
|
stackId: string;
|
|
43
|
-
|
|
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({
|
|
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(
|
|
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,
|
|
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(
|
|
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,
|
|
2
|
-
export declare function invoke(id: string, options:
|
|
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,6 +1,11 @@
|
|
|
1
1
|
import type { AuthParams } from '../../utils/types.js';
|
|
2
|
-
|
|
2
|
+
/** internal */
|
|
3
|
+
export interface LoggingOptions {
|
|
4
|
+
limit: number;
|
|
5
|
+
}
|
|
6
|
+
export declare function logs(id: string, options: LoggingOptions, auth: AuthParams): Promise<{
|
|
3
7
|
ok: boolean;
|
|
4
8
|
error: any;
|
|
5
9
|
logs: any;
|
|
10
|
+
total: any;
|
|
6
11
|
}>;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import config from '../../config.js';
|
|
2
2
|
import getHeaders from '../../utils/get-headers.js';
|
|
3
3
|
const { functions } = config.server;
|
|
4
|
-
export async function logs(id, auth) {
|
|
5
|
-
const
|
|
4
|
+
export async function logs(id, options, auth) {
|
|
5
|
+
const { limit } = options;
|
|
6
|
+
const response = await fetch(`${functions}vX/functions/${id}/logs?limit=${limit}`, {
|
|
6
7
|
headers: getHeaders(auth),
|
|
7
8
|
method: 'GET',
|
|
8
9
|
});
|
|
@@ -11,5 +12,6 @@ export async function logs(id, auth) {
|
|
|
11
12
|
ok: response.ok,
|
|
12
13
|
error: response.ok ? null : json?.error?.message,
|
|
13
14
|
logs: response.ok ? json.logs : [],
|
|
15
|
+
total: response.ok ? json.total : 0,
|
|
14
16
|
};
|
|
15
17
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { InvocationResponse,
|
|
2
|
-
export declare function testAction(srcPath: string, options:
|
|
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
|
|
53
|
-
const functionResources = validResources
|
|
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
|
|
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
|
|
72
|
+
const stackPayload = {
|
|
73
73
|
name,
|
|
74
74
|
projectId,
|
|
75
75
|
document: { resources: validResources },
|
|
76
76
|
};
|
|
77
|
-
this.debug('BLUEPRINT DOCUMENT:',
|
|
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,
|
|
81
|
-
: await createStack({
|
|
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)}>`);
|
|
@@ -11,8 +11,8 @@ export default class Invoke extends Command {
|
|
|
11
11
|
};
|
|
12
12
|
static description = 'Invoke a remote Sanity Function';
|
|
13
13
|
static examples = [
|
|
14
|
-
`<%= config.bin %> <%= command.id %> <
|
|
15
|
-
`<%= config.bin %> <%= command.id %> <
|
|
14
|
+
`<%= config.bin %> <%= command.id %> <name> --data '{ "id": 1 }'`,
|
|
15
|
+
`<%= config.bin %> <%= command.id %> <name> --file 'payload.json'`,
|
|
16
16
|
];
|
|
17
17
|
static flags = {
|
|
18
18
|
data: Flags.string({ char: 'd', description: 'Data to send to the function', required: false }),
|
|
@@ -5,5 +5,9 @@ export default class Logs extends Command {
|
|
|
5
5
|
};
|
|
6
6
|
static description: string;
|
|
7
7
|
static examples: string[];
|
|
8
|
+
static flags: {
|
|
9
|
+
limit: import("@oclif/core/interfaces").OptionFlag<number, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
json: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
11
|
+
};
|
|
8
12
|
run(): Promise<void>;
|
|
9
13
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Args, Command } from '@oclif/core';
|
|
1
|
+
import { Args, Command, Flags } from '@oclif/core';
|
|
2
2
|
import Spinner from 'yocto-spinner';
|
|
3
3
|
import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
|
|
4
4
|
import { logs as logsAction } from '../../actions/functions/logs.js';
|
|
@@ -10,7 +10,7 @@ function logLevel(level) {
|
|
|
10
10
|
if (level === 'ERROR') {
|
|
11
11
|
return red(level);
|
|
12
12
|
}
|
|
13
|
-
if (level === '
|
|
13
|
+
if (level === 'WARN') {
|
|
14
14
|
return yellow(level);
|
|
15
15
|
}
|
|
16
16
|
return green(level);
|
|
@@ -20,16 +20,33 @@ export default class Logs extends Command {
|
|
|
20
20
|
name: Args.string({ description: 'The name of the Sanity Function', required: true }),
|
|
21
21
|
};
|
|
22
22
|
static description = 'Retrieve logs for a Sanity Function';
|
|
23
|
-
static examples = [
|
|
23
|
+
static examples = [
|
|
24
|
+
'<%= config.bin %> <%= command.id %> <name>',
|
|
25
|
+
'<%= config.bin %> <%= command.id %> <name> --json',
|
|
26
|
+
'<%= config.bin %> <%= command.id %> <name> --limit 100',
|
|
27
|
+
];
|
|
28
|
+
static flags = {
|
|
29
|
+
limit: Flags.integer({
|
|
30
|
+
char: 'l',
|
|
31
|
+
description: 'Total number of log entries to retrieve',
|
|
32
|
+
required: false,
|
|
33
|
+
default: 50,
|
|
34
|
+
}),
|
|
35
|
+
json: Flags.boolean({
|
|
36
|
+
char: 'j',
|
|
37
|
+
description: 'Return logs in JSON format',
|
|
38
|
+
required: false,
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
24
41
|
async run() {
|
|
25
|
-
const { args } = await this.parse(Logs);
|
|
42
|
+
const { args, flags } = await this.parse(Logs);
|
|
26
43
|
const spinner = Spinner({ text: `Finding logs for function "${args.name}"` }).start();
|
|
27
44
|
const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token: config.token });
|
|
28
45
|
if (!deployedStack)
|
|
29
46
|
this.error('Stack not found'); // returns
|
|
30
|
-
const {
|
|
47
|
+
const { projectId } = deployedStack;
|
|
31
48
|
const { externalId } = findFunctionByName(deployedStack, args.name);
|
|
32
|
-
const { ok, error, logs } = await logsAction(externalId, { token: config.token, projectId });
|
|
49
|
+
const { ok, error, logs, total } = await logsAction(externalId, { limit: flags.limit }, { token: config.token, projectId });
|
|
33
50
|
if (!ok) {
|
|
34
51
|
spinner.error(`${red('Failed')} to retrieve logs`);
|
|
35
52
|
this.log(`Error: ${error || 'Unknown error'}`);
|
|
@@ -41,11 +58,20 @@ export default class Logs extends Command {
|
|
|
41
58
|
return;
|
|
42
59
|
}
|
|
43
60
|
spinner.success(`${formatTitle('Function', args.name)} Logs`);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
61
|
+
if (!flags.json) {
|
|
62
|
+
this.log(`Found ${bold(total)} log entries for function ${yellow(args.name)}`);
|
|
63
|
+
if (logs.length < total) {
|
|
64
|
+
this.log(`Here are the last ${bold(filteredLogs.length.toString())} entries`);
|
|
65
|
+
}
|
|
66
|
+
this.log('\n');
|
|
67
|
+
for (const log of filteredLogs) {
|
|
68
|
+
const { time, level, message } = log;
|
|
69
|
+
const date = new Date(time);
|
|
70
|
+
this.log(`${bold(date.toLocaleDateString())} ${bold(blue(date.toLocaleTimeString()))} ${logLevel(level)} ${message}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
this.log(JSON.stringify(filteredLogs, null, 2));
|
|
49
75
|
}
|
|
50
76
|
}
|
|
51
77
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
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 {
|
|
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 }),
|
|
8
8
|
};
|
|
9
9
|
static description = 'Invoke a local Sanity Function';
|
|
10
10
|
static examples = [
|
|
11
|
-
`<%= config.bin %> <%= command.id %>
|
|
12
|
-
`<%= config.bin %> <%= command.id %>
|
|
13
|
-
`<%= config.bin %> <%= command.id %>
|
|
11
|
+
`<%= config.bin %> <%= command.id %> <name> --data '{ "id": 1 }'`,
|
|
12
|
+
`<%= config.bin %> <%= command.id %> <name> --file 'payload.json'`,
|
|
13
|
+
`<%= config.bin %> <%= command.id %> <name> --data '{ "id": 1 }' --timeout 60`,
|
|
14
14
|
];
|
|
15
15
|
static flags = {
|
|
16
16
|
data: Flags.string({ char: 'd', description: 'Data to send to the function', required: false }),
|
|
@@ -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
|
|
33
|
-
|
|
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():
|
|
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):
|
|
1278
|
-
reconfigure: (state: any, oldState: any) =>
|
|
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 {
|
|
2
|
-
export default function buildPayload(options:
|
|
1
|
+
import type { InvokePayloadOptions } from './types.js';
|
|
2
|
+
export default function buildPayload(options: InvokePayloadOptions): object | null;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
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:
|
|
4
|
-
export declare function formatStackInfo(stack:
|
|
5
|
-
export declare function formatStacksListing(stacks:
|
|
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
|
|
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
|
|
41
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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 {
|
|
2
|
-
export declare function findFunctionByName(
|
|
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
|
-
|
|
2
|
-
|
|
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export default function formatError(response, json) {
|
|
2
|
+
if (response.ok) {
|
|
3
|
+
return null;
|
|
4
|
+
}
|
|
5
|
+
if (response.status === 401 || response.status === 403) {
|
|
6
|
+
return 'Please login to the Sanity CLI to run this command';
|
|
7
|
+
}
|
|
8
|
+
return json?.error?.message || 'Unknown Error';
|
|
9
|
+
}
|
package/dist/utils/types.d.ts
CHANGED
|
@@ -1,74 +1,81 @@
|
|
|
1
1
|
/** @internal */
|
|
2
|
-
export interface
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
16
|
-
|
|
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
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
|
27
|
-
|
|
21
|
+
export interface LocalFunctionResource extends LocalResource {
|
|
22
|
+
src?: string;
|
|
23
|
+
memory?: number;
|
|
24
|
+
timeout?: number;
|
|
25
|
+
env?: Record<string, string>;
|
|
28
26
|
}
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
export interface Stack {
|
|
28
|
+
id: string;
|
|
31
29
|
name: string;
|
|
30
|
+
displayName: string;
|
|
32
31
|
projectId: string;
|
|
33
|
-
|
|
32
|
+
resources: Array<StackResource>;
|
|
33
|
+
createdAt?: string;
|
|
34
|
+
updatedAt?: string;
|
|
35
|
+
recentOperation?: StackOperation;
|
|
34
36
|
}
|
|
35
37
|
/** @internal */
|
|
36
|
-
export interface
|
|
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
|
|
56
|
+
export interface StackOperation {
|
|
46
57
|
id: string;
|
|
47
58
|
status: string;
|
|
48
59
|
createdAt?: string;
|
|
49
60
|
completedAt?: string;
|
|
50
61
|
}
|
|
51
|
-
|
|
52
|
-
|
|
62
|
+
/** @internal */
|
|
63
|
+
export interface StackPayload {
|
|
53
64
|
name: string;
|
|
54
|
-
displayName: string;
|
|
55
65
|
projectId: string;
|
|
56
|
-
|
|
57
|
-
createdAt?: string;
|
|
58
|
-
updatedAt?: string;
|
|
59
|
-
recentOperation?: BlueprintOperation;
|
|
66
|
+
document: LocalBlueprint;
|
|
60
67
|
}
|
|
61
68
|
/** @internal */
|
|
62
|
-
export
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
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
|
+
}
|
package/dist/utils/types.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/** @internal */
|
|
2
|
-
export var
|
|
3
|
-
(function (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
})(
|
|
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 = {}));
|
package/oclif.manifest.json
CHANGED
|
@@ -274,8 +274,8 @@
|
|
|
274
274
|
},
|
|
275
275
|
"description": "Invoke a remote Sanity Function",
|
|
276
276
|
"examples": [
|
|
277
|
-
"<%= config.bin %> <%= command.id %> <
|
|
278
|
-
"<%= config.bin %> <%= command.id %> <
|
|
277
|
+
"<%= config.bin %> <%= command.id %> <name> --data '{ \"id\": 1 }'",
|
|
278
|
+
"<%= config.bin %> <%= command.id %> <name> --file 'payload.json'"
|
|
279
279
|
],
|
|
280
280
|
"flags": {
|
|
281
281
|
"data": {
|
|
@@ -324,9 +324,30 @@
|
|
|
324
324
|
},
|
|
325
325
|
"description": "Retrieve logs for a Sanity Function",
|
|
326
326
|
"examples": [
|
|
327
|
-
"<%= config.bin %> <%= command.id %> <
|
|
327
|
+
"<%= config.bin %> <%= command.id %> <name>",
|
|
328
|
+
"<%= config.bin %> <%= command.id %> <name> --json",
|
|
329
|
+
"<%= config.bin %> <%= command.id %> <name> --limit 100"
|
|
328
330
|
],
|
|
329
|
-
"flags": {
|
|
331
|
+
"flags": {
|
|
332
|
+
"limit": {
|
|
333
|
+
"char": "l",
|
|
334
|
+
"description": "Total number of log entries to retrieve",
|
|
335
|
+
"name": "limit",
|
|
336
|
+
"required": false,
|
|
337
|
+
"default": 50,
|
|
338
|
+
"hasDynamicHelp": false,
|
|
339
|
+
"multiple": false,
|
|
340
|
+
"type": "option"
|
|
341
|
+
},
|
|
342
|
+
"json": {
|
|
343
|
+
"char": "j",
|
|
344
|
+
"description": "Return logs in JSON format",
|
|
345
|
+
"name": "json",
|
|
346
|
+
"required": false,
|
|
347
|
+
"allowNo": false,
|
|
348
|
+
"type": "boolean"
|
|
349
|
+
}
|
|
350
|
+
},
|
|
330
351
|
"hasDynamicHelp": false,
|
|
331
352
|
"hiddenAliases": [],
|
|
332
353
|
"id": "functions:logs",
|
|
@@ -354,9 +375,9 @@
|
|
|
354
375
|
},
|
|
355
376
|
"description": "Invoke a local Sanity Function",
|
|
356
377
|
"examples": [
|
|
357
|
-
"<%= config.bin %> <%= command.id %>
|
|
358
|
-
"<%= config.bin %> <%= command.id %>
|
|
359
|
-
"<%= config.bin %> <%= command.id %>
|
|
378
|
+
"<%= config.bin %> <%= command.id %> <name> --data '{ \"id\": 1 }'",
|
|
379
|
+
"<%= config.bin %> <%= command.id %> <name> --file 'payload.json'",
|
|
380
|
+
"<%= config.bin %> <%= command.id %> <name> --data '{ \"id\": 1 }' --timeout 60"
|
|
360
381
|
],
|
|
361
382
|
"flags": {
|
|
362
383
|
"data": {
|
|
@@ -481,5 +502,5 @@
|
|
|
481
502
|
]
|
|
482
503
|
}
|
|
483
504
|
},
|
|
484
|
-
"version": "2.
|
|
505
|
+
"version": "2.4.0"
|
|
485
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.
|
|
4
|
+
"version": "2.4.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.
|
|
66
|
-
"oclif": "^4.17.
|
|
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.
|
|
73
|
+
"typescript": "^5.8.3",
|
|
74
74
|
"vitest": "3.1.1"
|
|
75
75
|
},
|
|
76
76
|
"oclif": {
|