@sanity/runtime-cli 3.0.0 → 3.1.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/3.0.0 linux-x64 node-v22.14.0
23
+ @sanity/runtime-cli/3.1.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/v3.0.0/src/commands/blueprints/add.ts)_
66
+ _See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/blueprints/config.ts)_
88
+ _See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/blueprints/deploy.ts)_
105
+ _See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/blueprints/info.ts)_
127
+ _See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/blueprints/init.ts)_
144
+ _See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/blueprints/logs.ts)_
166
+ _See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/blueprints/plan.ts)_
183
+ _See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/blueprints/stacks.ts)_
200
+ _See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/functions/dev.ts)_
220
+ _See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/functions/env/add.ts)_
242
+ _See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.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/v3.0.0/src/commands/functions/env/remove.ts)_
263
+ _See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.0/src/commands/functions/env/remove.ts)_
264
264
 
265
265
  ## `sanity-run functions invoke NAME`
266
266
 
@@ -286,25 +286,27 @@ 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/v3.0.0/src/commands/functions/invoke.ts)_
289
+ _See code: [src/commands/functions/invoke.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.0/src/commands/functions/invoke.ts)_
290
290
 
291
291
  ## `sanity-run functions logs NAME`
292
292
 
293
- Retrieve logs for a Sanity Function
293
+ Retrieve or delete logs for a Sanity Function
294
294
 
295
295
  ```
296
296
  USAGE
297
- $ sanity-run functions logs NAME [-l <value>] [-j]
297
+ $ sanity-run functions logs NAME [-f [-d | -l <value> | -j]]
298
298
 
299
299
  ARGUMENTS
300
300
  NAME The name of the Sanity Function
301
301
 
302
302
  FLAGS
303
+ -d, --delete Delete all logs for the function
304
+ -f, --force Skip confirmation for deleting logs
303
305
  -j, --json Return logs in JSON format
304
306
  -l, --limit=<value> [default: 50] Total number of log entries to retrieve
305
307
 
306
308
  DESCRIPTION
307
- Retrieve logs for a Sanity Function
309
+ Retrieve or delete logs for a Sanity Function
308
310
 
309
311
  EXAMPLES
310
312
  $ sanity-run functions logs <name>
@@ -312,9 +314,11 @@ EXAMPLES
312
314
  $ sanity-run functions logs <name> --json
313
315
 
314
316
  $ sanity-run functions logs <name> --limit 100
317
+
318
+ $ sanity-run functions logs <name> --delete
315
319
  ```
316
320
 
317
- _See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/functions/logs.ts)_
321
+ _See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.0/src/commands/functions/logs.ts)_
318
322
 
319
323
  ## `sanity-run functions test NAME`
320
324
 
@@ -347,7 +351,7 @@ EXAMPLES
347
351
  $ sanity-run functions test <name> --data '{ "id": 1 }' --timeout 60
348
352
  ```
349
353
 
350
- _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v3.0.0/src/commands/functions/test.ts)_
354
+ _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v3.1.0/src/commands/functions/test.ts)_
351
355
 
352
356
  ## `sanity-run help [COMMAND]`
353
357
 
@@ -1,5 +1,5 @@
1
1
  import type { AuthParams } from '../../utils/types.js';
2
- /** internal */
2
+ /** @internal */
3
3
  export interface LoggingOptions {
4
4
  limit: number;
5
5
  }
@@ -9,3 +9,7 @@ export declare function logs(id: string, options: LoggingOptions, auth: AuthPara
9
9
  logs: any;
10
10
  total: any;
11
11
  }>;
12
+ export declare function deleteLogs(id: string, auth: AuthParams): Promise<{
13
+ ok: boolean;
14
+ error: any;
15
+ }>;
@@ -15,3 +15,14 @@ export async function logs(id, options, auth) {
15
15
  total: response.ok ? json.total : 0,
16
16
  };
17
17
  }
18
+ export async function deleteLogs(id, auth) {
19
+ const response = await fetch(`${functions}vX/functions/${id}/logs`, {
20
+ headers: getHeaders(auth),
21
+ method: 'DELETE',
22
+ });
23
+ const json = response.ok ? null : await response.json();
24
+ return {
25
+ ok: response.ok,
26
+ error: response.ok ? null : json?.error?.message,
27
+ };
28
+ }
@@ -6,6 +6,7 @@ import { readBlueprintOnDisk, writeConfigFile } from '../../actions/blueprints/b
6
6
  import { createStack, updateStack } from '../../actions/blueprints/stacks.js';
7
7
  import config from '../../config.js';
8
8
  import { bold, green, red, yellow } from '../../utils/display/colors.js';
9
+ import { isLocalFunctionResource } from '../../utils/types.js';
9
10
  const { token } = config;
10
11
  export default class Deploy extends Command {
11
12
  static description = 'Deploy a Blueprint';
@@ -50,7 +51,7 @@ export default class Deploy extends Command {
50
51
  if (!name)
51
52
  this.error('Stack name is required');
52
53
  const validResources = resources?.filter((r) => r.type);
53
- const functionResources = validResources?.filter((r) => r.type.startsWith('sanity.function.'));
54
+ const functionResources = validResources?.filter(isLocalFunctionResource);
54
55
  // First stash all function assets
55
56
  if (functionResources?.length) {
56
57
  for (const resource of functionResources) {
@@ -33,6 +33,9 @@ export default class Invoke extends Command {
33
33
  const result = await invoke(externalId, { data: flags.data, file: flags.file }, { token: config.token, projectId });
34
34
  if (result.ok) {
35
35
  spinner.success(`Invocation of ${args.name} succeeded`);
36
+ if (result.json?.data?.type === 'Buffer') {
37
+ return;
38
+ }
36
39
  this.log(JSON.stringify(result.json, null, 2));
37
40
  }
38
41
  else {
@@ -1,4 +1,11 @@
1
1
  import { Command } from '@oclif/core';
2
+ type RunDeleteOptions = {
3
+ force: boolean;
4
+ };
5
+ type RunGetOptions = {
6
+ limit: number;
7
+ json?: boolean;
8
+ };
2
9
  export default class Logs extends Command {
3
10
  static args: {
4
11
  name: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
@@ -8,6 +15,11 @@ export default class Logs extends Command {
8
15
  static flags: {
9
16
  limit: import("@oclif/core/interfaces").OptionFlag<number, import("@oclif/core/interfaces").CustomOptions>;
10
17
  json: import("@oclif/core/interfaces").BooleanFlag<boolean>;
18
+ delete: import("@oclif/core/interfaces").BooleanFlag<boolean>;
19
+ force: import("@oclif/core/interfaces").BooleanFlag<boolean>;
11
20
  };
12
21
  run(): Promise<void>;
22
+ runDeleteLogs(name: string, options: RunDeleteOptions): Promise<void>;
23
+ runGetLogs(name: string, options: RunGetOptions): Promise<void>;
13
24
  }
25
+ export {};
@@ -1,7 +1,8 @@
1
1
  import { Args, Command, Flags } from '@oclif/core';
2
+ import inquirer from 'inquirer';
2
3
  import Spinner from 'yocto-spinner';
3
4
  import { readBlueprintOnDisk } from '../../actions/blueprints/blueprint.js';
4
- import { logs as logsAction } from '../../actions/functions/logs.js';
5
+ import { deleteLogs as deleteLogsAction, logs as logsAction } from '../../actions/functions/logs.js';
5
6
  import config from '../../config.js';
6
7
  import { formatTitle } from '../../utils/display/blueprints-formatting.js';
7
8
  import { blue, bold, green, red, yellow } from '../../utils/display/colors.js';
@@ -15,15 +16,26 @@ function logLevel(level) {
15
16
  }
16
17
  return green(level);
17
18
  }
19
+ // TODO extract to a shared module
20
+ async function getProjectAndExternalId(name) {
21
+ const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token: config.token });
22
+ if (!deployedStack) {
23
+ return {};
24
+ }
25
+ const { projectId } = deployedStack;
26
+ const { externalId } = findFunctionByName(deployedStack, name);
27
+ return { projectId, externalId };
28
+ }
18
29
  export default class Logs extends Command {
19
30
  static args = {
20
31
  name: Args.string({ description: 'The name of the Sanity Function', required: true }),
21
32
  };
22
- static description = 'Retrieve logs for a Sanity Function';
33
+ static description = 'Retrieve or delete logs for a Sanity Function';
23
34
  static examples = [
24
35
  '<%= config.bin %> <%= command.id %> <name>',
25
36
  '<%= config.bin %> <%= command.id %> <name> --json',
26
37
  '<%= config.bin %> <%= command.id %> <name> --limit 100',
38
+ '<%= config.bin %> <%= command.id %> <name> --delete',
27
39
  ];
28
40
  static flags = {
29
41
  limit: Flags.integer({
@@ -37,16 +49,61 @@ export default class Logs extends Command {
37
49
  description: 'Return logs in JSON format',
38
50
  required: false,
39
51
  }),
52
+ delete: Flags.boolean({
53
+ char: 'd',
54
+ exclusive: ['limit', 'json'],
55
+ description: 'Delete all logs for the function',
56
+ required: false,
57
+ }),
58
+ force: Flags.boolean({
59
+ char: 'f',
60
+ dependsOn: ['delete'],
61
+ description: 'Skip confirmation for deleting logs',
62
+ required: false,
63
+ }),
40
64
  };
41
65
  async run() {
42
66
  const { args, flags } = await this.parse(Logs);
43
- const spinner = Spinner({ text: `Finding logs for function "${args.name}"` }).start();
44
- const { deployedStack } = await readBlueprintOnDisk({ getStack: true, token: config.token });
45
- if (!deployedStack)
46
- this.error('Stack not found'); // returns
47
- const { projectId } = deployedStack;
48
- const { externalId } = findFunctionByName(deployedStack, args.name);
49
- const { ok, error, logs, total } = await logsAction(externalId, { limit: flags.limit }, { token: config.token, projectId });
67
+ if (flags.delete === true) {
68
+ await this.runDeleteLogs(args.name, flags);
69
+ }
70
+ else {
71
+ await this.runGetLogs(args.name, flags);
72
+ }
73
+ }
74
+ async runDeleteLogs(name, options) {
75
+ if (!options.force) {
76
+ const { certain } = await inquirer.prompt({
77
+ type: 'confirm',
78
+ name: 'certain',
79
+ message: `Are you sure you want to delete ${bold('all')} logs for function ${yellow(name)}?`,
80
+ default: false,
81
+ });
82
+ if (!certain)
83
+ return;
84
+ }
85
+ const { projectId, externalId } = await getProjectAndExternalId(name);
86
+ if (!projectId || !externalId) {
87
+ this.error('Stack not found');
88
+ return;
89
+ }
90
+ const spinner = Spinner({ text: `Deleting logs for function ${yellow(name)}` }).start();
91
+ const { ok, error } = await deleteLogsAction(externalId, { token: config.token, projectId });
92
+ if (!ok) {
93
+ spinner.error(`${red('Failed')} to retrieve logs`);
94
+ this.log(`Error: ${error || 'Unknown error'}`);
95
+ return;
96
+ }
97
+ spinner.success('Logs deleted');
98
+ }
99
+ async runGetLogs(name, options) {
100
+ const spinner = Spinner({ text: `Finding logs for function "${name}"` }).start();
101
+ const { projectId, externalId } = await getProjectAndExternalId(name);
102
+ if (!projectId || !externalId) {
103
+ this.error('Stack not found');
104
+ return;
105
+ }
106
+ const { ok, error, logs, total } = await logsAction(externalId, { limit: options.limit }, { token: config.token, projectId });
50
107
  if (!ok) {
51
108
  spinner.error(`${red('Failed')} to retrieve logs`);
52
109
  this.log(`Error: ${error || 'Unknown error'}`);
@@ -54,12 +111,12 @@ export default class Logs extends Command {
54
111
  }
55
112
  const filteredLogs = logs.filter((entry) => entry.level && entry.message);
56
113
  if (filteredLogs.length === 0) {
57
- spinner.info(`No logs found for function ${args.name}`);
114
+ spinner.info(`No logs found for function ${name}`);
58
115
  return;
59
116
  }
60
- spinner.success(`${formatTitle('Function', args.name)} Logs`);
61
- if (!flags.json) {
62
- this.log(`Found ${bold(total)} log entries for function ${yellow(args.name)}`);
117
+ spinner.success(`${formatTitle('Function', name)} Logs`);
118
+ if (!options.json) {
119
+ this.log(`Found ${bold(total)} log entries for function ${yellow(name)}`);
63
120
  if (logs.length < total) {
64
121
  this.log(`Here are the last ${bold(filteredLogs.length.toString())} entries`);
65
122
  }
@@ -1,3 +1,4 @@
1
1
  import type { LocalBlueprint, LocalFunctionResource, Stack, StackFunctionResource } from './types.js';
2
- export declare function findFunctionByName(blueprintOrStack: LocalBlueprint | Stack, name: string): LocalFunctionResource | StackFunctionResource;
2
+ export declare function findFunctionByName(blueprintOrStack: LocalBlueprint, name: string): LocalFunctionResource;
3
+ export declare function findFunctionByName(blueprintOrStack: Stack, name: string): StackFunctionResource;
3
4
  export declare function getFunctionSource(blueprintOrStack: LocalBlueprint | Stack, name: string): string;
@@ -15,8 +15,8 @@ export interface LocalResource {
15
15
  name: string;
16
16
  type: string;
17
17
  displayName?: string;
18
- [key: string]: unknown;
19
18
  }
19
+ export declare function isLocalFunctionResource(r: LocalResource): r is LocalFunctionResource;
20
20
  /** @internal */
21
21
  export interface LocalFunctionResource extends LocalResource {
22
22
  src?: string;
@@ -1,3 +1,6 @@
1
+ export function isLocalFunctionResource(r) {
2
+ return r.type.startsWith('sanity.function.');
3
+ }
1
4
  /** @internal */
2
5
  export var BlueprintParserErrorType;
3
6
  (function (BlueprintParserErrorType) {
@@ -322,11 +322,12 @@
322
322
  "required": true
323
323
  }
324
324
  },
325
- "description": "Retrieve logs for a Sanity Function",
325
+ "description": "Retrieve or delete logs for a Sanity Function",
326
326
  "examples": [
327
327
  "<%= config.bin %> <%= command.id %> <name>",
328
328
  "<%= config.bin %> <%= command.id %> <name> --json",
329
- "<%= config.bin %> <%= command.id %> <name> --limit 100"
329
+ "<%= config.bin %> <%= command.id %> <name> --limit 100",
330
+ "<%= config.bin %> <%= command.id %> <name> --delete"
330
331
  ],
331
332
  "flags": {
332
333
  "limit": {
@@ -346,6 +347,29 @@
346
347
  "required": false,
347
348
  "allowNo": false,
348
349
  "type": "boolean"
350
+ },
351
+ "delete": {
352
+ "char": "d",
353
+ "description": "Delete all logs for the function",
354
+ "exclusive": [
355
+ "limit",
356
+ "json"
357
+ ],
358
+ "name": "delete",
359
+ "required": false,
360
+ "allowNo": false,
361
+ "type": "boolean"
362
+ },
363
+ "force": {
364
+ "char": "f",
365
+ "dependsOn": [
366
+ "delete"
367
+ ],
368
+ "description": "Skip confirmation for deleting logs",
369
+ "name": "force",
370
+ "required": false,
371
+ "allowNo": false,
372
+ "type": "boolean"
349
373
  }
350
374
  },
351
375
  "hasDynamicHelp": false,
@@ -527,5 +551,5 @@
527
551
  ]
528
552
  }
529
553
  },
530
- "version": "3.0.0"
554
+ "version": "3.1.0"
531
555
  }
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": "3.0.0",
4
+ "version": "3.1.0",
5
5
  "author": "Sanity Runtime Team",
6
6
  "type": "module",
7
7
  "license": "MIT",